|
| 1 | +// RUN: %{build} -o %t.out |
| 2 | +// RUN: %{run} %t.out |
| 3 | +// |
| 4 | +// REQUIRES: aspect-usm_shared_allocations |
| 5 | +#include <numeric> |
| 6 | + |
| 7 | +#include <sycl/detail/core.hpp> |
| 8 | + |
| 9 | +#include <sycl/sycl_span.hpp> |
| 10 | +#include <sycl/usm.hpp> |
| 11 | +#include <sycl/usm/usm_allocator.hpp> |
| 12 | + |
| 13 | +using namespace sycl; |
| 14 | + |
| 15 | +void testSpanCapture() { |
| 16 | + // This test creates spans that are backed by USM. |
| 17 | + // ensures they can be captured by device lambda |
| 18 | + // and that read and write operations function correctly |
| 19 | + // across capture. |
| 20 | + queue Q; |
| 21 | + |
| 22 | + constexpr long numReadTests = 2; |
| 23 | + const range<1> NumberOfReadTestsRange(numReadTests); |
| 24 | + buffer<int, 1> SpanRead(NumberOfReadTestsRange); |
| 25 | + |
| 26 | + // span from a vector |
| 27 | + // We will create a vector, backed by a USM allocator. And a span from that. |
| 28 | + using vec_alloc = usm_allocator<int, usm::alloc::shared>; |
| 29 | + // Create allocator for device associated with q |
| 30 | + vec_alloc myAlloc(Q); |
| 31 | + // Create std vector with the allocator |
| 32 | + std::vector<int, vec_alloc> vecUSM(4, myAlloc); |
| 33 | + std::iota(vecUSM.begin(), vecUSM.end(), 1); |
| 34 | + sycl::span<int> vecUSM_span{vecUSM}; |
| 35 | + vecUSM_span[0] += 100; // 101 modify first value using span affordance. |
| 36 | + |
| 37 | + // span from USM memory |
| 38 | + auto *usm_data = malloc_shared<int>(4, Q); |
| 39 | + sycl::span<int> usm_span(usm_data, 4); |
| 40 | + std::iota(usm_span.begin(), usm_span.end(), 1); |
| 41 | + usm_span[0] += 100; // 101 modify first value using span affordance. |
| 42 | + |
| 43 | + event E = Q.submit([&](handler &cgh) { |
| 44 | + auto can_read_from_span_acc = SpanRead.get_access<access::mode::write>(cgh); |
| 45 | + cgh.single_task<class hi>([=] { |
| 46 | + // read from the spans. |
| 47 | + can_read_from_span_acc[0] = vecUSM_span[0]; |
| 48 | + can_read_from_span_acc[1] = usm_span[0]; |
| 49 | + |
| 50 | + // write to the spans |
| 51 | + vecUSM_span[1] += 1000; |
| 52 | + usm_span[1] += 1000; |
| 53 | + }); |
| 54 | + }); |
| 55 | + E.wait(); |
| 56 | + |
| 57 | + // check out the read operations, should have gotten 101 from each |
| 58 | + host_accessor can_read_from_span_acc(SpanRead, read_only); |
| 59 | + for (int i = 0; i < numReadTests; i++) { |
| 60 | + assert(can_read_from_span_acc[i] == 101 && |
| 61 | + "read check should have gotten 100"); |
| 62 | + } |
| 63 | + |
| 64 | + // were the spans successfully modified via write? |
| 65 | + assert(vecUSM_span[1] == 1002 && |
| 66 | + "vecUSM_span write check should have gotten 1001"); |
| 67 | + assert(usm_span[1] == 1002 && "usm_span write check should have gotten 1001"); |
| 68 | + |
| 69 | + free(usm_data, Q); |
| 70 | +} |
| 71 | + |
| 72 | +void set_all_span_values(sycl::span<int> container, int v) { |
| 73 | + for (auto &e : container) |
| 74 | + e = v; |
| 75 | +} |
| 76 | + |
| 77 | +void testSpanOnDevice() { |
| 78 | + // this test creates a simple span on device, |
| 79 | + // passes it to a function that operates on it |
| 80 | + // and ensures it worked correctly |
| 81 | + queue Q; |
| 82 | + constexpr long numReadTests = 4; |
| 83 | + const range<1> NumberOfReadTestsRange(numReadTests); |
| 84 | + buffer<int, 1> SpanRead(NumberOfReadTestsRange); |
| 85 | + |
| 86 | + event E = Q.submit([&](handler &cgh) { |
| 87 | + auto can_read_from_span_acc = SpanRead.get_access<access::mode::write>(cgh); |
| 88 | + cgh.single_task<class ha>([=] { |
| 89 | + // create a span on device, pass it to function that modifies it |
| 90 | + // read values back out. |
| 91 | + int a[]{1, 2, 3, 4}; |
| 92 | + sycl::span<int> a_span{a}; |
| 93 | + set_all_span_values(a_span, 10); |
| 94 | + for (int i = 0; i < numReadTests; i++) |
| 95 | + can_read_from_span_acc[i] = a_span[i]; |
| 96 | + }); |
| 97 | + }); |
| 98 | + E.wait(); |
| 99 | + |
| 100 | + // check out the read operations, should have gotten 10 from each |
| 101 | + host_accessor can_read_from_span_acc(SpanRead, read_only); |
| 102 | + for (int i = 0; i < numReadTests; i++) { |
| 103 | + assert(can_read_from_span_acc[i] == 10 && |
| 104 | + "read check should have gotten 10"); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +int main() { |
| 109 | + testSpanCapture(); |
| 110 | + testSpanOnDevice(); |
| 111 | + |
| 112 | + return 0; |
| 113 | +} |
0 commit comments