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.