Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

[SYCL] Add LIT tests for handler::host_task() (#3837) #299

Merged
merged 3 commits into from
Jun 8, 2021
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
8 changes: 8 additions & 0 deletions SYCL/Basic/accessor/Inputs/host_task_accessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ int main() {
sycl::id<1>(1), sycl::write_only);
#endif

#if defined(accessor_new_api_test) || defined(buffer_new_api_test)
cgh.host_task([=]() {
#else
cgh.codeplay_host_task([=]() {
#endif
acc_7[6] = acc_1[0];
acc_8[7] = acc_2[1];
acc_9[7] = acc_3[1];
Expand Down Expand Up @@ -109,7 +113,11 @@ int main() {
sycl::write_only, sycl::noinit);
#endif

#if defined(accessor_new_api_test) || defined(buffer_new_api_test)
cgh.host_task([=]() {
#else
cgh.codeplay_host_task([=]() {
#endif
acc_7[6] = acc_1[0];
acc_8[7] = acc_2[1];
acc_9[7] = acc_3[1];
Expand Down
94 changes: 60 additions & 34 deletions SYCL/Basic/host-task-dependency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@
// TODO: Behaviour is unstable for level zero on Windows. Enable when fixed.
// UNSUPPORTED: windows && level_zero

#define SYCL2020_DISABLE_DEPRECATION_WARNINGS

#include <CL/sycl.hpp>

#include <atomic>
#include <condition_variable>
#include <future>
#include <mutex>
#include <thread>

#include <CL/sycl.hpp>

namespace S = cl::sycl;

template <typename T, bool B> class NameGen;

struct Context {
std::atomic_bool Flag;
S::queue &Queue;
Expand All @@ -26,7 +30,37 @@ struct Context {
std::condition_variable CV;
};

void Thread1Fn(Context *Ctx) {
template <bool UseSYCL2020HostTask>
S::event HostTask_CopyBuf1ToBuf2(Context *Ctx) {
S::event Event = Ctx->Queue.submit([&](S::handler &CGH) {
S::accessor<int, 1, S::access::mode::read, S::access::target::host_buffer>
CopierSrcAcc(Ctx->Buf1, CGH);
S::accessor<int, 1, S::access::mode::write, S::access::target::host_buffer>
CopierDstAcc(Ctx->Buf2, CGH);

auto CopierHostTask = [=] {
for (size_t Idx = 0; Idx < CopierDstAcc.get_count(); ++Idx)
CopierDstAcc[Idx] = CopierSrcAcc[Idx];

bool Expected = false;
bool Desired = true;
assert(Ctx->Flag.compare_exchange_strong(Expected, Desired));

{
std::lock_guard<std::mutex> Lock(Ctx->Mutex);
Ctx->CV.notify_all();
}
};

if constexpr (UseSYCL2020HostTask)
CGH.host_task(CopierHostTask);
else
CGH.codeplay_host_task(CopierHostTask);
});
return Event;
}

template <bool UseSYCL2020HostTask> void Thread1Fn(Context *Ctx) {
// 0. initialize resulting buffer with apriori wrong result
{
S::accessor<int, 1, S::access::mode::write, S::access::target::host_buffer>
Expand Down Expand Up @@ -63,32 +97,11 @@ void Thread1Fn(Context *Ctx) {
GeneratorAcc[Idx] = Idx;
};

CGH.single_task<class GeneratorTask>(GeneratorKernel);
CGH.single_task<NameGen<class Gen, UseSYCL2020HostTask>>(GeneratorKernel);
});

// 2. submit host task writing from buf 1 to buf 2
auto HostTaskEvent = Ctx->Queue.submit([&](S::handler &CGH) {
S::accessor<int, 1, S::access::mode::read, S::access::target::host_buffer>
CopierSrcAcc(Ctx->Buf1, CGH);
S::accessor<int, 1, S::access::mode::write, S::access::target::host_buffer>
CopierDstAcc(Ctx->Buf2, CGH);

auto CopierHostTask = [CopierSrcAcc, CopierDstAcc, &Ctx] {
for (size_t Idx = 0; Idx < CopierDstAcc.get_count(); ++Idx)
CopierDstAcc[Idx] = CopierSrcAcc[Idx];

bool Expected = false;
bool Desired = true;
assert(Ctx->Flag.compare_exchange_strong(Expected, Desired));

{
std::lock_guard<std::mutex> Lock(Ctx->Mutex);
Ctx->CV.notify_all();
}
};

CGH.codeplay_host_task(CopierHostTask);
});
S::event HostTaskEvent = HostTask_CopyBuf1ToBuf2<UseSYCL2020HostTask>(Ctx);

// 3. submit simple task to move data between two buffers
Ctx->Queue.submit([&](S::handler &CGH) {
Expand All @@ -105,7 +118,7 @@ void Thread1Fn(Context *Ctx) {
DstAcc[Idx] = SrcAcc[Idx];
};

CGH.single_task<class CopierTask>(CopierKernel);
CGH.single_task<NameGen<class Copier, UseSYCL2020HostTask>>(CopierKernel);
});

// 4. check data in buffer #3
Expand Down Expand Up @@ -134,7 +147,7 @@ void Thread2Fn(Context *Ctx) {
assert(Ctx->Flag.load());
}

void test() {
template <bool UseSYCL2020HostTask> void test() {
auto EH = [](S::exception_list EL) {
for (const std::exception_ptr &E : EL) {
throw E;
Expand All @@ -146,7 +159,8 @@ void test() {
Context Ctx{{false}, Queue, {10}, {10}, {10}, {}, {}};

// 0. setup: thread 1 T1: exec smth; thread 2 T2: waits; init flag F = false
auto A1 = std::async(std::launch::async, Thread1Fn, &Ctx);
auto A1 =
std::async(std::launch::async, Thread1Fn<UseSYCL2020HostTask>, &Ctx);
auto A2 = std::async(std::launch::async, Thread2Fn, &Ctx);

A1.get();
Expand All @@ -171,21 +185,33 @@ void test() {
}

int main() {
test();
test<true>();
test<false>();

return 0;
}

// launch of GeneratorTask kernel
// launch of Gen kernel
// CHECK:---> piKernelCreate(
// CHECK: GeneratorTask
// CHECK: NameGen
// CHECK:---> piEnqueueKernelLaunch(
// prepare for host task
// CHECK:---> piEnqueueMemBuffer{{Map|Read}}(
// launch of CopierTask kernel
// launch of Copier kernel
// CHECK:---> piKernelCreate(
// CHECK: CopierTask
// CHECK: Copier
// CHECK:---> piEnqueueKernelLaunch(

// CHECK:---> piKernelCreate(
// CHECK: NameGen
// CHECK:---> piEnqueueKernelLaunch(
// prepare for host task
// CHECK:---> piEnqueueMemBuffer{{Map|Read}}(
// launch of Copier kernel
// CHECK:---> piKernelCreate(
// CHECK: Copier
// CHECK:---> piEnqueueKernelLaunch(

// TODO need to check for piEventsWait as "wait on dependencies of host task".
// At the same time this piEventsWait may occur anywhere after
// piEnqueueMemBufferMap ("prepare for host task").
32 changes: 22 additions & 10 deletions SYCL/HostInteropTask/host-task-dependency2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ static auto EH = [](exception_list EL) {

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

template <bool UseSYCL2020HostTask> void test(queue &Q, size_t Count) {
static constexpr size_t BufferSize = 10 * 1024;

buffer<int, 1> B0{range<1>{BufferSize}};
Expand All @@ -45,11 +43,15 @@ void test(size_t Count) {
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([=] {
auto Func = [=] {
Acc0[0] = 1 * Idx;
Acc1[0] = 2 * Idx;
Acc2[0] = 3 * Idx;
});
};
if constexpr (UseSYCL2020HostTask)
CGH.host_task(Func);
else
CGH.codeplay_host_task(Func);
});

// This host task is going to depend on blocked empty node of the first
Expand All @@ -60,10 +62,14 @@ void test(size_t Count) {
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([=] {
auto Func = [=] {
Acc2[1] = 1 * Idx;
Acc3[1] = 2 * Idx;
});
};
if constexpr (UseSYCL2020HostTask)
CGH.host_task(Func);
else
CGH.codeplay_host_task(Func);
});

// This host-task only depends on the second host-task via
Expand All @@ -77,10 +83,14 @@ void test(size_t Count) {
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([=] {
auto Func = [=] {
Acc4[2] = 1 * Idx;
Acc5[2] = 2 * Idx;
});
};
if constexpr (UseSYCL2020HostTask)
CGH.host_task(Func);
else
CGH.codeplay_host_task(Func);
});
}

Expand All @@ -92,6 +102,8 @@ int main(int Argc, const char *Argv[]) {
if (Argc > 1)
Count = std::stoi(Argv[1]);

test(Count);
queue Q(EH);
test<true>(Q, Count);
test<false>(Q, Count);
return 0;
}
8 changes: 4 additions & 4 deletions SYCL/HostInteropTask/host-task-dependency3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void test(size_t Count) {

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

CGH.codeplay_host_task([=] {
CGH.host_task([=] {
std::this_thread::sleep_for(SleepFor);
Acc0[0] = 1 * Idx;
});
Expand All @@ -76,7 +76,7 @@ void test(size_t Count) {

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

CGH.codeplay_host_task([=] {
CGH.host_task([=] {
std::this_thread::sleep_for(SleepFor);
Acc2[3] = 1 * Idx;
});
Expand All @@ -91,7 +91,7 @@ void test(size_t Count) {
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([=] {
CGH.host_task([=] {
std::this_thread::sleep_for(SleepFor);
Acc0[4] = 1 * Idx;
Acc1[4] = 2 * Idx;
Expand All @@ -109,7 +109,7 @@ void test(size_t Count) {

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

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

Expand Down
4 changes: 2 additions & 2 deletions SYCL/HostInteropTask/host-task-dependency4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
cl::sycl::event submit(cl::sycl::queue &Q, cl::sycl::buffer<int> &B) {
return Q.submit([&](cl::sycl::handler &CGH) {
auto A = B.template get_access<cl::sycl::access::mode::read_write>(CGH);
CGH.codeplay_host_task([=]() { (void)A; });
CGH.host_task([=]() { (void)A; });
});
}

Expand All @@ -23,7 +23,7 @@ int main() {
Events.push_back(submit(Q, A));
Q.submit([&](sycl::handler &CGH) {
CGH.depends_on(Events);
CGH.codeplay_host_task([&] { printf("all done\n"); });
CGH.host_task([&] { printf("all done\n"); });
}).wait_and_throw();

return 0;
Expand Down
2 changes: 1 addition & 1 deletion SYCL/HostInteropTask/host-task-failure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void copy(buffer<DataT, 1> &Src, buffer<DataT, 1> &Dst, queue &Q) {
auto SrcA = Src.template get_access<mode::read>(CGH);
auto DstA = Dst.template get_access<mode::write>(CGH);

CGH.codeplay_host_task([=]() {
CGH.host_task([=]() {
for (size_t Idx = 0; Idx < SrcA.get_count(); ++Idx)
DstA[Idx] = SrcA[Idx];
});
Expand Down
2 changes: 1 addition & 1 deletion SYCL/HostInteropTask/host-task-two-queues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void test() {
auto AccB = BufB.get_access<sycl::access::mode::read>(CGH);
auto AccC = BufC.get_access<sycl::access::mode::read_write>(CGH);

CGH.codeplay_host_task([=] {
CGH.host_task([=] {
for (size_t I = 0; I < WIDTH; ++I)
for (size_t J = 0; J < HEIGHT; ++J) {
std::cout << "C[" << I << "][" << J << "] = " << AccC[I][J]
Expand Down
Loading