Skip to content

Commit

Permalink
[L0] Refactor to remove default constructor inits
Browse files Browse the repository at this point in the history
- Remove all the default constructor inits to address error prone code
  changes and force setting of options and flags individually.

Signed-off-by: Neil R. Spruit <neil.r.spruit@intel.com>
  • Loading branch information
nrspruit committed Sep 5, 2024
1 parent d50a59e commit 97dd83d
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 79 deletions.
49 changes: 30 additions & 19 deletions source/adapters/level_zero/command_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ ur_result_t createSyncPointAndGetZeEvents(
UR_CALL(getEventsFromSyncPoints(CommandBuffer, NumSyncPointsInWaitList,
SyncPointWaitList, ZeEventList));
ur_event_handle_t LaunchEvent;
UR_CALL(EventCreate(CommandBuffer->Context, nullptr, false, HostVisible,
&LaunchEvent, false, !CommandBuffer->IsProfilingEnabled));
UR_CALL(EventCreate(CommandBuffer->Context, nullptr /*Queue*/,
false /*IsMultiDevice*/, HostVisible, &LaunchEvent,
false /*CounterBasedEventEnabled*/,
!CommandBuffer->IsProfilingEnabled));
LaunchEvent->CommandType = CommandType;
ZeLaunchEvent = LaunchEvent->ZeEvent;

Expand Down Expand Up @@ -325,22 +327,26 @@ void ur_exp_command_buffer_handle_t_::cleanupCommandBufferResources() {

// Release additional signal and wait events used by command_buffer
if (SignalEvent) {
CleanupCompletedEvent(SignalEvent, false);
CleanupCompletedEvent(SignalEvent, false /*QueueLocked*/,
false /*SetEventCompleted*/);
urEventReleaseInternal(SignalEvent);
}
if (WaitEvent) {
CleanupCompletedEvent(WaitEvent, false);
CleanupCompletedEvent(WaitEvent, false /*QueueLocked*/,
false /*SetEventCompleted*/);
urEventReleaseInternal(WaitEvent);
}
if (AllResetEvent) {
CleanupCompletedEvent(AllResetEvent, false);
CleanupCompletedEvent(AllResetEvent, false /*QueueLocked*/,
false /*SetEventCompleted*/);
urEventReleaseInternal(AllResetEvent);
}

// Release events added to the command_buffer
for (auto &Sync : SyncPoints) {
auto &Event = Sync.second;
CleanupCompletedEvent(Event, false);
CleanupCompletedEvent(Event, false /*QueueLocked*/,
false /*SetEventCompleted*/);
urEventReleaseInternal(Event);
}

Expand Down Expand Up @@ -514,12 +520,15 @@ urCommandBufferCreateExp(ur_context_handle_t Context, ur_device_handle_t Device,
ur_event_handle_t WaitEvent;
ur_event_handle_t AllResetEvent;

UR_CALL(EventCreate(Context, nullptr, false, false, &SignalEvent, false,
!EnableProfiling));
UR_CALL(EventCreate(Context, nullptr, false, false, &WaitEvent, false,
!EnableProfiling));
UR_CALL(EventCreate(Context, nullptr, false, false, &AllResetEvent, false,
!EnableProfiling));
UR_CALL(EventCreate(Context, nullptr /*Queue*/, false /*IsMultiDevice*/,
false /*HostVisible*/, &SignalEvent,
false /*CounterBasedEventEnabled*/, !EnableProfiling));
UR_CALL(EventCreate(Context, nullptr /*Queue*/, false /*IsMultiDevice*/,
false /*HostVisible*/, &WaitEvent,
false /*CounterBasedEventEnabled*/, !EnableProfiling));
UR_CALL(EventCreate(Context, nullptr /*Queue*/, false /*IsMultiDevice*/,
false /*HostVisible*/, &AllResetEvent,
false /*CounterBasedEventEnabled*/, !EnableProfiling));
std::vector<ze_event_handle_t> PrecondEvents = {WaitEvent->ZeEvent,
AllResetEvent->ZeEvent};

Expand Down Expand Up @@ -1151,14 +1160,15 @@ ur_result_t waitForDependencies(ur_exp_command_buffer_handle_t CommandBuffer,
// when `EventWaitList` dependencies are complete.
ur_command_list_ptr_t WaitCommandList{};
UR_CALL(Queue->Context->getAvailableCommandList(
Queue, WaitCommandList, false, NumEventsInWaitList, EventWaitList,
false));
Queue, WaitCommandList, false /*UseCopyEngine*/, NumEventsInWaitList,
EventWaitList, false /*AllowBatching*/, nullptr /*ForcedCmdQueue*/));

ZE2UR_CALL(zeCommandListAppendBarrier,
(WaitCommandList->first, CommandBuffer->WaitEvent->ZeEvent,
CommandBuffer->WaitEvent->WaitList.Length,
CommandBuffer->WaitEvent->WaitList.ZeEventList));
Queue->executeCommandList(WaitCommandList, false, false);
Queue->executeCommandList(WaitCommandList, false /*IsBlocking*/,
false /*OKToBatchCommand*/);
MustSignalWaitEvent = false;
}
}
Expand Down Expand Up @@ -1270,9 +1280,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferEnqueueExp(

// Create a command-list to signal the Event on completion
ur_command_list_ptr_t SignalCommandList{};
UR_CALL(Queue->Context->getAvailableCommandList(Queue, SignalCommandList,
false, NumEventsInWaitList,
EventWaitList, false));
UR_CALL(Queue->Context->getAvailableCommandList(
Queue, SignalCommandList, false /*UseCopyEngine*/, NumEventsInWaitList,
EventWaitList, false /*AllowBatching*/, nullptr /*ForcedCmdQueue*/));

// Reset the wait-event for the UR command-buffer that is signaled when its
// submission dependencies have been satisfied.
Expand All @@ -1287,7 +1297,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferEnqueueExp(
// parameter with signal command-list completing.
UR_CALL(createUserEvent(CommandBuffer, Queue, SignalCommandList, Event));

UR_CALL(Queue->executeCommandList(SignalCommandList, false, false));
UR_CALL(Queue->executeCommandList(SignalCommandList, false /*IsBlocking*/,
false /*OKToBatchCommand*/));

return UR_RESULT_SUCCESS;
}
Expand Down
4 changes: 2 additions & 2 deletions source/adapters/level_zero/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,8 @@ struct ur_context_handle_t_ : _ur_object {
ur_result_t getAvailableCommandList(
ur_queue_handle_t Queue, ur_command_list_ptr_t &CommandList,
bool UseCopyEngine, uint32_t NumEventsInWaitList,
const ur_event_handle_t *EventWaitList, bool AllowBatching = false,
ze_command_queue_handle_t *ForcedCmdQueue = nullptr);
const ur_event_handle_t *EventWaitList, bool AllowBatching,
ze_command_queue_handle_t *ForcedCmdQueue);

// Checks if Device is covered by this context.
// For that the Device or its root devices need to be in the context.
Expand Down
43 changes: 29 additions & 14 deletions source/adapters/level_zero/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueEventsWait(
// Get a new command list to be used on this call
ur_command_list_ptr_t CommandList{};
UR_CALL(Queue->Context->getAvailableCommandList(
Queue, CommandList, UseCopyEngine, NumEventsInWaitList, EventWaitList));
Queue, CommandList, UseCopyEngine, NumEventsInWaitList, EventWaitList,
false /*AllowBatching*/, nullptr /*ForceCmdQueue*/));

ze_event_handle_t ZeEvent = nullptr;
ur_event_handle_t InternalEvent;
Expand All @@ -106,7 +107,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueEventsWait(

// Execute command list asynchronously as the event will be used
// to track down its completion.
return Queue->executeCommandList(CommandList);
return Queue->executeCommandList(CommandList, false /*IsBlocking*/,
false /*OKToBatchCommand*/);
}

{
Expand Down Expand Up @@ -258,12 +260,13 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueEventsWaitWithBarrier(
ur_command_list_ptr_t CmdList;
UR_CALL(Queue->Context->getAvailableCommandList(
Queue, CmdList, false /*UseCopyEngine=*/, NumEventsInWaitList,
EventWaitList, OkToBatch));
EventWaitList, OkToBatch, nullptr /*ForcedCmdQueue*/));

// Insert the barrier into the command-list and execute.
UR_CALL(insertBarrierIntoCmdList(CmdList, TmpWaitList, *Event, IsInternal));

UR_CALL(Queue->executeCommandList(CmdList, false, OkToBatch));
UR_CALL(
Queue->executeCommandList(CmdList, false /*IsBlocking*/, OkToBatch));

// Because of the dependency between commands in the in-order queue we don't
// need to keep track of any active barriers if we have in-order queue.
Expand Down Expand Up @@ -328,7 +331,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueEventsWaitWithBarrier(
ur_command_list_ptr_t CmdList;
UR_CALL(Queue->Context->getAvailableCommandList(
Queue, CmdList, false /*UseCopyEngine=*/, NumEventsInWaitList,
EventWaitList, OkToBatch));
EventWaitList, OkToBatch, nullptr /*ForcedCmdQueue*/));
CmdLists.push_back(CmdList);
}

Expand Down Expand Up @@ -377,7 +380,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueEventsWaitWithBarrier(
// Only batch if the matching CmdList is already open.
OkToBatch = CommandBatch.OpenCommandList == CmdList;

UR_CALL(Queue->executeCommandList(CmdList, false, OkToBatch));
UR_CALL(
Queue->executeCommandList(CmdList, false /*IsBlocking*/, OkToBatch));
}

UR_CALL(Queue->ActiveBarriers.clear());
Expand Down Expand Up @@ -687,7 +691,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueTimestampRecordingExp(
ur_command_list_ptr_t CommandList{};
UR_CALL(Queue->Context->getAvailableCommandList(
Queue, CommandList, UseCopyEngine, NumEventsInWaitList, EventWaitList,
/* AllowBatching */ false));
/* AllowBatching */ false, nullptr /*ForcedCmdQueue*/));

UR_CALL(createEventAndAssociateQueue(
Queue, OutEvent, UR_COMMAND_TIMESTAMP_RECORDING_EXP, CommandList,
Expand All @@ -710,7 +714,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueTimestampRecordingExp(
(*OutEvent)->WaitList.ZeEventList));

UR_CALL(
Queue->executeCommandList(CommandList, Blocking, /* OkToBatch */ false));
Queue->executeCommandList(CommandList, Blocking, false /* OkToBatch */));

return UR_RESULT_SUCCESS;
}
Expand Down Expand Up @@ -738,7 +742,8 @@ ur_result_t ur_event_handle_t_::getOrCreateHostVisibleEvent(

ur_command_list_ptr_t CommandList{};
UR_CALL(UrQueue->Context->getAvailableCommandList(
UrQueue, CommandList, false /* UseCopyEngine */, 0, nullptr, OkToBatch))
UrQueue, CommandList, false /* UseCopyEngine */, 0, nullptr, OkToBatch,
nullptr /*ForcedCmdQueue*/))

// Create a "proxy" host-visible event.
UR_CALL(createEventAndAssociateQueue(
Expand All @@ -756,7 +761,8 @@ ur_result_t ur_event_handle_t_::getOrCreateHostVisibleEvent(
ZE2UR_CALL(zeCommandListAppendSignalEvent,
(CommandList->first, HostVisibleEvent->ZeEvent));

UR_CALL(UrQueue->executeCommandList(CommandList, false, OkToBatch))
UR_CALL(UrQueue->executeCommandList(CommandList, false /*IsBlocking*/,
OkToBatch))
this->IsCreatingHostProxyEvent = false;
}

Expand Down Expand Up @@ -835,7 +841,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventWait(
else {
// NOTE: we are cleaning up after the event here to free resources
// sooner in case run-time is not calling urEventRelease soon enough.
CleanupCompletedEvent(reinterpret_cast<ur_event_handle_t>(Event));
CleanupCompletedEvent(reinterpret_cast<ur_event_handle_t>(Event),
false /*QueueLocked*/,
false /*SetEventCompleted*/);
// For the case when we have out-of-order queue or regular command
// lists its more efficient to check fences so put the queue in the
// set to cleanup later.
Expand Down Expand Up @@ -903,7 +911,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urExtEventCreate(
ur_event_handle_t
*Event ///< [out] pointer to the handle of the event object created.
) {
UR_CALL(EventCreate(Context, nullptr, false, true, Event));
UR_CALL(EventCreate(Context, nullptr /*Queue*/, false /*IsMultiDevice*/,
true /*HostVisible*/, Event,
false /*CounterBasedEventEnabled*/,
false /*ForceDisableProfiling*/));

(*Event)->RefCountExternal++;
if (!(*Event)->CounterBasedEventsEnabled)
Expand All @@ -922,7 +933,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventCreateWithNativeHandle(
// we dont have urEventCreate, so use this check for now to know that
// the call comes from urEventCreate()
if (reinterpret_cast<ze_event_handle_t>(NativeEvent) == nullptr) {
UR_CALL(EventCreate(Context, nullptr, false, true, Event));
UR_CALL(EventCreate(Context, nullptr /*Queue*/, false /*IsMultiDevice*/,
true /*HostVisible*/, Event,
false /*CounterBasedEventEnabled*/,
false /*ForceDisableProfiling*/));

(*Event)->RefCountExternal++;
if (!(*Event)->CounterBasedEventsEnabled)
Expand Down Expand Up @@ -1497,7 +1511,8 @@ ur_result_t _ur_ze_event_list_t::createAndRetainUrZeEventList(
// This prevents a potential deadlock with recursive
// event locks.
UR_CALL(Queue->Context->getAvailableCommandList(
Queue, CommandList, false, 0, nullptr, true));
Queue, CommandList, false /*UseCopyEngine*/, 0, nullptr,
true /*AllowBatching*/, nullptr /*ForcedCmdQueue*/));
}

std::shared_lock<ur_shared_mutex> Lock(EventList[I]->Mutex);
Expand Down
9 changes: 4 additions & 5 deletions source/adapters/level_zero/event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ ur_result_t urEventReleaseInternal(ur_event_handle_t Event);
ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
bool IsMultiDevice, bool HostVisible,
ur_event_handle_t *RetEvent,
bool CounterBasedEventEnabled = false,
bool ForceDisableProfiling = false);
bool CounterBasedEventEnabled,
bool ForceDisableProfiling);
} // extern "C"

// This is an experimental option that allows to disable caching of events in
Expand Down Expand Up @@ -273,9 +273,8 @@ template <> ze_result_t zeHostSynchronize(ze_command_queue_handle_t Handle);
// the event, updates the last command event in the queue and cleans up all dep
// events of the event.
// If the caller locks queue mutex then it must pass 'true' to QueueLocked.
ur_result_t CleanupCompletedEvent(ur_event_handle_t Event,
bool QueueLocked = false,
bool SetEventCompleted = false);
ur_result_t CleanupCompletedEvent(ur_event_handle_t Event, bool QueueLocked,
bool SetEventCompleted);

// Get value of device scope events env var setting or default setting
static const EventsScope DeviceEventsSetting = [] {
Expand Down
2 changes: 1 addition & 1 deletion source/adapters/level_zero/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urBindlessImagesImageCopyExp(
ur_command_list_ptr_t CommandList{};
UR_CALL(hQueue->Context->getAvailableCommandList(
hQueue, CommandList, UseCopyEngine, numEventsInWaitList, phEventWaitList,
OkToBatch));
OkToBatch, nullptr /*ForcedCmdQueue*/));

ze_event_handle_t ZeEvent = nullptr;
ur_event_handle_t InternalEvent;
Expand Down
10 changes: 6 additions & 4 deletions source/adapters/level_zero/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueKernelLaunch(
ur_command_list_ptr_t CommandList{};
UR_CALL(Queue->Context->getAvailableCommandList(
Queue, CommandList, UseCopyEngine, NumEventsInWaitList, EventWaitList,
true /* AllowBatching */));
true /* AllowBatching */, nullptr /*ForcedCmdQueue*/));

ze_event_handle_t ZeEvent = nullptr;
ur_event_handle_t InternalEvent{};
Expand Down Expand Up @@ -199,7 +199,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueKernelLaunch(

// Execute command list asynchronously, as the event will be used
// to track down its completion.
UR_CALL(Queue->executeCommandList(CommandList, false, true));
UR_CALL(Queue->executeCommandList(CommandList, false /*IsBlocking*/,
true /*OKToBatchCommand*/));

return UR_RESULT_SUCCESS;
}
Expand Down Expand Up @@ -400,7 +401,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueCooperativeKernelLaunchExp(
ur_command_list_ptr_t CommandList{};
UR_CALL(Queue->Context->getAvailableCommandList(
Queue, CommandList, UseCopyEngine, NumEventsInWaitList, EventWaitList,
true /* AllowBatching */));
true /* AllowBatching */, nullptr /*ForcedCmdQueue*/));

ze_event_handle_t ZeEvent = nullptr;
ur_event_handle_t InternalEvent{};
Expand Down Expand Up @@ -463,7 +464,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueCooperativeKernelLaunchExp(

// Execute command list asynchronously, as the event will be used
// to track down its completion.
UR_CALL(Queue->executeCommandList(CommandList, false, true));
UR_CALL(Queue->executeCommandList(CommandList, false /*IsBlocking*/,
true /*OKToBatchCommand*/));

return UR_RESULT_SUCCESS;
}
Expand Down
Loading

0 comments on commit 97dd83d

Please sign in to comment.