Kumar Kumar - 1 month ago 8
C++ Question

assigning values to variables in GPU memory in CUDA

My question is can I explicitly assign values to variables that are sitting in GPU memory or do I always have to use cudaMemcpy?

for example can i do this:

int * a;
cudaMalloc((void**)&a, sizeof(int));
*a = 5;


or can i only do:

int * dev_a;
int a = 5;
cudaMalloc((void**)&dev_a, sizeof(int));
cudaMemcpy(dev_a, &a, sizeof(int) , cudaMemcpyHostToDevice);

Answer

Simple answer is, that you can not assign values to device memory. This has two reasons. The reason is, that the device memory is not in ram, but physically on the gpu and you have to transfer the values over the pcie bridge.

The more complex answer is, that you can use unified memory since cuda 6, which does what you want to do. Keep in mind, that the transfer does happen in the background and does take time.

https://devblogs.nvidia.com/parallelforall/unified-memory-in-cuda-6/