Skip to content

Commit c9ac1ba

Browse files
committed
Simplify enqueue
1 parent 21593b6 commit c9ac1ba

File tree

1 file changed

+73
-50
lines changed

1 file changed

+73
-50
lines changed

source/adapters/level_zero/command_buffer.cpp

Lines changed: 73 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,20 +1177,13 @@ ur_result_t ur_exp_command_buffer_handle_t_::getZeCommandQueue(
11771177
return UR_RESULT_SUCCESS;
11781178
}
11791179

1180-
UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferEnqueueExp(
1181-
ur_exp_command_buffer_handle_t CommandBuffer, ur_queue_handle_t UrQueue,
1182-
uint32_t NumEventsInWaitList, const ur_event_handle_t *EventWaitList,
1183-
ur_event_handle_t *Event) {
1184-
auto Queue = Legacy(UrQueue);
1185-
std::scoped_lock<ur_shared_mutex> lock(Queue->Mutex);
1186-
1187-
const auto UseCopyEngine = false;
1188-
ze_command_queue_handle_t ZeCommandQueue;
1189-
CommandBuffer->getZeCommandQueue(Queue, false, ZeCommandQueue);
1190-
1191-
ze_fence_handle_t ZeFence;
1192-
CommandBuffer->getFence(ZeCommandQueue, ZeFence);
1193-
1180+
// FIXME Refactor Document
1181+
static ur_result_t
1182+
waitForDependencies(ur_exp_command_buffer_handle_t CommandBuffer,
1183+
ur_queue_handle_t Queue, uint32_t NumEventsInWaitList,
1184+
const ur_event_handle_t *EventWaitList) {
1185+
_ur_ze_event_list_t TmpWaitList;
1186+
const bool UseCopyEngine = false;
11941187
bool MustSignalWaitEvent = true;
11951188
if (NumEventsInWaitList) {
11961189
_ur_ze_event_list_t TmpWaitList;
@@ -1225,6 +1218,70 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferEnqueueExp(
12251218
if (MustSignalWaitEvent) {
12261219
ZE2UR_CALL(zeEventHostSignal, (CommandBuffer->WaitEvent->ZeEvent));
12271220
}
1221+
return UR_RESULT_SUCCESS;
1222+
}
1223+
1224+
// FIXME Refactor Document
1225+
static ur_result_t createUserEvent(ur_exp_command_buffer_handle_t CommandBuffer,
1226+
ur_queue_handle_t Queue,
1227+
ur_command_list_ptr_t SignalCommandList,
1228+
ur_event_handle_t &Event) {
1229+
// Execution event for this enqueue of the UR command-buffer
1230+
ur_event_handle_t RetEvent{};
1231+
1232+
UR_CALL(createEventAndAssociateQueue(Queue, &RetEvent,
1233+
UR_COMMAND_COMMAND_BUFFER_ENQUEUE_EXP,
1234+
SignalCommandList, false, false, true));
1235+
1236+
if ((Queue->Properties & UR_QUEUE_FLAG_PROFILING_ENABLE) &&
1237+
(!CommandBuffer->IsInOrderCmdList) &&
1238+
(CommandBuffer->IsProfilingEnabled)) {
1239+
// Multiple submissions of a command buffer implies that we need to save
1240+
// the event timestamps before resubmiting the command buffer. We
1241+
// therefore copy the these timestamps in a dedicated USM memory section
1242+
// before completing the command buffer execution, and then attach this
1243+
// memory to the event returned to users to allow to allow the profiling
1244+
// engine to recover these timestamps.
1245+
command_buffer_profiling_t *Profiling = new command_buffer_profiling_t();
1246+
1247+
Profiling->NumEvents = CommandBuffer->ZeEventsList.size();
1248+
Profiling->Timestamps =
1249+
new ze_kernel_timestamp_result_t[Profiling->NumEvents];
1250+
1251+
ZE2UR_CALL(zeCommandListAppendQueryKernelTimestamps,
1252+
(SignalCommandList->first, CommandBuffer->ZeEventsList.size(),
1253+
CommandBuffer->ZeEventsList.data(),
1254+
(void *)Profiling->Timestamps, 0, RetEvent->ZeEvent, 1,
1255+
&(CommandBuffer->SignalEvent->ZeEvent)));
1256+
1257+
RetEvent->CommandData = static_cast<void *>(Profiling);
1258+
} else {
1259+
ZE2UR_CALL(zeCommandListAppendBarrier,
1260+
(SignalCommandList->first, RetEvent->ZeEvent, 1,
1261+
&(CommandBuffer->SignalEvent->ZeEvent)));
1262+
}
1263+
1264+
Event = RetEvent;
1265+
1266+
return UR_RESULT_SUCCESS;
1267+
}
1268+
1269+
UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferEnqueueExp(
1270+
ur_exp_command_buffer_handle_t CommandBuffer, ur_queue_handle_t UrQueue,
1271+
uint32_t NumEventsInWaitList, const ur_event_handle_t *EventWaitList,
1272+
ur_event_handle_t *Event) {
1273+
auto Queue = Legacy(UrQueue);
1274+
std::scoped_lock<ur_shared_mutex> lock(Queue->Mutex);
1275+
1276+
const auto UseCopyEngine = false;
1277+
ze_command_queue_handle_t ZeCommandQueue;
1278+
CommandBuffer->getZeCommandQueue(Queue, false, ZeCommandQueue);
1279+
1280+
ze_fence_handle_t ZeFence;
1281+
CommandBuffer->getFence(ZeCommandQueue, ZeFence);
1282+
1283+
UR_CALL(waitForDependencies(CommandBuffer, Queue, NumEventsInWaitList,
1284+
EventWaitList));
12281285

12291286
// Submit reset events command-list. This command-list is of a batch
12301287
// command-list type, regardless of the UR Queue type. We therefore need to
@@ -1271,44 +1328,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferEnqueueExp(
12711328
(SignalCommandList->first, CommandBuffer->AllResetEvent->ZeEvent));
12721329

12731330
if (Event) {
1274-
UR_CALL(createEventAndAssociateQueue(
1275-
Queue, &RetEvent, UR_COMMAND_COMMAND_BUFFER_ENQUEUE_EXP,
1276-
SignalCommandList, false, false, true));
1277-
1278-
if ((Queue->Properties & UR_QUEUE_FLAG_PROFILING_ENABLE) &&
1279-
(!CommandBuffer->IsInOrderCmdList) &&
1280-
(CommandBuffer->IsProfilingEnabled)) {
1281-
// Multiple submissions of a command buffer implies that we need to save
1282-
// the event timestamps before resubmiting the command buffer. We
1283-
// therefore copy the these timestamps in a dedicated USM memory section
1284-
// before completing the command buffer execution, and then attach this
1285-
// memory to the event returned to users to allow to allow the profiling
1286-
// engine to recover these timestamps.
1287-
command_buffer_profiling_t *Profiling = new command_buffer_profiling_t();
1288-
1289-
Profiling->NumEvents = CommandBuffer->ZeEventsList.size();
1290-
Profiling->Timestamps =
1291-
new ze_kernel_timestamp_result_t[Profiling->NumEvents];
1292-
1293-
ZE2UR_CALL(zeCommandListAppendQueryKernelTimestamps,
1294-
(SignalCommandList->first, CommandBuffer->ZeEventsList.size(),
1295-
CommandBuffer->ZeEventsList.data(),
1296-
(void *)Profiling->Timestamps, 0, RetEvent->ZeEvent, 1,
1297-
&(CommandBuffer->SignalEvent->ZeEvent)));
1298-
1299-
RetEvent->CommandData = static_cast<void *>(Profiling);
1300-
} else {
1301-
ZE2UR_CALL(zeCommandListAppendBarrier,
1302-
(SignalCommandList->first, RetEvent->ZeEvent, 1,
1303-
&(CommandBuffer->SignalEvent->ZeEvent)));
1304-
}
1331+
UR_CALL(createUserEvent(CommandBuffer, Queue, SignalCommandList, *Event));
13051332
}
13061333

1307-
Queue->executeCommandList(SignalCommandList, false, false);
1308-
1309-
if (Event) {
1310-
*Event = RetEvent;
1311-
}
1334+
UR_CALL(Queue->executeCommandList(SignalCommandList, false, false));
13121335

13131336
return UR_RESULT_SUCCESS;
13141337
}

0 commit comments

Comments
 (0)