Skip to content

[SYCL][L0] Optimize the LastCommandEvent of the synchronized queue #6159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions sycl/plugins/level_zero/pi_level_zero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3388,13 +3388,13 @@ pi_result piQueueFinish(pi_queue Queue) {

if (UseImmediateCommandLists) {
// Lock automatically releases when this goes out of scope.
std::scoped_lock lock(Queue->Mutex);
std::scoped_lock Lock(Queue->Mutex);

Queue->synchronize();
return PI_SUCCESS;
}

std::unique_lock lock(Queue->Mutex);
std::unique_lock Lock(Queue->Mutex);
std::vector<ze_command_queue_handle_t> ZeQueues;

// execute any command list that may still be open.
Expand All @@ -3407,6 +3407,9 @@ pi_result piQueueFinish(pi_queue Queue) {
Queue->ComputeQueueGroup.ZeQueues.end(),
std::back_inserter(ZeQueues));

// Remember the last command's event.
auto LastCommandEvent = Queue->LastCommandEvent;

// Don't hold a lock to the queue's mutex while waiting.
// This allows continue working with the queue from other threads.
// TODO: this currently exhibits some issues in the driver, so
Expand All @@ -3415,13 +3418,25 @@ pi_result piQueueFinish(pi_queue Queue) {
static bool HoldLock =
std::getenv("SYCL_PI_LEVEL_ZERO_QUEUE_FINISH_HOLD_LOCK") != nullptr;
if (!HoldLock) {
lock.unlock();
Lock.unlock();
}

for (auto ZeQueue : ZeQueues) {
if (ZeQueue)
ZE_CALL(zeHostSynchronize, (ZeQueue));
}

// Prevent unneeded already finished events to show up in the wait list.
// We can only do so if nothing else was submitted to the queue
// while we were synchronizing it.
if (!HoldLock) {
std::scoped_lock Lock(Queue->Mutex);
if (LastCommandEvent == Queue->LastCommandEvent) {
Queue->LastCommandEvent = nullptr;
}
} else {
Queue->LastCommandEvent = nullptr;
}
return PI_SUCCESS;
}

Expand Down