|
3 | 3 | // RUN: %HOST_RUN_PLACEHOLDER %t.out
|
4 | 4 | // RUN: %GPU_RUN_PLACEHOLDER %t.out
|
5 | 5 | // RUN: %CPU_RUN_PLACEHOLDER %t.out
|
| 6 | +// RUN: %ACC_RUN_PLACEHOLDER %t.out |
6 | 7 |
|
7 |
| -#include <CL/sycl.hpp> |
8 |
| -#include <algorithm> |
9 |
| -#include <cassert> |
| 8 | +#include "add.h" |
10 | 9 | #include <iostream>
|
11 |
| -#include <numeric> |
12 |
| -#include <vector> |
13 | 10 | using namespace sycl;
|
14 |
| -using namespace sycl::ONEAPI; |
15 |
| - |
16 |
| -template <typename T, typename Difference = T> |
17 |
| -void add_fetch_test(queue q, size_t N) { |
18 |
| - T sum = 0; |
19 |
| - std::vector<T> output(N); |
20 |
| - std::fill(output.begin(), output.end(), T(0)); |
21 |
| - { |
22 |
| - buffer<T> sum_buf(&sum, 1); |
23 |
| - buffer<T> output_buf(output.data(), output.size()); |
24 |
| - |
25 |
| - q.submit([&](handler &cgh) { |
26 |
| - auto sum = sum_buf.template get_access<access::mode::read_write>(cgh); |
27 |
| - auto out = |
28 |
| - output_buf.template get_access<access::mode::discard_write>(cgh); |
29 |
| - cgh.parallel_for(range<1>(N), [=](item<1> it) { |
30 |
| - int gid = it.get_id(0); |
31 |
| - auto atm = atomic_ref<T, memory_order::relaxed, memory_scope::device, |
32 |
| - access::address_space::global_space>(sum[0]); |
33 |
| - out[gid] = atm.fetch_add(Difference(1)); |
34 |
| - }); |
35 |
| - }); |
36 |
| - } |
37 |
| - |
38 |
| - // All work-items increment by 1, so final value should be equal to N |
39 |
| - assert(sum == T(N)); |
40 |
| - |
41 |
| - // Fetch returns original value: will be in [0, N-1] |
42 |
| - auto min_e = std::min_element(output.begin(), output.end()); |
43 |
| - auto max_e = std::max_element(output.begin(), output.end()); |
44 |
| - assert(*min_e == T(0) && *max_e == T(N - 1)); |
45 |
| - |
46 |
| - // Intermediate values should be unique |
47 |
| - std::sort(output.begin(), output.end()); |
48 |
| - assert(std::unique(output.begin(), output.end()) == output.end()); |
49 |
| -} |
50 |
| - |
51 |
| -template <typename T, typename Difference = T> |
52 |
| -void add_plus_equal_test(queue q, size_t N) { |
53 |
| - T sum = 0; |
54 |
| - std::vector<T> output(N); |
55 |
| - std::fill(output.begin(), output.end(), T(0)); |
56 |
| - { |
57 |
| - buffer<T> sum_buf(&sum, 1); |
58 |
| - buffer<T> output_buf(output.data(), output.size()); |
59 |
| - |
60 |
| - q.submit([&](handler &cgh) { |
61 |
| - auto sum = sum_buf.template get_access<access::mode::read_write>(cgh); |
62 |
| - auto out = |
63 |
| - output_buf.template get_access<access::mode::discard_write>(cgh); |
64 |
| - cgh.parallel_for(range<1>(N), [=](item<1> it) { |
65 |
| - int gid = it.get_id(0); |
66 |
| - auto atm = atomic_ref<T, memory_order::relaxed, memory_scope::device, |
67 |
| - access::address_space::global_space>(sum[0]); |
68 |
| - out[gid] = atm += Difference(1); |
69 |
| - }); |
70 |
| - }); |
71 |
| - } |
72 |
| - |
73 |
| - // All work-items increment by 1, so final value should be equal to N |
74 |
| - assert(sum == T(N)); |
75 |
| - |
76 |
| - // += returns updated value: will be in [1, N] |
77 |
| - auto min_e = std::min_element(output.begin(), output.end()); |
78 |
| - auto max_e = std::max_element(output.begin(), output.end()); |
79 |
| - assert(*min_e == T(1) && *max_e == T(N)); |
80 |
| - |
81 |
| - // Intermediate values should be unique |
82 |
| - std::sort(output.begin(), output.end()); |
83 |
| - assert(std::unique(output.begin(), output.end()) == output.end()); |
84 |
| -} |
85 |
| - |
86 |
| -template <typename T, typename Difference = T> |
87 |
| -void add_pre_inc_test(queue q, size_t N) { |
88 |
| - T sum = 0; |
89 |
| - std::vector<T> output(N); |
90 |
| - std::fill(output.begin(), output.end(), T(0)); |
91 |
| - { |
92 |
| - buffer<T> sum_buf(&sum, 1); |
93 |
| - buffer<T> output_buf(output.data(), output.size()); |
94 |
| - |
95 |
| - q.submit([&](handler &cgh) { |
96 |
| - auto sum = sum_buf.template get_access<access::mode::read_write>(cgh); |
97 |
| - auto out = |
98 |
| - output_buf.template get_access<access::mode::discard_write>(cgh); |
99 |
| - cgh.parallel_for(range<1>(N), [=](item<1> it) { |
100 |
| - int gid = it.get_id(0); |
101 |
| - auto atm = atomic_ref<T, memory_order::relaxed, memory_scope::device, |
102 |
| - access::address_space::global_space>(sum[0]); |
103 |
| - out[gid] = ++atm; |
104 |
| - }); |
105 |
| - }); |
106 |
| - } |
107 |
| - |
108 |
| - // All work-items increment by 1, so final value should be equal to N |
109 |
| - assert(sum == T(N)); |
110 |
| - |
111 |
| - // Pre-increment returns updated value: will be in [1, N] |
112 |
| - auto min_e = std::min_element(output.begin(), output.end()); |
113 |
| - auto max_e = std::max_element(output.begin(), output.end()); |
114 |
| - assert(*min_e == T(1) && *max_e == T(N)); |
115 |
| - |
116 |
| - // Intermediate values should be unique |
117 |
| - std::sort(output.begin(), output.end()); |
118 |
| - assert(std::unique(output.begin(), output.end()) == output.end()); |
119 |
| -} |
120 |
| - |
121 |
| -template <typename T, typename Difference = T> |
122 |
| -void add_post_inc_test(queue q, size_t N) { |
123 |
| - T sum = 0; |
124 |
| - std::vector<T> output(N); |
125 |
| - std::fill(output.begin(), output.end(), T(0)); |
126 |
| - { |
127 |
| - buffer<T> sum_buf(&sum, 1); |
128 |
| - buffer<T> output_buf(output.data(), output.size()); |
129 |
| - |
130 |
| - q.submit([&](handler &cgh) { |
131 |
| - auto sum = sum_buf.template get_access<access::mode::read_write>(cgh); |
132 |
| - auto out = |
133 |
| - output_buf.template get_access<access::mode::discard_write>(cgh); |
134 |
| - cgh.parallel_for(range<1>(N), [=](item<1> it) { |
135 |
| - int gid = it.get_id(0); |
136 |
| - auto atm = atomic_ref<T, memory_order::relaxed, memory_scope::device, |
137 |
| - access::address_space::global_space>(sum[0]); |
138 |
| - out[gid] = atm++; |
139 |
| - }); |
140 |
| - }); |
141 |
| - } |
142 |
| - |
143 |
| - // All work-items increment by 1, so final value should be equal to N |
144 |
| - assert(sum == T(N)); |
145 |
| - |
146 |
| - // Post-increment returns original value: will be in [0, N-1] |
147 |
| - auto min_e = std::min_element(output.begin(), output.end()); |
148 |
| - auto max_e = std::max_element(output.begin(), output.end()); |
149 |
| - assert(*min_e == T(0) && *max_e == T(N - 1)); |
150 |
| - |
151 |
| - // Intermediate values should be unique |
152 |
| - std::sort(output.begin(), output.end()); |
153 |
| - assert(std::unique(output.begin(), output.end()) == output.end()); |
154 |
| -} |
155 |
| - |
156 |
| -template <typename T, typename Difference = T> |
157 |
| -void add_test(queue q, size_t N) { |
158 |
| - add_fetch_test<T, Difference>(q, N); |
159 |
| - add_plus_equal_test<T, Difference>(q, N); |
160 |
| - add_pre_inc_test<T, Difference>(q, N); |
161 |
| - add_post_inc_test<T, Difference>(q, N); |
162 |
| -} |
163 | 11 |
|
164 | 12 | // Floating-point types do not support pre- or post-increment
|
165 | 13 | template <> void add_test<float>(queue q, size_t N) {
|
166 | 14 | add_fetch_test<float>(q, N);
|
167 | 15 | add_plus_equal_test<float>(q, N);
|
168 | 16 | }
|
169 |
| -template <> void add_test<double>(queue q, size_t N) { |
170 |
| - add_fetch_test<double>(q, N); |
171 |
| - add_plus_equal_test<double>(q, N); |
172 |
| -} |
173 | 17 |
|
174 | 18 | int main() {
|
175 | 19 | queue q;
|
176 |
| - std::string version = q.get_device().get_info<info::device::version>(); |
177 | 20 |
|
178 | 21 | constexpr int N = 32;
|
179 | 22 | add_test<int>(q, N);
|
180 | 23 | add_test<unsigned int>(q, N);
|
181 |
| - add_test<long>(q, N); |
182 |
| - add_test<unsigned long>(q, N); |
183 |
| - add_test<long long>(q, N); |
184 |
| - add_test<unsigned long long>(q, N); |
185 | 24 | add_test<float>(q, N);
|
186 |
| - add_test<double>(q, N); |
187 |
| - add_test<char *, ptrdiff_t>(q, N); |
| 25 | + |
| 26 | + // Include long tests if they are 32 bits wide |
| 27 | + if constexpr (sizeof(long) == 4) { |
| 28 | + add_test<long>(q, N); |
| 29 | + add_test<unsigned long>(q, N); |
| 30 | + } |
| 31 | + |
| 32 | + // Include pointer tests if they are 32 bits wide |
| 33 | + if constexpr (sizeof(char *) == 4) { |
| 34 | + add_test<char *, ptrdiff_t>(q, N); |
| 35 | + } |
188 | 36 |
|
189 | 37 | std::cout << "Test passed." << std::endl;
|
190 | 38 | }
|
0 commit comments