Skip to content

Commit

Permalink
[L0] track ze queue usage before destroy
Browse files Browse the repository at this point in the history
Signed-off-by: Neil R. Spruit <neil.r.spruit@intel.com>
  • Loading branch information
nrspruit committed Apr 3, 2024
1 parent 0086401 commit ed1e677
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
29 changes: 21 additions & 8 deletions source/adapters/level_zero/queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1314,10 +1314,13 @@ ur_result_t urQueueReleaseInternal(ur_queue_handle_t Queue) {
for (auto &QueueGroup : QueueMap)
for (auto &ZeQueue : QueueGroup.second.ZeQueues)
if (ZeQueue) {
auto ZeResult = ZE_CALL_NOCHECK(zeCommandQueueDestroy, (ZeQueue));
// Gracefully handle the case that L0 was already unloaded.
if (ZeResult && ZeResult != ZE_RESULT_ERROR_UNINITIALIZED)
return ze2urResult(ZeResult);
{
std::scoped_lock<ur_shared_mutex> Lock(UrQueue->zeQueueInUseMutex);
auto ZeResult = ZE_CALL_NOCHECK(zeCommandQueueDestroy, (ZeQueue));
// Gracefully handle the case that L0 was already unloaded.
if (ZeResult && ZeResult != ZE_RESULT_ERROR_UNINITIALIZED)
return ze2urResult(ZeResult);
}
}
}

Expand Down Expand Up @@ -1392,7 +1395,6 @@ ur_result_t CleanupEventListFromResetCmdList(
// runtime. Need to investigate whether relase can be done earlier, at sync
// points such as this, to reduce total number of active Events.
ur_result_t ur_queue_handle_t_::synchronize() {
// std::shared_lock<ur_shared_mutex> QueueLock(this->Mutex);
this->Mutex.lock();
if (!Healthy) {
this->Mutex.unlock();
Expand All @@ -1406,7 +1408,10 @@ ur_result_t ur_queue_handle_t_::synchronize() {

// wait for all commands previously submitted to this immediate command list
Queue->Mutex.unlock();
ZE2UR_CALL(zeCommandListHostSynchronize, (ImmCmdList->first, UINT64_MAX));\
{
std::scoped_lock<ur_shared_mutex> Lock(Queue->zeQueueInUseMutex);
ZE2UR_CALL(zeCommandListHostSynchronize, (ImmCmdList->first, UINT64_MAX));
}
Queue->Mutex.lock();

// Cleanup all events from the synced command list.
Expand All @@ -1421,7 +1426,12 @@ ur_result_t ur_queue_handle_t_::synchronize() {
// zero handle can have device scope, so we can't synchronize the last
// event.
if (isInOrderQueue() && !LastCommandEvent->IsDiscarded) {
ZE2UR_CALL(zeHostSynchronize, (LastCommandEvent->ZeEvent));
this->Mutex.unlock();
{
std::scoped_lock<ur_shared_mutex> Lock(this->zeQueueInUseMutex);
ZE2UR_CALL(zeHostSynchronize, (LastCommandEvent->ZeEvent));
}
this->Mutex.lock();

// clean up all events known to have been completed as well,
// so they can be reused later
Expand Down Expand Up @@ -1450,7 +1460,10 @@ ur_result_t ur_queue_handle_t_::synchronize() {
for (auto &ZeQueue : QueueGroup.second.ZeQueues)
if (ZeQueue) {
this->Mutex.unlock();
ZE2UR_CALL(zeHostSynchronize, (ZeQueue));
{
std::scoped_lock<ur_shared_mutex> Lock(this->zeQueueInUseMutex);
ZE2UR_CALL(zeHostSynchronize, (ZeQueue));
}
this->Mutex.lock();
}
}
Expand Down
4 changes: 3 additions & 1 deletion source/adapters/level_zero/queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ struct ur_queue_handle_t_ : _ur_object {
// Map of all command lists used in this queue.
ur_command_list_map_t CommandListMap;

ur_shared_mutex zeQueueInUseMutex;

// Helper data structure to hold all variables related to batching
struct command_batch {
// These two members are used to keep track of how often the
Expand Down Expand Up @@ -548,4 +550,4 @@ ur_result_t createEventAndAssociateQueue(
// list.
ur_result_t CleanupEventListFromResetCmdList(
std::vector<ur_event_handle_t> &EventListToCleanup,
bool QueueLocked = false);
bool QueueLocked = false);

0 comments on commit ed1e677

Please sign in to comment.