Skip to content

Commit 686e32b

Browse files
[SYCL] Make queue's non-USM event ownership temporary (#1561)
Signed-off-by: Sergey Semenov <[email protected]>
1 parent 7e3cca4 commit 686e32b

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

sycl/source/detail/queue_impl.cpp

+16-5
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ event queue_impl::memset(shared_ptr_class<detail::queue_impl> Impl, void *Ptr,
4949
return event();
5050

5151
event ResEvent{pi::cast<cl_event>(Event), Context};
52-
addEvent(ResEvent);
52+
addUSMEvent(ResEvent);
5353
return ResEvent;
5454
}
5555

@@ -63,7 +63,7 @@ event queue_impl::memcpy(shared_ptr_class<detail::queue_impl> Impl, void *Dest,
6363
return event();
6464

6565
event ResEvent{pi::cast<cl_event>(Event), Context};
66-
addEvent(ResEvent);
66+
addUSMEvent(ResEvent);
6767
return ResEvent;
6868
}
6969

@@ -81,13 +81,19 @@ event queue_impl::mem_advise(const void *Ptr, size_t Length,
8181
Advice, &Event);
8282

8383
event ResEvent{pi::cast<cl_event>(Event), Context};
84-
addEvent(ResEvent);
84+
addUSMEvent(ResEvent);
8585
return ResEvent;
8686
}
8787

8888
void queue_impl::addEvent(event Event) {
89+
std::weak_ptr<event_impl> EventWeakPtr{getSyclObjImpl(Event)};
8990
std::lock_guard<mutex_class> Guard(MMutex);
90-
MEvents.push_back(std::move(Event));
91+
MEvents.push_back(std::move(EventWeakPtr));
92+
}
93+
94+
void queue_impl::addUSMEvent(event Event) {
95+
std::lock_guard<mutex_class> Guard(MMutex);
96+
MUSMEvents.push_back(std::move(Event));
9197
}
9298

9399
void *queue_impl::instrumentationProlog(const detail::code_location &CodeLoc,
@@ -175,8 +181,13 @@ void queue_impl::wait(const detail::code_location &CodeLoc) {
175181
#endif
176182

177183
std::lock_guard<mutex_class> Guard(MMutex);
178-
for (auto &Event : MEvents)
184+
for (std::weak_ptr<event_impl> &EventImplWeakPtr : MEvents) {
185+
if (std::shared_ptr<event_impl> EventImplPtr = EventImplWeakPtr.lock())
186+
EventImplPtr->wait(EventImplPtr);
187+
}
188+
for (event &Event : MUSMEvents) {
179189
Event.wait();
190+
}
180191
MEvents.clear();
181192

182193
#ifdef XPTI_ENABLE_INSTRUMENTATION

sycl/source/detail/queue_impl.hpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -382,12 +382,20 @@ class queue_impl {
382382
/// \param Event is the event to be stored
383383
void addEvent(event Event);
384384

385+
/// Stores a USM operation event that should be associated with the queue
386+
///
387+
/// \param Event is the event to be stored
388+
void addUSMEvent(event Event);
389+
385390
/// Protects all the fields that can be changed by class' methods.
386391
mutex_class MMutex;
387392

388393
DeviceImplPtr MDevice;
389394
const ContextImplPtr MContext;
390-
vector_class<event> MEvents;
395+
vector_class<std::weak_ptr<event_impl>> MEvents;
396+
// USM operations are not added to the scheduler command graph,
397+
// queue is the only owner on the runtime side.
398+
vector_class<event> MUSMEvents;
391399
exception_list MExceptions;
392400
const async_handler MAsyncHandler;
393401
const property_list MPropList;
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// RUN: %clangxx -fsycl %s -o %t.out
2+
// RUN: env SYCL_PI_TRACE=1 %CPU_RUN_PLACEHOLDER %t.out 2>&1 %CPU_CHECK_PLACEHOLDER
3+
#include <CL/sycl.hpp>
4+
#include <cassert>
5+
#include <iostream>
6+
7+
// The test checks that pi_events are released without queue destruction
8+
// or call to queue::wait, when the corresponding commands are cleaned up.
9+
10+
using namespace cl::sycl;
11+
12+
class Foo;
13+
14+
int main() {
15+
int Val = 0;
16+
int Gold = 42;
17+
18+
queue Q;
19+
20+
{
21+
buffer<int, 1> Buf{&Val, range<1>(1)};
22+
Q.submit([&](handler &Cgh) {
23+
auto Acc = Buf.get_access<access::mode::discard_write>(Cgh);
24+
Cgh.single_task<Foo>([=]() {
25+
Acc[0] = Gold;
26+
});
27+
});
28+
}
29+
30+
// Buffer destruction triggers execution graph cleanup, check that both
31+
// events (one for launching the kernel and one for memory transfer to host)
32+
// are released.
33+
// CHECK: piEventRelease
34+
// CHECK: piEventRelease
35+
assert(Val == Gold);
36+
// CHECK: End of main scope
37+
std::cout << "End of main scope" << std::endl;
38+
}

0 commit comments

Comments
 (0)