Skip to content

Commit a9839b0

Browse files
authored
[SYCL] Try to enqueue host command depencies (#2561)
Signed-off-by: Sergey Kanaev <[email protected]>
1 parent bb68eef commit a9839b0

File tree

7 files changed

+76
-16
lines changed

7 files changed

+76
-16
lines changed

sycl/source/detail/scheduler/commands.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1737,7 +1737,8 @@ void DispatchNativeKernel(void *Blob) {
17371737
}
17381738

17391739
cl_int ExecCGCommand::enqueueImp() {
1740-
waitForPreparedHostEvents();
1740+
if (getCG().getType() != CG::CGTYPE::CODEPLAY_HOST_TASK)
1741+
waitForPreparedHostEvents();
17411742
std::vector<EventImplPtr> EventImpls = MPreparedDepsEvents;
17421743
auto RawEvents = getPiEvents(EventImpls);
17431744

sycl/source/detail/scheduler/commands.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ class Command {
217217
friend class DispatchHostTask;
218218

219219
public:
220+
const std::vector<EventImplPtr> getPreparedHostDepsEvents() const {
221+
return MPreparedHostDepsEvents;
222+
}
223+
220224
/// Contains list of dependencies(edges)
221225
std::vector<DepDesc> MDeps;
222226
/// Contains list of commands that depend on the command.

sycl/source/detail/scheduler/graph_processor.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,22 @@ bool Scheduler::GraphProcessor::enqueueCommand(Command *Cmd,
7171
return false;
7272
}
7373

74+
// Asynchronous host operations (amongst dependencies of an arbitrary command)
75+
// are not supported (see Command::processDepEvent method). This impacts
76+
// operation of host-task feature a lot with hangs and long-runs. Hence we
77+
// have this workaround here.
78+
// This workaround is safe as long as the only asynchronous host operation we
79+
// have is a host task.
80+
// This may iterate over some of dependencies in Cmd->MDeps. Though, the
81+
// enqueue operation is idempotent and the second call will result in no-op.
82+
// TODO remove the workaround when proper fix for host-task dispatching is
83+
// implemented.
84+
for (const EventImplPtr &Event : Cmd->getPreparedHostDepsEvents()) {
85+
if (Command *DepCmd = static_cast<Command *>(Event->getCommand()))
86+
if (!enqueueCommand(DepCmd, EnqueueResult, Blocking))
87+
return false;
88+
}
89+
7490
return Cmd->enqueue(EnqueueResult, Blocking);
7591
}
7692

sycl/test/host-interop-task/host-task-dependency2.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
22

3-
// RUNx: %CPU_RUN_PLACEHOLDER %t.out
4-
// RUNx: %GPU_RUN_PLACEHOLDER %t.out
5-
// RUNx: %ACC_RUN_PLACEHOLDER %t.out
3+
// RUN: %CPU_RUN_PLACEHOLDER %t.out
4+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
5+
// RUN: %ACC_RUN_PLACEHOLDER %t.out
66

7-
// RUNx: %CPU_RUN_PLACEHOLDER %t.out 10
8-
// RUNx: %GPU_RUN_PLACEHOLDER %t.out 10
9-
// RUNx: %ACC_RUN_PLACEHOLDER %t.out 10
7+
// RUN: %CPU_RUN_PLACEHOLDER %t.out 10
8+
// RUN: %GPU_RUN_PLACEHOLDER %t.out 10
9+
// RUN: %ACC_RUN_PLACEHOLDER %t.out 10
1010

1111
#include <CL/sycl.hpp>
1212
#include <iostream>

sycl/test/host-interop-task/host-task-dependency3.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
22

3-
// RUNx: %CPU_RUN_PLACEHOLDER %t.out
4-
// RUNx: %GPU_RUN_PLACEHOLDER %t.out
5-
// RUNx: %ACC_RUN_PLACEHOLDER %t.out
3+
// RUN: %CPU_RUN_PLACEHOLDER %t.out
4+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
5+
// RUN: %ACC_RUN_PLACEHOLDER %t.out
66

7-
// RUNx: %CPU_RUN_PLACEHOLDER %t.out 10
8-
// RUNx: %GPU_RUN_PLACEHOLDER %t.out 10
9-
// RUNx: %ACC_RUN_PLACEHOLDER %t.out 10
7+
// RUN: %CPU_RUN_PLACEHOLDER %t.out 10
8+
// RUN: %GPU_RUN_PLACEHOLDER %t.out 10
9+
// RUN: %ACC_RUN_PLACEHOLDER %t.out 10
1010

1111
#include <CL/sycl.hpp>
1212
#include <chrono>

sycl/test/host-interop-task/host-task-dependency4.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
22

3-
// RUNx: %CPU_RUN_PLACEHOLDER %t.out
4-
// RUNx: %GPU_RUN_PLACEHOLDER %t.out
5-
// RUNx: %ACC_RUN_PLACEHOLDER %t.out
3+
// RUN: %CPU_RUN_PLACEHOLDER %t.out
4+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
5+
// RUN: %ACC_RUN_PLACEHOLDER %t.out
66

77
#include <CL/sycl.hpp>
88

sycl/unittests/scheduler/BlockedCommands.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,42 @@ TEST_F(SchedulerTest, EnqueueBlockedCommandEarlyExit) {
130130
<< "Result of enqueueing blocked command should be BLOCKED.\n";
131131
ASSERT_EQ(&B, Res.MCmd) << "Expected different failed command.\n";
132132
}
133+
134+
// This unit test is for workaround described in GraphProcessor::enqueueCommand
135+
// method.
136+
TEST_F(SchedulerTest, EnqueueHostDependency) {
137+
MockCommand A(detail::getSyclObjImpl(MQueue));
138+
A.MEnqueueStatus = detail::EnqueueResultT::SyclEnqueueReady;
139+
A.MIsBlockable = true;
140+
A.MRetVal = CL_SUCCESS;
141+
142+
MockCommand B(detail::getSyclObjImpl(MQueue));
143+
B.MEnqueueStatus = detail::EnqueueResultT::SyclEnqueueReady;
144+
B.MIsBlockable = true;
145+
B.MRetVal = CL_SUCCESS;
146+
147+
cl::sycl::detail::EventImplPtr DepEvent{
148+
new cl::sycl::detail::event_impl(detail::getSyclObjImpl(MQueue))};
149+
DepEvent->setCommand(&B);
150+
151+
A.addDep(DepEvent);
152+
153+
// We have such a "graph":
154+
//
155+
// A
156+
// |
157+
// B
158+
//
159+
// A depends on B. B is host command.
160+
// "Graph" is quoted as we don't have this dependency in MDeps. Instead, we
161+
// have this dependecy as result of handler::depends_on() call.
162+
163+
EXPECT_CALL(A, enqueue(_, _)).Times(1);
164+
EXPECT_CALL(B, enqueue(_, _)).Times(1);
165+
166+
detail::EnqueueResultT Res;
167+
bool Enqueued = MockScheduler::enqueueCommand(&A, Res, detail::NON_BLOCKING);
168+
ASSERT_TRUE(Enqueued) << "The command should be enqueued\n";
169+
ASSERT_EQ(detail::EnqueueResultT::SyclEnqueueSuccess, Res.MResult)
170+
<< "Enqueue operation should return successfully.\n";
171+
}

0 commit comments

Comments
 (0)