Skip to content

[SYCL] Fix possible failure when enqueing only a single host-task #1937

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
Jun 26, 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
33 changes: 24 additions & 9 deletions sycl/source/detail/scheduler/scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,18 +156,33 @@ void Scheduler::cleanupFinishedCommands(EventImplPtr FinishedEvent) {
}

void Scheduler::removeMemoryObject(detail::SYCLMemObjI *MemObj) {
MemObjRecord *Record = nullptr;
std::unique_lock<std::shared_timed_mutex> 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 needs a shared mutex as it only involves enqueueing and
// awaiting for events
std::shared_lock<std::shared_timed_mutex> Lock(MGraphLock);
waitForRecordToFinish(Record);
}

{
lockSharedTimedMutex(Lock);
MGraphBuilder.decrementLeafCountersForRecord(Record);
MGraphBuilder.cleanupCommandsForRecord(Record);
MGraphBuilder.removeRecordForMemObj(MemObj);
}
}

EventImplPtr Scheduler::addHostAccessor(Requirement *Req) {
Expand Down
58 changes: 58 additions & 0 deletions sycl/test/host-interop-task/host-task-failure.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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 <CL/sycl.hpp>

using namespace cl::sycl;
using namespace cl::sycl::access;

static constexpr size_t BUFFER_SIZE = 1024;

template <typename T>
class Modifier;

template <typename T>
class Init;

template <typename DataT>
void copy(buffer<DataT, 1> &Src, buffer<DataT, 1> &Dst, queue &Q) {
Q.submit([&](handler &CGH) {
auto SrcA = Src.template get_access<mode::read>(CGH);
auto DstA = Dst.template get_access<mode::write>(CGH);

CGH.codeplay_host_task([=]() {
for (size_t Idx = 0; Idx < SrcA.get_count(); ++Idx)
DstA[Idx] = SrcA[Idx];
});
});
}

template <typename DataT>
void init(buffer<DataT, 1> &B1, buffer<DataT, 1> &B2, queue &Q) {
Q.submit([&](handler &CGH) {
auto Acc1 = B1.template get_access<mode::write>(CGH);
auto Acc2 = B2.template get_access<mode::write>(CGH);

CGH.parallel_for<Init<DataT>>(BUFFER_SIZE, [=](item<1> Id) {
Acc1[Id] = -1;
Acc2[Id] = -2;
});
});
}

void test() {
queue Q;
buffer<int, 1> Buffer1{BUFFER_SIZE};
buffer<int, 1> Buffer2{BUFFER_SIZE};

init<int>(Buffer1, Buffer2, Q);

copy(Buffer1, Buffer2, Q);
}

int main() {
test();
return 0;
}