|
16 | 16 |
|
17 | 17 | using namespace cl::sycl;
|
18 | 18 |
|
19 |
| -constexpr int count = 100; |
20 |
| -constexpr int pattern = 42; |
| 19 | +template <typename T> class usm_device_transfer; |
| 20 | +template <typename T> class usm_aligned_device_transfer; |
| 21 | + |
| 22 | +static constexpr int N = 100; |
| 23 | + |
| 24 | +struct test_struct { |
| 25 | + short a; |
| 26 | + int b; |
| 27 | + long c; |
| 28 | + long long d; |
| 29 | + sycl::half e; |
| 30 | + float f; |
| 31 | + double g; |
| 32 | +}; |
| 33 | + |
| 34 | +bool operator==(const test_struct &lhs, const test_struct &rhs) { |
| 35 | + return lhs.a == rhs.a && lhs.b == rhs.b && lhs.c == rhs.c && lhs.d == rhs.d && |
| 36 | + lhs.e == rhs.e && lhs.f == rhs.f && lhs.g == rhs.g; |
| 37 | +} |
| 38 | + |
| 39 | +template <typename T> |
| 40 | +void runHostTests(device dev, context ctxt, queue q, T val) { |
| 41 | + T *array; |
| 42 | + |
| 43 | + array = (T *)malloc_host(N * sizeof(T), q); |
| 44 | + q.submit([&](handler &h) { h.fill(array, val, N); }).wait(); |
| 45 | + for (int i = 0; i < N; ++i) { |
| 46 | + assert(array[i] == val); |
| 47 | + } |
| 48 | + free(array, ctxt); |
| 49 | + |
| 50 | + array = (T *)aligned_alloc_host(alignof(long long), N * sizeof(T), ctxt); |
| 51 | + q.submit([&](handler &h) { h.fill(array, val, N); }).wait(); |
| 52 | + for (int i = 0; i < N; ++i) { |
| 53 | + assert(array[i] == val); |
| 54 | + } |
| 55 | + free(array, ctxt); |
| 56 | +} |
| 57 | + |
| 58 | +template <typename T> |
| 59 | +void runSharedTests(device dev, context ctxt, queue q, T val) { |
| 60 | + T *array; |
| 61 | + |
| 62 | + array = (T *)malloc_shared(N * sizeof(T), q); |
| 63 | + q.submit([&](handler &h) { h.fill(array, val, N); }).wait(); |
| 64 | + for (int i = 0; i < N; ++i) { |
| 65 | + assert(array[i] == val); |
| 66 | + } |
| 67 | + free(array, ctxt); |
| 68 | + |
| 69 | + array = |
| 70 | + (T *)aligned_alloc_shared(alignof(long long), N * sizeof(T), dev, ctxt); |
| 71 | + q.submit([&](handler &h) { h.fill(array, val, N); }).wait(); |
| 72 | + for (int i = 0; i < N; ++i) { |
| 73 | + assert(array[i] == val); |
| 74 | + } |
| 75 | + free(array, ctxt); |
| 76 | +} |
| 77 | + |
| 78 | +template <typename T> |
| 79 | +void runDeviceTests(device dev, context ctxt, queue q, T val) { |
| 80 | + T *array; |
| 81 | + std::vector<T> out; |
| 82 | + out.resize(N); |
| 83 | + |
| 84 | + array = (T *)malloc_device(N * sizeof(T), q); |
| 85 | + q.submit([&](handler &h) { h.fill(array, val, N); }).wait(); |
| 86 | + |
| 87 | + { |
| 88 | + buffer<T, 1> buf{&out[0], range<1>{N}}; |
| 89 | + q.submit([&](handler &h) { |
| 90 | + auto acc = buf.template get_access<access::mode::write>(h); |
| 91 | + h.parallel_for<usm_device_transfer<T>>( |
| 92 | + range<1>(N), [=](id<1> item) { acc[item] = array[item]; }); |
| 93 | + }).wait(); |
| 94 | + } |
| 95 | + |
| 96 | + for (int i = 0; i < N; ++i) { |
| 97 | + assert(out[i] == val); |
| 98 | + } |
| 99 | + free(array, ctxt); |
| 100 | + |
| 101 | + out.clear(); |
| 102 | + out.resize(N); |
| 103 | + |
| 104 | + array = |
| 105 | + (T *)aligned_alloc_device(alignof(long long), N * sizeof(T), dev, ctxt); |
| 106 | + q.submit([&](handler &h) { h.fill(array, val, N); }).wait(); |
| 107 | + |
| 108 | + { |
| 109 | + buffer<T, 1> buf{&out[0], range<1>{N}}; |
| 110 | + q.submit([&](handler &h) { |
| 111 | + auto acc = buf.template get_access<access::mode::write>(h); |
| 112 | + h.parallel_for<usm_aligned_device_transfer<T>>( |
| 113 | + range<1>(N), [=](id<1> item) { acc[item] = array[item]; }); |
| 114 | + }).wait(); |
| 115 | + } |
| 116 | + |
| 117 | + for (int i = 0; i < N; ++i) { |
| 118 | + assert(out[i] == val); |
| 119 | + } |
| 120 | + free(array, ctxt); |
| 121 | +} |
21 | 122 |
|
22 | 123 | int main() {
|
23 | 124 | queue q;
|
24 |
| - if (q.get_device().get_info<info::device::usm_shared_allocations>()) { |
25 |
| - int *mem = malloc_shared<int>(count, q); |
| 125 | + auto dev = q.get_device(); |
| 126 | + auto ctxt = q.get_context(); |
26 | 127 |
|
27 |
| - for (int i = 0; i < count; i++) |
28 |
| - mem[i] = 0; |
| 128 | + test_struct test_obj{4, 42, 424, 4242, 4.2f, 4.242, 4.24242}; |
29 | 129 |
|
30 |
| - q.fill(mem, pattern, count); |
31 |
| - q.wait(); |
| 130 | + if (dev.get_info<info::device::usm_host_allocations>()) { |
| 131 | + runHostTests<short>(dev, ctxt, q, 4); |
| 132 | + runHostTests<int>(dev, ctxt, q, 42); |
| 133 | + runHostTests<long>(dev, ctxt, q, 424); |
| 134 | + runHostTests<long long>(dev, ctxt, q, 4242); |
| 135 | + runHostTests<sycl::half>(dev, ctxt, q, sycl::half(4.2f)); |
| 136 | + runHostTests<float>(dev, ctxt, q, 4.242f); |
| 137 | + runHostTests<double>(dev, ctxt, q, 4.24242); |
| 138 | + runHostTests<test_struct>(dev, ctxt, q, test_obj); |
| 139 | + } |
| 140 | + |
| 141 | + if (dev.get_info<info::device::usm_shared_allocations>()) { |
| 142 | + runSharedTests<short>(dev, ctxt, q, 4); |
| 143 | + runSharedTests<int>(dev, ctxt, q, 42); |
| 144 | + runSharedTests<long>(dev, ctxt, q, 424); |
| 145 | + runSharedTests<long long>(dev, ctxt, q, 4242); |
| 146 | + runSharedTests<sycl::half>(dev, ctxt, q, sycl::half(4.2f)); |
| 147 | + runSharedTests<float>(dev, ctxt, q, 4.242f); |
| 148 | + runSharedTests<double>(dev, ctxt, q, 4.24242); |
| 149 | + runSharedTests<test_struct>(dev, ctxt, q, test_obj); |
| 150 | + } |
32 | 151 |
|
33 |
| - for (int i = 0; i < count; i++) { |
34 |
| - assert(mem[i] == pattern); |
35 |
| - } |
| 152 | + if (dev.get_info<info::device::usm_device_allocations>()) { |
| 153 | + runDeviceTests<short>(dev, ctxt, q, 4); |
| 154 | + runDeviceTests<int>(dev, ctxt, q, 42); |
| 155 | + runDeviceTests<long>(dev, ctxt, q, 420); |
| 156 | + runDeviceTests<long long>(dev, ctxt, q, 4242); |
| 157 | + runDeviceTests<sycl::half>(dev, ctxt, q, sycl::half(4.2f)); |
| 158 | + runDeviceTests<float>(dev, ctxt, q, 4.242f); |
| 159 | + runDeviceTests<double>(dev, ctxt, q, 4.24242); |
| 160 | + runDeviceTests<test_struct>(dev, ctxt, q, test_obj); |
36 | 161 | }
|
37 |
| - std::cout << "Passed\n"; |
| 162 | + |
38 | 163 | return 0;
|
39 | 164 | }
|
0 commit comments