Skip to content

Commit 7c8617b

Browse files
authored
Merge pull request #965 from Bensuo/ewan/L0_internal_event_fix
[Command-buffer][L0] Reset sync-point events
2 parents 20d4f49 + 9a13afd commit 7c8617b

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

source/adapters/level_zero/command_buffer.cpp

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
3939
There is also a WaitEvent used by the `ur_exp_command_buffer_handle_t` class
4040
in the prefix to wait on any dependencies passed in the enqueue wait-list.
41+
This WaitEvent is reset at the end of the suffix, along with reset commands
42+
to reset the L0 events used to implement the UR sync-points.
4143
4244
┌──────────┬────────────────────────────────────────────────┬─────────┐
4345
│ Prefix │ Commands added to UR command-buffer by UR user │ Suffix │
@@ -47,10 +49,10 @@
4749
Prefix │Reset signal event │ Barrier waiting on wait event│
4850
└───────────────────┴──────────────────────────────┘
4951
50-
┌─────────────────────────────────────────┐
51-
Suffix │Signal the UR command-buffer signal event│
52-
└─────────────────────────────────────────┘
53-
52+
┌─────────────────────────────────────────────┐──────────────
53+
Suffix │Barrier waiting on sync-point event, │ Reset events
54+
│signalling the UR command-buffer signal event│ │
55+
└─────────────────────────────────────────────┘──────────────┘
5456
5557
For a call to `urCommandBufferEnqueueExp` with an event_list `EL`,
5658
command-buffer `CB`, and return event `RE` our implementation has to create
@@ -293,7 +295,7 @@ static ur_result_t enqueueCommandBufferMemCopyHelper(
293295
SyncPointWaitList, ZeEventList));
294296

295297
ur_event_handle_t LaunchEvent;
296-
UR_CALL(EventCreate(CommandBuffer->Context, nullptr, true, &LaunchEvent));
298+
UR_CALL(EventCreate(CommandBuffer->Context, nullptr, false, &LaunchEvent));
297299
LaunchEvent->CommandType = CommandType;
298300

299301
// Get sync point and register the event with it.
@@ -358,7 +360,7 @@ static ur_result_t enqueueCommandBufferMemCopyRectHelper(
358360
SyncPointWaitList, ZeEventList));
359361

360362
ur_event_handle_t LaunchEvent;
361-
UR_CALL(EventCreate(CommandBuffer->Context, nullptr, true, &LaunchEvent));
363+
UR_CALL(EventCreate(CommandBuffer->Context, nullptr, false, &LaunchEvent));
362364
LaunchEvent->CommandType = CommandType;
363365

364366
// Get sync point and register the event with it.
@@ -407,7 +409,7 @@ urCommandBufferCreateExp(ur_context_handle_t Context, ur_device_handle_t Device,
407409
// Create signal & wait events to be used in the command-list for sync
408410
// on command-buffer enqueue.
409411
auto RetCommandBuffer = *CommandBuffer;
410-
UR_CALL(EventCreate(Context, nullptr, true, &RetCommandBuffer->SignalEvent));
412+
UR_CALL(EventCreate(Context, nullptr, false, &RetCommandBuffer->SignalEvent));
411413
UR_CALL(EventCreate(Context, nullptr, false, &RetCommandBuffer->WaitEvent));
412414

413415
// Add prefix commands
@@ -437,11 +439,31 @@ urCommandBufferReleaseExp(ur_exp_command_buffer_handle_t CommandBuffer) {
437439

438440
UR_APIEXPORT ur_result_t UR_APICALL
439441
urCommandBufferFinalizeExp(ur_exp_command_buffer_handle_t CommandBuffer) {
440-
// We need to append signal that will indicate that command-buffer has
441-
// finished executing.
442-
ZE2UR_CALL(
443-
zeCommandListAppendSignalEvent,
444-
(CommandBuffer->ZeCommandList, CommandBuffer->SignalEvent->ZeEvent));
442+
// Create a list of events for our signal event to wait on
443+
const size_t NumEvents = CommandBuffer->SyncPoints.size();
444+
std::vector<ze_event_handle_t> WaitEventList{NumEvents};
445+
for (size_t i = 0; i < NumEvents; i++) {
446+
WaitEventList[i] = CommandBuffer->SyncPoints[i]->ZeEvent;
447+
}
448+
449+
// Wait for all the user added commands to complete, and signal the
450+
// command-buffer signal-event when they are done.
451+
ZE2UR_CALL(zeCommandListAppendBarrier,
452+
(CommandBuffer->ZeCommandList, CommandBuffer->SignalEvent->ZeEvent,
453+
NumEvents, WaitEventList.data()));
454+
455+
// Reset the wait-event for the UR command-buffer that is signalled when its
456+
// submission dependencies have been satisfied.
457+
ZE2UR_CALL(zeCommandListAppendEventReset,
458+
(CommandBuffer->ZeCommandList, CommandBuffer->WaitEvent->ZeEvent));
459+
460+
// Reset the L0 events we use for command-buffer internal sync-points to the
461+
// non-signalled state
462+
for (auto Event : WaitEventList) {
463+
ZE2UR_CALL(zeCommandListAppendEventReset,
464+
(CommandBuffer->ZeCommandList, Event));
465+
}
466+
445467
// Close the command list and have it ready for dispatch.
446468
ZE2UR_CALL(zeCommandListClose, (CommandBuffer->ZeCommandList));
447469
return UR_RESULT_SUCCESS;
@@ -497,7 +519,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
497519
UR_CALL(getEventsFromSyncPoints(CommandBuffer, NumSyncPointsInWaitList,
498520
SyncPointWaitList, ZeEventList));
499521
ur_event_handle_t LaunchEvent;
500-
UR_CALL(EventCreate(CommandBuffer->Context, nullptr, true, &LaunchEvent));
522+
UR_CALL(EventCreate(CommandBuffer->Context, nullptr, false, &LaunchEvent));
501523
LaunchEvent->CommandType = UR_COMMAND_KERNEL_LAUNCH;
502524

503525
// Get sync point and register the event with it.
@@ -727,7 +749,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferEnqueueExp(
727749
(WaitCommandList->first, CommandBuffer->WaitEvent->ZeEvent));
728750
}
729751

730-
// Execution event for this enqueue of the PI command-buffer
752+
// Execution event for this enqueue of the UR command-buffer
731753
ur_event_handle_t RetEvent{};
732754
// Create a command-list to signal RetEvent on completion
733755
ur_command_list_ptr_t SignalCommandList{};

0 commit comments

Comments
 (0)