Skip to content

[SYCL] Make queue's non-USM event ownership temporary #1561

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 3 commits into from
Apr 22, 2020
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
21 changes: 16 additions & 5 deletions sycl/source/detail/queue_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ event queue_impl::memset(shared_ptr_class<detail::queue_impl> Impl, void *Ptr,
return event();

event ResEvent{pi::cast<cl_event>(Event), Context};
addEvent(ResEvent);
addUSMEvent(ResEvent);
return ResEvent;
}

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

event ResEvent{pi::cast<cl_event>(Event), Context};
addEvent(ResEvent);
addUSMEvent(ResEvent);
return ResEvent;
}

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

event ResEvent{pi::cast<cl_event>(Event), Context};
addEvent(ResEvent);
addUSMEvent(ResEvent);
return ResEvent;
}

void queue_impl::addEvent(event Event) {
std::weak_ptr<event_impl> EventWeakPtr{getSyclObjImpl(Event)};
std::lock_guard<mutex_class> Guard(MMutex);
MEvents.push_back(std::move(Event));
MEvents.push_back(std::move(EventWeakPtr));
}

void queue_impl::addUSMEvent(event Event) {
std::lock_guard<mutex_class> Guard(MMutex);
MUSMEvents.push_back(std::move(Event));
}

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

std::lock_guard<mutex_class> Guard(MMutex);
for (auto &Event : MEvents)
for (std::weak_ptr<event_impl> &EventImplWeakPtr : MEvents) {
if (std::shared_ptr<event_impl> EventImplPtr = EventImplWeakPtr.lock())
EventImplPtr->wait(EventImplPtr);
}
for (event &Event : MUSMEvents) {
Event.wait();
}
MEvents.clear();

#ifdef XPTI_ENABLE_INSTRUMENTATION
Expand Down
10 changes: 9 additions & 1 deletion sycl/source/detail/queue_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,12 +382,20 @@ class queue_impl {
/// \param Event is the event to be stored
void addEvent(event Event);

/// Stores a USM operation event that should be associated with the queue
///
/// \param Event is the event to be stored
void addUSMEvent(event Event);

/// Protects all the fields that can be changed by class' methods.
mutex_class MMutex;

DeviceImplPtr MDevice;
const ContextImplPtr MContext;
vector_class<event> MEvents;
vector_class<std::weak_ptr<event_impl>> MEvents;
// USM operations are not added to the scheduler command graph,
// queue is the only owner on the runtime side.
vector_class<event> MUSMEvents;
exception_list MExceptions;
const async_handler MAsyncHandler;
const property_list MPropList;
Expand Down
38 changes: 38 additions & 0 deletions sycl/test/basic_tests/event_release.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// RUN: %clangxx -fsycl %s -o %t.out
// RUN: env SYCL_PI_TRACE=1 %CPU_RUN_PLACEHOLDER %t.out 2>&1 %CPU_CHECK_PLACEHOLDER
#include <CL/sycl.hpp>
#include <cassert>
#include <iostream>

// The test checks that pi_events are released without queue destruction
// or call to queue::wait, when the corresponding commands are cleaned up.

using namespace cl::sycl;

class Foo;

int main() {
int Val = 0;
int Gold = 42;

queue Q;

{
buffer<int, 1> Buf{&Val, range<1>(1)};
Q.submit([&](handler &Cgh) {
auto Acc = Buf.get_access<access::mode::discard_write>(Cgh);
Cgh.single_task<Foo>([=]() {
Acc[0] = Gold;
});
});
}

// Buffer destruction triggers execution graph cleanup, check that both
// events (one for launching the kernel and one for memory transfer to host)
// are released.
// CHECK: piEventRelease
// CHECK: piEventRelease
assert(Val == Gold);
// CHECK: End of main scope
std::cout << "End of main scope" << std::endl;
}