xref: /aosp_15_r20/external/OpenCL-CTS/test_conformance/subgroups/test_subgroup.cpp (revision 6467f958c7de8070b317fc65bcb0f6472e388d82)
1 //
2 // Copyright (c) 2017 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 "subgroup_common_kernels.h"
19 #include "subgroup_common_templates.h"
20 #include "harness/conversions.h"
21 #include "harness/typeWrappers.h"
22 
23 namespace {
24 // Any/All test functions
25 template <NonUniformVoteOp operation> struct AA
26 {
log_test__anon4a8586e70111::AA27     static void log_test(const WorkGroupParams &test_params,
28                          const char *extra_text)
29     {
30         log_info("  sub_group_%s...%s\n", operation_names(operation),
31                  extra_text);
32     }
33 
gen__anon4a8586e70111::AA34     static void gen(cl_int *x, cl_int *t, cl_int *m,
35                     const WorkGroupParams &test_params)
36     {
37         int i, ii, j, k, n;
38         int ng = test_params.global_workgroup_size;
39         int nw = test_params.local_workgroup_size;
40         int ns = test_params.subgroup_size;
41         int nj = (nw + ns - 1) / ns;
42         int e;
43         ng = ng / nw;
44         ii = 0;
45         for (k = 0; k < ng; ++k)
46         {
47             for (j = 0; j < nj; ++j)
48             {
49                 ii = j * ns;
50                 n = ii + ns > nw ? nw - ii : ns;
51                 e = (int)(genrand_int32(gMTdata) % 3);
52 
53                 // Initialize data matrix indexed by local id and sub group id
54                 switch (e)
55                 {
56                     case 0: memset(&t[ii], 0, n * sizeof(cl_int)); break;
57                     case 1:
58                         memset(&t[ii], 0, n * sizeof(cl_int));
59                         i = (int)(genrand_int32(gMTdata) % (cl_uint)n);
60                         t[ii + i] = 41;
61                         break;
62                     case 2: memset(&t[ii], 0xff, n * sizeof(cl_int)); break;
63                 }
64             }
65 
66             // Now map into work group using map from device
67             for (j = 0; j < nw; ++j)
68             {
69                 x[j] = t[j];
70             }
71 
72             x += nw;
73             m += 4 * nw;
74         }
75     }
76 
chk__anon4a8586e70111::AA77     static test_status chk(cl_int *x, cl_int *y, cl_int *mx, cl_int *my,
78                            cl_int *m, const WorkGroupParams &test_params)
79     {
80         int ii, i, j, k, n;
81         int ng = test_params.global_workgroup_size;
82         int nw = test_params.local_workgroup_size;
83         int ns = test_params.subgroup_size;
84         int nj = (nw + ns - 1) / ns;
85         cl_int taa, raa;
86         ng = ng / nw;
87 
88         for (k = 0; k < ng; ++k)
89         {
90             // Map to array indexed to array indexed by local ID and sub group
91             for (j = 0; j < nw; ++j)
92             {
93                 mx[j] = x[j];
94                 my[j] = y[j];
95             }
96 
97             for (j = 0; j < nj; ++j)
98             {
99                 ii = j * ns;
100                 n = ii + ns > nw ? nw - ii : ns;
101 
102                 // Compute target
103                 if (operation == NonUniformVoteOp::any)
104                 {
105                     taa = 0;
106                     for (i = 0; i < n; ++i) taa |= mx[ii + i] != 0;
107                 }
108 
109                 if (operation == NonUniformVoteOp::all)
110                 {
111                     taa = 1;
112                     for (i = 0; i < n; ++i) taa &= mx[ii + i] != 0;
113                 }
114 
115                 // Check result
116                 for (i = 0; i < n; ++i)
117                 {
118                     raa = my[ii + i] != 0;
119                     if (raa != taa)
120                     {
121                         log_error("ERROR: sub_group_%s mismatch for local id "
122                                   "%d in sub group %d in group %d\n",
123                                   operation_names(operation), i, j, k);
124                         return TEST_FAIL;
125                     }
126                 }
127             }
128 
129             x += nw;
130             y += nw;
131             m += 4 * nw;
132         }
133         return TEST_PASS;
134     }
135 };
136 
137 template <typename T>
run_broadcast_scan_reduction_for_type(RunTestForType rft)138 int run_broadcast_scan_reduction_for_type(RunTestForType rft)
139 {
140     int error = rft.run_impl<T, BC<T, SubgroupsBroadcastOp::broadcast>>(
141         "sub_group_broadcast");
142     error |=
143         rft.run_impl<T, RED_NU<T, ArithmeticOp::add_>>("sub_group_reduce_add");
144     error |=
145         rft.run_impl<T, RED_NU<T, ArithmeticOp::max_>>("sub_group_reduce_max");
146     error |=
147         rft.run_impl<T, RED_NU<T, ArithmeticOp::min_>>("sub_group_reduce_min");
148     error |= rft.run_impl<T, SCIN_NU<T, ArithmeticOp::add_>>(
149         "sub_group_scan_inclusive_add");
150     error |= rft.run_impl<T, SCIN_NU<T, ArithmeticOp::max_>>(
151         "sub_group_scan_inclusive_max");
152     error |= rft.run_impl<T, SCIN_NU<T, ArithmeticOp::min_>>(
153         "sub_group_scan_inclusive_min");
154     error |= rft.run_impl<T, SCEX_NU<T, ArithmeticOp::add_>>(
155         "sub_group_scan_exclusive_add");
156     error |= rft.run_impl<T, SCEX_NU<T, ArithmeticOp::max_>>(
157         "sub_group_scan_exclusive_max");
158     error |= rft.run_impl<T, SCEX_NU<T, ArithmeticOp::min_>>(
159         "sub_group_scan_exclusive_min");
160     return error;
161 }
162 
163 }
164 // Entry point from main
test_subgroup_functions(cl_device_id device,cl_context context,cl_command_queue queue,int num_elements,bool useCoreSubgroups)165 int test_subgroup_functions(cl_device_id device, cl_context context,
166                             cl_command_queue queue, int num_elements,
167                             bool useCoreSubgroups)
168 {
169     constexpr size_t global_work_size = 2000;
170     constexpr size_t local_work_size = 200;
171     WorkGroupParams test_params(global_work_size, local_work_size);
172     test_params.save_kernel_source(sub_group_reduction_scan_source);
173     test_params.save_kernel_source(sub_group_generic_source,
174                                    "sub_group_broadcast");
175 
176     RunTestForType rft(device, context, queue, num_elements, test_params);
177     int error =
178         rft.run_impl<cl_int, AA<NonUniformVoteOp::any>>("sub_group_any");
179     error |= rft.run_impl<cl_int, AA<NonUniformVoteOp::all>>("sub_group_all");
180     error |= run_broadcast_scan_reduction_for_type<cl_int>(rft);
181     error |= run_broadcast_scan_reduction_for_type<cl_uint>(rft);
182     error |= run_broadcast_scan_reduction_for_type<cl_long>(rft);
183     error |= run_broadcast_scan_reduction_for_type<cl_ulong>(rft);
184     error |= run_broadcast_scan_reduction_for_type<cl_float>(rft);
185     error |= run_broadcast_scan_reduction_for_type<cl_double>(rft);
186     error |= run_broadcast_scan_reduction_for_type<subgroups::cl_half>(rft);
187     return error;
188 }
189 
test_subgroup_functions_core(cl_device_id device,cl_context context,cl_command_queue queue,int num_elements)190 int test_subgroup_functions_core(cl_device_id device, cl_context context,
191                                  cl_command_queue queue, int num_elements)
192 {
193     return test_subgroup_functions(device, context, queue, num_elements, true);
194 }
195 
test_subgroup_functions_ext(cl_device_id device,cl_context context,cl_command_queue queue,int num_elements)196 int test_subgroup_functions_ext(cl_device_id device, cl_context context,
197                                 cl_command_queue queue, int num_elements)
198 {
199     bool hasExtension = is_extension_available(device, "cl_khr_subgroups");
200 
201     if (!hasExtension)
202     {
203         log_info(
204             "Device does not support 'cl_khr_subgroups'. Skipping the test.\n");
205         return TEST_SKIPPED_ITSELF;
206     }
207     return test_subgroup_functions(device, context, queue, num_elements, false);
208 }
209