How to stop all Threads

I am coding a tool which shall compare hash values on the GPU. Once the right hash is found all threads should stop working and the program should jump back to the main function. How can I stop all threads?

Using a bool in shared memory and check inside your kernel if it is true or false when it is false break / exit?

I don’t know if this will work but you can at least try.

You could do this easily enough on a per-block basis with voting intrinsics on Compute 1.2 cards or above (Tesla C1060, GTX 2x0), but it’s a lot trickier if you want to do it across the entire kernel. All I can think of there is maybe some tricky atomic operations, but if you’re going to try to do tricky atomics like that it’s probably better to just do multiple kernels.

set a single global value to 1

check in the beginning of the kernel if the value is 1, if it is 1 do your thing. If you find something: write 0 to the global location.

Like that all currently running blocks will continue, but all blocks that would be launched afterwards will in fact return immediately.

For more than one block of threads:

global void hashkernel(int *quit, …)


do {

  hash = hashfunc(....);

  if(hash == whatIamlookingfor)

      quit[0] = 1;

} while(quit[0]);


For a single block of threads with warp voting support:

global void hashkernel(…)


int quit = 0;

do {

    <same as above block only assigning to local quit>

} while(__all(quit));