exit(1) in device?

Hi all,

I would like to know if there is an equivalent to the function exit(1) in C to use in a device function?

Thanks for answering.


What do you expect it to do? If you want to exit the current thread, use [font=“Courier New”]return[/font].

If you want all other threads to abort as well, use [font=“Courier New”]asm(“trap;”)[/font], potentially with a [font=“Courier New”]threadfence()[/font] or [font=“Courier New”]threadfence_system()[/font] before it if you want to make sure previous writes hit memory first. In the [font=“Courier New”]asm(“trap;”)[/font] case, the next CUDA call will return an error code to indicate that the kernel has been aborted.

Thank you very much for your answer.

My goal was to stop the entire program, for example if a malloc fail, it is useless to continue. But finally after the reading of your answer I think I will stop only the thread with the problem.

If you want to just exit the thread, but not abort the whole program, then return will not do,
if you are currently 50 subroutine calls deep.

This is handled in normal CPU programming by having both return and exit available.
On the device, you can use : asm(“exit;”)
which seems to work like “exit” in a host routine.

Had the same problem, ended up with a global “terminate” flag checked by all threads. asm(“trap;”) sort of works, but an Nvidia employee on this forum pointed out that it can leave the context in an inconsistent state.

I used asm(“exit”;), but I have to add the -ptx flag. I am compiling files containing asm(“exit”;) into object files, and linking them later. The exact commands I use are:

nvcc -c amigpu.cu -o amigpu.o -ptx

g++ -std=c++11 -pthread -fopenmp -I/usr/local/cuda/include src/Quantization.cpp -o output pkg-config --cflags --libs opencv src/amigpu.o -L/usr/local/cuda/lib64 -lcudart.

The file amigpu.o is the one obtained with -ptx flag and I get the error:
amigpu.o: file format not recognized; treating as linker script .
Is this a problem related to -ptx?
Thank you.