12
12
13
13
using namespace cl ::sycl;
14
14
15
+ // Note that this function is created only to test that if the accessor
16
+ // object passed to ONEAPI::reduction is destroyed right after
17
+ // ONEAPI::reduction creation, then the reduction still works properly,
18
+ // i.e. it holds a COPY of user's accessor.
19
+ template <typename T, typename BOpT>
20
+ auto createReduction (sycl::buffer<T, 1 > Buffer, handler &CGH, T Identity,
21
+ BOpT BOp) {
22
+ auto Acc = Buffer.template get_access <access ::mode::discard_write>(CGH);
23
+ return ONEAPI::reduction (Acc, Identity, BOp);
24
+ }
25
+
15
26
template <class KernelName , typename T, class BinaryOperation >
16
- void test (T Identity, BinaryOperation BOp, size_t WGSize, size_t NWItems) {
27
+ void test (queue &Q, T Identity, BinaryOperation BOp, size_t WGSize,
28
+ size_t NWItems) {
17
29
buffer<T, 1 > InBuf (NWItems);
18
30
buffer<T, 1 > OutBuf (1 );
19
31
@@ -22,15 +34,11 @@ void test(T Identity, BinaryOperation BOp, size_t WGSize, size_t NWItems) {
22
34
initInputData (InBuf, CorrectOut, Identity, BOp, NWItems);
23
35
24
36
// Compute.
25
- queue Q;
26
37
Q.submit ([&](handler &CGH) {
27
38
auto In = InBuf.template get_access <access ::mode::read >(CGH);
28
- auto Out = OutBuf.template get_access <access ::mode::discard_write>(CGH);
29
- auto Redu = ONEAPI::reduction (Out, Identity, BOp);
39
+ auto Redu = createReduction (OutBuf, CGH, Identity, BOp);
30
40
31
- range<1 > GlobalRange (NWItems);
32
- range<1 > LocalRange (WGSize);
33
- nd_range<1 > NDRange (GlobalRange, LocalRange);
41
+ nd_range<1 > NDRange (range<1 >{NWItems}, range<1 >{WGSize});
34
42
CGH.parallel_for <KernelName>(NDRange, Redu,
35
43
[=](nd_item<1 > NDIt, auto &Sum) {
36
44
Sum.combine (In[NDIt.get_global_linear_id ()]);
@@ -41,22 +49,23 @@ void test(T Identity, BinaryOperation BOp, size_t WGSize, size_t NWItems) {
41
49
auto Out = OutBuf.template get_access <access ::mode::read >();
42
50
T ComputedOut = *(Out.get_pointer ());
43
51
if (ComputedOut != CorrectOut) {
44
- std::cout << " NWItems = " << NWItems << " , WGSize = " << WGSize << " \n " ;
45
- std::cout << " Computed value: " << ComputedOut
52
+ std::cerr << " NWItems = " << NWItems << " , WGSize = " << WGSize << " \n " ;
53
+ std::cerr << " Computed value: " << ComputedOut
46
54
<< " , Expected value: " << CorrectOut << " \n " ;
47
55
assert (0 && " Wrong value." );
48
56
}
49
57
}
50
58
51
59
int main () {
60
+ queue Q;
52
61
test<class AddTestName , int >(
53
- 0 , [](auto x, auto y) { return (x + y); }, 1 , 1024 );
62
+ Q, 0 , [](auto x, auto y) { return (x + y); }, 1 , 1024 );
54
63
test<class MulTestName , int >(
55
- 0 , [](auto x, auto y) { return (x * y); }, 8 , 32 );
64
+ Q, 0 , [](auto x, auto y) { return (x * y); }, 8 , 32 );
56
65
57
66
// Check with CUSTOM type.
58
67
test<class CustomAddTestname , CustomVec<long long >>(
59
- CustomVec<long long >(0 ),
68
+ Q, CustomVec<long long >(0 ),
60
69
[](auto x, auto y) {
61
70
CustomVecPlus<long long > BOp;
62
71
return BOp (x, y);
0 commit comments