how to find the min value

In my program, each thread generate many values. I want to find the min value in all the values. Firstly, I want to save all the value into global memory and then compare them, but there are so many values that the global memory cannot contain them. I want to use atomic function atomicMin(), but it only support capability 1.0 and float type. Can you give me some advice?


Your best bet is to compute the min per block then have thread #0 of each block use the atomicMin operator to write its block minimum to global memory

The code would go something like this:


store results to a shared array


for(i = blockDim.x / 2; i; i >>= 1)


if(threadIdx.x < i && array[threadIdx.x] < array[threadIdx.x + i])

      array[threadIdx.x] = array[threadIdx.x + i];



if(threadIdx.x == 0)


 atomicMin(&global_min, array[0]);


If you’re not using multiple blocks, just stick to the shared memory.


And if you don’t have atomic support (device 1.0) you could write the per-block values out to global memory, then do a short CPU loop over them to find the minimum. Or, make a second kernel to do it on the device in a second pass.