Search code examples
cudathrust

Thrust device_malloc and device_new


What are the advantages for using Thrust device_malloc instead of the normal cudaMalloc and what does device_new do?

For device_malloc it seems the only reason to use it is that it's just a bit cleaner.

The device_new documentation says:

"device_new implements the placement new operator for types resident in device memory. device_new calls T's null constructor on a array of objects in device memory. No memory is allocated by this function."

Which I don't understand...


Solution

  • device_malloc returns the proper type of object if you plan on using Thrust for other things. There is normally no reason to use cudaMalloc if you are using Thrust. Encapsulating CUDA calls makes it easier and usually cleaner. The same thing goes for C++ and STL containers versus C-style arrays and malloc.

    For device_new, you should read the following line of the documentation:

     template<typename T>
     device_ptr<T> thrust::device_new (device_ptr< void > p, const size_t n = 1) 
    

    p: A device_ptr to a region of device memory into which to construct one or many Ts.

    Basically, this function can be used if memory has already been allocated. Only the default constructor will be called, and this will return a device_pointer casted to T's type.

    On the other hand, the following method allocates memory and returns a device_ptr<T>:

    template<typename T >
    device_ptr<T> thrust::device_new (const size_t n = 1)