Passing references to CUDA

Quick question from a new CUDA user,

I have a single value, say ‘cnt’, that is an integer on the CPU and gets passed by-reference to various functions in C++ code. The ‘cnt’ variable is updated in some of the various functions it is passed to. I would like to create CUDA/GPU versions of the CPU functions but need to be able to pass the ‘cnt’ as a reference but I don’t think this is possible - is it?

If it is, how?

If it is not, I thought about collapsing all kernels into one and just storing the ‘cnt’ variable on the GPU. Can this be done? I found info on vector/arrays being stored on the GPU via CUDA but not about scalar values.

Thanks all.

You cannot pass variables by reference to kernels. You can declare a global memory scalar and modify it from a kernel if you want, but beware that, unless strict, explicit precautions are taken, that is a race condition waiting to happen. Launching a kernel might seem analogous to calling a subroutine, but what you are really doing is launching anything up to millions of threads, which will run in an undefined order, and will all modify your global memory scalar unless you stop them, or use atomic memory access to serialize their access to it.