Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should CUDA events and streams always be destroyed?

I am reading CUDA By Example and I found that when they introduced events, they called cudaEventDestroy for each event they created.

However I noticed that some later examples neglected this cleanup function. Are there any undesirable side-effects of forgetting to destroy created events and streams (i.e. like a memory leak when you forget to free allocated memory)?

like image 987
tskuzzy Avatar asked Jun 06 '12 18:06

tskuzzy


People also ask

What are CUDA streams?

A stream in CUDA is a sequence of operations that execute on the device in the order in which they are issued by the host code. While operations within a stream are guaranteed to execute in the prescribed order, operations in different streams can be interleaved and, when possible, they can even run concurrently.

What is a CUDA event?

CUDA events are synchronization markers that can be used to monitor the device's progress, to accurately measure timing, and to synchronize CUDA streams. The underlying CUDA events are lazily initialized when the event is first recorded or exported to another process.

How many CUDA streams can I create?

There is no realistic limit to the number of streams you can create (at least 1000's). However, there's a limit to the number of streams you can use effectively to achieve concurrency. In Fermi, the architecture supports 16-way concurrent kernel launches, but there is only a single connection from the host to the GPU.


2 Answers

Any resources the app is still holding at the time it exits will be automatically free'ed by the OS / drivers. So, if the app creates only a limited number of events, it is not strictly necessary to free them manually. Still, letting the app exit without freeing all resources on purpose is bad practice because it becomes hard to distinguish between genuine leaks and "on purpose" leaks.

like image 79
Roger Dahl Avatar answered Oct 23 '22 04:10

Roger Dahl


You have identified bugs in the book's sample code.

CUDA events are lightweight, but a resource leak is a resource leak. Over time, if you leak enough of them, you won't be able to create them anymore.

like image 25
ArchaeaSoftware Avatar answered Oct 23 '22 03:10

ArchaeaSoftware