|
11 | 11 | //
|
12 | 12 | //===----------------------------------------------------------------------===//
|
13 | 13 |
|
14 |
| -#include <cassert> |
15 |
| -#include <iostream> |
16 |
| -#include <sycl/sycl.hpp> |
17 |
| -#include <type_traits> |
18 |
| - |
19 |
| -using namespace sycl; |
20 |
| - |
21 |
| -/* This is the class used to name the kernel for the runtime. |
22 |
| - * This must be done when the kernel is expressed as a lambda. */ |
23 |
| -template <typename T, access::decorated IsDecorated> class testMultPtrKernel; |
24 |
| -template <typename T, access::decorated IsDecorated> |
25 |
| -class testMultPtrArrowOperatorKernel; |
26 |
| - |
27 |
| -template <typename T> struct point { |
28 |
| - point(const point &rhs) = default; |
29 |
| - point(T x, T y) : x(x), y(y) {} |
30 |
| - point(T v) : x(v), y(v) {} |
31 |
| - point() : x(0), y(0) {} |
32 |
| - bool operator==(const T &rhs) { return rhs == x && rhs == y; } |
33 |
| - bool operator==(const point<T> &rhs) { return rhs.x == x && rhs.y == y; } |
34 |
| - T x; |
35 |
| - T y; |
36 |
| -}; |
37 |
| - |
38 |
| -template <typename T, access::decorated IsDecorated> |
39 |
| -void innerFunc(id<1> wiID, global_ptr<T, IsDecorated> ptr_1, |
40 |
| - global_ptr<T, IsDecorated> ptr_2, |
41 |
| - global_ptr<T, IsDecorated> ptr_3, |
42 |
| - global_ptr<T, IsDecorated> ptr_4, |
43 |
| - global_ptr<T, IsDecorated> ptr_5, |
44 |
| - local_ptr<T, IsDecorated> local_ptr, |
45 |
| - private_ptr<T, IsDecorated> priv_ptr) { |
46 |
| - T t = ptr_1[wiID.get(0)]; |
47 |
| - |
48 |
| - // Write to ptr_2 using local_ptr subscript. |
49 |
| - local_ptr[wiID.get(0)] = t; |
50 |
| - ptr_2[wiID.get(0)] = local_ptr[wiID.get(0)]; |
51 |
| - |
52 |
| - // Reset local ptr |
53 |
| - local_ptr[wiID.get(0)] = 0; |
54 |
| - |
55 |
| - // Write to ptr_3 using dereferencing. |
56 |
| - *(local_ptr + wiID.get(0)) = t; |
57 |
| - *(ptr_3 + wiID.get(0)) = *(local_ptr + wiID.get(0)); |
58 |
| - |
59 |
| - // Write to ptr_2 using priv_ptr subscript. |
60 |
| - priv_ptr[wiID.get(0)] = t; |
61 |
| - ptr_4[wiID.get(0)] = priv_ptr[wiID.get(0)]; |
62 |
| - |
63 |
| - // Reset local ptr |
64 |
| - priv_ptr[wiID.get(0)] = 0; |
65 |
| - |
66 |
| - // Write to ptr_3 using dereferencing. |
67 |
| - *(priv_ptr + wiID.get(0)) = t; |
68 |
| - *(ptr_5 + wiID.get(0)) = *(priv_ptr + wiID.get(0)); |
69 |
| -} |
70 |
| - |
71 |
| -template <typename T, access::decorated IsDecorated> void testMultPtr() { |
72 |
| - T data_1[10]; |
73 |
| - T data_2[10]; |
74 |
| - T data_3[10]; |
75 |
| - T data_4[10]; |
76 |
| - T data_5[10]; |
77 |
| - for (size_t i = 0; i < 10; ++i) { |
78 |
| - data_1[i] = 1; |
79 |
| - data_2[i] = 2; |
80 |
| - data_3[i] = 3; |
81 |
| - data_4[i] = 4; |
82 |
| - data_5[i] = 5; |
83 |
| - } |
84 |
| - |
85 |
| - { |
86 |
| - range<1> numOfItems{10}; |
87 |
| - buffer<T, 1> bufferData_1(data_1, numOfItems); |
88 |
| - buffer<T, 1> bufferData_2(data_2, numOfItems); |
89 |
| - buffer<T, 1> bufferData_3(data_3, numOfItems); |
90 |
| - buffer<T, 1> bufferData_4(data_4, numOfItems); |
91 |
| - buffer<T, 1> bufferData_5(data_5, numOfItems); |
92 |
| - queue myQueue; |
93 |
| - myQueue.submit([&](handler &cgh) { |
94 |
| - accessor<T, 1, access::mode::read, access::target::device, |
95 |
| - access::placeholder::false_t> |
96 |
| - accessorData_1(bufferData_1, cgh); |
97 |
| - accessor<T, 1, access::mode::read_write, access::target::device, |
98 |
| - access::placeholder::false_t> |
99 |
| - accessorData_2(bufferData_2, cgh); |
100 |
| - accessor<T, 1, access::mode::read_write, access::target::device, |
101 |
| - access::placeholder::false_t> |
102 |
| - accessorData_3(bufferData_3, cgh); |
103 |
| - accessor<T, 1, access::mode::read_write, access::target::device, |
104 |
| - access::placeholder::false_t> |
105 |
| - accessorData_4(bufferData_4, cgh); |
106 |
| - accessor<T, 1, access::mode::read_write, access::target::device, |
107 |
| - access::placeholder::false_t> |
108 |
| - accessorData_5(bufferData_5, cgh); |
109 |
| - local_accessor<T, 1> localAccessor(numOfItems, cgh); |
110 |
| - |
111 |
| - cgh.parallel_for<class testMultPtrKernel<T, IsDecorated>>( |
112 |
| - range<1>{10}, [=](id<1> wiID) { |
113 |
| - T private_data[10]; |
114 |
| - for (size_t i = 0; i < 10; ++i) |
115 |
| - private_data[i] = 0; |
116 |
| - localAccessor[wiID] = 0; |
117 |
| - |
118 |
| - auto ptr_1 = |
119 |
| - multi_ptr<T, access::address_space::global_space, IsDecorated>( |
120 |
| - accessorData_1); |
121 |
| - auto ptr_2 = |
122 |
| - multi_ptr<T, access::address_space::global_space, IsDecorated>( |
123 |
| - accessorData_2); |
124 |
| - auto ptr_3 = |
125 |
| - multi_ptr<T, access::address_space::global_space, IsDecorated>( |
126 |
| - accessorData_3); |
127 |
| - auto ptr_4 = |
128 |
| - multi_ptr<T, access::address_space::global_space, IsDecorated>( |
129 |
| - accessorData_4); |
130 |
| - auto ptr_5 = |
131 |
| - multi_ptr<T, access::address_space::global_space, IsDecorated>( |
132 |
| - accessorData_5); |
133 |
| - auto local_ptr = |
134 |
| - multi_ptr<T, access::address_space::local_space, IsDecorated>( |
135 |
| - localAccessor); |
136 |
| - auto priv_ptr = |
137 |
| - address_space_cast<access::address_space::private_space, |
138 |
| - IsDecorated>(private_data); |
139 |
| - static_assert( |
140 |
| - std::is_same_v<private_ptr<T, IsDecorated>, decltype(priv_ptr)>, |
141 |
| - "Incorrect type for priv_ptr."); |
142 |
| - |
143 |
| - // General conversions in multi_ptr class |
144 |
| - T *RawPtr = nullptr; |
145 |
| - global_ptr<T, IsDecorated> ptr_6 = |
146 |
| - address_space_cast<access::address_space::global_space, |
147 |
| - IsDecorated>(RawPtr); |
148 |
| - |
149 |
| - global_ptr<T, IsDecorated> ptr_7(accessorData_1); |
150 |
| - |
151 |
| - global_ptr<void, IsDecorated> ptr_8 = |
152 |
| - address_space_cast<access::address_space::global_space, |
153 |
| - IsDecorated>((void *)RawPtr); |
154 |
| - |
155 |
| - // Explicit conversions for device_ptr/host_ptr to global_ptr |
156 |
| - device_ptr<void, IsDecorated> ptr_9 = address_space_cast< |
157 |
| - access::address_space::ext_intel_global_device_space, |
158 |
| - IsDecorated>((void *)RawPtr); |
159 |
| - global_ptr<void, IsDecorated> ptr_10 = |
160 |
| - global_ptr<void, IsDecorated>(ptr_9); |
161 |
| - host_ptr<void, IsDecorated> ptr_11 = address_space_cast< |
162 |
| - access::address_space::ext_intel_global_host_space, |
163 |
| - IsDecorated>((void *)RawPtr); |
164 |
| - global_ptr<void, IsDecorated> ptr_12 = |
165 |
| - global_ptr<void, IsDecorated>(ptr_11); |
166 |
| - |
167 |
| - innerFunc<T, IsDecorated>(wiID.get(0), ptr_1, ptr_2, ptr_3, ptr_4, |
168 |
| - ptr_5, local_ptr, priv_ptr); |
169 |
| - }); |
170 |
| - }); |
171 |
| - } |
172 |
| - for (size_t i = 0; i < 10; ++i) { |
173 |
| - assert(data_1[i] == 1 && "Expected data_1[i] == 1"); |
174 |
| - assert(data_2[i] == 1 && "Expected data_2[i] == 1"); |
175 |
| - assert(data_3[i] == 1 && "Expected data_3[i] == 1"); |
176 |
| - assert(data_4[i] == 1 && "Expected data_4[i] == 1"); |
177 |
| - assert(data_5[i] == 1 && "Expected data_5[i] == 1"); |
178 |
| - } |
179 |
| -} |
180 |
| - |
181 |
| -template <typename T, access::decorated IsDecorated> |
182 |
| -void testMultPtrArrowOperator() { |
183 |
| - point<T> data_1[1] = {1}; |
184 |
| - point<T> data_2[1] = {2}; |
185 |
| - point<T> data_3[1] = {3}; |
186 |
| - |
187 |
| - { |
188 |
| - range<1> numOfItems{1}; |
189 |
| - buffer<point<T>, 1> bufferData_1(data_1, numOfItems); |
190 |
| - buffer<point<T>, 1> bufferData_2(data_2, numOfItems); |
191 |
| - buffer<point<T>, 1> bufferData_3(data_3, numOfItems); |
192 |
| - queue myQueue; |
193 |
| - myQueue.submit([&](handler &cgh) { |
194 |
| - accessor<point<T>, 1, access::mode::read, access::target::device, |
195 |
| - access::placeholder::false_t> |
196 |
| - accessorData_1(bufferData_1, cgh); |
197 |
| - local_accessor<point<T>, 1> accessorData_2(1, cgh); |
198 |
| - accessor<point<T>, 1, access::mode::read, access::target::device, |
199 |
| - access::placeholder::false_t> |
200 |
| - accessorData_3(bufferData_3, cgh); |
201 |
| - |
202 |
| - cgh.single_task<class testMultPtrArrowOperatorKernel<T, IsDecorated>>( |
203 |
| - [=]() { |
204 |
| - point<T> private_val = 0; |
205 |
| - ; |
206 |
| - |
207 |
| - auto ptr_1 = |
208 |
| - multi_ptr<point<T>, access::address_space::global_space, |
209 |
| - IsDecorated>(accessorData_1); |
210 |
| - auto ptr_2 = multi_ptr<point<T>, access::address_space::local_space, |
211 |
| - IsDecorated>(accessorData_2); |
212 |
| - auto ptr_3 = |
213 |
| - multi_ptr<point<T>, |
214 |
| - access::address_space::ext_intel_global_device_space, |
215 |
| - IsDecorated>(accessorData_3); |
216 |
| - auto ptr_4 = |
217 |
| - address_space_cast<access::address_space::private_space, |
218 |
| - IsDecorated>(&private_val); |
219 |
| - static_assert(std::is_same_v<private_ptr<point<T>, IsDecorated>, |
220 |
| - decltype(ptr_4)>, |
221 |
| - "Incorrect type for ptr_4."); |
222 |
| - |
223 |
| - auto x1 = ptr_1->x; |
224 |
| - auto x2 = ptr_2->x; |
225 |
| - auto x3 = ptr_3->x; |
226 |
| - auto x4 = ptr_4->x; |
227 |
| - |
228 |
| - static_assert(std::is_same<decltype(x1), T>::value, |
229 |
| - "Expected decltype(ptr_1->x) == T"); |
230 |
| - static_assert(std::is_same<decltype(x2), T>::value, |
231 |
| - "Expected decltype(ptr_2->x) == T"); |
232 |
| - static_assert(std::is_same<decltype(x3), T>::value, |
233 |
| - "Expected decltype(ptr_3->x) == T"); |
234 |
| - static_assert(std::is_same<decltype(x4), T>::value, |
235 |
| - "Expected decltype(ptr_4->x) == T"); |
236 |
| - }); |
237 |
| - }); |
238 |
| - } |
239 |
| -} |
240 |
| - |
241 |
| -template <access::decorated IsDecorated> void runTestsForDecoration() { |
242 |
| - testMultPtr<int, IsDecorated>(); |
243 |
| - testMultPtr<float, IsDecorated>(); |
244 |
| - testMultPtr<point<int>, IsDecorated>(); |
245 |
| - testMultPtr<point<float>, IsDecorated>(); |
246 |
| - |
247 |
| - testMultPtrArrowOperator<int, IsDecorated>(); |
248 |
| - testMultPtrArrowOperator<float, IsDecorated>(); |
249 |
| -} |
250 |
| - |
251 |
| -int main() { |
252 |
| - runTestsForDecoration<access::decorated::yes>(); |
253 |
| - runTestsForDecoration<access::decorated::no>(); |
254 |
| - return 0; |
255 |
| -} |
| 14 | +#include "multi_ptr.hpp" |
0 commit comments