Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
20 changes: 14 additions & 6 deletions sycl/source/detail/queue_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,22 @@ event queue_impl::mem_advise(const shared_ptr_class<detail::queue_impl> &Self,
}

void queue_impl::addEvent(const event &Event) {
std::weak_ptr<event_impl> EventWeakPtr{getSyclObjImpl(Event)};
std::lock_guard<mutex_class> Lock(MMutex);
MEvents.push_back(std::move(EventWeakPtr));
EventImplPtr Eimpl = getSyclObjImpl(Event);
Command *Cmd = (Command *)(Eimpl->getCommand());
if (!Cmd) {
// if there is no command on the event, we cannot track it with MEventsWeak
// as that will leave it with no owner. Track in MEventsShared
addUSMEvent(Event);
} else {
std::weak_ptr<event_impl> EventWeakPtr{Eimpl};
std::lock_guard<mutex_class> Lock{MMutex};
MEventsWeak.push_back(std::move(EventWeakPtr));
}
}

void queue_impl::addUSMEvent(const event &Event) {
std::lock_guard<mutex_class> Lock(MMutex);
MUSMEvents.push_back(Event);
MEventsShared.push_back(Event);
}

void *queue_impl::instrumentationProlog(const detail::code_location &CodeLoc,
Expand Down Expand Up @@ -204,8 +212,8 @@ void queue_impl::wait(const detail::code_location &CodeLoc) {
vector_class<event> USMEvents;
{
std::lock_guard<mutex_class> Lock(MMutex);
Events = std::move(MEvents);
USMEvents = std::move(MUSMEvents);
Events = std::move(MEventsWeak);
USMEvents = std::move(MEventsShared);
}

for (std::weak_ptr<event_impl> &EventImplWeakPtr : Events)
Expand Down
12 changes: 8 additions & 4 deletions sycl/source/detail/queue_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,10 +415,14 @@ class queue_impl {

DeviceImplPtr MDevice;
const ContextImplPtr MContext;
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;

/// These events are tracked, but not owned, by the queue.
vector_class<std::weak_ptr<event_impl>> MEventsWeak;

/// Events without data dependencies (such as USM) need an owner,
/// additionally, USM operations are not added to the scheduler command graph,
/// queue is the only owner on the runtime side.
vector_class<event> MEventsShared;
exception_list MExceptions;
const async_handler MAsyncHandler;
const property_list MPropList;
Expand Down
6 changes: 6 additions & 0 deletions sycl/source/detail/scheduler/scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ EventImplPtr Scheduler::addCG(std::unique_ptr<detail::CG> CommandGroup,
if (!Enqueued && EnqueueResultT::SyclEnqueueFailed == Res.MResult)
throw runtime_error("Enqueue process failed.", PI_INVALID_OPERATION);

if (NewCmd->MDeps.size() == 0) {
NewEvent->setCommand(nullptr); // if there are no memory dependencies,
// decouple and free the command
delete NewCmd;
}

if (IsKernel)
Streams = ((ExecCGCommand *)NewCmd)->getStreams();
}
Expand Down
19 changes: 19 additions & 0 deletions sycl/test/basic_tests/queue/release.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
// RUN: env SYCL_PI_TRACE=2 %GPU_RUN_PLACEHOLDER %t.out | FileCheck %s

#include <CL/sycl.hpp>
int main() {
sycl::queue q;

q.single_task<class test>([]() {});
// No wait. Ensure resources are released anyway.

return 0;
}

//CHECK: ---> piEnqueueKernelLaunch(
//CHECK: ---> piQueueRelease(
//CHECK: ---> piEventRelease(
//CHECK: ---> piContextRelease(
//CHECK: ---> piKernelRelease(
//CHECK: ---> piProgramRelease(