thrust::get_temporary_buffer

Defined in thrust/memory.h

template<typename T, typename DerivedPolicy>
thrust::pair<thrust::pointer<T, DerivedPolicy>, typename thrust::pointer<T, DerivedPolicy>::difference_type> thrust::get_temporary_buffer(const thrust::detail::execution_policy_base<DerivedPolicy> &system, typename thrust::pointer<T, DerivedPolicy>::difference_type n)

get_temporary_buffer returns a pointer to storage associated with a given Thrust system sufficient to store up to n objects of type T. If not enough storage is available to accommodate n objects, an implementation may return a smaller buffer. The number of objects the returned buffer can accommodate is also returned.

Thrust uses get_temporary_buffer internally when allocating temporary storage required by algorithm implementations.

The storage allocated with get_temporary_buffer must be returned to the system with return_temporary_buffer.

The following code snippet demonstrates how to use get_temporary_buffer to allocate a range of memory to accommodate integers associated with Thrust’s device system.

#include <thrust/memory.h>
...
// allocate storage for 100 ints with thrust::get_temporary_buffer
const int N = 100;

using ptr_and_size_t = thrust::pair<
  thrust::pointer<int,thrust::device_system_tag>,
  std::ptrdiff_t
>;

thrust::device_system_tag device_sys;
ptr_and_size_t ptr_and_size = thrust::get_temporary_buffer<int>(device_sys, N);

// manipulate up to 100 ints
for(int i = 0; i < ptr_and_size.second; ++i)
{
  *ptr_and_size.first = i;
}

// deallocate storage with thrust::return_temporary_buffer
thrust::return_temporary_buffer(device_sys, ptr_and_size.first);

See also

malloc

See also

return_temporary_buffer

Parameters
  • system – The Thrust system with which to associate the storage.

  • n – The requested number of objects of type T the storage should accommodate.

Template Parameters

DerivedPolicy – The name of the derived execution policy.

Returns

A pair p such that p.first is a pointer to the allocated storage and p.second is the number of contiguous objects of type T that the storage can accommodate. If no storage can be allocated, p.first if no storage can be obtained. The storage must be returned to the system using return_temporary_buffer.

Pre

DerivedPolicy must be publicly derived from thrust::execution_policy<DerivedPolicy>.