2
votes

I am trying to use thrust in CUDA and have something as follows:

data = thrust::device_malloc<float>(N);

Now, I have another method where I want to check if the data pointer is initialized properly. Is this the correct usage or is there a more preferred approach?

if (data->get() == NULL) // throw some exception.

I was surprised I could not use bool comparators on the device_ptr object directly like:

if (!data) // compilation error.

Also, if I want to use thrust::device_free, do I need to check again for NULL (as in C style free) or is it safe to use thrust::device_free for NULL input pointers?

1

1 Answers

5
votes

thrust::device_malloc raises an exception if the allocation fails, so there really isn't a scenario I can think of where it should be necessary to check the value of a device_ptr for validity or throw an exception in user code. In every case the code should either abort with an uncaught exception, or your host catches the exception raised by thrust and reacts accordingly.

That said, the source of thrust::device_malloc suggests that in the case of failed memory allocation, the returned device_ptr will hold a raw pointer value of 0. You should be able to confirm this with the following:

#include <thrust/device_malloc.h>
#include <thrust/device_ptr.h>
#include <iostream>
#include <new>

void try_alloc(unsigned int N)
{
    thrust::device_ptr<float> data;

    std::cout << "trying N=" << N;

    try
    {
        data = thrust::device_malloc<float>(N);
    }
    catch (std::bad_alloc& e)
    {
        std::cerr << " bad_alloc caught: " << e.what() << std::endl;
    }
    std::cout << " data.get() returns: " << std::hex << data.get() << std::endl;

}

int main()
{

    try_alloc(2<<4);
    try_alloc(2<<9);
    try_alloc(2<<14);
    try_alloc(2<<19);
    try_alloc(2<<24);
    try_alloc(2<<29);

    return 0;
}

So to answer your question, for

data = thrust::device_malloc<float>(N); 

a "correct" test would be

if (!data.get()) { .. } // Pointer is invalid 

Noting that a std::bad_alloc should already have been raised a priori.