-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathonedpl_test_for_each.cpp
More file actions
408 lines (339 loc) · 11.9 KB
/
onedpl_test_for_each.cpp
File metadata and controls
408 lines (339 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// ====------ onedpl_test_for_each.cpp---------- -*- C++ -* ----===////
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//
// ===----------------------------------------------------------------------===//
#include "oneapi/dpl/execution"
#include "oneapi/dpl/iterator"
#include "oneapi/dpl/algorithm"
#include "dpct/dpct.hpp"
#include "dpct/dpl_utils.hpp"
#include <sycl/sycl.hpp>
#include <iostream>
template<typename String, typename _T1, typename _T2>
int ASSERT_EQUAL(String msg, _T1&& X, _T2&& Y) {
if(X!=Y) {
std::cout << "FAIL: " << msg << " - (" << X << "," << Y << ")" << std::endl;
return 1;
}
return 0;
}
int test_passed(int failing_elems, std::string test_name) {
if (failing_elems == 0) {
std::cout << "PASS: " << test_name << std::endl;
return 0;
}
return 1;
}
template<typename Buffer> void fill_buffer(Buffer& src_buf, int start_index, int end_index, uint64_t value) {
auto src = src_buf.template get_access<sycl::access::mode::write>();
for (int i = start_index; i != end_index; ++i) {
src[i] = value;
}
}
template<typename Buffer> void iota_buffer(Buffer& dst_buf, int start_index, int end_index) {
auto dst = dst_buf.template get_access<sycl::access::mode::write>();
for (int i = start_index; i != end_index; ++i) {
dst[i] = i;
}
}
struct square {
void operator()(uint64_t& x) const {
x = x*x;
}
// function operator used in for_each test that uses zip_iterator
template<typename T>
void operator()(T&& t) const {
std::get<1>(t) = std::get<0>(t) * std::get<0>(t);
}
};
template<class To, class From, bool compress>
struct compressFunctor
{
To to;
const From from;
const int nCmpts;
compressFunctor
(
To _to,
const From _from,
int _nCmpts
):
to(_to),
from(_from),
nCmpts(_nCmpts)
{}
void operator()(const int& i) const
{
if(compress)
from[i] = (to[i] - i%nCmpts);
else
from[i] = (to[i] + i%nCmpts);
}
};
template <typename T>
struct absolute_value {
void operator()(T &x) const { x = (x >= 0 ? x : -x); }
};
int main() {
// used to detect failures
int failed_tests = 0;
int num_failing = 0;
std::string test_name = "";
// First Group: Testing regular call to std::for_each
{
// test 1/1
// create buffer
sycl::buffer<uint64_t> src_buf { sycl::range<1>(8) };
auto src_it = oneapi::dpl::begin(src_buf);
iota_buffer(src_buf, 0, 8);
// call algorithm
std::for_each(oneapi::dpl::execution::dpcpp_default, src_it, src_it + 4, square());
{
test_name = "Regular call to std::for_each";
auto src = src_it.get_buffer().template get_access<sycl::access::mode::read>();
for (int i = 0; i != 8; ++i) {
if (i < 4)
num_failing += ASSERT_EQUAL(test_name, src[i], i*i);
else
num_failing += ASSERT_EQUAL(test_name, src[i], i);
}
failed_tests += test_passed(num_failing, test_name);
num_failing = 0;
}
}
// Second Group: Testing calls to std::for_each using make_counting_iterator
{
// test 1/2
// create queue
sycl::queue myQueue;
auto dev = myQueue.get_device();
auto ctxt = myQueue.get_context();
// create arrays`
int *srcArray = (int*) malloc_shared(8 * sizeof(int), dev, ctxt);
int *dstArray = (int*) malloc_shared(8 * sizeof(int), dev, ctxt);
// fill srcArray and dstArray
for (int i = 0; i != 8; ++i) {
srcArray[i] = i;
dstArray[i] = 0;
}
// call algorithm
std::for_each
(
oneapi::dpl::execution::make_device_policy<class ForEach>(myQueue),
dpct::make_counting_iterator(0),
dpct::make_counting_iterator(4),
compressFunctor<int*, int*, true>
(
srcArray,
dstArray,
2
)
);
myQueue.wait();
{
test_name = "std::for_each using make_counting_it 1/2";
for (int i = 0; i != 8; ++i) {
if (i < 4)
num_failing += ASSERT_EQUAL(test_name, dstArray[i], srcArray[i] - i%2);
else
num_failing += ASSERT_EQUAL(test_name, dstArray[i], 0);
}
failed_tests += test_passed(num_failing, test_name);
num_failing = 0;
}
// test 2/2
// fill srcArray and dstArray
for (int i = 0; i != 8; ++i) {
srcArray[i] = i;
dstArray[i] = 0;
}
// call algorithm
std::for_each
(
oneapi::dpl::execution::make_device_policy<class ForEach2>(myQueue),
dpct::make_counting_iterator(2),
dpct::make_counting_iterator(6),
([=](int i) {
dstArray[i] = srcArray[i] - 5;
})
);
myQueue.wait();
{
test_name = "std::for_each using make_counting_it 2/2";
for (int i = 0; i != 8; ++i) {
if (i > 1 && i < 6)
num_failing += ASSERT_EQUAL(test_name, dstArray[i], srcArray[i] - 5);
else
num_failing += ASSERT_EQUAL(test_name, dstArray[i], 0);
}
failed_tests += test_passed(num_failing, test_name);
num_failing = 0;
}
}
// Third Group: Testing calls to std::for_each using make_permutation_iterator
{
// test 1/1
// create buffer
sycl::buffer<uint64_t> src_buf { sycl::range<1>(8) };
sycl::buffer<uint64_t> map_buf { sycl::range<1>(4) };
auto src_it = oneapi::dpl::begin(src_buf);
auto map_it = oneapi::dpl::begin(map_buf);
iota_buffer(src_buf, 0, 8);
{
auto map = map_it.get_buffer().template get_access<sycl::access::mode::write>();
map[0] = 4; map[1] = 3; map[2] = 6; map[3] = 5;
}
{
auto perm_begin = oneapi::dpl::make_permutation_iterator(src_it, map_it);
auto perm_end = perm_begin + 4;
// call algorithm
std::for_each
(
oneapi::dpl::execution::dpcpp_default,
perm_begin,
perm_end,
square()
);
}
{
test_name = "std::for_each using make_perm_it";
auto src = src_it.get_buffer().template get_access<sycl::access::mode::read>();
for (int i = 0; i != 8; ++i) {
if (i < 3 || i == 7)
num_failing += ASSERT_EQUAL(test_name, src[i], i);
else
num_failing += ASSERT_EQUAL(test_name, src[i], i*i);
}
failed_tests += test_passed(num_failing, test_name);
num_failing = 0;
}
}
// Fourth Group: Testing calls to std::for_each using make_zip_iterator
{
// test 1/2
// create buffer
sycl::buffer<uint64_t> src_buf { sycl::range<1>(8) };
auto src_it = oneapi::dpl::begin(src_buf);
auto src_end_it = oneapi::dpl::end(src_buf);
iota_buffer(src_buf, 0, 8);
// call algorithm
std::for_each
(
oneapi::dpl::execution::dpcpp_default,
oneapi::dpl::make_zip_iterator
(
dpct::make_counting_iterator(4),
src_it + 4
),
oneapi::dpl::make_zip_iterator
(
dpct::make_counting_iterator(8),
src_it + 8
),
square()
);
{
test_name = "std::for_each using make_zip_it 1/2";
auto src = src_it.get_buffer().template get_access<sycl::access::mode::read>();
for (int i = 0; i != 8; ++i) {
if (i < 4)
num_failing += ASSERT_EQUAL(test_name, src[i], i);
else
num_failing += ASSERT_EQUAL(test_name, src[i], i*i);
}
failed_tests += test_passed(num_failing, test_name);
num_failing = 0;
}
// test 2/2
// create buffer
sycl::buffer<uint64_t> map_buf { sycl::range<1>(8) };
auto map_it = oneapi::dpl::begin(map_buf);
auto map_end_it = oneapi::dpl::end(map_buf);
{
auto map = map_it.get_buffer().template get_access<sycl::access::mode::write>();
map[0] = 5; map[1] = 2; map[2] = 4; map[3] = 3;
}
{
// call algorithm
std::for_each
(
oneapi::dpl::execution::dpcpp_default,
oneapi::dpl::make_zip_iterator
(
dpct::make_counting_iterator(0),
oneapi::dpl::make_permutation_iterator
(
src_it,
map_it
)
),
oneapi::dpl::make_zip_iterator
(
dpct::make_counting_iterator(4),
oneapi::dpl::make_permutation_iterator
(
src_it,
map_end_it
)
),
square()
);
}
{
test_name = "std::for_each using make_zip_it 2/2";
auto src = src_it.get_buffer().template get_access<sycl::access::mode::read>();
// check that src is now { 0, 1, 1, 9, 4, 0, 36, 49 }
num_failing += ASSERT_EQUAL(test_name, src[0], 0);
num_failing += ASSERT_EQUAL(test_name, src[1], 1);
num_failing += ASSERT_EQUAL(test_name, src[2], 1);
num_failing += ASSERT_EQUAL(test_name, src[3], 9);
num_failing += ASSERT_EQUAL(test_name, src[4], 4);
num_failing += ASSERT_EQUAL(test_name, src[5], 0);
num_failing += ASSERT_EQUAL(test_name, src[6], 36);
num_failing += ASSERT_EQUAL(test_name, src[7], 49);
failed_tests += test_passed(num_failing, test_name);
num_failing = 0;
}
}
// Fifth Group: Testing calls to std::for_each using device_vector
{
// test 1/1
// create device_vector and src vector
std::vector<int> src(8);
src[0] = -3; src[1] = -2; src[2] = 1; src[3] = 0; src[4] = -1; src[5] = -4; src[6] = 2; src[7] = 3;
// src: { -3, -2, 1, 0, -1, -4, 2, 3 }
dpct::device_vector<int> dv(src);
{
// call algorithm on dv
std::for_each
(
oneapi::dpl::execution::dpcpp_default,
dv.begin(),
dv.begin() + 4,
absolute_value<int>()
);
}
// check that src is now { 3, 2, 1, 0, -1, -4, 2, 3 }
// actual result is no change in src elements
test_name = "std::for_each using device_vector";
num_failing += ASSERT_EQUAL(test_name, dv[0], 3);
num_failing += ASSERT_EQUAL(test_name, dv[1], 2);
num_failing += ASSERT_EQUAL(test_name, dv[2], 1);
num_failing += ASSERT_EQUAL(test_name, dv[3], 0);
num_failing += ASSERT_EQUAL(test_name, dv[4], -1);
num_failing += ASSERT_EQUAL(test_name, dv[5], -4);
num_failing += ASSERT_EQUAL(test_name, dv[6], 2);
num_failing += ASSERT_EQUAL(test_name, dv[7], 3);
failed_tests += test_passed(num_failing, test_name);
num_failing = 0;
}
std::cout << std::endl << failed_tests << " failing test(s) detected." << std::endl;
if (failed_tests == 0) {
return 0;
}
return 1;
}