Skip to content

Commit 441dc3b

Browse files
authored
[SYCL] Fast launch of kernels that have no dependencies (#4188)
This patch aims to reduce the time for the specific case when the kernel doesn't have any dependencies. If the kernel doesn't use accessors, then the vector MRequirements is empty. If the kernel doesn't depend on other events, then the vector MEvents is empty. In most cases this is the kernel that uses USM memory only, for such cases, the patch is intended. Since the vectors MRequirements and MEvents are empty for ExecCGCommand, ExecCGCommand of such kernel doesn't affect the command graph and is not added as a node. Since this command doesn't depend on the graph in any way, it can be safely executed independently, without going through the mechanism of placing it into the queue, which in this case is meaningless. Thus, the command can be executed instantly. This significantly saves time that could be wasted in: a lot of extra checks trying to add a command to the graph using 1 write-mutex to the graph, many extra checks in queuing process using 1 read-mutex and 1 write-mutex. Here printGraphAsDot is useless because the graph isn't changed and this is used only to support the old behavior, and this can be removed if desired.
1 parent 7735139 commit 441dc3b

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

sycl/source/detail/scheduler/commands.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,7 @@ class ExecCGCommand : public Command {
543543
std::unique_ptr<detail::CommandGroup> MCommandGroup;
544544

545545
friend class Command;
546+
friend class Scheduler;
546547
};
547548

548549
class UpdateHostRequirementCommand : public Command {

sycl/source/detail/scheduler/scheduler.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,59 @@ Scheduler::addCG(std::unique_ptr<detail::CommandGroup> CommandGroup,
8888
initStream(Stream, Queue);
8989
}
9090
}
91+
92+
if (CommandGroup->MRequirements.size() + CommandGroup->MEvents.size() ==
93+
0) {
94+
ExecCGCommand *NewCmd(
95+
new ExecCGCommand(std::move(CommandGroup), std::move(Queue)));
96+
if (!NewCmd)
97+
throw runtime_error("Out of host memory", PI_OUT_OF_HOST_MEMORY);
98+
NewEvent = NewCmd->getEvent();
99+
100+
auto CleanUp = [&]() {
101+
NewEvent->setCommand(nullptr);
102+
delete NewCmd;
103+
};
104+
105+
if (MGraphBuilder
106+
.MPrintOptionsArray[GraphBuilder::PrintOptions::BeforeAddCG])
107+
MGraphBuilder.printGraphAsDot("before_addCG");
108+
if (MGraphBuilder
109+
.MPrintOptionsArray[GraphBuilder::PrintOptions::AfterAddCG])
110+
MGraphBuilder.printGraphAsDot("after_addCG");
111+
112+
try {
113+
#ifdef XPTI_ENABLE_INSTRUMENTATION
114+
NewCmd->emitInstrumentation(xpti::trace_task_begin, nullptr);
115+
#endif
116+
117+
cl_int Res = NewCmd->enqueueImp();
118+
119+
// Emit this correlation signal before the task end
120+
NewCmd->emitEnqueuedEventSignal(NewEvent->getHandleRef());
121+
#ifdef XPTI_ENABLE_INSTRUMENTATION
122+
NewCmd->emitInstrumentation(xpti::trace_task_end, nullptr);
123+
#endif
124+
125+
if (CL_SUCCESS != Res)
126+
throw runtime_error("Enqueue process failed.", PI_INVALID_OPERATION);
127+
else if (NewEvent->is_host() || NewEvent->getHandleRef() == nullptr)
128+
NewEvent->setComplete();
129+
} catch (...) {
130+
// enqueueImp() func and if statement above may throw an exception,
131+
// so destroy required resources to avoid memory leak
132+
CleanUp();
133+
std::rethrow_exception(std::current_exception());
134+
}
135+
136+
CleanUp();
137+
138+
for (auto StreamImplPtr : Streams) {
139+
StreamImplPtr->flush();
140+
}
141+
142+
return NewEvent;
143+
}
91144
}
92145

93146
{

sycl/source/detail/scheduler/scheduler.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,7 @@ class Scheduler {
612612
const ContextImplPtr &Context);
613613

614614
friend class Command;
615+
friend class Scheduler;
615616

616617
private:
617618
friend class ::MockScheduler;

0 commit comments

Comments
 (0)