From 463336e0e32205c484cc78aba6faf988652b9011 Mon Sep 17 00:00:00 2001 From: Sergey Kanaev Date: Fri, 19 Jun 2020 19:52:10 +0300 Subject: [PATCH 1/4] [SYCL] Fix possible failure when enqueing only a single host-task Signed-off-by: Sergey Kanaev --- sycl/source/detail/scheduler/scheduler.cpp | 33 +++++++--- .../host-interop-task/host-task-failure.cpp | 60 +++++++++++++++++++ 2 files changed, 84 insertions(+), 9 deletions(-) create mode 100644 sycl/test/host-interop-task/host-task-failure.cpp diff --git a/sycl/source/detail/scheduler/scheduler.cpp b/sycl/source/detail/scheduler/scheduler.cpp index d896dfc6be08b..238c7147bf169 100644 --- a/sycl/source/detail/scheduler/scheduler.cpp +++ b/sycl/source/detail/scheduler/scheduler.cpp @@ -156,18 +156,33 @@ void Scheduler::cleanupFinishedCommands(EventImplPtr FinishedEvent) { } void Scheduler::removeMemoryObject(detail::SYCLMemObjI *MemObj) { + MemObjRecord *Record = nullptr; std::unique_lock Lock(MGraphLock, std::defer_lock); - lockSharedTimedMutex(Lock); - MemObjRecord *Record = MGraphBuilder.getMemObjRecord(MemObj); - if (!Record) - // No operations were performed on the mem object - return; + { + lockSharedTimedMutex(Lock); + + Record = MGraphBuilder.getMemObjRecord(MemObj); + if (!Record) + // No operations were performed on the mem object + return; - waitForRecordToFinish(Record); - MGraphBuilder.decrementLeafCountersForRecord(Record); - MGraphBuilder.cleanupCommandsForRecord(Record); - MGraphBuilder.removeRecordForMemObj(MemObj); + Lock.unlock(); + } + + { + // This only need a shared mutex as it only involves enqueueing and awaiting + // for events + std::shared_lock Lock(MGraphLock); + waitForRecordToFinish(Record); + } + + { + lockSharedTimedMutex(Lock); + MGraphBuilder.decrementLeafCountersForRecord(Record); + MGraphBuilder.cleanupCommandsForRecord(Record); + MGraphBuilder.removeRecordForMemObj(MemObj); + } } EventImplPtr Scheduler::addHostAccessor(Requirement *Req) { diff --git a/sycl/test/host-interop-task/host-task-failure.cpp b/sycl/test/host-interop-task/host-task-failure.cpp new file mode 100644 index 0000000000000..05f125310c04d --- /dev/null +++ b/sycl/test/host-interop-task/host-task-failure.cpp @@ -0,0 +1,60 @@ +// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out +// RUN: %CPU_RUN_PLACEHOLDER %t.out +// RUN: %GPU_RUN_PLACEHOLDER %t.out +// RUN: %ACC_RUN_PLACEHOLDER %t.out + +#include +#include +#include + +using namespace cl::sycl; +using namespace cl::sycl::access; + +static constexpr size_t BUFFER_SIZE = 1024; + +template +class Modifier; + +template +class Init; + +template +void copy(buffer &Src, buffer &Dst, queue &Q) { + Q.submit([&](handler &CGH) { + auto SrcA = Src.template get_access(CGH); + auto DstA = Dst.template get_access(CGH); + + CGH.codeplay_host_task([=]() { + for (size_t Idx = 0; Idx < SrcA.get_count(); ++Idx) + DstA[Idx] = SrcA[Idx]; + }); + }); +} + +template +void init(buffer &B1, buffer &B2, queue &Q) { + Q.submit([&](handler &CGH) { + auto Acc1 = B1.template get_access(CGH); + auto Acc2 = B2.template get_access(CGH); + + CGH.parallel_for>(BUFFER_SIZE, [=](item<1> Id) { + Acc1[Id] = -1; + Acc2[Id] = -2; + }); + }); +} + +void test() { + queue Q; + buffer Buffer1{BUFFER_SIZE}; + buffer Buffer2{BUFFER_SIZE}; + + init(Buffer1, Buffer2, Q); + + copy(Buffer1, Buffer2, Q); +} + +int main() { + test(); + return 0; +} From 5ea8b72b0a8e8d2b415f54c0c9bce10e333aebe5 Mon Sep 17 00:00:00 2001 From: Sergey Kanaev Date: Thu, 25 Jun 2020 13:55:57 +0300 Subject: [PATCH 2/4] [SYCL] Remove unneeded includes Signed-off-by: Sergey Kanaev --- sycl/test/host-interop-task/host-task-failure.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/sycl/test/host-interop-task/host-task-failure.cpp b/sycl/test/host-interop-task/host-task-failure.cpp index 05f125310c04d..423082b53198d 100644 --- a/sycl/test/host-interop-task/host-task-failure.cpp +++ b/sycl/test/host-interop-task/host-task-failure.cpp @@ -4,8 +4,6 @@ // RUN: %ACC_RUN_PLACEHOLDER %t.out #include -#include -#include using namespace cl::sycl; using namespace cl::sycl::access; From b551c2a64e3b9185c2462fbb0f44a3244e2df46d Mon Sep 17 00:00:00 2001 From: sergei <57672082+s-kanaev@users.noreply.github.com> Date: Thu, 25 Jun 2020 14:53:19 +0300 Subject: [PATCH 3/4] Update sycl/source/detail/scheduler/scheduler.cpp Signed-off-by: Sergey Kanaev Co-authored-by: Sergey Semenov <43845535+sergey-semenov@users.noreply.github.com> --- sycl/source/detail/scheduler/scheduler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/source/detail/scheduler/scheduler.cpp b/sycl/source/detail/scheduler/scheduler.cpp index 238c7147bf169..e02821a91744f 100644 --- a/sycl/source/detail/scheduler/scheduler.cpp +++ b/sycl/source/detail/scheduler/scheduler.cpp @@ -171,7 +171,7 @@ void Scheduler::removeMemoryObject(detail::SYCLMemObjI *MemObj) { } { - // This only need a shared mutex as it only involves enqueueing and awaiting + // This only needs a shared mutex as it only involves enqueueing and awaiting // for events std::shared_lock Lock(MGraphLock); waitForRecordToFinish(Record); From b6f892d5cdb17cf075e29b4a0cc2cc92c4bc462a Mon Sep 17 00:00:00 2001 From: Sergey Kanaev Date: Thu, 25 Jun 2020 14:58:12 +0300 Subject: [PATCH 4/4] [SYCL] Fix style issue Signed-off-by: Sergey Kanaev --- sycl/source/detail/scheduler/scheduler.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sycl/source/detail/scheduler/scheduler.cpp b/sycl/source/detail/scheduler/scheduler.cpp index e02821a91744f..c4f2ab6534551 100644 --- a/sycl/source/detail/scheduler/scheduler.cpp +++ b/sycl/source/detail/scheduler/scheduler.cpp @@ -171,8 +171,8 @@ void Scheduler::removeMemoryObject(detail::SYCLMemObjI *MemObj) { } { - // This only needs a shared mutex as it only involves enqueueing and awaiting - // for events + // This only needs a shared mutex as it only involves enqueueing and + // awaiting for events std::shared_lock Lock(MGraphLock); waitForRecordToFinish(Record); }