In our large code base, I could find there are multiple cudaStreamCreate()
functions. However, I could not find cudaStreamDestroy()
anywhere. Is it important to destroy streams after program is complete or one does not need to worry about this? What is a good programming practice in this regard?
-
As CUDA is a C++ dialect, I would argue that it would be good practice to package streams into an RAII wrapper which takes care of this at destruction. Not sure if asking for good practice is already to opinion-based and therefore OT. – paleonix Jan 30 '23 at 08:21
-
@paleonix: Right you are, and see my answer about that. – einpoklum Jan 31 '23 at 23:08
2 Answers
Is it important to destroy streams after program is complete or one does not need to worry about this?
The runtime API will clean up all resources allocated (streams, memory, events, etc) by the context owned by the process during normal process termination. It isn't necessary to explicitly destroy streams in most situations.

- 70,661
- 34
- 192
- 269
While talonmies answer is correct, it is still often important to destroy your streams, and other entities created in CUDA:
- If you're writing a library - you may finish your work well before the application exits. (Although in that case you might be working in a different CUDA context, and maybe you'll simply destroy the whole context).
- If your code which creates streams might be called many times.
also, if you don't synchronize your streams after completing all work on them, then you might be missing some errors (and the results of your last bits of work); and if you do have a "last synch", that often means an opportunity to also destroy the stream.
Finally, if you use C++-flavored wrappers, like mine, then streams get destroyed when you leave the scope in which they were created, and you don't have to worry about it (but you pay the overhead of stream destruction API calls).

- 118,144
- 57
- 340
- 684