• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (c) 2021 The Khronos Group Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //    http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 #include "procs.h"
17 #include "subhelpers.h"
18 #include "harness/typeWrappers.h"
19 #include <set>
20 
21 namespace {
22 
23 template <typename T, NonUniformVoteOp operation> struct VOTE
24 {
gen__anon20349c9e0111::VOTE25     static void gen(T *x, T *t, cl_int *m, const WorkGroupParams &test_params)
26     {
27         int i, ii, j, k, n;
28         int nw = test_params.local_workgroup_size;
29         int ns = test_params.subgroup_size;
30         int ng = test_params.global_workgroup_size;
31         uint32_t work_items_mask = test_params.work_items_mask;
32         int nj = (nw + ns - 1) / ns;
33         int non_uniform_size = ng % nw;
34         ng = ng / nw;
35         int last_subgroup_size = 0;
36         ii = 0;
37 
38         log_info("  sub_group_%s%s... \n",
39                  (operation == NonUniformVoteOp::elect) ? "" : "non_uniform_",
40                  operation_names(operation));
41 
42         log_info("  test params: global size = %d local size = %d subgroups "
43                  "size = %d work item mask = 0x%x data type (%s)\n",
44                  test_params.global_workgroup_size, nw, ns, work_items_mask,
45                  TypeManager<T>::name());
46         if (non_uniform_size)
47         {
48             log_info("  non uniform work group size mode ON\n");
49         }
50         if (operation == NonUniformVoteOp::elect) return;
51 
52         for (k = 0; k < ng; ++k)
53         { // for each work_group
54             if (non_uniform_size && k == ng - 1)
55             {
56                 set_last_workgroup_params(non_uniform_size, nj, ns, nw,
57                                           last_subgroup_size);
58             }
59             for (j = 0; j < nj; ++j)
60             { // for each subgroup
61                 ii = j * ns;
62                 if (last_subgroup_size && j == nj - 1)
63                 {
64                     n = last_subgroup_size;
65                 }
66                 else
67                 {
68                     n = ii + ns > nw ? nw - ii : ns;
69                 }
70                 int e = genrand_int32(gMTdata) % 3;
71 
72                 for (i = 0; i < n; i++)
73                 {
74                     if (e == 2)
75                     { // set once 0 and once 1 alternately
76                         int value = i % 2;
77                         set_value(t[ii + i], value);
78                     }
79                     else
80                     { // set 0/1 for all work items in subgroup
81                         set_value(t[ii + i], e);
82                     }
83                 }
84             }
85             // Now map into work group using map from device
86             for (j = 0; j < nw; ++j)
87             {
88                 x[j] = t[j];
89             }
90             x += nw;
91             m += 4 * nw;
92         }
93     }
94 
chk__anon20349c9e0111::VOTE95     static int chk(T *x, T *y, T *mx, T *my, cl_int *m,
96                    const WorkGroupParams &test_params)
97     {
98         int ii, i, j, k, n;
99         int nw = test_params.local_workgroup_size;
100         int ns = test_params.subgroup_size;
101         int ng = test_params.global_workgroup_size;
102         uint32_t work_items_mask = test_params.work_items_mask;
103         int nj = (nw + ns - 1) / ns;
104         cl_int tr, rr;
105         int non_uniform_size = ng % nw;
106         ng = ng / nw;
107         if (non_uniform_size) ng++;
108         int last_subgroup_size = 0;
109 
110         for (k = 0; k < ng; ++k)
111         { // for each work_group
112             if (non_uniform_size && k == ng - 1)
113             {
114                 set_last_workgroup_params(non_uniform_size, nj, ns, nw,
115                                           last_subgroup_size);
116             }
117             for (j = 0; j < nw; ++j)
118             { // inside the work_group
119                 mx[j] = x[j]; // read host inputs for work_group
120                 my[j] = y[j]; // read device outputs for work_group
121             }
122 
123             for (j = 0; j < nj; ++j)
124             { // for each subgroup
125                 ii = j * ns;
126                 if (last_subgroup_size && j == nj - 1)
127                 {
128                     n = last_subgroup_size;
129                 }
130                 else
131                 {
132                     n = ii + ns > nw ? nw - ii : ns;
133                 }
134 
135                 rr = 0;
136                 if (operation == NonUniformVoteOp::all
137                     || operation == NonUniformVoteOp::all_equal)
138                     tr = 1;
139                 if (operation == NonUniformVoteOp::any) tr = 0;
140 
141                 std::set<int> active_work_items;
142                 for (i = 0; i < n; ++i)
143                 {
144                     uint32_t check_work_item = 1 << (i % 32);
145                     if (work_items_mask & check_work_item)
146                     {
147                         active_work_items.insert(i);
148                         switch (operation)
149                         {
150                             case NonUniformVoteOp::elect: break;
151 
152                             case NonUniformVoteOp::all:
153                                 tr &=
154                                     !compare_ordered<T>(mx[ii + i], 0) ? 1 : 0;
155                                 break;
156                             case NonUniformVoteOp::any:
157                                 tr |=
158                                     !compare_ordered<T>(mx[ii + i], 0) ? 1 : 0;
159                                 break;
160                             case NonUniformVoteOp::all_equal:
161                                 tr &= compare_ordered<T>(
162                                           mx[ii + i],
163                                           mx[ii + *active_work_items.begin()])
164                                     ? 1
165                                     : 0;
166                                 break;
167                             default:
168                                 log_error("Unknown operation\n");
169                                 return TEST_FAIL;
170                         }
171                     }
172                 }
173                 if (active_work_items.empty())
174                 {
175                     log_info("  no one workitem acitve... in workgroup id = %d "
176                              "subgroup id = %d\n",
177                              k, j);
178                 }
179                 else
180                 {
181                     auto lowest_active = active_work_items.begin();
182                     for (const int &active_work_item : active_work_items)
183                     {
184                         i = active_work_item;
185                         if (operation == NonUniformVoteOp::elect)
186                         {
187                             i == *lowest_active ? tr = 1 : tr = 0;
188                         }
189 
190                         // normalize device values on host, non zero set 1.
191                         rr = compare_ordered<T>(my[ii + i], 0) ? 0 : 1;
192 
193                         if (rr != tr)
194                         {
195                             log_error("ERROR: sub_group_%s() \n",
196                                       operation_names(operation));
197                             log_error(
198                                 "mismatch for work item %d sub group %d in "
199                                 "work group %d. Expected: %d Obtained: %d\n",
200                                 i, j, k, tr, rr);
201                             return TEST_FAIL;
202                         }
203                     }
204                 }
205             }
206 
207             x += nw;
208             y += nw;
209             m += 4 * nw;
210         }
211 
212         log_info("  sub_group_%s%s... passed\n",
213                  (operation == NonUniformVoteOp::elect) ? "" : "non_uniform_",
214                  operation_names(operation));
215         return TEST_PASS;
216     }
217 };
218 static const char *elect_source = R"(
219     __kernel void test_elect(const __global Type *in, __global int4 *xy, __global Type *out) {
220         int gid = get_global_id(0);
221         XY(xy,gid);
222         uint elect_work_item = 1 << (get_sub_group_local_id() % 32);
223             if (elect_work_item & WORK_ITEMS_MASK){
224                 out[gid] = sub_group_elect();
225             }
226     }
227 )";
228 
229 static const char *non_uniform_any_source = R"(
230     __kernel void test_non_uniform_any(const __global Type *in, __global int4 *xy, __global Type *out) {
231         int gid = get_global_id(0);
232         XY(xy,gid);
233         uint elect_work_item = 1 << (get_sub_group_local_id() % 32);
234             if (elect_work_item & WORK_ITEMS_MASK){
235                 out[gid] = sub_group_non_uniform_any(in[gid]);
236             }
237     }
238 )";
239 
240 static const char *non_uniform_all_source = R"(
241     __kernel void test_non_uniform_all(const __global Type *in, __global int4 *xy, __global Type *out) {
242         int gid = get_global_id(0);
243         XY(xy,gid);
244         uint elect_work_item = 1 << (get_sub_group_local_id() % 32);
245             if (elect_work_item & WORK_ITEMS_MASK){
246                 out[gid] = sub_group_non_uniform_all(in[gid]);
247             }
248     }
249 )";
250 
251 static const char *non_uniform_all_equal_source = R"(
252     __kernel void test_non_uniform_all_equal(const __global Type *in, __global int4 *xy, __global Type *out) {
253         int gid = get_global_id(0);
254         XY(xy,gid);
255         uint elect_work_item = 1 << (get_sub_group_local_id() % 32);
256             if (elect_work_item & WORK_ITEMS_MASK){
257                 out[gid] = sub_group_non_uniform_all_equal(in[gid]);
258             }
259     }
260 )";
261 
run_vote_all_equal_for_type(RunTestForType rft)262 template <typename T> int run_vote_all_equal_for_type(RunTestForType rft)
263 {
264     int error = rft.run_impl<T, VOTE<T, NonUniformVoteOp::all_equal>>(
265         "test_non_uniform_all_equal", non_uniform_all_equal_source);
266     return error;
267 }
268 }
269 
test_subgroup_functions_non_uniform_vote(cl_device_id device,cl_context context,cl_command_queue queue,int num_elements)270 int test_subgroup_functions_non_uniform_vote(cl_device_id device,
271                                              cl_context context,
272                                              cl_command_queue queue,
273                                              int num_elements)
274 {
275     std::vector<std::string> required_extensions = {
276         "cl_khr_subgroup_non_uniform_vote"
277     };
278 
279     std::vector<uint32_t> masks{ 0xffffffff, 0x55aaaa55, 0x5555aaaa, 0xaaaa5555,
280                                  0x0f0ff0f0, 0x0f0f0f0f, 0xff0000ff, 0xff00ff00,
281                                  0x00ffff00, 0x80000000 };
282     constexpr size_t global_work_size = 170;
283     constexpr size_t local_work_size = 64;
284     WorkGroupParams test_params(global_work_size, local_work_size,
285                                 required_extensions, masks);
286     RunTestForType rft(device, context, queue, num_elements, test_params);
287 
288     int error = run_vote_all_equal_for_type<cl_int>(rft);
289     error |= run_vote_all_equal_for_type<cl_uint>(rft);
290     error |= run_vote_all_equal_for_type<cl_long>(rft);
291     error |= run_vote_all_equal_for_type<cl_ulong>(rft);
292     error |= run_vote_all_equal_for_type<cl_float>(rft);
293     error |= run_vote_all_equal_for_type<cl_double>(rft);
294     error |= run_vote_all_equal_for_type<subgroups::cl_half>(rft);
295 
296     error |= rft.run_impl<cl_int, VOTE<cl_int, NonUniformVoteOp::all>>(
297         "test_non_uniform_all", non_uniform_all_source);
298     error |= rft.run_impl<cl_int, VOTE<cl_int, NonUniformVoteOp::elect>>(
299         "test_elect", elect_source);
300     error |= rft.run_impl<cl_int, VOTE<cl_int, NonUniformVoteOp::any>>(
301         "test_non_uniform_any", non_uniform_any_source);
302     return error;
303 }
304