Global variables ?

Hello,

I have a program in which each thread calculates a value. I need to store only the smallest value of all. I tried to make all threads read and write into a same buffer cell to find the minimum, but I guess this is a very bad idea since I get a totally random behaviour (which was expectable indeed).

So my question is : is there any way to create a global variable that can be accessed and modified by any thread with some kind of lock mechanism ?

Thanks,
Hamza

You could use CUDA atomics (http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#atomic-functions).
I’d rather store all values in memory and find the smallest using a reduction algorithm such as thrust::reduce (https://github.com/thrust/thrust/wiki/Quick-Start-Guide#reductions).

Thanks, that’s exactly what I was looking for !

Just a quick note:

  • in addition to the Thrust open source project hosted on Github, a production-tested version of Thrust is included in the CUDA Toolkit
  • (https://developer.nvidia.com/Thrust)