Skip to content

Commit ce87652

Browse files
authored
Merge pull request #2561 from Bensuo/ben/cmd-buffer-l0-fence
[L0][CMDBUF] Optimize fence/event waits during update
2 parents 48c7618 + 87b6158 commit ce87652

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

source/adapters/level_zero/command_buffer.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -1753,6 +1753,8 @@ ur_result_t urCommandBufferEnqueueExp(
17531753
EventWaitList, OutEvent, ZeCommandListHelper,
17541754
DoProfiling));
17551755
}
1756+
// Mark that synchronization will be required for later updates
1757+
CommandBuffer->NeedsUpdateSynchronization = true;
17561758

17571759
return UR_RESULT_SUCCESS;
17581760
}
@@ -2117,6 +2119,13 @@ ur_result_t updateKernelCommand(
21172119
*/
21182120
ur_result_t
21192121
waitForOngoingExecution(ur_exp_command_buffer_handle_t CommandBuffer) {
2122+
// Calling function has taken a lock for the command buffer so we can safely
2123+
// check and modify this value here.
2124+
// If command buffer was recently synchronized we can return early.
2125+
if (!CommandBuffer->NeedsUpdateSynchronization) {
2126+
return UR_RESULT_SUCCESS;
2127+
}
2128+
21202129
if (CommandBuffer->UseImmediateAppendPath) {
21212130
if (ur_event_handle_t &CurrentSubmissionEvent =
21222131
CommandBuffer->CurrentSubmissionEvent) {
@@ -2128,7 +2137,8 @@ waitForOngoingExecution(ur_exp_command_buffer_handle_t CommandBuffer) {
21282137
} else if (ze_fence_handle_t &ZeFence = CommandBuffer->ZeActiveFence) {
21292138
ZE2UR_CALL(zeFenceHostSynchronize, (ZeFence, UINT64_MAX));
21302139
}
2131-
2140+
// Mark that command buffer was recently synchronized
2141+
CommandBuffer->NeedsUpdateSynchronization = false;
21322142
return UR_RESULT_SUCCESS;
21332143
}
21342144

source/adapters/level_zero/command_buffer.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ struct ur_exp_command_buffer_handle_t_ : public _ur_object {
141141
// This list is needed to release all kernels retained by the
142142
// command_buffer.
143143
std::vector<ur_kernel_handle_t> KernelsList;
144+
// Track whether synchronization is required when updating the command buffer
145+
// Set this value to true when a command buffer is enqueued, and false after
146+
// any fence or event synchronization to avoid repeated calls to synchronize.
147+
bool NeedsUpdateSynchronization = false;
144148
};
145149

146150
struct ur_exp_command_buffer_command_handle_t_ : public _ur_object {

0 commit comments

Comments
 (0)