Skip to content

Commit 2799ba3

Browse files
authored
[SYCL] Always store last event (for ioq) if scheduler was not bypassed (#18867)
This fixes an issue for cases where the kernel depends on events coming from the scheduler (host task events or unenqueued commands). The logic in `submit_impl` for calculating `noLastEventPath` did not take into account all dependencies which could result in skipping storing last even if `handler.finalize()` ended up submitting work to the scheduler. I also restored the lock on the submit path. It is needed to store the last event in case `handler.finalize()` returns an event coming from the scheduler.
1 parent a938c18 commit 2799ba3

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

sycl/source/detail/queue_impl.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,13 @@ queue_impl::submit_impl(const detail::type_erased_cgfo_ty &CGF,
349349
!requiresPostProcess;
350350

351351
if (noLastEventPath) {
352-
return finalizeHandlerInOrderNoEventsUnlocked(Handler);
352+
std::unique_lock<std::mutex> Lock(MMutex);
353+
354+
// Check if we are still in no last event mode. There could
355+
// have been a concurrent submit.
356+
if (MNoLastEventMode.load(std::memory_order_relaxed)) {
357+
return finalizeHandlerInOrderNoEventsUnlocked(Handler);
358+
}
353359
}
354360

355361
detail::EventImplPtr EventImpl;

sycl/source/detail/queue_impl.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,14 @@ class queue_impl : public std::enable_shared_from_this<queue_impl> {
750750

751751
synchronizeWithExternalEvent(Handler);
752752

753-
return parseEvent(Handler.finalize());
753+
auto Event = parseEvent(Handler.finalize());
754+
755+
if (Event && !Scheduler::CheckEventReadiness(*MContext, Event)) {
756+
MDefaultGraphDeps.LastEventPtr = Event;
757+
MNoLastEventMode.store(false, std::memory_order_relaxed);
758+
}
759+
760+
return Event;
754761
}
755762

756763
template <typename HandlerType = handler>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// RUN: %{build} -o %t.out
2+
// RUN: %{run} %t.out
3+
//==-------- in_order_multi_queue_host_task.cpp ---------------------------==//
4+
//
5+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6+
// See https://llvm.org/LICENSE.txt for license information.
7+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
//
9+
//===----------------------------------------------------------------------===//
10+
#include <condition_variable>
11+
#include <iostream>
12+
13+
#include <sycl/detail/core.hpp>
14+
#include <sycl/properties/all_properties.hpp>
15+
#include <sycl/usm.hpp>
16+
17+
using namespace sycl;
18+
19+
const int dataSize = 1024;
20+
21+
int main() {
22+
queue Queue1{property::queue::in_order()};
23+
queue Queue2{property::queue::in_order()};
24+
25+
int *dataA = malloc_host<int>(dataSize, Queue1);
26+
int *dataB = malloc_host<int>(dataSize, Queue1);
27+
int *dataC = malloc_host<int>(dataSize, Queue1);
28+
29+
bool ready = false;
30+
std::mutex host_task_mtx;
31+
std::condition_variable cv;
32+
33+
auto Event1 = Queue1.submit([&](handler &cgh) {
34+
cgh.host_task([&] {
35+
std::unique_lock<std::mutex> lk(host_task_mtx);
36+
cv.wait(lk, [&] { return ready; });
37+
for (size_t i = 0; i < dataSize; ++i) {
38+
dataA[i] = i;
39+
}
40+
});
41+
});
42+
43+
Queue2.submit([&](handler &cgh) {
44+
cgh.depends_on(Event1);
45+
cgh.parallel_for(range<1>(dataSize),
46+
[=](id<1> idx) { dataB[idx[0]] = dataA[idx[0]]; });
47+
});
48+
49+
{
50+
std::unique_lock<std::mutex> lk(host_task_mtx);
51+
ready = true;
52+
}
53+
cv.notify_one();
54+
55+
Queue2.wait();
56+
57+
for (size_t i = 0; i != dataSize; ++i) {
58+
if (dataB[i] != i) {
59+
std::cout << "Result mismatches " << dataB[i] << " vs expected " << i
60+
<< " for index " << i << std::endl;
61+
return 1;
62+
}
63+
}
64+
65+
return 0;
66+
}

0 commit comments

Comments
 (0)