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
17 #include "common.h"
18 #include "function_list.h"
19 #include "test_functions.h"
20 #include "utility.h"
21
22 #include <cinttypes>
23 #include <cstring>
24
25 namespace {
26
BuildKernel(const char * name,int vectorSize,cl_kernel * k,cl_program * p,bool relaxedMode)27 int BuildKernel(const char *name, int vectorSize, cl_kernel *k, cl_program *p,
28 bool relaxedMode)
29 {
30 const char *c[] = { "__kernel void math_kernel",
31 sizeNames[vectorSize],
32 "( __global float",
33 sizeNames[vectorSize],
34 "* out, __global uint",
35 sizeNames[vectorSize],
36 "* in )\n"
37 "{\n"
38 " size_t i = get_global_id(0);\n"
39 " out[i] = ",
40 name,
41 "( in[i] );\n"
42 "}\n" };
43
44 const char *c3[] = {
45 "__kernel void math_kernel",
46 sizeNames[vectorSize],
47 "( __global float* out, __global uint* in)\n"
48 "{\n"
49 " size_t i = get_global_id(0);\n"
50 " if( i + 1 < get_global_size(0) )\n"
51 " {\n"
52 " uint3 u0 = vload3( 0, in + 3 * i );\n"
53 " float3 f0 = ",
54 name,
55 "( u0 );\n"
56 " vstore3( f0, 0, out + 3*i );\n"
57 " }\n"
58 " else\n"
59 " {\n"
60 " size_t parity = i & 1; // Figure out how many elements are "
61 "left over after BUFFER_SIZE % (3*sizeof(float)). Assume power of two "
62 "buffer size \n"
63 " uint3 u0;\n"
64 " float3 f0;\n"
65 " switch( parity )\n"
66 " {\n"
67 " case 1:\n"
68 " u0 = (uint3)( in[3*i], 0xdead, 0xdead ); \n"
69 " break;\n"
70 " case 0:\n"
71 " u0 = (uint3)( in[3*i], in[3*i+1], 0xdead ); \n"
72 " break;\n"
73 " }\n"
74 " f0 = ",
75 name,
76 "( u0 );\n"
77 " switch( parity )\n"
78 " {\n"
79 " case 0:\n"
80 " out[3*i+1] = f0.y; \n"
81 " // fall through\n"
82 " case 1:\n"
83 " out[3*i] = f0.x; \n"
84 " break;\n"
85 " }\n"
86 " }\n"
87 "}\n"
88 };
89
90 const char **kern = c;
91 size_t kernSize = sizeof(c) / sizeof(c[0]);
92
93 if (sizeValues[vectorSize] == 3)
94 {
95 kern = c3;
96 kernSize = sizeof(c3) / sizeof(c3[0]);
97 }
98
99 char testName[32];
100 snprintf(testName, sizeof(testName) - 1, "math_kernel%s",
101 sizeNames[vectorSize]);
102
103 return MakeKernel(kern, (cl_uint)kernSize, testName, k, p, relaxedMode);
104 }
105
106 struct BuildKernelInfo2
107 {
108 cl_kernel *kernels;
109 Programs &programs;
110 const char *nameInCode;
111 bool relaxedMode; // Whether to build with -cl-fast-relaxed-math.
112 };
113
BuildKernelFn(cl_uint job_id,cl_uint thread_id UNUSED,void * p)114 cl_int BuildKernelFn(cl_uint job_id, cl_uint thread_id UNUSED, void *p)
115 {
116 BuildKernelInfo2 *info = (BuildKernelInfo2 *)p;
117 cl_uint vectorSize = gMinVectorSizeIndex + job_id;
118 return BuildKernel(info->nameInCode, vectorSize, info->kernels + vectorSize,
119 &(info->programs[vectorSize]), info->relaxedMode);
120 }
121
122 } // anonymous namespace
123
TestFunc_Float_UInt(const Func * f,MTdata d,bool relaxedMode)124 int TestFunc_Float_UInt(const Func *f, MTdata d, bool relaxedMode)
125 {
126 int error;
127 Programs programs;
128 cl_kernel kernels[VECTOR_SIZE_COUNT];
129 float maxError = 0.0f;
130 int ftz = f->ftz || gForceFTZ || 0 == (CL_FP_DENORM & gFloatCapabilities);
131 float maxErrorVal = 0.0f;
132 uint64_t step = getTestStep(sizeof(float), BUFFER_SIZE);
133 int scale = (int)((1ULL << 32) / (16 * BUFFER_SIZE / sizeof(double)) + 1);
134
135 logFunctionInfo(f->name, sizeof(cl_float), relaxedMode);
136
137 float float_ulps;
138 if (gIsEmbedded)
139 float_ulps = f->float_embedded_ulps;
140 else
141 float_ulps = f->float_ulps;
142
143 // Init the kernels
144 {
145 BuildKernelInfo2 build_info{ kernels, programs, f->nameInCode,
146 relaxedMode };
147 if ((error = ThreadPool_Do(BuildKernelFn,
148 gMaxVectorSizeIndex - gMinVectorSizeIndex,
149 &build_info)))
150 return error;
151 }
152
153 for (uint64_t i = 0; i < (1ULL << 32); i += step)
154 {
155 // Init input array
156 uint32_t *p = (uint32_t *)gIn;
157 if (gWimpyMode)
158 {
159 for (size_t j = 0; j < BUFFER_SIZE / sizeof(float); j++)
160 p[j] = (uint32_t)i + j * scale;
161 }
162 else
163 {
164 for (size_t j = 0; j < BUFFER_SIZE / sizeof(float); j++)
165 p[j] = (uint32_t)i + j;
166 }
167 if ((error = clEnqueueWriteBuffer(gQueue, gInBuffer, CL_FALSE, 0,
168 BUFFER_SIZE, gIn, 0, NULL, NULL)))
169 {
170 vlog_error("\n*** Error %d in clEnqueueWriteBuffer ***\n", error);
171 return error;
172 }
173
174 // write garbage into output arrays
175 for (auto j = gMinVectorSizeIndex; j < gMaxVectorSizeIndex; j++)
176 {
177 uint32_t pattern = 0xffffdead;
178 memset_pattern4(gOut[j], &pattern, BUFFER_SIZE);
179 if ((error =
180 clEnqueueWriteBuffer(gQueue, gOutBuffer[j], CL_FALSE, 0,
181 BUFFER_SIZE, gOut[j], 0, NULL, NULL)))
182 {
183 vlog_error("\n*** Error %d in clEnqueueWriteBuffer2(%d) ***\n",
184 error, j);
185 goto exit;
186 }
187 }
188
189 // Run the kernels
190 for (auto j = gMinVectorSizeIndex; j < gMaxVectorSizeIndex; j++)
191 {
192 size_t vectorSize = sizeValues[j] * sizeof(cl_float);
193 size_t localCount = (BUFFER_SIZE + vectorSize - 1) / vectorSize;
194 if ((error = clSetKernelArg(kernels[j], 0, sizeof(gOutBuffer[j]),
195 &gOutBuffer[j])))
196 {
197 LogBuildError(programs[j]);
198 goto exit;
199 }
200 if ((error = clSetKernelArg(kernels[j], 1, sizeof(gInBuffer),
201 &gInBuffer)))
202 {
203 LogBuildError(programs[j]);
204 goto exit;
205 }
206
207 if ((error =
208 clEnqueueNDRangeKernel(gQueue, kernels[j], 1, NULL,
209 &localCount, NULL, 0, NULL, NULL)))
210 {
211 vlog_error("FAILED -- could not execute kernel\n");
212 goto exit;
213 }
214 }
215
216 // Get that moving
217 if ((error = clFlush(gQueue))) vlog("clFlush failed\n");
218
219 // Calculate the correctly rounded reference result
220 float *r = (float *)gOut_Ref;
221 cl_uint *s = (cl_uint *)gIn;
222 for (size_t j = 0; j < BUFFER_SIZE / sizeof(float); j++)
223 r[j] = (float)f->func.f_u(s[j]);
224
225 // Read the data back
226 for (auto j = gMinVectorSizeIndex; j < gMaxVectorSizeIndex; j++)
227 {
228 if ((error =
229 clEnqueueReadBuffer(gQueue, gOutBuffer[j], CL_TRUE, 0,
230 BUFFER_SIZE, gOut[j], 0, NULL, NULL)))
231 {
232 vlog_error("ReadArray failed %d\n", error);
233 goto exit;
234 }
235 }
236
237 if (gSkipCorrectnessTesting) break;
238
239 // Verify data
240 uint32_t *t = (uint32_t *)gOut_Ref;
241 for (size_t j = 0; j < BUFFER_SIZE / sizeof(float); j++)
242 {
243 for (auto k = gMinVectorSizeIndex; k < gMaxVectorSizeIndex; k++)
244 {
245 uint32_t *q = (uint32_t *)(gOut[k]);
246
247 // If we aren't getting the correctly rounded result
248 if (t[j] != q[j])
249 {
250 float test = ((float *)q)[j];
251 double correct = f->func.f_u(s[j]);
252 float err = Ulp_Error(test, correct);
253 int fail = !(fabsf(err) <= float_ulps);
254
255 if (fail)
256 {
257 if (ftz || relaxedMode)
258 {
259 // retry per section 6.5.3.2
260 if (IsFloatResultSubnormal(correct, float_ulps))
261 {
262 fail = fail && (test != 0.0f);
263 if (!fail) err = 0.0f;
264 }
265 }
266 }
267 if (fabsf(err) > maxError)
268 {
269 maxError = fabsf(err);
270 maxErrorVal = s[j];
271 }
272 if (fail)
273 {
274 vlog_error(
275 "\n%s%s: %f ulp error at 0x%8.8x: *%a vs. %a\n",
276 f->name, sizeNames[k], err, ((uint32_t *)gIn)[j],
277 ((float *)gOut_Ref)[j], test);
278 error = -1;
279 goto exit;
280 }
281 }
282 }
283 }
284
285 if (0 == (i & 0x0fffffff))
286 {
287 if (gVerboseBruteForce)
288 {
289 vlog("base:%14" PRIu64 " step:%10" PRIu64
290 " bufferSize:%10d \n",
291 i, step, BUFFER_SIZE);
292 }
293 else
294 {
295 vlog(".");
296 }
297 fflush(stdout);
298 }
299 }
300
301 if (!gSkipCorrectnessTesting)
302 {
303 if (gWimpyMode)
304 vlog("Wimp pass");
305 else
306 vlog("passed");
307
308 vlog("\t%8.2f @ %a", maxError, maxErrorVal);
309 }
310
311 vlog("\n");
312
313 exit:
314 // Release
315 for (auto k = gMinVectorSizeIndex; k < gMaxVectorSizeIndex; k++)
316 {
317 clReleaseKernel(kernels[k]);
318 }
319
320 return error;
321 }
322