Skip to content

Commit a9485b7

Browse files
[SYCL] Force closing an open batch with interop event (#5328)
1 parent 90c8f05 commit a9485b7

File tree

2 files changed

+38
-18
lines changed

2 files changed

+38
-18
lines changed

sycl/plugins/level_zero/pi_level_zero.cpp

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,7 +1228,15 @@ pi_result _pi_queue::executeCommandList(pi_command_list_ptr_t CommandList,
12281228
// either that no command has ever been issued to the queue
12291229
// or it means that the LastCommandEvent has been signalled and
12301230
// therefore that this Queue is idle.
1231-
bool CurrentlyEmpty = this->LastCommandEvent == nullptr;
1231+
//
1232+
// NOTE: this behavior adds some flakyness to the batching
1233+
// since last command's event may or may not be completed by the
1234+
// time we get here depending on timings and system/gpu load.
1235+
// So, disable it for modes where we print PI traces. Printing
1236+
// traces incurs much different timings than real execution
1237+
// ansyway, and many regression tests use it.
1238+
//
1239+
bool CurrentlyEmpty = !PrintPiTrace && this->LastCommandEvent == nullptr;
12321240

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

1447+
pi_result _pi_queue::executeOpenCommandListWithEvent(pi_event Event) {
1448+
// TODO: see if we can reliably tell if the event is copy or compute.
1449+
// Meanwhile check both open command-lists.
1450+
using IsCopy = bool;
1451+
if (hasOpenCommandList(IsCopy{false}) &&
1452+
ComputeCommandBatch.OpenCommandList->first == Event->ZeCommandList) {
1453+
if (auto Res = executeOpenCommandList(IsCopy{false}))
1454+
return Res;
1455+
}
1456+
if (hasOpenCommandList(IsCopy{true}) &&
1457+
CopyCommandBatch.OpenCommandList->first == Event->ZeCommandList) {
1458+
if (auto Res = executeOpenCommandList(IsCopy{true}))
1459+
return Res;
1460+
}
1461+
return PI_SUCCESS;
1462+
}
1463+
14391464
pi_result _pi_queue::executeOpenCommandList(bool IsCopy) {
14401465
auto &CommandBatch = IsCopy ? CopyCommandBatch : ComputeCommandBatch;
14411466
// If there are any commands still in the open command list for this
@@ -4797,23 +4822,7 @@ pi_result piEventGetInfo(pi_event Event, pi_event_info ParamName,
47974822
if (Event->Queue) {
47984823
// Lock automatically releases when this goes out of scope.
47994824
std::lock_guard<std::mutex> lock(Event->Queue->PiQueueMutex);
4800-
4801-
// Only do the execute of the open command list if the event that
4802-
// is being queried and event that is to be signalled by something
4803-
// currently in that open command list.
4804-
using IsCopy = bool;
4805-
if (Event->Queue->hasOpenCommandList(IsCopy{false}) &&
4806-
Event->Queue->ComputeCommandBatch.OpenCommandList->first ==
4807-
Event->ZeCommandList) {
4808-
if (auto Res = Event->Queue->executeOpenCommandList(IsCopy{false}))
4809-
return Res;
4810-
}
4811-
if (Event->Queue->hasOpenCommandList(IsCopy{true}) &&
4812-
Event->Queue->CopyCommandBatch.OpenCommandList->first ==
4813-
Event->ZeCommandList) {
4814-
if (auto Res = Event->Queue->executeOpenCommandList(IsCopy{true}))
4815-
return Res;
4816-
}
4825+
Event->Queue->executeOpenCommandListWithEvent(Event);
48174826
}
48184827

48194828
// Make sure that we query a host-visible event only.
@@ -5145,6 +5154,14 @@ pi_result piextEventGetNativeHandle(pi_event Event,
51455154

51465155
auto *ZeEvent = pi_cast<ze_event_handle_t *>(NativeHandle);
51475156
*ZeEvent = Event->ZeEvent;
5157+
5158+
// Event can potentially be in an open command-list, make sure that
5159+
// it is submitted for execution to avoid potential deadlock if
5160+
// interop app is going to wait for it.
5161+
if (Event->Queue) {
5162+
std::lock_guard<std::mutex> lock(Event->Queue->PiQueueMutex);
5163+
Event->Queue->executeOpenCommandListWithEvent(Event);
5164+
}
51485165
return PI_SUCCESS;
51495166
}
51505167

sycl/plugins/level_zero/pi_level_zero.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,9 @@ struct _pi_queue : _pi_object {
776776
// executed.
777777
pi_result executeOpenCommandList(bool IsCopy);
778778

779+
// Execute the open command containing the event.
780+
pi_result executeOpenCommandListWithEvent(pi_event Event);
781+
779782
// Wrapper function to execute both OpenCommandLists (Copy and Compute).
780783
// This wrapper is helpful when all 'open' commands need to be executed.
781784
// Call-sites instances: piQuueueFinish, piQueueRelease, etc.

0 commit comments

Comments
 (0)