forked from intel/llvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel_functor.cpp
More file actions
179 lines (148 loc) · 4.89 KB
/
kernel_functor.cpp
File metadata and controls
179 lines (148 loc) · 4.89 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
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple -o %t.out %s
// RUN: cd %T
// RUN: %RUN_ON_HOST %t.out
//==--- kernel_functor.cpp - Functors as SYCL kernel test ------------------==//
// This test illustrates defining kernels as named function objects (functors)
//
// 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 <CL/sycl.hpp>
#include <cassert>
constexpr auto sycl_read_write = cl::sycl::access::mode::read_write;
constexpr auto sycl_global_buffer = cl::sycl::access::target::global_buffer;
// Case 1:
// - functor class is defined in an anonymous namespace
// - the '()' operator:
// * does not have parameters (to be used in 'single_task').
// * has the 'const' qualifier
namespace {
class Functor1 {
public:
Functor1(
int X_,
cl::sycl::accessor<int, 1, sycl_read_write, sycl_global_buffer> &Acc_)
: X(X_), Acc(Acc_) {}
void operator()() const { Acc[0] += X; }
private:
int X;
cl::sycl::accessor<int, 1, sycl_read_write, sycl_global_buffer> Acc;
};
} // namespace
// Case 1:
// - functor class is defined in a namespace
// - the '()' operator:
// * does not have parameters (to be used in 'single_task').
// * has the 'const' qualifier
namespace ns {
class Functor2 {
public:
Functor2(
int X_,
cl::sycl::accessor<int, 1, sycl_read_write, sycl_global_buffer> &Acc_)
: X(X_), Acc(Acc_) {}
// cl::sycl::accessor's operator [] is const, hence 'const' is possible below
void operator()() const { Acc[0] += X; }
private:
int X;
cl::sycl::accessor<int, 1, sycl_read_write, sycl_global_buffer> Acc;
};
}
// Case 2:
// - functor class is templated and defined in the translation unit scope
// - the '()' operator:
// * has a parameter of type cl::sycl::id<1> (to be used in 'parallel_for').
// * has the 'const' qualifier
template <typename T> class TmplFunctor {
public:
TmplFunctor(
T X_, cl::sycl::accessor<T, 1, sycl_read_write, sycl_global_buffer> &Acc_)
: X(X_), Acc(Acc_) {}
void operator()(cl::sycl::id<1> id) const { Acc[id] += X; }
private:
T X;
cl::sycl::accessor<T, 1, sycl_read_write, sycl_global_buffer> Acc;
};
// Case 3:
// - functor class is templated and defined in the translation unit scope
// - the '()' operator:
// * has a parameter of type cl::sycl::id<1> (to be used in 'parallel_for').
// * has the 'const' qualifier
template <typename T> class TmplConstFunctor {
public:
TmplConstFunctor(
T X_, cl::sycl::accessor<T, 1, sycl_read_write, sycl_global_buffer> &Acc_)
: X(X_), Acc(Acc_) {}
void operator()(cl::sycl::id<1> id) const { Acc[id] += X; }
private:
T X;
cl::sycl::accessor<T, 1, sycl_read_write, sycl_global_buffer> Acc;
};
// Exercise non-templated functors in 'single_task'.
int foo(int X) {
int A[] = { 10 };
{
cl::sycl::queue Q;
cl::sycl::buffer<int, 1> Buf(A, 1);
Q.submit([&](cl::sycl::handler &cgh) {
auto Acc = Buf.get_access<sycl_read_write, sycl_global_buffer>(cgh);
Functor1 F(X, Acc);
cgh.single_task(F);
});
Q.submit([&](cl::sycl::handler &cgh) {
auto Acc = Buf.get_access<sycl_read_write, sycl_global_buffer>(cgh);
ns::Functor2 F(X, Acc);
cgh.single_task(F);
});
Q.submit([&](cl::sycl::handler &cgh) {
auto Acc = Buf.get_access<sycl_read_write, sycl_global_buffer>(cgh);
ns::Functor2 F(X, Acc);
cgh.single_task(F);
});
}
return A[0];
}
#define ARR_LEN(x) sizeof(x) / sizeof(x[0])
// Exercise templated functors in 'parallel_for'.
template <typename T> T bar(T X) {
T A[] = {(T)10, (T)10 };
{
cl::sycl::queue Q;
cl::sycl::buffer<T, 1> Buf(A, ARR_LEN(A));
Q.submit([&](cl::sycl::handler &cgh) {
auto Acc =
Buf.template get_access<sycl_read_write, sycl_global_buffer>(cgh);
TmplFunctor<T> F(X, Acc);
cgh.parallel_for(cl::sycl::range<1>(ARR_LEN(A)), F);
});
// Spice with lambdas to make sure functors and lambdas work together.
Q.submit([&](cl::sycl::handler &cgh) {
auto Acc =
Buf.template get_access<sycl_read_write, sycl_global_buffer>(cgh);
cgh.parallel_for<class LambdaKernel>(
cl::sycl::range<1>(ARR_LEN(A)),
[=](cl::sycl::id<1> id) { Acc[id] += X; });
});
Q.submit([&](cl::sycl::handler &cgh) {
auto Acc =
Buf.template get_access<sycl_read_write, sycl_global_buffer>(cgh);
TmplConstFunctor<T> F(X, Acc);
cgh.parallel_for(cl::sycl::range<1>(ARR_LEN(A)), F);
});
}
T res = (T)0;
for (int i = 0; i < ARR_LEN(A); i++)
res += A[i];
return res;
}
int main() {
const int Res1 = foo(10);
const int Res2 = bar(10);
const int Gold1 = 40;
const int Gold2 = 80;
assert(Res1 == Gold1);
assert(Res2 == Gold2);
return 0;
}