Emil Styrke
Emil Styrke

Reputation: 1071

Is zero a valid value for a CUDA device pointer?

Can a CUdeviceptr be set to zero (NULL) to indicate an uninitialized pointer, or can zero be a valid device pointer under any circumstances? And further, is it allowed to pass a null device pointer to cuMemFree? I'm assuming that this is analogous to how it works in C(++), but I haven't been able to find any documentation substantiating this. I'd like an answer that refers to some kind of credible source, if possible.

EDIT: What want to know if it's guaranteed that I can write code like this:

CUdeviceptr p=0;

[... do something else, maybe allocate memory for p ...]

if (p != 0) {
     cuMemFree(p);
     p = 0;
}

Or even better without the if statement, like I can do with delete. Or do I have to keep track of the allocation status of p by hand?

Upvotes: 1

Views: 1733

Answers (2)

ArchaeaSoftware
ArchaeaSoftware

Reputation: 4422

Yes, 0 is a guaranteed-invalid value for CUdeviceptr.

I am not sure cuMemFree(0) has always been valid, but in CUDA 4.0 cuMemFree(0) returns CUDA_SUCCESS.

Upvotes: 3

Danny Varod
Danny Varod

Reputation: 18118

The 0 address isn't a C++ feature - it is a hardware feature. The CPU prevents access to address 0 and raises an interrupt. The 0 address is an invalid address in the GPU too. Try passing 0 to cuMemFree, you should get a error code as a result.

Upvotes: 1

Related Questions