Skip to content

Commit

Permalink
[SYCL][Graph] Makes command graph functions thread-safe (#265)
Browse files Browse the repository at this point in the history
* [SYCL][Graph] Makes command graph functions thread-safe

Addresses comments made on the first PR commit.
Mutexes are now added to Graph implementation entry points
instead of end points as was the case in the previous commit.
Adds "build_pthread_inc" lit test macro to facilitate the
compilation of the threading tests.
Removes std::barrier (std-20) dependency in threading tests.

Addresses Issue: #85

* [SYCL][Graph] Makes command graph functions thread-safe

Moves threading tests that do not require a device to run to unitests

* Update sycl/source/detail/graph_impl.cpp

Co-authored-by: Ben Tracy <ben.tracy@codeplay.com>

* [SYCL][Graph] Makes command graph functions thread-safe

Adds some comments.

* Update sycl/source/handler.cpp

Co-authored-by: Pablo Reble <pablo.reble@intel.com>

* Update sycl/source/detail/graph_impl.hpp

Co-authored-by: Ewan Crawford <ewan@codeplay.com>

* [SYCL][Graph] Makes command graph functions thread-safe

Adds dedidacted sub-class to unitests for multi-threading unitests

* [SYCL][Graph] Makes command graph functions thread-safe

Adds comments

* [SYCL][Graph] thread-safe: bug fix after rebase

---------

Co-authored-by: Ben Tracy <ben.tracy@codeplay.com>
Co-authored-by: Pablo Reble <pablo.reble@intel.com>
Co-authored-by: Ewan Crawford <ewan@codeplay.com>
  • Loading branch information
4 people committed Aug 4, 2023
1 parent 8922f41 commit bfcd551
Show file tree
Hide file tree
Showing 13 changed files with 668 additions and 24 deletions.
18 changes: 16 additions & 2 deletions sycl/source/detail/graph_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ graph_impl::add(sycl::detail::CG::CGTYPE CGType,
checkForRequirement(Req, NodePtr, UniqueDeps);
}
}

// Add any nodes specified by event dependencies into the dependency list
for (auto Dep : CommandGroup->getEvents()) {
if (auto NodeImpl = MEventsMap.find(Dep); NodeImpl != MEventsMap.end()) {
Expand Down Expand Up @@ -474,6 +473,8 @@ void exec_graph_impl::createCommandBuffers(sycl::device Device) {
}

exec_graph_impl::~exec_graph_impl() {
WriteLock Lock(MMutex);

// clear all recording queue if not done before (no call to end_recording)
MGraphImpl->clearQueues();

Expand All @@ -499,6 +500,8 @@ exec_graph_impl::~exec_graph_impl() {
sycl::event
exec_graph_impl::enqueue(const std::shared_ptr<sycl::detail::queue_impl> &Queue,
sycl::detail::CG::StorageInitHelper CGData) {
WriteLock Lock(MMutex);

auto CreateNewEvent([&]() {
auto NewEvent = std::make_shared<sycl::detail::event_impl>(Queue);
NewEvent->setContextImpl(Queue->getContextImplPtr());
Expand Down Expand Up @@ -612,6 +615,7 @@ node modifiable_command_graph::addImpl(const std::vector<node> &Deps) {
DepImpls.push_back(sycl::detail::getSyclObjImpl(D));
}

graph_impl::WriteLock Lock(impl->MMutex);
std::shared_ptr<detail::node_impl> NodeImpl = impl->add(DepImpls);
return sycl::detail::createSyclObjFromImpl<node>(NodeImpl);
}
Expand All @@ -624,6 +628,7 @@ node modifiable_command_graph::addImpl(std::function<void(handler &)> CGF,
DepImpls.push_back(sycl::detail::getSyclObjImpl(D));
}

graph_impl::WriteLock Lock(impl->MMutex);
std::shared_ptr<detail::node_impl> NodeImpl =
impl->add(impl, CGF, {}, DepImpls);
return sycl::detail::createSyclObjFromImpl<node>(NodeImpl);
Expand All @@ -635,6 +640,7 @@ void modifiable_command_graph::make_edge(node &Src, node &Dest) {
std::shared_ptr<detail::node_impl> ReceiverImpl =
sycl::detail::getSyclObjImpl(Dest);

graph_impl::WriteLock Lock(impl->MMutex);
impl->makeEdge(SenderImpl, ReceiverImpl);
}

Expand Down Expand Up @@ -666,6 +672,7 @@ bool modifiable_command_graph::begin_recording(queue &RecordingQueue) {

if (QueueImpl->getCommandGraph() == nullptr) {
QueueImpl->setCommandGraph(impl);
graph_impl::WriteLock Lock(impl->MMutex);
impl->addQueue(QueueImpl);
return true;
}
Expand All @@ -687,12 +694,16 @@ bool modifiable_command_graph::begin_recording(
return QueueStateChanged;
}

bool modifiable_command_graph::end_recording() { return impl->clearQueues(); }
bool modifiable_command_graph::end_recording() {
graph_impl::WriteLock Lock(impl->MMutex);
return impl->clearQueues();
}

bool modifiable_command_graph::end_recording(queue &RecordingQueue) {
auto QueueImpl = sycl::detail::getSyclObjImpl(RecordingQueue);
if (QueueImpl->getCommandGraph() == impl) {
QueueImpl->setCommandGraph(nullptr);
graph_impl::WriteLock Lock(impl->MMutex);
impl->removeQueue(QueueImpl);
return true;
}
Expand All @@ -719,6 +730,9 @@ executable_command_graph::executable_command_graph(
const std::shared_ptr<detail::graph_impl> &Graph, const sycl::context &Ctx)
: MTag(rand()),
impl(std::make_shared<detail::exec_graph_impl>(Ctx, Graph)) {
// Graph is read and written in this scope so we lock
// this graph with full priviledges.
graph_impl::WriteLock Lock(Graph->MMutex);
finalizeImpl(); // Create backend representation for executable graph
}

Expand Down
Loading

0 comments on commit bfcd551

Please sign in to comment.