Skip to content

[SYCL] Visit explicit dependencies during finished command cleanup #2265

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

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 8 additions & 0 deletions sycl/source/detail/scheduler/commands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ class Command {

std::shared_ptr<event_impl> getEvent() const { return MEvent; }

const std::vector<EventImplPtr> &getPreparedDepsEvents() {
return MPreparedDepsEvents;
}

const std::vector<EventImplPtr> &getPreparedHostDepsEvents() {
return MPreparedHostDepsEvents;
}

// Methods needed to support SYCL instrumentation

/// Proxy method which calls emitInstrumentationData.
Expand Down
19 changes: 15 additions & 4 deletions sycl/source/detail/scheduler/graph_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,16 @@ void Scheduler::GraphBuilder::cleanupCommandsForRecord(MemObjRecord *Record) {
handleVisitedNodes(Visited);
}

static void
addEventDependenciesToQueue(std::queue<Command *> &Q,
const std::vector<EventImplPtr> &Events) {
for (const EventImplPtr &Event : Events) {
auto *Cmd = static_cast<Command *>(Event->getCommand());
if (Cmd)
Q.push(Cmd);
}
}

void Scheduler::GraphBuilder::cleanupFinishedCommands(Command *FinishedCmd) {
std::queue<Command *> CmdsToVisit({FinishedCmd});
std::vector<Command *> Visited;
Expand All @@ -926,10 +936,11 @@ void Scheduler::GraphBuilder::cleanupFinishedCommands(Command *FinishedCmd) {
if (!markNodeAsVisited(Cmd, Visited))
continue;

for (const DepDesc &Dep : Cmd->MDeps) {
if (Dep.MDepCommand)
CmdsToVisit.push(Dep.MDepCommand);
}
// Explicit dependencies are not represented by DepDesc edges, but also need
// to be visited. Traverse the graph using dependency events rather than
// DepDesc edges.
addEventDependenciesToQueue(CmdsToVisit, Cmd->getPreparedDepsEvents());
addEventDependenciesToQueue(CmdsToVisit, Cmd->getPreparedHostDepsEvents());

// Do not clean up the node if it is a leaf for any memory object
if (Cmd->MLeafCounter > 0)
Expand Down
42 changes: 42 additions & 0 deletions sycl/test/scheduler/ReleaseResourcesExplicitDep.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple -I %sycl_source_dir %s -o %t.out
// RUN: env SYCL_PI_TRACE=2 %CPU_RUN_PLACEHOLDER %t.out 2>&1 %CPU_CHECK_PLACEHOLDER
//==------------------- ReleaseResourcesExplicitDep.cpp --------------------==//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include <CL/sycl.hpp>

#include "../helpers.hpp"

class KernelNameA;
class KernelNameB;

// Check that the resources are correctly released for command groups with only
// explicit dependencies.
int main() {
// CHECK:---> piContextCreate
// CHECK:---> piQueueCreate
// CHECK:---> piProgramCreate

// CHECK:---> piKernelCreate
// CHECK:---> piEnqueueKernelLaunch
sycl::queue Q{sycl::cpu_selector{}};
sycl::event EventA = Q.single_task<KernelNameA>([]() {});

// CHECK:---> piKernelCreate
// CHECK:---> piEnqueueKernelLaunch
sycl::event EventB = Q.single_task<KernelNameB>(EventA, []() {});
EventB.wait();
}

// CHECK:---> piEventRelease
// CHECK:---> piEventRelease
// CHECK:---> piQueueRelease
// CHECK:---> piProgramRelease
// CHECK:---> piContextRelease
// CHECK:---> piKernelRelease
// CHECK:---> piKernelRelease
25 changes: 25 additions & 0 deletions sycl/unittests/scheduler/FinishedCmdCleanup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,28 @@ TEST_F(SchedulerTest, FinishedCmdCleanup) {
ASSERT_EQ(AllocaB.MUsers.size(), 1U);
EXPECT_EQ(*AllocaB.MUsers.begin(), &LeafB);
}

TEST_F(SchedulerTest, FinishedCmdCleanupExplicitDep) {
MockScheduler MS;
// Imitate an explicit dependency by adding an edge via Command::addDep (which
// registers the underlying event under the hood), then removing it directly.
buffer<int, 1> BufA(range<1>(1));
detail::Requirement MockReqA = getMockRequirement(BufA);
detail::AllocaCommand AllocaA{detail::getSyclObjImpl(MQueue), MockReqA};

int NCommandsAlive = 2;
std::function<void()> Callback = [&]() { --NCommandsAlive; };
MockCommand *CmdA =
new MockCommandWithCallback(detail::getSyclObjImpl(MQueue), Callback);
MockCommand *CmdB =
new MockCommandWithCallback(detail::getSyclObjImpl(MQueue), Callback);
addEdge(CmdB, CmdA, &AllocaA);
CmdB->MDeps.clear();
CmdA->MUsers.clear();

std::shared_ptr<detail::event_impl> Event{new detail::event_impl{}};
Event->setCommand(CmdB);
MS.cleanupFinishedCommands(Event);

EXPECT_EQ(NCommandsAlive, 0);
}
4 changes: 4 additions & 0 deletions sycl/unittests/scheduler/SchedulerTestUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class MockCommandWithCallback : public MockCommand {
std::function<void()> Callback)
: MockCommand(Queue, Req), MCallback(std::move(Callback)) {}

MockCommandWithCallback(cl::sycl::detail::QueueImplPtr Queue,
std::function<void()> Callback)
: MockCommand(Queue), MCallback(std::move(Callback)) {}

~MockCommandWithCallback() override { MCallback(); }

protected:
Expand Down