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