|
9 | 9 | //===----------------------------------------------------------------------===// |
10 | 10 |
|
11 | 11 | #include "kernel.hpp" |
| 12 | +#include "common.hpp" |
12 | 13 | #include "logger/ur_logger.hpp" |
| 14 | +#include "queue.hpp" |
13 | 15 | #include "ur_api.h" |
14 | 16 | #include "ur_interface_loader.hpp" |
15 | 17 |
|
@@ -221,17 +223,22 @@ ur_result_t urEnqueueKernelLaunch( |
221 | 223 | UR_CALL(Queue->executeCommandList(CommandList, false /*IsBlocking*/, |
222 | 224 | true /*OKToBatchCommand*/)); |
223 | 225 |
|
224 | | - // For internal events, trigger cleanup to prevent event pool exhaustion |
225 | | - // by ensuring completed internal events are cleaned up periodically |
226 | | - if (IsInternal) { |
227 | | - if (Queue->UsingImmCmdLists) { |
228 | | - UR_CALL(CleanupEventsInImmCmdLists(Queue, false /*QueueLocked*/, |
229 | | - false /*QueueSynced*/, |
230 | | - nullptr /*CompletedEvent*/)); |
231 | | - } else { |
232 | | - // For regular command lists, call resetCommandLists to clean up |
233 | | - // completed command lists and their associated events |
234 | | - UR_CALL(resetCommandLists(Queue)); |
| 226 | + // For internal events only, occasionally trigger cleanup to prevent event |
| 227 | + // pool exhaustion, but avoid doing this for in-order queues which are |
| 228 | + // commonly used in multi-threaded scenarios and may have stricter |
| 229 | + // synchronization requirements |
| 230 | + if (IsInternal && !Queue->isInOrderQueue()) { |
| 231 | + // Use a probabilistic approach - only cleanup 1 in 128 internal events |
| 232 | + // to minimize performance impact and reduce chances of race conditions |
| 233 | + static thread_local uint32_t CleanupCounter = 0; |
| 234 | + if ((++CleanupCounter & 127) == 0) { |
| 235 | + if (Queue->UsingImmCmdLists) { |
| 236 | + UR_CALL(CleanupEventsInImmCmdLists(Queue, false /*QueueLocked*/, |
| 237 | + false /*QueueSynced*/, |
| 238 | + nullptr /*CompletedEvent*/)); |
| 239 | + } else { |
| 240 | + UR_CALL(resetCommandLists(Queue)); |
| 241 | + } |
235 | 242 | } |
236 | 243 | } |
237 | 244 |
|
|
0 commit comments