Skip to content

Commit 21593b6

Browse files
author
Fábio Mestre
committed
Fences and QueueGroups
1 parent cb47e73 commit 21593b6

File tree

2 files changed

+50
-36
lines changed

2 files changed

+50
-36
lines changed

source/adapters/level_zero/command_buffer.cpp

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ ur_exp_command_buffer_handle_t_::ur_exp_command_buffer_handle_t_(
6767
ZeCommandListResetEvents(CommandListResetEvents),
6868
ZeCommandListDesc(ZeDesc), ZeCopyCommandList(CopyCommandList),
6969
ZeCopyCommandListDesc(ZeCopyDesc), ZeFencesMap(), ZeActiveFence(nullptr),
70-
QueueProperties(), SyncPoints(), NextSyncPoint(0),
70+
SyncPoints(), NextSyncPoint(0),
7171
IsUpdatable(Desc ? Desc->isUpdatable : false),
7272
IsProfilingEnabled(Desc ? Desc->enableProfiling : false),
7373
IsInOrderCmdList(IsInOrderCmdList) {
@@ -350,21 +350,22 @@ createSyncPoint(ur_command_t CommandType,
350350
}
351351

352352
ur_result_t ur_exp_command_buffer_handle_t_::chooseCommandList(
353-
bool PreferCopyEngine, ze_command_list_handle_t &ZeCommandList) {
353+
bool PreferCopyEngine, ze_command_list_handle_t *ZeCommandList) {
354354
// If the copy engine available, the command is enqueued in the
355355
// ZeCopyCommandList.
356356
if (PreferCopyEngine && this->UseCopyEngine()) {
357357
// We indicate that the ZeCopyCommandList contains commands to be
358358
// submitted.
359359
this->MCopyCommandListEmpty = false;
360-
ZeCommandList = this->ZeCopyCommandList;
360+
*ZeCommandList = this->ZeCopyCommandList;
361361
}
362-
ZeCommandList = this->ZeComputeCommandList;
362+
*ZeCommandList = this->ZeComputeCommandList;
363+
return UR_RESULT_SUCCESS;
363364
}
364365

365-
//FIXME Probably overkill?
366+
// FIXME Probably overkill?
366367
ur_result_t ur_exp_command_buffer_handle_t_::chooseCommandList(
367-
bool PreferCopyEngine, ze_command_list_handle_t &ZeCommandList,
368+
bool PreferCopyEngine, ze_command_list_handle_t *ZeCommandList,
368369
size_t PatternSize) {
369370
// If the copy engine available and patternsize is valid, the command is
370371
// enqueued in the ZeCopyCommandList, otherwise enqueue it in the compute
@@ -385,7 +386,7 @@ ur_result_t ur_exp_command_buffer_handle_t_::chooseCommandList(
385386
.ZeProperties.maxMemoryFillPatternSize,
386387
UR_RESULT_ERROR_INVALID_VALUE);
387388
}
388-
chooseCommandList(PreferCopyEngine, ZeCommandList);
389+
UR_CALL(chooseCommandList(PreferCopyEngine, ZeCommandList));
389390
}
390391

391392
// Shared by all memory read/write/copy PI interfaces.
@@ -412,7 +413,7 @@ static ur_result_t enqueueCommandBufferMemCopyHelper(
412413
LaunchEvent));
413414

414415
ze_command_list_handle_t ZeCommandList;
415-
CommandBuffer->chooseCommandList(PreferCopyEngine, ZeCommandList);
416+
UR_CALL(CommandBuffer->chooseCommandList(PreferCopyEngine, &ZeCommandList));
416417

417418
ZE2UR_CALL(zeCommandListAppendMemoryCopy,
418419
(ZeCommandList, Dst, Src, Size, LaunchEvent->ZeEvent,
@@ -483,7 +484,7 @@ static ur_result_t enqueueCommandBufferMemCopyRectHelper(
483484
LaunchEvent));
484485

485486
ze_command_list_handle_t ZeCommandList;
486-
CommandBuffer->chooseCommandList(PreferCopyEngine, ZeCommandList);
487+
UR_CALL(CommandBuffer->chooseCommandList(PreferCopyEngine, &ZeCommandList));
487488

488489
ZE2UR_CALL(zeCommandListAppendMemoryCopyRegion,
489490
(ZeCommandList, Dst, &ZeDstRegion, DstPitch, DstSlicePitch, Src,
@@ -510,8 +511,8 @@ static ur_result_t enqueueCommandBufferFillHelper(
510511
UR_RESULT_ERROR_INVALID_VALUE);
511512

512513
ze_command_list_handle_t ZeCommandList;
513-
CommandBuffer->chooseCommandList(PreferCopyEngine, ZeCommandList,
514-
PatternSize);
514+
UR_CALL(CommandBuffer->chooseCommandList(PreferCopyEngine, &ZeCommandList,
515+
PatternSize));
515516

516517
if (CommandBuffer->IsInOrderCmdList) {
517518
ZE2UR_CALL(zeCommandListAppendMemoryFill,
@@ -1149,31 +1150,46 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendUSMFillExp(
11491150
SyncPoint);
11501151
}
11511152

1152-
UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferEnqueueExp(
1153-
ur_exp_command_buffer_handle_t CommandBuffer, ur_queue_handle_t UrQueue,
1154-
uint32_t NumEventsInWaitList, const ur_event_handle_t *EventWaitList,
1155-
ur_event_handle_t *Event) {
1156-
auto Queue = Legacy(UrQueue);
1157-
std::scoped_lock<ur_shared_mutex> lock(Queue->Mutex);
1158-
// Use compute engine rather than copy engine
1159-
const auto UseCopyEngine = false;
1160-
auto &QGroup = Queue->getQueueGroup(UseCopyEngine);
1161-
uint32_t QueueGroupOrdinal;
1162-
auto &ZeCommandQueue = QGroup.getZeQueue(&QueueGroupOrdinal);
1163-
1153+
ur_result_t ur_exp_command_buffer_handle_t_::getFence(
1154+
ze_command_queue_handle_t &ZeCommandQueue, ze_fence_handle_t &ZeFence) {
11641155
// If we already have created a fence for this queue, first reset then reuse
11651156
// it, otherwise create a new fence.
1166-
ze_fence_handle_t &ZeFence = CommandBuffer->ZeActiveFence;
1167-
auto ZeWorkloadFenceForQueue =
1168-
CommandBuffer->ZeFencesMap.find(ZeCommandQueue);
1169-
if (ZeWorkloadFenceForQueue == CommandBuffer->ZeFencesMap.end()) {
1157+
// ZeFence = this->ZeActiveFence;
1158+
auto ZeWorkloadFenceForQueue = this->ZeFencesMap.find(ZeCommandQueue);
1159+
if (ZeWorkloadFenceForQueue == this->ZeFencesMap.end()) {
11701160
ZeStruct<ze_fence_desc_t> ZeFenceDesc;
11711161
ZE2UR_CALL(zeFenceCreate, (ZeCommandQueue, &ZeFenceDesc, &ZeFence));
1172-
CommandBuffer->ZeFencesMap.insert({{ZeCommandQueue, ZeFence}});
1162+
this->ZeFencesMap.insert({{ZeCommandQueue, ZeFence}});
11731163
} else {
11741164
ZeFence = ZeWorkloadFenceForQueue->second;
11751165
ZE2UR_CALL(zeFenceReset, (ZeFence));
11761166
}
1167+
return UR_RESULT_SUCCESS;
1168+
}
1169+
1170+
ur_result_t ur_exp_command_buffer_handle_t_::getZeCommandQueue(
1171+
ur_queue_handle_t Queue, bool UseCopyEngine,
1172+
ze_command_queue_handle_t &ZeCommandQueue) {
1173+
// Use compute engine rather than copy engine
1174+
auto &QGroup = Queue->getQueueGroup(UseCopyEngine);
1175+
uint32_t QueueGroupOrdinal;
1176+
ZeCommandQueue = QGroup.getZeQueue(&QueueGroupOrdinal);
1177+
return UR_RESULT_SUCCESS;
1178+
}
1179+
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);
11771193

11781194
bool MustSignalWaitEvent = true;
11791195
if (NumEventsInWaitList) {
@@ -1229,9 +1245,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferEnqueueExp(
12291245
// The Copy command-list is submitted to the main copy queue if it is not
12301246
// empty.
12311247
if (!CommandBuffer->MCopyCommandListEmpty) {
1232-
auto &QGroupCopy = Queue->getQueueGroup(true);
1233-
uint32_t QueueGroupOrdinal;
1234-
auto &ZeCopyCommandQueue = QGroupCopy.getZeQueue(&QueueGroupOrdinal);
1248+
ze_command_queue_handle_t ZeCopyCommandQueue;
1249+
CommandBuffer->getZeCommandQueue(Queue, true, ZeCopyCommandQueue);
12351250
ZE2UR_CALL(
12361251
zeCommandQueueExecuteCommandLists,
12371252
(ZeCopyCommandQueue, 1, &CommandBuffer->ZeCopyCommandList, nullptr));

source/adapters/level_zero/command_buffer.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,12 @@ struct ur_exp_command_buffer_handle_t_ : public _ur_object {
5151
bool UseCopyEngine() const { return ZeCopyCommandList != nullptr; }
5252

5353
// FIXME Refactor Documentation
54+
ur_result_t getFence(ze_command_queue_handle_t & ZeCommandQueue, ze_fence_handle_t &ZeFence);
55+
ur_result_t getZeCommandQueue(ur_queue_handle_t Queue, bool UseCopyEngine, ze_command_queue_handle_t& ZeCommandQueue);
5456
ur_result_t chooseCommandList(bool PreferCopyEngine,
55-
ze_command_list_handle_t &ZeCommandList);
57+
ze_command_list_handle_t *ZeCommandList);
5658
ur_result_t chooseCommandList(bool PreferCopyEngine,
57-
ze_command_list_handle_t &ZeCommandList,
59+
ze_command_list_handle_t *ZeCommandList,
5860
size_t PatternSize);
5961

6062
// UR context associated with this command-buffer
@@ -84,9 +86,6 @@ struct ur_exp_command_buffer_handle_t_ : public _ur_object {
8486
// Must be an element in ZeFencesMap, so is not required to be destroyed
8587
// itself.
8688
ze_fence_handle_t ZeActiveFence;
87-
// Queue properties from command-buffer descriptor
88-
// TODO: Do we need these?
89-
ur_queue_properties_t QueueProperties;
9089
// Map of sync_points to ur_events
9190
std::unordered_map<ur_exp_command_buffer_sync_point_t, ur_event_handle_t>
9291
SyncPoints;

0 commit comments

Comments
 (0)