Skip to content

Commit 4aef157

Browse files
authored
[SYCL] Test queue::ext_oneapi_empty() API (intel/llvm-test-suite#1427)
1 parent 2c4a22b commit 4aef157

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed

SYCL/Basic/in_order_queue_status.cpp

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
2+
// RUN: %ACC_RUN_PLACEHOLDER %t.out
3+
// RUN: %CPU_RUN_PLACEHOLDER %t.out
4+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
5+
6+
// Test checks that queue::ext_oneapi_empty() returns status of the in-order
7+
// queue.
8+
9+
#include <chrono>
10+
#include <sycl.hpp>
11+
#include <thread>
12+
13+
static void CheckArray(int *x, size_t buffer_size, int expected) {
14+
for (size_t i = 0; i < buffer_size; ++i) {
15+
assert(x[i] == expected);
16+
}
17+
}
18+
19+
using namespace sycl;
20+
21+
void TestFunc(queue &Q) {
22+
static constexpr int Size = 100;
23+
24+
assert(Q.ext_oneapi_empty() && "Queue is expected to be empty");
25+
26+
int *X = malloc_host<int>(Size, Q);
27+
int *Y = malloc_host<int>(Size, Q);
28+
29+
auto FillEv = Q.fill(X, 99, Size);
30+
auto SingleTaskEv = Q.submit([&](handler &CGH) {
31+
auto SingleTask = [=] {
32+
for (int I = 0; I < Size; I++)
33+
X[I] += 1;
34+
};
35+
CGH.single_task(SingleTask);
36+
});
37+
auto MemCpyEv = Q.copy(X, Y, Size);
38+
constexpr int NumIter = 5;
39+
for (int I = 0; I < NumIter; I++) {
40+
Q.submit([&](handler &CGH) {
41+
CGH.parallel_for<class Kernel1>(sycl::range<1>(Size),
42+
[=](sycl::id<1> WI) { Y[WI] *= 2; });
43+
});
44+
}
45+
46+
// Wait a bit to give a chance for tasks to complete.
47+
std::this_thread::sleep_for(std::chrono::milliseconds(500));
48+
49+
// We expect that all submitted tasks are finished if ext_oneapi_empty is
50+
// true.
51+
if (Q.ext_oneapi_empty())
52+
CheckArray(Y, Size, 3200);
53+
54+
Q.wait();
55+
56+
// After synchronization queue must be empty.
57+
assert(Q.ext_oneapi_empty() && "Queue is expected to be empty");
58+
59+
free(X, Q);
60+
free(Y, Q);
61+
}
62+
63+
int main() {
64+
// Test in-order queue.
65+
queue Q1{property::queue::in_order()};
66+
TestFunc(Q1);
67+
68+
// Test in-order queue with discard_events property.
69+
sycl::property_list Props{
70+
property::queue::in_order{},
71+
sycl::ext::oneapi::property::queue::discard_events{}};
72+
queue Q2{Props};
73+
74+
bool ExceptionThrown = false;
75+
try {
76+
TestFunc(Q2);
77+
} catch (sycl::exception &E) {
78+
ExceptionThrown = true;
79+
}
80+
81+
// Feature is not supported for OpenCL, exception must be thrown.
82+
if (Q2.get_device().get_backend() == backend::opencl)
83+
return ExceptionThrown ? 0 : -1;
84+
85+
return 0;
86+
}
+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
2+
// RUN: %ACC_RUN_PLACEHOLDER %t.out
3+
// RUN: %CPU_RUN_PLACEHOLDER %t.out
4+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
5+
6+
// Test checks that queue::ext_oneapi_empty() returns status of the out-of-order
7+
// queue.
8+
9+
#include <chrono>
10+
#include <sycl.hpp>
11+
#include <thread>
12+
13+
static void CheckArray(int *x, size_t buffer_size, int expected) {
14+
for (size_t i = 0; i < buffer_size; ++i) {
15+
assert(x[i] == expected);
16+
}
17+
}
18+
19+
using namespace sycl;
20+
21+
void TestFunc(queue &Q) {
22+
static constexpr int Size = 100;
23+
24+
assert(Q.ext_oneapi_empty() && "Queue is expected to be empty");
25+
26+
int *X = malloc_host<int>(Size, Q);
27+
int *Y = malloc_host<int>(Size, Q);
28+
29+
auto FillEv = Q.fill(X, 99, Size);
30+
auto HostEv = Q.submit([&](handler &CGH) {
31+
CGH.depends_on(FillEv);
32+
auto HostTask = [=] {
33+
for (int I = 0; I < Size; I++)
34+
X[I] += 1;
35+
};
36+
CGH.host_task(HostTask);
37+
});
38+
auto MemCpyEv = Q.copy(X, Y, Size, {HostEv});
39+
constexpr int NumIter = 5;
40+
for (int I = 0; I < NumIter; I++) {
41+
Q.submit([&](handler &CGH) {
42+
CGH.depends_on(MemCpyEv);
43+
CGH.parallel_for<class Kernel1>(
44+
sycl::range<1>(Size / NumIter),
45+
[=](sycl::id<1> WI) { Y[WI + I * Size / NumIter] *= 2; });
46+
});
47+
}
48+
49+
// Wait a bit to give a chance for tasks to complete.
50+
std::this_thread::sleep_for(std::chrono::milliseconds(500));
51+
52+
// We expect that all submitted tasks are finished if ext_oneapi_empty is
53+
// true.
54+
if (Q.ext_oneapi_empty())
55+
CheckArray(Y, Size, 200);
56+
57+
Q.wait();
58+
59+
// After synchronization queue must be empty.
60+
assert(Q.ext_oneapi_empty() && "Queue is expected to be empty");
61+
62+
free(X, Q);
63+
free(Y, Q);
64+
}
65+
66+
int main() {
67+
queue Q;
68+
69+
bool ExceptionThrown = false;
70+
try {
71+
TestFunc(Q);
72+
} catch (sycl::exception &E) {
73+
ExceptionThrown = true;
74+
}
75+
76+
// Feature is not supported for OpenCL, exception must be thrown.
77+
if (Q.get_device().get_backend() == backend::opencl)
78+
return ExceptionThrown ? 0 : -1;
79+
80+
return 0;
81+
}

0 commit comments

Comments
 (0)