Template Function thrust::get_temporary_buffer

Function Documentation

template<typename T, typename DerivedPolicy>__host__ __device__ 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 accomodate n objects, an implementation may return a smaller buffer. The number of objects the returned buffer can accomodate 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 accomodate integers associated with Thrust’s device system.
Return

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 accomodate. 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 publically derived from thrust::execution_policy<DerivedPolicy>.

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

  • n: The requested number of objects of type T the storage should accomodate.

Template Parameters
  • DerivedPolicy: The name of the derived execution policy.

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

typedef thrust::pair<
  thrust::pointer<int,thrust::device_system_tag>,
  std::ptrdiff_t
> ptr_and_size_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

malloc

See

return_temporary_buffer