Skip to content

[SYCL] Fix thread safety issues in scheduler #2281

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 1 commit into from
Aug 11, 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
8 changes: 2 additions & 6 deletions sycl/source/detail/scheduler/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1196,12 +1196,8 @@ AllocaCommandBase *ExecCGCommand::getAllocaForReq(Requirement *Req) {
throw runtime_error("Alloca for command not found", PI_INVALID_OPERATION);
}

void ExecCGCommand::flushStreams() {
assert(MCommandGroup->getType() == CG::KERNEL && "Expected kernel");
for (auto StreamImplPtr :
((CGExecKernel *)MCommandGroup.get())->getStreams()) {
StreamImplPtr->flush();
}
vector_class<StreamImplPtr> ExecCGCommand::getStreams() const {
return ((CGExecKernel *)MCommandGroup.get())->getStreams();
}

cl_int UpdateHostRequirementCommand::enqueueImp() {
Expand Down
3 changes: 2 additions & 1 deletion sycl/source/detail/scheduler/commands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class DispatchHostTask;
using QueueImplPtr = std::shared_ptr<detail::queue_impl>;
using EventImplPtr = std::shared_ptr<detail::event_impl>;
using ContextImplPtr = std::shared_ptr<detail::context_impl>;
using StreamImplPtr = std::shared_ptr<detail::stream_impl>;

class Command;
class AllocaCommand;
Expand Down Expand Up @@ -480,7 +481,7 @@ class ExecCGCommand : public Command {
public:
ExecCGCommand(std::unique_ptr<detail::CG> CommandGroup, QueueImplPtr Queue);

void flushStreams();
vector_class<StreamImplPtr> getStreams() const;

void printDot(std::ostream &Stream) const final override;
void emitInstrumentationData() final override;
Expand Down
29 changes: 20 additions & 9 deletions sycl/source/detail/scheduler/scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <CL/sycl/device_selector.hpp>
#include <detail/queue_impl.hpp>
#include <detail/scheduler/scheduler.hpp>
#include <detail/stream_impl.hpp>

#include <memory>
#include <mutex>
Expand Down Expand Up @@ -63,12 +64,14 @@ void Scheduler::waitForRecordToFinish(MemObjRecord *Record) {

EventImplPtr Scheduler::addCG(std::unique_ptr<detail::CG> CommandGroup,
QueueImplPtr Queue) {
Command *NewCmd = nullptr;
EventImplPtr NewEvent = nullptr;
const bool IsKernel = CommandGroup->getType() == CG::KERNEL;
vector_class<StreamImplPtr> Streams;
{
std::unique_lock<std::shared_timed_mutex> Lock(MGraphLock, std::defer_lock);
lockSharedTimedMutex(Lock);

Command *NewCmd = nullptr;
switch (CommandGroup->getType()) {
case CG::UPDATE_HOST:
NewCmd = MGraphBuilder.addCGUpdateHost(std::move(CommandGroup),
Expand All @@ -80,22 +83,30 @@ EventImplPtr Scheduler::addCG(std::unique_ptr<detail::CG> CommandGroup,
default:
NewCmd = MGraphBuilder.addCG(std::move(CommandGroup), std::move(Queue));
}
NewEvent = NewCmd->getEvent();
}

{
std::shared_lock<std::shared_timed_mutex> Lock(MGraphLock);

// TODO: Check if lazy mode.
EnqueueResultT Res;
bool Enqueued = GraphProcessor::enqueueCommand(NewCmd, Res);
if (!Enqueued && EnqueueResultT::SyclEnqueueFailed == Res.MResult)
throw runtime_error("Enqueue process failed.", PI_INVALID_OPERATION);
Command *NewCmd = static_cast<Command *>(NewEvent->getCommand());
if (NewCmd) {
// TODO: Check if lazy mode.
EnqueueResultT Res;
bool Enqueued = GraphProcessor::enqueueCommand(NewCmd, Res);
if (!Enqueued && EnqueueResultT::SyclEnqueueFailed == Res.MResult)
throw runtime_error("Enqueue process failed.", PI_INVALID_OPERATION);

if (IsKernel)
Streams = ((ExecCGCommand *)NewCmd)->getStreams();
}
}

if (IsKernel)
((ExecCGCommand *)NewCmd)->flushStreams();
for (auto StreamImplPtr : Streams) {
StreamImplPtr->flush();
}

return NewCmd->getEvent();
return NewEvent;
}

EventImplPtr Scheduler::addCopyBack(Requirement *Req) {
Expand Down