|
| 1 | +// REQUIRES: level_zero |
| 2 | +// |
| 3 | +// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out |
| 4 | +// |
| 5 | +// RUN: env SYCL_PI_LEVEL_ZERO_BATCH_SIZE=0 SYCL_DEVICE_FILTER=level_zero %GPU_RUN_PLACEHOLDER %t.out |
| 6 | +// RUN: env SYCL_PI_LEVEL_ZERO_BATCH_SIZE=1 SYCL_DEVICE_FILTER=level_zero %GPU_RUN_PLACEHOLDER %t.out |
| 7 | +// RUN: env SYCL_PI_LEVEL_ZERO_BATCH_SIZE=2 SYCL_DEVICE_FILTER=level_zero %GPU_RUN_PLACEHOLDER %t.out |
| 8 | +// RUN: env SYCL_PI_LEVEL_ZERO_BATCH_SIZE=3 SYCL_DEVICE_FILTER=level_zero %GPU_RUN_PLACEHOLDER %t.out |
| 9 | +// |
| 10 | +// The test checks that interleaving using copy and kernel operations are |
| 11 | +// performed in-order, regardless of batching. IMPORTANT NOTE: this is a |
| 12 | +// critical test, double-check if your changes are related to L0 events and |
| 13 | +// links between commands. |
| 14 | + |
| 15 | +#include <CL/sycl.hpp> |
| 16 | +#include <cassert> |
| 17 | +#include <iostream> |
| 18 | +#include <numeric> |
| 19 | + |
| 20 | +static constexpr int MAGIC_NUM1 = 2; |
| 21 | +static constexpr int buffer_size = 100; |
| 22 | +sycl::usm::alloc AllocType = sycl::usm::alloc::device; |
| 23 | + |
| 24 | +const size_t PartSize = 5; |
| 25 | +const bool PartiallyPrint = buffer_size > 2 * PartSize; |
| 26 | + |
| 27 | +void ValidationPrint(const std::string &vectName, const std::vector<int> &vect, |
| 28 | + const std::function<int(size_t)> &ExpectedVal) { |
| 29 | + std::cerr << vectName; |
| 30 | + if (!PartiallyPrint) { |
| 31 | + for (size_t i = 0u; i < buffer_size; ++i) { |
| 32 | + std::cerr << " " << vect[i]; |
| 33 | + } |
| 34 | + } else { |
| 35 | + for (size_t i = 0u; i < PartSize; ++i) { |
| 36 | + std::cerr << " " << vect[i]; |
| 37 | + } |
| 38 | + std::cerr << " ... "; |
| 39 | + for (size_t i = buffer_size - PartSize; i < buffer_size; ++i) { |
| 40 | + std::cerr << " " << vect[i]; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + std::cerr << std::endl << "expected[] = "; |
| 45 | + if (!PartiallyPrint) { |
| 46 | + for (size_t i = 0u; i < buffer_size; ++i) { |
| 47 | + std::cerr << " " << ExpectedVal(i); |
| 48 | + } |
| 49 | + } else { |
| 50 | + for (size_t i = 0u; i < PartSize; ++i) { |
| 51 | + std::cerr << " " << ExpectedVal(i); |
| 52 | + } |
| 53 | + std::cerr << " ... "; |
| 54 | + for (size_t i = buffer_size - PartSize; i < buffer_size; ++i) { |
| 55 | + std::cerr << " " << ExpectedVal(i); |
| 56 | + } |
| 57 | + } |
| 58 | + std::cerr << std::endl; |
| 59 | + for (int i = 0; i < buffer_size; ++i) { |
| 60 | + if (vect[i] != ExpectedVal(i)) { |
| 61 | + std::cerr << "i = " << i << " is wrong!!! " << std::endl; |
| 62 | + break; |
| 63 | + } |
| 64 | + } |
| 65 | + std::cerr << std::endl; |
| 66 | +} |
| 67 | + |
| 68 | +void RunCalculation(sycl::queue Q) { |
| 69 | + sycl::range<1> Range(buffer_size); |
| 70 | + auto Dev = Q.get_device(); |
| 71 | + if (!Dev.has(sycl::aspect::usm_device_allocations)) |
| 72 | + return; |
| 73 | + |
| 74 | + int *Dvalues = |
| 75 | + sycl::malloc<int>(buffer_size, Dev, Q.get_context(), AllocType); |
| 76 | + int *DvaluesTmp = |
| 77 | + sycl::malloc<int>(buffer_size, Dev, Q.get_context(), AllocType); |
| 78 | + |
| 79 | + std::vector<int> Hvalues1(buffer_size, 0); |
| 80 | + std::vector<int> HvaluesTmp(buffer_size, 0); |
| 81 | + std::iota(Hvalues1.begin(), Hvalues1.end(), 0); |
| 82 | + |
| 83 | + try { |
| 84 | + Q.memcpy(Dvalues, Hvalues1.data(), buffer_size * sizeof(int)); |
| 85 | + |
| 86 | + Q.submit([&](sycl::handler &cgh) { |
| 87 | + cgh.parallel_for(Range, [=](sycl::item<1> itemID) { |
| 88 | + size_t i = itemID.get_id(0); |
| 89 | + if (Dvalues[i] == i) |
| 90 | + Dvalues[i] += 1; |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + Q.submit([&](sycl::handler &cgh) { |
| 95 | + cgh.parallel_for(Range, [=](sycl::item<1> itemID) { |
| 96 | + size_t i = itemID.get_id(0); |
| 97 | + if (Dvalues[i] == i + 1) |
| 98 | + Dvalues[i] += 10; |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + Q.memcpy(Hvalues1.data(), Dvalues, buffer_size * sizeof(int)); |
| 103 | + Q.memcpy(DvaluesTmp, Hvalues1.data(), buffer_size * sizeof(int)); |
| 104 | + |
| 105 | + Q.submit([&](sycl::handler &cgh) { |
| 106 | + cgh.parallel_for(Range, [=](sycl::item<1> itemID) { |
| 107 | + size_t i = itemID.get_id(0); |
| 108 | + if (Dvalues[i] == i + 11) |
| 109 | + if (DvaluesTmp[i] == i + 11) |
| 110 | + Dvalues[i] += 100; |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + Q.submit([&](sycl::handler &cgh) { |
| 115 | + cgh.parallel_for(Range, [=](sycl::item<1> itemID) { |
| 116 | + size_t i = itemID.get_id(0); |
| 117 | + if (Dvalues[i] == i + 111) |
| 118 | + Dvalues[i] += 1000; |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + Q.submit([&](sycl::handler &cgh) { |
| 123 | + cgh.parallel_for(Range, [=](sycl::item<1> itemID) { |
| 124 | + size_t i = itemID.get_id(0); |
| 125 | + if (Dvalues[i] == i + 1111) |
| 126 | + Dvalues[i] += 10000; |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + Q.memcpy(Hvalues1.data(), Dvalues, buffer_size * sizeof(int)); |
| 131 | + Q.memcpy(HvaluesTmp.data(), DvaluesTmp, buffer_size * sizeof(int)); |
| 132 | + Q.wait(); |
| 133 | + |
| 134 | + ValidationPrint("vector1[] = ", Hvalues1, |
| 135 | + [&](size_t i) { return i + 11111; }); |
| 136 | + ValidationPrint("vector2[] = ", HvaluesTmp, |
| 137 | + [&](size_t i) { return i + 11; }); |
| 138 | + |
| 139 | + for (int i = 0; i < buffer_size; ++i) { |
| 140 | + int expected = i + 11111; |
| 141 | + assert(Hvalues1[i] == expected); |
| 142 | + } |
| 143 | + |
| 144 | + } catch (sycl::exception &e) { |
| 145 | + std::cout << "Exception: " << std::string(e.what()) << std::endl; |
| 146 | + } |
| 147 | + |
| 148 | + free(Dvalues, Q); |
| 149 | + free(DvaluesTmp, Q); |
| 150 | +} |
| 151 | + |
| 152 | +int main(int argc, char *argv[]) { |
| 153 | + sycl::queue Q({sycl::property::queue::in_order{}}); |
| 154 | + |
| 155 | + RunCalculation(Q); |
| 156 | + |
| 157 | + std::cout << "The test passed." << std::endl; |
| 158 | + return 0; |
| 159 | +} |
0 commit comments