Skip to content

[SYCL] Additional enqueue for host-tasks #2553

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

Closed
wants to merge 10 commits into from
Closed
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
2 changes: 2 additions & 0 deletions sycl/source/detail/scheduler/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ class DispatchHostTask {

for (const DepDesc &Dep : Deps)
Scheduler::enqueueLeavesOfReqUnlocked(Dep.MDepRequirement);

Sched.enqueueHostTasksUnlocked();
}
}
};
Expand Down
8 changes: 6 additions & 2 deletions sycl/source/detail/scheduler/graph_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ static void unmarkVisitedNodes(std::vector<Command *> &Visited) {
Cmd->MMarks.MVisited = false;
}

static void handleVisitedNodes(std::vector<Command *> &Visited) {
void
Scheduler::GraphBuilder::handleVisitedNodes(std::vector<Command *> &Visited) {
for (Command *Cmd : Visited) {
if (Cmd->MMarks.MToBeDeleted) {
Cmd->getEvent()->setCommand(nullptr);
Scheduler::getInstance().removeHostTaskCommandUnlocked(Cmd);
delete Cmd;
} else
Cmd->MMarks.MVisited = false;
Expand Down Expand Up @@ -803,9 +805,11 @@ Scheduler::GraphBuilder::addCG(std::unique_ptr<detail::CG> CommandGroup,
NewCmd->addDep(e);
}

if (CGType == CG::CGTYPE::CODEPLAY_HOST_TASK)
if (CGType == CG::CGTYPE::CODEPLAY_HOST_TASK) {
NewCmd->MEmptyCmd = addEmptyCmd(NewCmd.get(), NewCmd->getCG().MRequirements,
Queue, Command::BlockReason::HostTask);
Scheduler::getInstance().addHostTaskCommandUnlocked(NewCmd.get());
}

if (MPrintOptionsArray[AfterAddCG])
printGraphAsDot("after_addCG");
Expand Down
26 changes: 26 additions & 0 deletions sycl/source/detail/scheduler/scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,32 @@ MemObjRecord *Scheduler::getMemObjRecord(const Requirement *const Req) {
return Req->MSYCLMemObj->MRecord.get();
}

void Scheduler::addHostTaskCommandUnlocked(Command *Cmd) {
HostTaskCommandXRefT XRef = HostTaskCmds.insert(HostTaskCmds.end(), Cmd);
HostTaskCmdXRefs[Cmd] = XRef;
}

void Scheduler::removeHostTaskCommandUnlocked(Command *Cmd) {
auto It = HostTaskCmdXRefs.find(Cmd);

if (It == HostTaskCmdXRefs.end())
return;

HostTaskCommandXRefT &XRef = It->second;
HostTaskCmds.erase(XRef);

HostTaskCmdXRefs.erase(It);
}

void Scheduler::enqueueHostTasksUnlocked() {
for (Command *Cmd : HostTaskCmds) {
EnqueueResultT Res;
bool Enqueued = GraphProcessor::enqueueCommand(Cmd, Res);
if (!Enqueued && EnqueueResultT::SyclEnqueueFailed == Res.MResult)
throw runtime_error("Enqueue process failed.", PI_INVALID_OPERATION);
}
}

} // namespace detail
} // namespace sycl
} // __SYCL_INLINE_NAMESPACE(cl)
20 changes: 20 additions & 0 deletions sycl/source/detail/scheduler/scheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <set>
#include <shared_mutex>
#include <unordered_set>
#include <unordered_map>
#include <vector>

/// \defgroup sycl_graph DPC++ Execution Graph
Expand Down Expand Up @@ -585,6 +586,8 @@ class Scheduler {
private:
friend class ::MockScheduler;

static void handleVisitedNodes(std::vector<Command *> &Visited);

/// Searches for suitable alloca in memory record.
///
/// If none found, creates new one.
Expand Down Expand Up @@ -743,6 +746,23 @@ class Scheduler {

friend class stream_impl;

// List of host-task commands. This data structure is employed to overcome
// certain use-cases with deadlocks involving host-task. The use of this list
// is to enqueue (if possible) host-tasks when another host task is finished.
// List is used in order to remain the order of host-tasks unchanged.
// A map is employed to allow for quick lookup and removal of host-task
// command upon cleanup.
// Access to this data structure is guarded with graph read-write lock.
using HostTaskCommandsT = std::list<Command *>;
using HostTaskCommandXRefT = HostTaskCommandsT::iterator;
HostTaskCommandsT HostTaskCmds;
std::unordered_map<Command *, HostTaskCommandXRefT> HostTaskCmdXRefs;

void addHostTaskCommandUnlocked(Command *Cmd);
void removeHostTaskCommandUnlocked(Command *Cmd);
void enqueueHostTasksUnlocked();


// Protects stream buffers pool
std::mutex StreamBuffersPoolMutex;
std::map<stream_impl *, StreamBuffers> StreamBuffersPool;
Expand Down
197 changes: 159 additions & 38 deletions sycl/test/host-interop-task/host-task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,26 @@
// RUN: %GPU_RUN_PLACEHOLDER %t.out 3
// RUN: %ACC_RUN_PLACEHOLDER %t.out 3

// RUNx: %CPU_RUN_PLACEHOLDER %t.out 4
// RUNx: %GPU_RUN_PLACEHOLDER %t.out 4
// RUNx: %ACC_RUN_PLACEHOLDER %t.out 4
// RUN: %CPU_RUN_PLACEHOLDER %t.out 4
// RUN: %GPU_RUN_PLACEHOLDER %t.out 4
// RUN: %ACC_RUN_PLACEHOLDER %t.out 4

// RUN: %CPU_RUN_PLACEHOLDER %t.out 5
// RUN: %GPU_RUN_PLACEHOLDER %t.out 5
// RUN: %ACC_RUN_PLACEHOLDER %t.out 5

// RUN: %CPU_RUN_PLACEHOLDER %t.out 6
// RUN: %GPU_RUN_PLACEHOLDER %t.out 6
// RUN: %ACC_RUN_PLACEHOLDER %t.out 6

// RUN: %CPU_RUN_PLACEHOLDER %t.out 7
// RUN: %GPU_RUN_PLACEHOLDER %t.out 7
// RUN: %ACC_RUN_PLACEHOLDER %t.out 7

#include <CL/sycl.hpp>
#include <chrono>
#include <iostream>
#include <thread>
#include <vector>

using namespace cl::sycl;
Expand Down Expand Up @@ -103,7 +116,6 @@ void test3() {

std::vector<event> Deps;

using namespace std::chrono_literals;
static constexpr size_t Count = 10;

auto Start = std::chrono::steady_clock::now();
Expand Down Expand Up @@ -146,14 +158,15 @@ void test3() {
Q.wait_and_throw();
auto End = std::chrono::steady_clock::now();

using namespace std::chrono_literals;
constexpr auto Threshold = 2s;

assert(End - Start < Threshold && "Host tasks were waiting for too long");
}

// Host-task depending on another host-task via handler::depends_on() only
// should not hang
void test4() {
void test4(size_t Count = 1) {
queue Q(EH);

static constexpr size_t BufferSize = 10 * 1024;
Expand All @@ -165,51 +178,150 @@ void test4() {
buffer<int, 1> B4{range<1>{BufferSize}};
buffer<int, 1> B5{range<1>{BufferSize}};

// This host task should be submitted without hesitation
event E1 = Q.submit([&](handler &CGH) {
std::cout << "Submit 1" << std::endl;
for (size_t Idx = 1; Idx <= Count; ++Idx) {
// This host task should be submitted without hesitation
event E1 = Q.submit([&](handler &CGH) {
std::cout << "Submit 1" << std::endl;

auto Acc0 = B0.get_access<mode::read_write, target::host_buffer>(CGH);
auto Acc1 = B1.get_access<mode::read_write, target::host_buffer>(CGH);
auto Acc2 = B2.get_access<mode::read_write, target::host_buffer>(CGH);
auto Acc0 = B0.get_access<mode::read_write, target::host_buffer>(CGH);
auto Acc1 = B1.get_access<mode::read_write, target::host_buffer>(CGH);
auto Acc2 = B2.get_access<mode::read_write, target::host_buffer>(CGH);

CGH.codeplay_host_task([=] {
Acc0[0] = 1;
Acc1[0] = 2;
Acc2[0] = 3;
CGH.codeplay_host_task([=] {
Acc0[0] = 1 * Idx;
Acc1[0] = 2 * Idx;
Acc2[0] = 3 * Idx;
});
});
});

// This host task is going to depend on blocked empty node of the first
// host-task (via buffer #2). Still this one should be enqueued.
event E2 = Q.submit([&](handler &CGH) {
std::cout << "Submit 2" << std::endl;
// This host task is going to depend on blocked empty node of the first
// host-task (via buffer #2). Still this one should be enqueued.
event E2 = Q.submit([&](handler &CGH) {
std::cout << "Submit 2" << std::endl;

auto Acc2 = B2.get_access<mode::read_write, target::host_buffer>(CGH);
auto Acc3 = B3.get_access<mode::read_write, target::host_buffer>(CGH);
auto Acc2 = B2.get_access<mode::read_write, target::host_buffer>(CGH);
auto Acc3 = B3.get_access<mode::read_write, target::host_buffer>(CGH);

CGH.codeplay_host_task([=] {
Acc2[1] = 1;
Acc3[1] = 2;
CGH.codeplay_host_task([=] {
Acc2[1] = 1 * Idx;
Acc3[1] = 2 * Idx;
});
});
});

// This host-task only depends on the second host-task via
// handler::depends_on(). This one should not hang and should be enqueued
// after host-task #2.
event E3 = Q.submit([&](handler &CGH) {
CGH.depends_on(E2);
// This host-task only depends on the second host-task via
// handler::depends_on(). This one should not hang and should be eexecuted
// after host-task #2.
event E3 = Q.submit([&](handler &CGH) {
CGH.depends_on(E2);

std::cout << "Submit 3" << std::endl;
std::cout << "Submit 3" << std::endl;

auto Acc4 = B4.get_access<mode::read_write, target::host_buffer>(CGH);
auto Acc5 = B5.get_access<mode::read_write, target::host_buffer>(CGH);
auto Acc4 = B4.get_access<mode::read_write, target::host_buffer>(CGH);
auto Acc5 = B5.get_access<mode::read_write, target::host_buffer>(CGH);

CGH.codeplay_host_task([=] {
Acc4[2] = 1;
Acc5[2] = 2;
CGH.codeplay_host_task([=] {
Acc4[2] = 1 * Idx;
Acc5[2] = 2 * Idx;
});
});
});
}

Q.wait_and_throw();
}

// Host-task depending on another host-task via handler::depends_on() only
// should not hang. A bit more complicated case with kernels depending on
// host-task being involved.
void test5(size_t Count = 1) {
queue Q(EH);

static constexpr size_t BufferSize = 10 * 1024;

buffer<int, 1> B0{range<1>{BufferSize}};
buffer<int, 1> B1{range<1>{BufferSize}};
buffer<int, 1> B2{range<1>{BufferSize}};
buffer<int, 1> B3{range<1>{BufferSize}};
buffer<int, 1> B4{range<1>{BufferSize}};
buffer<int, 1> B5{range<1>{BufferSize}};

using namespace std::chrono_literals;

for (size_t Idx = 1; Idx <= Count; ++Idx) {
// This host task should be submitted without hesitation
Q.submit([&](handler &CGH) {
std::cout << "Submit HT-1" << std::endl;

auto Acc0 = B0.get_access<mode::read_write, target::host_buffer>(CGH);

CGH.codeplay_host_task([=] {
std::this_thread::sleep_for(2s);
Acc0[0] = 1 * Idx;
});
});

Q.submit([&](handler &CGH) {
std::cout << "Submit Kernel-1" << std::endl;

auto Acc0 = B0.get_access<mode::read_write>(CGH);

CGH.single_task<class Test5_Kernel1>([=] {
Acc0[1] = 1 * Idx;
});
});

Q.submit([&](handler &CGH) {
std::cout << "Submit Kernel-2" << std::endl;

auto Acc1 = B1.get_access<mode::read_write>(CGH);

CGH.single_task<class Test5_Kernel2>([=] {
Acc1[2] = 1 * Idx;
});
});

Q.submit([&](handler &CGH) {
std::cout << "Submit HT-2" << std::endl;

auto Acc2 = B2.get_access<mode::read_write, target::host_buffer>(CGH);

CGH.codeplay_host_task([=] {
std::this_thread::sleep_for(2s);
Acc2[3] = 1 * Idx;
});
});

// This host task is going to depend on blocked empty node of the second
// host-task (via buffer #0). Still this one should be enqueued.
event EHT3 = Q.submit([&](handler &CGH) {
std::cout << "Submit HT-3" << std::endl;

auto Acc0 = B0.get_access<mode::read_write, target::host_buffer>(CGH);
auto Acc1 = B1.get_access<mode::read_write, target::host_buffer>(CGH);
auto Acc2 = B2.get_access<mode::read_write, target::host_buffer>(CGH);

CGH.codeplay_host_task([=] {
std::this_thread::sleep_for(2s);
Acc0[4] = 1 * Idx;
Acc1[4] = 2 * Idx;
Acc2[4] = 3 * Idx;
});
});

// This host-task only depends on the third host-task via
// handler::depends_on(). This one should not hang and should be executed
// after host-task #3.
Q.submit([&](handler &CGH) {
std::cout << "Submit HT-4" << std::endl;

CGH.depends_on(EHT3);

auto Acc5 = B5.get_access<mode::read_write, target::host_buffer>(CGH);

CGH.codeplay_host_task([=] {
Acc5[5] = 1 * Idx;
});
});
}

Q.wait_and_throw();
}
Expand All @@ -233,6 +345,15 @@ int main(int Argc, const char *Argv[]) {
case 4:
test4();
break;
case 5:
test5();
break;
case 6:
test4(10);
break;
case 7:
test5(10);
break;
default:
return 1;
}
Expand Down