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

[SYCL] Test queue::ext_oneapi_empty() API #1427

Merged
merged 3 commits into from
Dec 13, 2022
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
86 changes: 86 additions & 0 deletions SYCL/Basic/in_order_queue_status.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
// RUN: %ACC_RUN_PLACEHOLDER %t.out
// RUN: %CPU_RUN_PLACEHOLDER %t.out
// RUN: %GPU_RUN_PLACEHOLDER %t.out

// Test checks that queue::ext_oneapi_empty() returns status of the in-order
// queue.

#include <chrono>
#include <sycl.hpp>
#include <thread>

static void CheckArray(int *x, size_t buffer_size, int expected) {
for (size_t i = 0; i < buffer_size; ++i) {
assert(x[i] == expected);
}
}

using namespace sycl;

void TestFunc(queue &Q) {
static constexpr int Size = 100;

assert(Q.ext_oneapi_empty() && "Queue is expected to be empty");

int *X = malloc_host<int>(Size, Q);
int *Y = malloc_host<int>(Size, Q);

auto FillEv = Q.fill(X, 99, Size);
auto SingleTaskEv = Q.submit([&](handler &CGH) {
auto SingleTask = [=] {
for (int I = 0; I < Size; I++)
X[I] += 1;
};
CGH.single_task(SingleTask);
});
auto MemCpyEv = Q.copy(X, Y, Size);
constexpr int NumIter = 5;
for (int I = 0; I < NumIter; I++) {
Q.submit([&](handler &CGH) {
CGH.parallel_for<class Kernel1>(sycl::range<1>(Size),
[=](sycl::id<1> WI) { Y[WI] *= 2; });
});
}

// Wait a bit to give a chance for tasks to complete.
std::this_thread::sleep_for(std::chrono::milliseconds(500));
Copy link

@KseniyaTikhomirova KseniyaTikhomirova Dec 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we try to avoid sleeps in tests? we could not rely on such approach.
what if to add host_task as the last task that would signal cv, atomic or lock/unlock mutex and simply wait for it instead of sleep, will it help?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if to add host_task as the last task that would signal cv, atomic or lock/unlock mutex and simply wait for it instead of sleep, will it help?

Unfortunately, as far as I know SYCL doesn't guarantee that such waiting will ever complete. User must perform synchronization to guarantee completion. I mean it is not guaranteed that queue will be flushed and we may just wait forever.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KseniyaTikhomirova, unfortunately, I don't have good ideas how to get rid of sleep right now for the reasons I described above. I can probably replace it with some loop - try several times to see if queue is empty and give up if it is not empty - but I'm not sure whether it is better or not.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, unfortunately I also do not have better ideas about how to get rid of sleep call. If we want to check that the right value could be returned even without wait call - may be it could be done with unittest. Although I do not want to block you here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Unittest may be a good idea.


// We expect that all submitted tasks are finished if ext_oneapi_empty is
// true.
if (Q.ext_oneapi_empty())
CheckArray(Y, Size, 3200);

Q.wait();

// After synchronization queue must be empty.
assert(Q.ext_oneapi_empty() && "Queue is expected to be empty");

free(X, Q);
free(Y, Q);
}

int main() {
// Test in-order queue.
queue Q1{property::queue::in_order()};
TestFunc(Q1);

// Test in-order queue with discard_events property.
sycl::property_list Props{
property::queue::in_order{},
sycl::ext::oneapi::property::queue::discard_events{}};
queue Q2{Props};

bool ExceptionThrown = false;
try {
TestFunc(Q2);
} catch (sycl::exception &E) {
ExceptionThrown = true;
}

// Feature is not supported for OpenCL, exception must be thrown.
if (Q2.get_device().get_backend() == backend::opencl)
return ExceptionThrown ? 0 : -1;

return 0;
}
81 changes: 81 additions & 0 deletions SYCL/Basic/out_of_order_queue_status.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
// RUN: %ACC_RUN_PLACEHOLDER %t.out
// RUN: %CPU_RUN_PLACEHOLDER %t.out
// RUN: %GPU_RUN_PLACEHOLDER %t.out

// Test checks that queue::ext_oneapi_empty() returns status of the out-of-order
// queue.

#include <chrono>
#include <sycl.hpp>
#include <thread>

static void CheckArray(int *x, size_t buffer_size, int expected) {
for (size_t i = 0; i < buffer_size; ++i) {
assert(x[i] == expected);
}
}

using namespace sycl;

void TestFunc(queue &Q) {
static constexpr int Size = 100;

assert(Q.ext_oneapi_empty() && "Queue is expected to be empty");

int *X = malloc_host<int>(Size, Q);
int *Y = malloc_host<int>(Size, Q);

auto FillEv = Q.fill(X, 99, Size);
auto HostEv = Q.submit([&](handler &CGH) {
CGH.depends_on(FillEv);
auto HostTask = [=] {
for (int I = 0; I < Size; I++)
X[I] += 1;
};
CGH.host_task(HostTask);
});
auto MemCpyEv = Q.copy(X, Y, Size, {HostEv});
constexpr int NumIter = 5;
for (int I = 0; I < NumIter; I++) {
Q.submit([&](handler &CGH) {
CGH.depends_on(MemCpyEv);
CGH.parallel_for<class Kernel1>(
sycl::range<1>(Size / NumIter),
[=](sycl::id<1> WI) { Y[WI + I * Size / NumIter] *= 2; });
});
}

// Wait a bit to give a chance for tasks to complete.
std::this_thread::sleep_for(std::chrono::milliseconds(500));

// We expect that all submitted tasks are finished if ext_oneapi_empty is
// true.
if (Q.ext_oneapi_empty())
CheckArray(Y, Size, 200);

Q.wait();

// After synchronization queue must be empty.
assert(Q.ext_oneapi_empty() && "Queue is expected to be empty");

free(X, Q);
free(Y, Q);
}

int main() {
queue Q;

bool ExceptionThrown = false;
try {
TestFunc(Q);
} catch (sycl::exception &E) {
ExceptionThrown = true;
}

// Feature is not supported for OpenCL, exception must be thrown.
if (Q.get_device().get_backend() == backend::opencl)
return ExceptionThrown ? 0 : -1;

return 0;
}