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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With