|
1 |
| -// RUN: %clang -I %S/Inputs -std=c++11 --sycl -Xclang -fsycl-int-header=%t.h %s -c -o %t.spv |
2 |
| -// RUN: FileCheck %s --input-file=%t.h |
3 |
| - |
4 |
| -// Checks that functors are supported as SYCL kernels. |
5 |
| - |
6 |
| -#include "sycl.hpp" |
7 |
| - |
8 |
| -constexpr auto sycl_read_write = cl::sycl::access::mode::read_write; |
9 |
| -constexpr auto sycl_global_buffer = cl::sycl::access::target::global_buffer; |
10 |
| - |
11 |
| -// Case 1: |
12 |
| -// - functor class is defined in an anonymous namespace |
13 |
| -// - the '()' operator: |
14 |
| -// * does not have parameters (to be used in 'single_task'). |
15 |
| -// * has no 'const' qualifier |
16 |
| -namespace { |
17 |
| - class Functor1 { |
18 |
| - public: |
19 |
| - Functor1(int X_, cl::sycl::accessor<int, 1, sycl_read_write, sycl_global_buffer> &Acc_) : |
20 |
| - X(X_), Acc(Acc_) |
21 |
| - {} |
22 |
| - |
23 |
| - void operator()() { |
24 |
| - Acc.use(X); |
25 |
| - } |
26 |
| - |
27 |
| - private: |
28 |
| - int X; |
29 |
| - cl::sycl::accessor<int, 1, sycl_read_write, sycl_global_buffer> Acc; |
30 |
| - }; |
31 |
| -} |
32 |
| - |
33 |
| -// Case 2: |
34 |
| -// - functor class is defined in a namespace |
35 |
| -// - the '()' operator: |
36 |
| -// * does not have parameters (to be used in 'single_task'). |
37 |
| -// * has the 'const' qualifier |
38 |
| -namespace ns { |
39 |
| - class Functor2 { |
40 |
| - public: |
41 |
| - Functor2(int X_, cl::sycl::accessor<int, 1, sycl_read_write, sycl_global_buffer> &Acc_) : |
42 |
| - X(X_), Acc(Acc_) |
43 |
| - {} |
44 |
| - |
45 |
| - void operator()() const { |
46 |
| - Acc.use(X); |
47 |
| - } |
48 |
| - |
49 |
| - private: |
50 |
| - int X; |
51 |
| - cl::sycl::accessor<int, 1, sycl_read_write, sycl_global_buffer> Acc; |
52 |
| - }; |
53 |
| -} |
54 |
| - |
55 |
| -// Case 3: |
56 |
| -// - functor class is templated and defined in the translation unit scope |
57 |
| -// - the '()' operator: |
58 |
| -// * has a parameter of type cl::sycl::id<1> (to be used in 'parallel_for'). |
59 |
| -// * has no 'const' qualifier |
60 |
| -template <typename T> class TmplFunctor { |
61 |
| -public: |
62 |
| - TmplFunctor(T X_, cl::sycl::accessor<T, 1, sycl_read_write, sycl_global_buffer> &Acc_) : |
63 |
| - X(X_), Acc(Acc_) |
64 |
| - {} |
65 |
| - |
66 |
| - void operator()(cl::sycl::id<1> id) { |
67 |
| - Acc.use(id, X); |
68 |
| - } |
69 |
| - |
70 |
| -private: |
71 |
| - T X; |
72 |
| - cl::sycl::accessor<T, 1, sycl_read_write, sycl_global_buffer> Acc; |
73 |
| -}; |
74 |
| - |
75 |
| -// Case 4: |
76 |
| -// - functor class is templated and defined in the translation unit scope |
77 |
| -// - the '()' operator: |
78 |
| -// * has a parameter of type cl::sycl::id<1> (to be used in 'parallel_for'). |
79 |
| -// * has the 'const' qualifier |
80 |
| -template <typename T> class TmplConstFunctor { |
81 |
| -public: |
82 |
| - TmplConstFunctor(T X_, cl::sycl::accessor<T, 1, sycl_read_write, sycl_global_buffer> &Acc_) : |
83 |
| - X(X_), Acc(Acc_) |
84 |
| - {} |
85 |
| - |
86 |
| - void operator()(cl::sycl::id<1> id) const { |
87 |
| - Acc.use(id, X); |
88 |
| - } |
89 |
| - |
90 |
| -private: |
91 |
| - T X; |
92 |
| - cl::sycl::accessor<T, 1, sycl_read_write, sycl_global_buffer> Acc; |
93 |
| -}; |
94 |
| - |
95 |
| -// Exercise non-templated functors in 'single_task'. |
96 |
| -int foo(int X) { |
97 |
| - int A[] = { 10 }; |
98 |
| - { |
99 |
| - cl::sycl::queue Q; |
100 |
| - cl::sycl::buffer<int, 1> Buf(A, 1); |
101 |
| - |
102 |
| - Q.submit([&](cl::sycl::handler& cgh) { |
103 |
| - auto Acc = Buf.get_access<sycl_read_write, sycl_global_buffer>(cgh); |
104 |
| - Functor1 F(X, Acc); |
105 |
| - |
106 |
| - cgh.single_task(F); |
107 |
| - }); |
108 |
| - Q.submit([&](cl::sycl::handler& cgh) { |
109 |
| - auto Acc = Buf.get_access<sycl_read_write, sycl_global_buffer>(cgh); |
110 |
| - ns::Functor2 F(X, Acc); |
111 |
| - |
112 |
| - cgh.single_task(F); |
113 |
| - }); |
114 |
| - Q.submit([&](cl::sycl::handler& cgh) { |
115 |
| - auto Acc = Buf.get_access<sycl_read_write, sycl_global_buffer>(cgh); |
116 |
| - ns::Functor2 F(X, Acc); |
117 |
| - |
118 |
| - cgh.single_task(F); |
119 |
| - }); |
120 |
| - } |
121 |
| - return A[0]; |
122 |
| -} |
123 |
| - |
124 |
| -#define ARR_LEN(x) sizeof(x)/sizeof(x[0]) |
125 |
| - |
126 |
| -// Exercise templated functors in 'parallel_for'. |
127 |
| -template <typename T> T bar(T X) { |
128 |
| - T A[] = { (T)10, (T)10 }; |
129 |
| - { |
130 |
| - cl::sycl::queue Q; |
131 |
| - cl::sycl::buffer<T, 1> Buf(A, ARR_LEN(A)); |
132 |
| - |
133 |
| - Q.submit([&](cl::sycl::handler& cgh) { |
134 |
| - auto Acc = Buf.template get_access<sycl_read_write, sycl_global_buffer>(cgh); |
135 |
| - TmplFunctor<T> F(X, Acc); |
136 |
| - |
137 |
| - cgh.parallel_for(cl::sycl::range<1>(ARR_LEN(A)), F); |
138 |
| - }); |
139 |
| - // Spice with lambdas to make sure functors and lambdas work together. |
140 |
| - Q.submit([&](cl::sycl::handler& cgh) { |
141 |
| - auto Acc = Buf.template get_access<sycl_read_write, sycl_global_buffer>(cgh); |
142 |
| - cgh.parallel_for<class LambdaKernel>( |
143 |
| - cl::sycl::range<1>(ARR_LEN(A)), |
144 |
| - [=](cl::sycl::id<1> id) { Acc.use(id, X); }); |
145 |
| - }); |
146 |
| - Q.submit([&](cl::sycl::handler& cgh) { |
147 |
| - auto Acc = Buf.template get_access<sycl_read_write, sycl_global_buffer>(cgh); |
148 |
| - TmplConstFunctor<T> F(X, Acc); |
149 |
| - |
150 |
| - cgh.parallel_for(cl::sycl::range<1>(ARR_LEN(A)), F); |
151 |
| - }); |
152 |
| - } |
153 |
| - T res = (T)0; |
154 |
| - |
155 |
| - for (int i = 0; i < ARR_LEN(A); i++) { |
156 |
| - res += A[i]; |
157 |
| - } |
158 |
| - return res; |
159 |
| -} |
160 |
| - |
161 |
| -int main() { |
162 |
| - const int Res1 = foo(10); |
163 |
| - const int Res2 = bar(10); |
164 |
| - const int Gold1 = 40; |
165 |
| - const int Gold2 = 80; |
166 |
| - |
167 |
| -#ifndef __SYCL_DEVICE_ONLY__ |
168 |
| - cl::sycl::detail::KernelInfo<Functor1>::getName(); |
169 |
| - // CHECK: Functor1 |
170 |
| - cl::sycl::detail::KernelInfo<ns::Functor2>::getName(); |
171 |
| - // CHECK: ::ns::Functor2 |
172 |
| - cl::sycl::detail::KernelInfo<TmplFunctor<int>>::getName(); |
173 |
| - // CHECK: TmplFunctor<int> |
174 |
| - cl::sycl::detail::KernelInfo<TmplConstFunctor<int>>::getName(); |
175 |
| - // CHECK: TmplConstFunctor<int> |
176 |
| -#endif // __SYCL_DEVICE_ONLY__ |
177 |
| - |
178 |
| - return 0; |
179 |
| -} |
180 |
| - |
| 1 | +// RUN: %clang -I %S/Inputs -std=c++11 --sycl -Xclang -fsycl-int-header=%t.h %s -c -o %t.spv |
| 2 | +// RUN: FileCheck %s --input-file=%t.h |
| 3 | + |
| 4 | +// Checks that functors are supported as SYCL kernels. |
| 5 | + |
| 6 | +#include "sycl.hpp" |
| 7 | + |
| 8 | +constexpr auto sycl_read_write = cl::sycl::access::mode::read_write; |
| 9 | +constexpr auto sycl_global_buffer = cl::sycl::access::target::global_buffer; |
| 10 | + |
| 11 | +// Case 1: |
| 12 | +// - functor class is defined in an anonymous namespace |
| 13 | +// - the '()' operator: |
| 14 | +// * does not have parameters (to be used in 'single_task'). |
| 15 | +// * has no 'const' qualifier |
| 16 | +namespace { |
| 17 | + class Functor1 { |
| 18 | + public: |
| 19 | + Functor1(int X_, cl::sycl::accessor<int, 1, sycl_read_write, sycl_global_buffer> &Acc_) : |
| 20 | + X(X_), Acc(Acc_) |
| 21 | + {} |
| 22 | + |
| 23 | + void operator()() { |
| 24 | + Acc.use(X); |
| 25 | + } |
| 26 | + |
| 27 | + private: |
| 28 | + int X; |
| 29 | + cl::sycl::accessor<int, 1, sycl_read_write, sycl_global_buffer> Acc; |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +// Case 2: |
| 34 | +// - functor class is defined in a namespace |
| 35 | +// - the '()' operator: |
| 36 | +// * does not have parameters (to be used in 'single_task'). |
| 37 | +// * has the 'const' qualifier |
| 38 | +namespace ns { |
| 39 | + class Functor2 { |
| 40 | + public: |
| 41 | + Functor2(int X_, cl::sycl::accessor<int, 1, sycl_read_write, sycl_global_buffer> &Acc_) : |
| 42 | + X(X_), Acc(Acc_) |
| 43 | + {} |
| 44 | + |
| 45 | + void operator()() const { |
| 46 | + Acc.use(X); |
| 47 | + } |
| 48 | + |
| 49 | + private: |
| 50 | + int X; |
| 51 | + cl::sycl::accessor<int, 1, sycl_read_write, sycl_global_buffer> Acc; |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +// Case 3: |
| 56 | +// - functor class is templated and defined in the translation unit scope |
| 57 | +// - the '()' operator: |
| 58 | +// * has a parameter of type cl::sycl::id<1> (to be used in 'parallel_for'). |
| 59 | +// * has no 'const' qualifier |
| 60 | +template <typename T> class TmplFunctor { |
| 61 | +public: |
| 62 | + TmplFunctor(T X_, cl::sycl::accessor<T, 1, sycl_read_write, sycl_global_buffer> &Acc_) : |
| 63 | + X(X_), Acc(Acc_) |
| 64 | + {} |
| 65 | + |
| 66 | + void operator()(cl::sycl::id<1> id) { |
| 67 | + Acc.use(id, X); |
| 68 | + } |
| 69 | + |
| 70 | +private: |
| 71 | + T X; |
| 72 | + cl::sycl::accessor<T, 1, sycl_read_write, sycl_global_buffer> Acc; |
| 73 | +}; |
| 74 | + |
| 75 | +// Case 4: |
| 76 | +// - functor class is templated and defined in the translation unit scope |
| 77 | +// - the '()' operator: |
| 78 | +// * has a parameter of type cl::sycl::id<1> (to be used in 'parallel_for'). |
| 79 | +// * has the 'const' qualifier |
| 80 | +template <typename T> class TmplConstFunctor { |
| 81 | +public: |
| 82 | + TmplConstFunctor(T X_, cl::sycl::accessor<T, 1, sycl_read_write, sycl_global_buffer> &Acc_) : |
| 83 | + X(X_), Acc(Acc_) |
| 84 | + {} |
| 85 | + |
| 86 | + void operator()(cl::sycl::id<1> id) const { |
| 87 | + Acc.use(id, X); |
| 88 | + } |
| 89 | + |
| 90 | +private: |
| 91 | + T X; |
| 92 | + cl::sycl::accessor<T, 1, sycl_read_write, sycl_global_buffer> Acc; |
| 93 | +}; |
| 94 | + |
| 95 | +// Exercise non-templated functors in 'single_task'. |
| 96 | +int foo(int X) { |
| 97 | + int A[] = { 10 }; |
| 98 | + { |
| 99 | + cl::sycl::queue Q; |
| 100 | + cl::sycl::buffer<int, 1> Buf(A, 1); |
| 101 | + |
| 102 | + Q.submit([&](cl::sycl::handler& cgh) { |
| 103 | + auto Acc = Buf.get_access<sycl_read_write, sycl_global_buffer>(cgh); |
| 104 | + Functor1 F(X, Acc); |
| 105 | + |
| 106 | + cgh.single_task(F); |
| 107 | + }); |
| 108 | + Q.submit([&](cl::sycl::handler& cgh) { |
| 109 | + auto Acc = Buf.get_access<sycl_read_write, sycl_global_buffer>(cgh); |
| 110 | + ns::Functor2 F(X, Acc); |
| 111 | + |
| 112 | + cgh.single_task(F); |
| 113 | + }); |
| 114 | + Q.submit([&](cl::sycl::handler& cgh) { |
| 115 | + auto Acc = Buf.get_access<sycl_read_write, sycl_global_buffer>(cgh); |
| 116 | + ns::Functor2 F(X, Acc); |
| 117 | + |
| 118 | + cgh.single_task(F); |
| 119 | + }); |
| 120 | + } |
| 121 | + return A[0]; |
| 122 | +} |
| 123 | + |
| 124 | +#define ARR_LEN(x) sizeof(x)/sizeof(x[0]) |
| 125 | + |
| 126 | +// Exercise templated functors in 'parallel_for'. |
| 127 | +template <typename T> T bar(T X) { |
| 128 | + T A[] = { (T)10, (T)10 }; |
| 129 | + { |
| 130 | + cl::sycl::queue Q; |
| 131 | + cl::sycl::buffer<T, 1> Buf(A, ARR_LEN(A)); |
| 132 | + |
| 133 | + Q.submit([&](cl::sycl::handler& cgh) { |
| 134 | + auto Acc = Buf.template get_access<sycl_read_write, sycl_global_buffer>(cgh); |
| 135 | + TmplFunctor<T> F(X, Acc); |
| 136 | + |
| 137 | + cgh.parallel_for(cl::sycl::range<1>(ARR_LEN(A)), F); |
| 138 | + }); |
| 139 | + // Spice with lambdas to make sure functors and lambdas work together. |
| 140 | + Q.submit([&](cl::sycl::handler& cgh) { |
| 141 | + auto Acc = Buf.template get_access<sycl_read_write, sycl_global_buffer>(cgh); |
| 142 | + cgh.parallel_for<class LambdaKernel>( |
| 143 | + cl::sycl::range<1>(ARR_LEN(A)), |
| 144 | + [=](cl::sycl::id<1> id) { Acc.use(id, X); }); |
| 145 | + }); |
| 146 | + Q.submit([&](cl::sycl::handler& cgh) { |
| 147 | + auto Acc = Buf.template get_access<sycl_read_write, sycl_global_buffer>(cgh); |
| 148 | + TmplConstFunctor<T> F(X, Acc); |
| 149 | + |
| 150 | + cgh.parallel_for(cl::sycl::range<1>(ARR_LEN(A)), F); |
| 151 | + }); |
| 152 | + } |
| 153 | + T res = (T)0; |
| 154 | + |
| 155 | + for (int i = 0; i < ARR_LEN(A); i++) { |
| 156 | + res += A[i]; |
| 157 | + } |
| 158 | + return res; |
| 159 | +} |
| 160 | + |
| 161 | +int main() { |
| 162 | + const int Res1 = foo(10); |
| 163 | + const int Res2 = bar(10); |
| 164 | + const int Gold1 = 40; |
| 165 | + const int Gold2 = 80; |
| 166 | + |
| 167 | +#ifndef __SYCL_DEVICE_ONLY__ |
| 168 | + cl::sycl::detail::KernelInfo<Functor1>::getName(); |
| 169 | + // CHECK: Functor1 |
| 170 | + cl::sycl::detail::KernelInfo<ns::Functor2>::getName(); |
| 171 | + // CHECK: ::ns::Functor2 |
| 172 | + cl::sycl::detail::KernelInfo<TmplFunctor<int>>::getName(); |
| 173 | + // CHECK: TmplFunctor<int> |
| 174 | + cl::sycl::detail::KernelInfo<TmplConstFunctor<int>>::getName(); |
| 175 | + // CHECK: TmplConstFunctor<int> |
| 176 | +#endif // __SYCL_DEVICE_ONLY__ |
| 177 | + |
| 178 | + return 0; |
| 179 | +} |
| 180 | + |
0 commit comments