Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CUDA device pointers

Tags:

c

cuda

Quick question about the standard CUDA memory allocation model:

double* x_device;
cudaMalloc(&x_device,myArraySize);

The variable x_device is a pointer-to-double. After I call cudaMalloc, does x_device now point to a memory location on the cuda device? So, in other words, *x_device would result in a segfault because we can't directly access device memory from the host.

Incidental question, the compiler doesn't complain that I don't use (void**)&x_device, is this required? I sometimes see it in examples, sometimes not.

Thanks!

like image 881
icurays1 Avatar asked Mar 23 '15 22:03

icurays1


1 Answers

You are right: cudaMalloc allocates memory on the device. You can't use this pointer directly on the host, but only as argument to functions like cudaMemcpy, and as arguments to kernel calls.

More recent CUDA versions support unified memory addressing, there you can use cudaMallocManaged to allocate device memory, and access it on the host directly via the device pointer.

For the second question: C++ doesn't allow implicit casts between pointer types, so there leaving out the explicit cast (void**)&x_device will result in a compiler error.

like image 146
roeland Avatar answered Sep 22 '22 22:09

roeland