Skip to content

[SYCL] Force closing an open batch with interop event #5328

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 2 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
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
53 changes: 35 additions & 18 deletions sycl/plugins/level_zero/pi_level_zero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1228,7 +1228,15 @@ pi_result _pi_queue::executeCommandList(pi_command_list_ptr_t CommandList,
// either that no command has ever been issued to the queue
// or it means that the LastCommandEvent has been signalled and
// therefore that this Queue is idle.
bool CurrentlyEmpty = this->LastCommandEvent == nullptr;
//
// NOTE: this behavior adds some flakyness to the batching
// since last command's event may or may not be completed by the
// time we get here depending on timings and system/gpu load.
// So, disable it for modes where we print PI traces. Printing
// traces incurs much different timings than real execution
// ansyway, and many regression tests use it.
//
bool CurrentlyEmpty = !PrintPiTrace && this->LastCommandEvent == nullptr;

// The list can be empty if command-list only contains signals of proxy
// events.
Expand Down Expand Up @@ -1436,6 +1444,23 @@ _pi_queue::getZeCopyCommandQueue(int *CopyQueueIndex,
return ZeCopyCommandQueue;
}

pi_result _pi_queue::executeOpenCommandListWithEvent(pi_event Event) {
// TODO: see if we can reliably tell if the event is copy or compute.
// Meanwhile check both open command-lists.
using IsCopy = bool;
if (hasOpenCommandList(IsCopy{false}) &&
ComputeCommandBatch.OpenCommandList->first == Event->ZeCommandList) {
if (auto Res = executeOpenCommandList(IsCopy{false}))
return Res;
}
if (hasOpenCommandList(IsCopy{true}) &&
CopyCommandBatch.OpenCommandList->first == Event->ZeCommandList) {
if (auto Res = executeOpenCommandList(IsCopy{true}))
return Res;
}
return PI_SUCCESS;
}

pi_result _pi_queue::executeOpenCommandList(bool IsCopy) {
auto &CommandBatch = IsCopy ? CopyCommandBatch : ComputeCommandBatch;
// If there are any commands still in the open command list for this
Expand Down Expand Up @@ -4794,23 +4819,7 @@ pi_result piEventGetInfo(pi_event Event, pi_event_info ParamName,
if (Event->Queue) {
// Lock automatically releases when this goes out of scope.
std::lock_guard<std::mutex> lock(Event->Queue->PiQueueMutex);

// Only do the execute of the open command list if the event that
// is being queried and event that is to be signalled by something
// currently in that open command list.
using IsCopy = bool;
if (Event->Queue->hasOpenCommandList(IsCopy{false}) &&
Event->Queue->ComputeCommandBatch.OpenCommandList->first ==
Event->ZeCommandList) {
if (auto Res = Event->Queue->executeOpenCommandList(IsCopy{false}))
return Res;
}
if (Event->Queue->hasOpenCommandList(IsCopy{true}) &&
Event->Queue->CopyCommandBatch.OpenCommandList->first ==
Event->ZeCommandList) {
if (auto Res = Event->Queue->executeOpenCommandList(IsCopy{true}))
return Res;
}
Event->Queue->executeOpenCommandListWithEvent(Event);
}

// Make sure that we query a host-visible event only.
Expand Down Expand Up @@ -5142,6 +5151,14 @@ pi_result piextEventGetNativeHandle(pi_event Event,

auto *ZeEvent = pi_cast<ze_event_handle_t *>(NativeHandle);
*ZeEvent = Event->ZeEvent;

// Event can potentially be in an open command-list, make sure that
// it is submitted for execution to avoid potential deadlock if
// interop app is going to wait for it.
if (Event->Queue) {
std::lock_guard<std::mutex> lock(Event->Queue->PiQueueMutex);
Event->Queue->executeOpenCommandListWithEvent(Event);
}
return PI_SUCCESS;
}

Expand Down
3 changes: 3 additions & 0 deletions sycl/plugins/level_zero/pi_level_zero.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,9 @@ struct _pi_queue : _pi_object {
// executed.
pi_result executeOpenCommandList(bool IsCopy);

// Execute the open command containing the event.
pi_result executeOpenCommandListWithEvent(pi_event Event);

// Wrapper function to execute both OpenCommandLists (Copy and Compute).
// This wrapper is helpful when all 'open' commands need to be executed.
// Call-sites instances: piQuueueFinish, piQueueRelease, etc.
Expand Down