Proper use of cudaDeviceReset ()

Since I suspect that the black box (GPU) does not close clean in some larger code ( others, perhaps, too ), I would include cudaDeviceReset() at the end of main() . But wait! This will be a Segmentation fault all instances of classes statically created in main() with non-trivial CUDA code in destructors, right? For instance.

 class A { public: cudaEvent_t tt; cudaEvent_t uu; A() { cudaEventCreate(&tt); cudaEventCreate(&uu); } ~A(){ cudaEventDestroy(tt); cudaEventDestroy(uu); } }; 

is created statically:

 int main() { A t; cudaDeviceReset(); return 0; } 

segfaults on exit. Question: perhaps cudaDeviceReset() is called automatically when exiting main() ?

Otherwise, all the useful main() code should be offset by some run() , and cudaDeviceReset() should be the last command in main() , right?

+4
source share
1 answer

As Talonmies pointed out, a class A destructor is called after the cudaDeviceReset () function is already called, namely when the main (..) function ends.

I think you can take cudaDeviceReset () in the atexit (..) function.

 void myexit() { cudaDeviceReset(); } int main(...) { atexit(myexit); A t; return 0; } 
+3
source

All Articles