Is passing by reference allowed?

e.g.

void f(int& smem){
smem = 5;
}

where smem is formal parameter. Can I pass a shared variable to this formal parameter and change the value of smem in the function? If it is not allowed, how can I achieve that effect?

Thanks.

Correct me if I’m wrong, but You can define shared memory variable globally, meaning visible from within every device function in Your code, outside any function. No parameters passing is needed in such situation.

MK

Pass by reference should work fine. The only time you can have a problem is if you are using a device with compute capability < 2.0 and if the compiler cannot decide at compile time what kind of memory the function is accessing.

Pass by reference works fine in the host code. if you write a kernel function and send arguments as pass-by reference , then it wont work, since the GPU can’t access the host memory unless it is in UVA.

Please correct me if this wrong.

We should be clear about the different function types in CUDA code:

  • Host functions (that run on the CPU) can obviously have reference arguments.

  • “Device” functions (as in functions with a device qualifier in the declaration) can also use pass by reference.

  • Functions with a global qualifier, which run on the device but are called by the host, cannot use pass by reference.

1 Like