Skip to content

[SYCL] Removed mutex leading to deadlock #889

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 5 commits into from
Dec 5, 2019
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
1 change: 0 additions & 1 deletion sycl/source/detail/scheduler/scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ std::vector<EventImplPtr> Scheduler::getWaitList(EventImplPtr Event) {
}

void Scheduler::waitForEvent(EventImplPtr Event) {
std::lock_guard<std::mutex> lock(MGraphLock);
GraphProcessor::waitForEvent(std::move(Event));
}

Expand Down
1 change: 1 addition & 0 deletions sycl/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ endfunction()

add_subdirectory(pi)
add_subdirectory(misc)
add_subdirectory(thread_safety)
3 changes: 3 additions & 0 deletions sycl/unittests/thread_safety/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_sycl_unittest(ThreadSafetyTests
HostAccessorDeadLock.cpp
)
34 changes: 34 additions & 0 deletions sycl/unittests/thread_safety/HostAccessorDeadLock.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//==----- HostAccessorDeadLock.cpp --- Thread Safety unit tests ------------==//
//
// 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 "ThreadUtils.h"
#include <CL/sycl.hpp>
#include <gtest/gtest.h>
#include <mutex>
#include <vector>

namespace {
constexpr auto sycl_read_write = cl::sycl::access::mode::read_write;

TEST(HostAccessorDeadLockTest, CheckThreadOrder) {
constexpr std::size_t size = 1;
constexpr std::size_t threadCount = 4, launchCount = 5;

{
cl::sycl::buffer<std::size_t, 1> buffer(size);

auto testLambda = [&](std::size_t threadId) {
auto acc = buffer.get_access<sycl_read_write>();
};

for (std::size_t k = 0; k < launchCount; ++k) {
ThreadPool MPool(threadCount, testLambda);
}
}
}
} // namespace
48 changes: 48 additions & 0 deletions sycl/unittests/thread_safety/ThreadUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <thread>
#include <vector>

class ThreadPool {
public:
ThreadPool() = delete;
ThreadPool(ThreadPool &) = delete;

template <typename Func>
ThreadPool(std::size_t N, Func func) {
for (std::size_t i = 0; i < N; ++i) {
enqueue(func, i);
}
}

template <typename Func, typename... Funcs>
ThreadPool(Func &&func, Funcs &&... funcs) {
constexpr int N = sizeof...(funcs);
enqueue(std::forward<Func>(func), N);
enqueueHelper<N>(std::forward<Funcs>(funcs)...);
}

~ThreadPool() { wait(); }

private:
template <int N, typename Func, typename... Funcs>
void enqueueHelper(Func &&func, Funcs &&... funcs) {
enqueue(std::forward<Func>(func), N - 1);
enqueueHelper<N - 1>(std::forward<Funcs>(funcs)...);
}

template <int N>
void enqueueHelper() {}

template <typename Func, typename... Args>
void enqueue(Func &&func, Args &&... args) {
MThreadPool.emplace_back(std::forward<Func>(func),
std::forward<Args>(args)...);
}

void wait() {
for (auto &t : MThreadPool) {
t.join();
}
}

std::vector<std::thread> MThreadPool;
};