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 <climits>
24 #include <cstring>
25
26 namespace {
27
BuildKernel(const char * name,int vectorSize,cl_kernel * k,cl_program * p,bool relaxedMode)28 int BuildKernel(const char *name, int vectorSize, cl_kernel *k, cl_program *p,
29 bool relaxedMode)
30 {
31 const char *c[] = { "__kernel void math_kernel",
32 sizeNames[vectorSize],
33 "( __global float",
34 sizeNames[vectorSize],
35 "* out, __global int",
36 sizeNames[vectorSize],
37 "* out2, __global float",
38 sizeNames[vectorSize],
39 "* in )\n"
40 "{\n"
41 " size_t i = get_global_id(0);\n"
42 " out[i] = ",
43 name,
44 "( in[i], out2 + i );\n"
45 "}\n" };
46
47 const char *c3[] = {
48 "__kernel void math_kernel",
49 sizeNames[vectorSize],
50 "( __global float* out, __global int* out2, __global float* in)\n"
51 "{\n"
52 " size_t i = get_global_id(0);\n"
53 " if( i + 1 < get_global_size(0) )\n"
54 " {\n"
55 " float3 f0 = vload3( 0, in + 3 * i );\n"
56 " int3 iout = INT_MIN;\n"
57 " f0 = ",
58 name,
59 "( f0, &iout );\n"
60 " vstore3( f0, 0, out + 3*i );\n"
61 " vstore3( iout, 0, out2 + 3*i );\n"
62 " }\n"
63 " else\n"
64 " {\n"
65 " size_t parity = i & 1; // Figure out how many elements are "
66 "left over after BUFFER_SIZE % (3*sizeof(float)). Assume power of two "
67 "buffer size \n"
68 " int3 iout = INT_MIN;\n"
69 " float3 f0;\n"
70 " switch( parity )\n"
71 " {\n"
72 " case 1:\n"
73 " f0 = (float3)( in[3*i], NAN, NAN ); \n"
74 " break;\n"
75 " case 0:\n"
76 " f0 = (float3)( in[3*i], in[3*i+1], NAN ); \n"
77 " break;\n"
78 " }\n"
79 " f0 = ",
80 name,
81 "( f0, &iout );\n"
82 " switch( parity )\n"
83 " {\n"
84 " case 0:\n"
85 " out[3*i+1] = f0.y; \n"
86 " out2[3*i+1] = iout.y; \n"
87 " // fall through\n"
88 " case 1:\n"
89 " out[3*i] = f0.x; \n"
90 " out2[3*i] = iout.x; \n"
91 " break;\n"
92 " }\n"
93 " }\n"
94 "}\n"
95 };
96
97 const char **kern = c;
98 size_t kernSize = sizeof(c) / sizeof(c[0]);
99
100 if (sizeValues[vectorSize] == 3)
101 {
102 kern = c3;
103 kernSize = sizeof(c3) / sizeof(c3[0]);
104 }
105
106 char testName[32];
107 snprintf(testName, sizeof(testName) - 1, "math_kernel%s",
108 sizeNames[vectorSize]);
109
110 return MakeKernel(kern, (cl_uint)kernSize, testName, k, p, relaxedMode);
111 }
112
113 struct BuildKernelInfo2
114 {
115 cl_kernel *kernels;
116 Programs &programs;
117 const char *nameInCode;
118 bool relaxedMode; // Whether to build with -cl-fast-relaxed-math.
119 };
120
BuildKernelFn(cl_uint job_id,cl_uint thread_id UNUSED,void * p)121 cl_int BuildKernelFn(cl_uint job_id, cl_uint thread_id UNUSED, void *p)
122 {
123 BuildKernelInfo2 *info = (BuildKernelInfo2 *)p;
124 cl_uint vectorSize = gMinVectorSizeIndex + job_id;
125 return BuildKernel(info->nameInCode, vectorSize, info->kernels + vectorSize,
126 &(info->programs[vectorSize]), info->relaxedMode);
127 }
128
abs_cl_long(cl_long i)129 cl_ulong abs_cl_long(cl_long i)
130 {
131 cl_long mask = i >> 63;
132 return (i ^ mask) - mask;
133 }
134
135 } // anonymous namespace
136
TestFunc_FloatI_Float(const Func * f,MTdata d,bool relaxedMode)137 int TestFunc_FloatI_Float(const Func *f, MTdata d, bool relaxedMode)
138 {
139 int error;
140 Programs programs;
141 cl_kernel kernels[VECTOR_SIZE_COUNT];
142 float maxError = 0.0f;
143 int64_t maxError2 = 0;
144 int ftz = f->ftz || gForceFTZ || 0 == (CL_FP_DENORM & gFloatCapabilities);
145 float maxErrorVal = 0.0f;
146 float maxErrorVal2 = 0.0f;
147 uint64_t step = getTestStep(sizeof(float), BUFFER_SIZE);
148 int scale = (int)((1ULL << 32) / (16 * BUFFER_SIZE / sizeof(float)) + 1);
149 cl_ulong maxiError;
150
151 logFunctionInfo(f->name, sizeof(cl_float), relaxedMode);
152
153 float float_ulps;
154 if (gIsEmbedded)
155 float_ulps = f->float_embedded_ulps;
156 else
157 float_ulps = f->float_ulps;
158
159 maxiError = float_ulps == INFINITY ? CL_ULONG_MAX : 0;
160
161 // Init the kernels
162 {
163 BuildKernelInfo2 build_info{ kernels, programs, f->nameInCode,
164 relaxedMode };
165 if ((error = ThreadPool_Do(BuildKernelFn,
166 gMaxVectorSizeIndex - gMinVectorSizeIndex,
167 &build_info)))
168 return error;
169 }
170
171 for (uint64_t i = 0; i < (1ULL << 32); i += step)
172 {
173 // Init input array
174 uint32_t *p = (uint32_t *)gIn;
175 if (gWimpyMode)
176 {
177 for (size_t j = 0; j < BUFFER_SIZE / sizeof(float); j++)
178 p[j] = (uint32_t)i + j * scale;
179 }
180 else
181 {
182 for (size_t j = 0; j < BUFFER_SIZE / sizeof(float); j++)
183 p[j] = (uint32_t)i + j;
184 }
185 if ((error = clEnqueueWriteBuffer(gQueue, gInBuffer, CL_FALSE, 0,
186 BUFFER_SIZE, gIn, 0, NULL, NULL)))
187 {
188 vlog_error("\n*** Error %d in clEnqueueWriteBuffer ***\n", error);
189 return error;
190 }
191
192 // write garbage into output arrays
193 for (auto j = gMinVectorSizeIndex; j < gMaxVectorSizeIndex; j++)
194 {
195 uint32_t pattern = 0xffffdead;
196 memset_pattern4(gOut[j], &pattern, BUFFER_SIZE);
197 if ((error =
198 clEnqueueWriteBuffer(gQueue, gOutBuffer[j], CL_FALSE, 0,
199 BUFFER_SIZE, gOut[j], 0, NULL, NULL)))
200 {
201 vlog_error("\n*** Error %d in clEnqueueWriteBuffer2(%d) ***\n",
202 error, j);
203 goto exit;
204 }
205
206 memset_pattern4(gOut2[j], &pattern, BUFFER_SIZE);
207 if ((error = clEnqueueWriteBuffer(gQueue, gOutBuffer2[j], CL_FALSE,
208 0, BUFFER_SIZE, gOut2[j], 0, NULL,
209 NULL)))
210 {
211 vlog_error("\n*** Error %d in clEnqueueWriteBuffer2b(%d) ***\n",
212 error, j);
213 goto exit;
214 }
215 }
216
217 // Run the kernels
218 for (auto j = gMinVectorSizeIndex; j < gMaxVectorSizeIndex; j++)
219 {
220 size_t vectorSize = sizeValues[j] * sizeof(cl_float);
221 size_t localCount = (BUFFER_SIZE + vectorSize - 1) / vectorSize;
222 if ((error = clSetKernelArg(kernels[j], 0, sizeof(gOutBuffer[j]),
223 &gOutBuffer[j])))
224 {
225 LogBuildError(programs[j]);
226 goto exit;
227 }
228 if ((error = clSetKernelArg(kernels[j], 1, sizeof(gOutBuffer2[j]),
229 &gOutBuffer2[j])))
230 {
231 LogBuildError(programs[j]);
232 goto exit;
233 }
234 if ((error = clSetKernelArg(kernels[j], 2, sizeof(gInBuffer),
235 &gInBuffer)))
236 {
237 LogBuildError(programs[j]);
238 goto exit;
239 }
240
241 if ((error =
242 clEnqueueNDRangeKernel(gQueue, kernels[j], 1, NULL,
243 &localCount, NULL, 0, NULL, NULL)))
244 {
245 vlog_error("FAILED -- could not execute kernel\n");
246 goto exit;
247 }
248 }
249
250 // Get that moving
251 if ((error = clFlush(gQueue))) vlog("clFlush failed\n");
252
253 // Calculate the correctly rounded reference result
254 float *r = (float *)gOut_Ref;
255 int *r2 = (int *)gOut_Ref2;
256 float *s = (float *)gIn;
257 for (size_t j = 0; j < BUFFER_SIZE / sizeof(float); j++)
258 r[j] = (float)f->func.f_fpI(s[j], r2 + j);
259
260 // Read the data back
261 for (auto j = gMinVectorSizeIndex; j < gMaxVectorSizeIndex; j++)
262 {
263 if ((error =
264 clEnqueueReadBuffer(gQueue, gOutBuffer[j], CL_TRUE, 0,
265 BUFFER_SIZE, gOut[j], 0, NULL, NULL)))
266 {
267 vlog_error("ReadArray failed %d\n", error);
268 goto exit;
269 }
270 if ((error =
271 clEnqueueReadBuffer(gQueue, gOutBuffer2[j], CL_TRUE, 0,
272 BUFFER_SIZE, gOut2[j], 0, NULL, NULL)))
273 {
274 vlog_error("ReadArray2 failed %d\n", error);
275 goto exit;
276 }
277 }
278
279 if (gSkipCorrectnessTesting) break;
280
281 // Verify data
282 uint32_t *t = (uint32_t *)gOut_Ref;
283 int32_t *t2 = (int32_t *)gOut_Ref2;
284 for (size_t j = 0; j < BUFFER_SIZE / sizeof(float); j++)
285 {
286 for (auto k = gMinVectorSizeIndex; k < gMaxVectorSizeIndex; k++)
287 {
288 uint32_t *q = (uint32_t *)(gOut[k]);
289 int32_t *q2 = (int32_t *)(gOut2[k]);
290
291 // If we aren't getting the correctly rounded result
292 if (t[j] != q[j] || t2[j] != q2[j])
293 {
294 float test = ((float *)q)[j];
295 int correct2 = INT_MIN;
296 double correct = f->func.f_fpI(s[j], &correct2);
297 float err = Ulp_Error(test, correct);
298 cl_long iErr = (int64_t)q2[j] - (int64_t)correct2;
299 int fail = !(fabsf(err) <= float_ulps
300 && abs_cl_long(iErr) <= maxiError);
301 if (ftz || relaxedMode)
302 {
303 // retry per section 6.5.3.2
304 if (IsFloatResultSubnormal(correct, float_ulps))
305 {
306 fail = fail && !(test == 0.0f && iErr == 0);
307 if (!fail) err = 0.0f;
308 }
309
310 // retry per section 6.5.3.3
311 if (IsFloatSubnormal(s[j]))
312 {
313 int correct5, correct6;
314 double correct3 = f->func.f_fpI(0.0, &correct5);
315 double correct4 = f->func.f_fpI(-0.0, &correct6);
316 float err2 = Ulp_Error(test, correct3);
317 float err3 = Ulp_Error(test, correct4);
318 cl_long iErr2 =
319 (long long)q2[j] - (long long)correct5;
320 cl_long iErr3 =
321 (long long)q2[j] - (long long)correct6;
322
323 // Did +0 work?
324 if (fabsf(err2) <= float_ulps
325 && abs_cl_long(iErr2) <= maxiError)
326 {
327 err = err2;
328 iErr = iErr2;
329 fail = 0;
330 }
331 // Did -0 work?
332 else if (fabsf(err3) <= float_ulps
333 && abs_cl_long(iErr3) <= maxiError)
334 {
335 err = err3;
336 iErr = iErr3;
337 fail = 0;
338 }
339
340 // retry per section 6.5.3.4
341 if (fail
342 && (IsFloatResultSubnormal(correct2, float_ulps)
343 || IsFloatResultSubnormal(correct3,
344 float_ulps)))
345 {
346 fail = fail
347 && !(test == 0.0f
348 && (abs_cl_long(iErr2) <= maxiError
349 || abs_cl_long(iErr3)
350 <= maxiError));
351 if (!fail)
352 {
353 err = 0.0f;
354 iErr = 0;
355 }
356 }
357 }
358 }
359 if (fabsf(err) > maxError)
360 {
361 maxError = fabsf(err);
362 maxErrorVal = s[j];
363 }
364 if (llabs(iErr) > maxError2)
365 {
366 maxError2 = llabs(iErr);
367 maxErrorVal2 = s[j];
368 }
369
370 if (fail)
371 {
372 vlog_error("\nERROR: %s%s: {%f, %d} ulp error at %a: "
373 "*{%a, %d} vs. {%a, %d}\n",
374 f->name, sizeNames[k], err, (int)iErr,
375 ((float *)gIn)[j], ((float *)gOut_Ref)[j],
376 ((int *)gOut_Ref2)[j], test, q2[j]);
377 error = -1;
378 goto exit;
379 }
380 }
381 }
382 }
383
384 if (0 == (i & 0x0fffffff))
385 {
386 if (gVerboseBruteForce)
387 {
388 vlog("base:%14" PRIu64 " step:%10" PRIu64
389 " bufferSize:%10d \n",
390 i, step, BUFFER_SIZE);
391 }
392 else
393 {
394 vlog(".");
395 }
396 fflush(stdout);
397 }
398 }
399
400 if (!gSkipCorrectnessTesting)
401 {
402 if (gWimpyMode)
403 vlog("Wimp pass");
404 else
405 vlog("passed");
406
407 vlog("\t{%8.2f, %" PRId64 "} @ {%a, %a}", maxError, maxError2,
408 maxErrorVal, maxErrorVal2);
409 }
410
411 vlog("\n");
412
413 exit:
414 // Release
415 for (auto k = gMinVectorSizeIndex; k < gMaxVectorSizeIndex; k++)
416 {
417 clReleaseKernel(kernels[k]);
418 }
419
420 return error;
421 }
422