[CUDA] Make CudaEvent work with multi-device#2614
Conversation
angeloskath
left a comment
There was a problem hiding this comment.
This looks great to me!
I am missing something here for sure, when will events be created and destroyed in a worker thread 🤔, I thought they would be created and destroyed in the eval loop. I guess that this is precisely your point, they will only be created and destroyed in the eval loop but in case they are created and destroyed somewhere else then skip the cache ...
Waiting a CPU stream is implemented by copying the event to the CPU thread, so the event could be destroyed there: Lines 91 to 97 in 7a6adda Another case is user may run |
* Set current device when creating cuda event * Separate cuda events by device * Avoid race condition in pool
Most APIs of CUDA event have requirements on being used on the same device with the device that the event was created on, this PR makes sure the
CudaEventclass set current device before invoking the APIs.There is also an issue that the
CudaEventcould be destroyed in worker thread when used for waiting on a GPU work in CPU stream, which would cause race conditions when accessing event cache, this PR uses a simple strategy by skipping the cache when event is created or destroyed on worker threads, so we don't have to add thread-safety overhead for those rare cases.Note that this PR does not guarantee multi-thread or multi-device safety, this is mostly a defensive change.