Skip to content

Commit 2bf09b9

Browse files
occasional sync/cleanup
Signed-off-by: Zhang, Winston <[email protected]>
1 parent 6f240fc commit 2bf09b9

File tree

1 file changed

+18
-11
lines changed
  • unified-runtime/source/adapters/level_zero

1 file changed

+18
-11
lines changed

unified-runtime/source/adapters/level_zero/kernel.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
//===----------------------------------------------------------------------===//
1010

1111
#include "kernel.hpp"
12+
#include "common.hpp"
1213
#include "logger/ur_logger.hpp"
14+
#include "queue.hpp"
1315
#include "ur_api.h"
1416
#include "ur_interface_loader.hpp"
1517

@@ -221,17 +223,22 @@ ur_result_t urEnqueueKernelLaunch(
221223
UR_CALL(Queue->executeCommandList(CommandList, false /*IsBlocking*/,
222224
true /*OKToBatchCommand*/));
223225

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+
}
235242
}
236243
}
237244

0 commit comments

Comments
 (0)