|
15 | 15 |
|
16 | 16 | using namespace sycl; |
17 | 17 |
|
18 | | -static constexpr int count = 100; |
| 18 | +static constexpr int Count = 100; |
19 | 19 |
|
20 | 20 | int main() { |
21 | 21 | queue q([](exception_list el) { |
22 | 22 | for (auto &e : el) |
23 | 23 | throw e; |
24 | 24 | }); |
25 | | - if (q.get_device().get_info<info::device::usm_shared_allocations>()) { |
26 | | - float *src = (float *)malloc_shared(sizeof(float) * count, q.get_device(), |
| 25 | + |
| 26 | + if (!q.get_device().get_info<info::device::usm_shared_allocations>()) { |
| 27 | + // USM not supported, skipping test and returning early. |
| 28 | + return 0; |
| 29 | + } |
| 30 | + |
| 31 | + float *Src = (float *)malloc_shared(sizeof(float) * Count, q.get_device(), |
| 32 | + q.get_context()); |
| 33 | + float *Dest = (float *)malloc_shared(sizeof(float) * Count, q.get_device(), |
27 | 34 | q.get_context()); |
28 | | - float *dest = (float *)malloc_shared(sizeof(float) * count, q.get_device(), |
29 | | - q.get_context()); |
30 | | - for (int i = 0; i < count; i++) |
31 | | - src[i] = i; |
32 | | - |
33 | | - { |
34 | | - // Test host to device handler::ext_oneapi_prefetch_exp |
35 | | - event init_prefetch = |
36 | | - ext::oneapi::experimental::submit_with_event(q, [&](handler &cgh) { |
37 | | - ext::oneapi::experimental::prefetch(cgh, src, |
38 | | - sizeof(float) * count); |
39 | | - }); |
40 | | - |
41 | | - q.submit([&](handler &cgh) { |
42 | | - cgh.depends_on(init_prefetch); |
43 | | - cgh.single_task<class double_dest>([=]() { |
44 | | - for (int i = 0; i < count; i++) |
45 | | - dest[i] = 2 * src[i]; |
| 35 | + for (int i = 0; i < Count; i++) |
| 36 | + Src[i] = i; |
| 37 | + |
| 38 | + { |
| 39 | + // Test host-to-device prefetch via prefetch(handler ...). |
| 40 | + event InitPrefetch = |
| 41 | + ext::oneapi::experimental::submit_with_event(q, [&](handler &CGH) { |
| 42 | + ext::oneapi::experimental::prefetch(CGH, Src, |
| 43 | + sizeof(float) * Count); |
46 | 44 | }); |
| 45 | + |
| 46 | + q.submit([&](handler &CGH) { |
| 47 | + CGH.depends_on(init_prefetch); |
| 48 | + CGH.single_task<class double_dest>([=]() { |
| 49 | + for (int i = 0; i < Count; i++) |
| 50 | + Dest[i] = 2 * Src[i]; |
47 | 51 | }); |
48 | | - q.wait_and_throw(); |
| 52 | + }); |
| 53 | + q.wait_and_throw(); |
49 | 54 |
|
50 | | - for (int i = 0; i < count; i++) { |
51 | | - assert(dest[i] == i * 2); |
52 | | - } |
| 55 | + for (int i = 0; i < Count; i++) { |
| 56 | + assert(Dest[i] == i * 2); |
| 57 | + } |
53 | 58 |
|
54 | | - // Test device to host handler::ext_oneapi_prefetch_exp |
55 | | - q.submit([&](handler &cgh) { |
56 | | - cgh.single_task<class quadruple_dest>([=]() { |
57 | | - for (int i = 0; i < count; i++) |
58 | | - dest[i] = 4 * src[i]; |
59 | | - }); |
| 59 | + // Test device-to-host prefetch via prefetch(handler ...). |
| 60 | + q.submit([&](handler &CGH) { |
| 61 | + CGH.single_task<class quadruple_dest>([=]() { |
| 62 | + for (int i = 0; i < Count; i++) |
| 63 | + Dest[i] = 4 * Src[i]; |
60 | 64 | }); |
61 | | - event init_prefetch_back = |
62 | | - ext::oneapi::experimental::submit_with_event(q, [&](handler &cgh) { |
63 | | - ext::oneapi::experimental::prefetch( |
64 | | - cgh, src, sizeof(float) * count, |
65 | | - ext::oneapi::experimental::prefetch_type::host); |
66 | | - }); |
67 | | - q.wait_and_throw(); |
68 | | - |
69 | | - for (int i = 0; i < count; i++) { |
70 | | - assert(dest[i] == i * 4); |
71 | | - } |
| 65 | + }); |
| 66 | + event InitPrefetchBack = |
| 67 | + ext::oneapi::experimental::submit_with_event(q, [&](handler &CGH) { |
| 68 | + ext::oneapi::experimental::prefetch( |
| 69 | + CGH, Src, sizeof(float) * Count, |
| 70 | + ext::oneapi::experimental::prefetch_type::host); |
| 71 | + }); |
| 72 | + q.wait_and_throw(); |
| 73 | + |
| 74 | + for (int i = 0; i < Count; i++) { |
| 75 | + assert(Dest[i] == i * 4); |
72 | 76 | } |
| 77 | + } |
73 | 78 |
|
74 | | - // Test queue::prefetch |
75 | | - { |
76 | | - ext::oneapi::experimental::prefetch( |
77 | | - q, src, sizeof(float) * count, |
78 | | - ext::oneapi::experimental::prefetch_type::device); |
79 | | - q.wait_and_throw(); |
80 | | - |
81 | | - q.submit([&](handler &cgh) { |
82 | | - cgh.single_task<class triple_dest>([=]() { |
83 | | - for (int i = 0; i < count; i++) |
84 | | - dest[i] = 3 * src[i]; |
85 | | - }); |
| 79 | + { |
| 80 | + // Test host-to-device prefetch via prefetch(queue ...). |
| 81 | + ext::oneapi::experimental::prefetch( |
| 82 | + q, Src, sizeof(float) * Count, |
| 83 | + ext::oneapi::experimental::prefetch_type::device); |
| 84 | + q.wait_and_throw(); |
| 85 | + q.submit([&](handler &CGH) { |
| 86 | + CGH.single_task<class triple_dest>([=]() { |
| 87 | + for (int i = 0; i < Count; i++) |
| 88 | + Dest[i] = 3 * Src[i]; |
86 | 89 | }); |
87 | | - q.wait_and_throw(); |
| 90 | + }); |
| 91 | + q.wait_and_throw(); |
88 | 92 |
|
89 | | - for (int i = 0; i < count; i++) { |
90 | | - assert(dest[i] == i * 3); |
91 | | - } |
| 93 | + for (int i = 0; i < Count; i++) { |
| 94 | + assert(Dest[i] == i * 3); |
| 95 | + } |
92 | 96 |
|
93 | | - q.submit([&](handler &cgh) { |
94 | | - cgh.single_task<class sixtuple_dest>([=]() { |
95 | | - for (int i = 0; i < count; i++) |
96 | | - dest[i] = 6 * src[i]; |
97 | | - }); |
| 97 | + // Test device-to-host prefetch via prefetch(queue ...). |
| 98 | + q.submit([&](handler &CGH) { |
| 99 | + CGH.single_task<class sixtuple_dest>([=]() { |
| 100 | + for (int i = 0; i < Count; i++) |
| 101 | + Dest[i] = 6 * Src[i]; |
98 | 102 | }); |
99 | | - q.wait_and_throw(); |
100 | | - ext::oneapi::experimental::prefetch( |
101 | | - q, src, sizeof(float) * count, |
102 | | - ext::oneapi::experimental::prefetch_type::host); |
103 | | - q.wait_and_throw(); |
104 | | - |
105 | | - for (int i = 0; i < count; i++) { |
106 | | - assert(dest[i] == i * 6); |
107 | | - } |
| 103 | + }); |
| 104 | + q.wait_and_throw(); |
| 105 | + ext::oneapi::experimental::prefetch( |
| 106 | + q, Src, sizeof(float) * Count, |
| 107 | + ext::oneapi::experimental::prefetch_type::host); |
| 108 | + q.wait_and_throw(); |
| 109 | + |
| 110 | + for (int i = 0; i < Count; i++) { |
| 111 | + assert(Dest[i] == i * 6); |
108 | 112 | } |
109 | | - free(src, q); |
110 | | - free(dest, q); |
111 | 113 | } |
112 | | - return 0; |
| 114 | + free(Src, q); |
| 115 | + free(Dest, q); |
113 | 116 | } |
0 commit comments