triggering kernel wide shutdown/crash on purpose

I would like to terminate a kernel from within a thread.
The kernel is doing an exhaustive search of some sort. When a thread finds the right value, it should put it in the gmem. After that I don’t care what happens to the rest of the grid. It would be nice to be able to terminate the kernel, so I could launch a big grid instead of loop-launching small ones and checking the result each time.
A sane way to terminate isn’t necessary, I’m perfectly happy with any sort of crash (that won’t spoil the result ofc) that I can conditionally trigger inside a thread. Should be sort of portable though.

You could just write some flag in gmem and check periodicaly during the search (for example you probably have some loop so at the beginning of such loop) and if this

flag is signaled - exit. Should be coalesced since all threads will use broadcasting to read it.

does that make sense or you need something faster/API?


There’s no good way to do this without setting a flag in global memory as eyal describes.

It’s alright, although I like to optimize every instruction if I can. And there’s always a chance someone figures out a better way to do the thing ;)

Tim’s right. Use a global memory flag. If you poll it at the right interval, it’s not too bad. Be careful to mark the variable volatile so it’s reloaded. (Hmm, I hope Fermi’s caches know that volatile variable lookups should avoid the cache… I would assume so.)

I did once experiment with zero-copy memory aborts where a thread setting a value would let the HOST poll for the abort flag and harshly kill the thread with the running CUDA context. This worked, but was horrible… the driver clearly does not like it and it took multiple seconds. And sometimes the display would momentarily freeze even when killing a non-display device context.