when is memory on host or device? host/device variables

Hi,

After looking at CUDA examples and at the manual, I still don’t find it clear when a variable is in device or host memory.

I would appreciate if you correct me if I’m wrong on the following examples:

  • Example 1:
    float array;
    cudaMalloc( (void **)&array, 10
    sizeof(float) );
    means that “array” is allocated on the device (global memory) and read/writable from both device and host?

  • Example 2:
    device float array;
    cudaMalloc( (void **)&array, 10
    sizeof(float) );
    means that “array” is allocated on the device (global memory) and read/writable ONLY from device?

  • Example 3:
    float *array;
    array = (float )malloc( 10sizeof(float) );
    means that “array” is allocated on the host and read/writable ONLY from host?

Thanks for your help

array is a pointer that points to memory allocated on the device

same as before, you can cudamemcpy to/from it

yes, the device knows only cudamalloced memory

Thanks for this, DenisR.

So what’s the difference between examples 1 & 2 ?

So when do you use the device prefix, since it’s more restrictive?

I suspect that you sometimes use it to help the compiler determine what exactly a variable is. Just a guess. Depends on what you’re doing, whether you’re declaring a function or a variable.

personally, I have never used the device qualifier. To find out what it does, I would check the programming guide, but I also think it is probably just a compiler hint.

You cannot do this. It would mean that the pointer array is located in device memory, not the memory it points to.

Ok, I see.

So you can’t dynamically allocate memory for a device variable, right?

cudaMalloc dynamically allocates device (GPU) memory, but puts the pointer in normal CPU memory.