• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 float",
35                         sizeNames[vectorSize],
36                         "* out2, __global float",
37                         sizeNames[vectorSize],
38                         "* in )\n"
39                         "{\n"
40                         "   size_t i = get_global_id(0);\n"
41                         "   out[i] = ",
42                         name,
43                         "( in[i], out2 + i );\n"
44                         "}\n" };
45 
46     const char *c3[] = {
47         "__kernel void math_kernel",
48         sizeNames[vectorSize],
49         "( __global float* out, __global float* out2, __global float* in)\n"
50         "{\n"
51         "   size_t i = get_global_id(0);\n"
52         "   if( i + 1 < get_global_size(0) )\n"
53         "   {\n"
54         "       float3 f0 = vload3( 0, in + 3 * i );\n"
55         "       float3 iout = NAN;\n"
56         "       f0 = ",
57         name,
58         "( f0, &iout );\n"
59         "       vstore3( f0, 0, out + 3*i );\n"
60         "       vstore3( iout, 0, out2 + 3*i );\n"
61         "   }\n"
62         "   else\n"
63         "   {\n"
64         "       size_t parity = i & 1;   // Figure out how many elements are "
65         "left over after BUFFER_SIZE % (3*sizeof(float)). Assume power of two "
66         "buffer size \n"
67         "       float3 iout = NAN;\n"
68         "       float3 f0;\n"
69         "       switch( parity )\n"
70         "       {\n"
71         "           case 1:\n"
72         "               f0 = (float3)( in[3*i], NAN, NAN ); \n"
73         "               break;\n"
74         "           case 0:\n"
75         "               f0 = (float3)( in[3*i], in[3*i+1], NAN ); \n"
76         "               break;\n"
77         "       }\n"
78         "       f0 = ",
79         name,
80         "( f0, &iout );\n"
81         "       switch( parity )\n"
82         "       {\n"
83         "           case 0:\n"
84         "               out[3*i+1] = f0.y; \n"
85         "               out2[3*i+1] = iout.y; \n"
86         "               // fall through\n"
87         "           case 1:\n"
88         "               out[3*i] = f0.x; \n"
89         "               out2[3*i] = iout.x; \n"
90         "               break;\n"
91         "       }\n"
92         "   }\n"
93         "}\n"
94     };
95 
96     const char **kern = c;
97     size_t kernSize = sizeof(c) / sizeof(c[0]);
98 
99     if (sizeValues[vectorSize] == 3)
100     {
101         kern = c3;
102         kernSize = sizeof(c3) / sizeof(c3[0]);
103     }
104 
105     char testName[32];
106     snprintf(testName, sizeof(testName) - 1, "math_kernel%s",
107              sizeNames[vectorSize]);
108 
109     return MakeKernel(kern, (cl_uint)kernSize, testName, k, p, relaxedMode);
110 }
111 
112 struct BuildKernelInfo2
113 {
114     cl_kernel *kernels;
115     Programs &programs;
116     const char *nameInCode;
117     bool relaxedMode; // Whether to build with -cl-fast-relaxed-math.
118 };
119 
BuildKernelFn(cl_uint job_id,cl_uint thread_id UNUSED,void * p)120 cl_int BuildKernelFn(cl_uint job_id, cl_uint thread_id UNUSED, void *p)
121 {
122     BuildKernelInfo2 *info = (BuildKernelInfo2 *)p;
123     cl_uint vectorSize = gMinVectorSizeIndex + job_id;
124     return BuildKernel(info->nameInCode, vectorSize, info->kernels + vectorSize,
125                        &(info->programs[vectorSize]), info->relaxedMode);
126 }
127 
128 } // anonymous namespace
129 
TestFunc_Float2_Float(const Func * f,MTdata d,bool relaxedMode)130 int TestFunc_Float2_Float(const Func *f, MTdata d, bool relaxedMode)
131 {
132     int error;
133     Programs programs;
134     cl_kernel kernels[VECTOR_SIZE_COUNT];
135     float maxError0 = 0.0f;
136     float maxError1 = 0.0f;
137     int ftz = f->ftz || gForceFTZ || 0 == (CL_FP_DENORM & gFloatCapabilities);
138     float maxErrorVal0 = 0.0f;
139     float maxErrorVal1 = 0.0f;
140     uint64_t step = getTestStep(sizeof(float), BUFFER_SIZE);
141     int scale = (int)((1ULL << 32) / (16 * BUFFER_SIZE / sizeof(float)) + 1);
142     cl_uchar overflow[BUFFER_SIZE / sizeof(float)];
143     int isFract = 0 == strcmp("fract", f->nameInCode);
144     int skipNanInf = isFract && !gInfNanSupport;
145 
146     logFunctionInfo(f->name, sizeof(cl_float), relaxedMode);
147 
148     float float_ulps = getAllowedUlpError(f, relaxedMode);
149     // Init the kernels
150     {
151         BuildKernelInfo2 build_info{ kernels, programs, f->nameInCode,
152                                      relaxedMode };
153         if ((error = ThreadPool_Do(BuildKernelFn,
154                                    gMaxVectorSizeIndex - gMinVectorSizeIndex,
155                                    &build_info)))
156             return error;
157     }
158 
159     for (uint64_t i = 0; i < (1ULL << 32); i += step)
160     {
161         // Init input array
162         uint32_t *p = (uint32_t *)gIn;
163         if (gWimpyMode)
164         {
165             for (size_t j = 0; j < BUFFER_SIZE / sizeof(float); j++)
166             {
167                 p[j] = (uint32_t)i + j * scale;
168                 if (relaxedMode && strcmp(f->name, "sincos") == 0)
169                 {
170                     float pj = *(float *)&p[j];
171                     if (fabs(pj) > M_PI) ((float *)p)[j] = NAN;
172                 }
173             }
174         }
175         else
176         {
177             for (size_t j = 0; j < BUFFER_SIZE / sizeof(float); j++)
178             {
179                 p[j] = (uint32_t)i + j;
180                 if (relaxedMode && strcmp(f->name, "sincos") == 0)
181                 {
182                     float pj = *(float *)&p[j];
183                     if (fabs(pj) > M_PI) ((float *)p)[j] = NAN;
184                 }
185             }
186         }
187 
188         if ((error = clEnqueueWriteBuffer(gQueue, gInBuffer, CL_FALSE, 0,
189                                           BUFFER_SIZE, gIn, 0, NULL, NULL)))
190         {
191             vlog_error("\n*** Error %d in clEnqueueWriteBuffer ***\n", error);
192             return error;
193         }
194 
195         // write garbage into output arrays
196         for (auto j = gMinVectorSizeIndex; j < gMaxVectorSizeIndex; j++)
197         {
198             uint32_t pattern = 0xffffdead;
199             memset_pattern4(gOut[j], &pattern, BUFFER_SIZE);
200             if ((error =
201                      clEnqueueWriteBuffer(gQueue, gOutBuffer[j], CL_FALSE, 0,
202                                           BUFFER_SIZE, gOut[j], 0, NULL, NULL)))
203             {
204                 vlog_error("\n*** Error %d in clEnqueueWriteBuffer2(%d) ***\n",
205                            error, j);
206                 goto exit;
207             }
208 
209             memset_pattern4(gOut2[j], &pattern, BUFFER_SIZE);
210             if ((error = clEnqueueWriteBuffer(gQueue, gOutBuffer2[j], CL_FALSE,
211                                               0, BUFFER_SIZE, gOut2[j], 0, NULL,
212                                               NULL)))
213             {
214                 vlog_error("\n*** Error %d in clEnqueueWriteBuffer2b(%d) ***\n",
215                            error, j);
216                 goto exit;
217             }
218         }
219 
220         // Run the kernels
221         for (auto j = gMinVectorSizeIndex; j < gMaxVectorSizeIndex; j++)
222         {
223             size_t vectorSize = sizeValues[j] * sizeof(cl_float);
224             size_t localCount = (BUFFER_SIZE + vectorSize - 1) / vectorSize;
225             if ((error = clSetKernelArg(kernels[j], 0, sizeof(gOutBuffer[j]),
226                                         &gOutBuffer[j])))
227             {
228                 LogBuildError(programs[j]);
229                 goto exit;
230             }
231             if ((error = clSetKernelArg(kernels[j], 1, sizeof(gOutBuffer2[j]),
232                                         &gOutBuffer2[j])))
233             {
234                 LogBuildError(programs[j]);
235                 goto exit;
236             }
237             if ((error = clSetKernelArg(kernels[j], 2, sizeof(gInBuffer),
238                                         &gInBuffer)))
239             {
240                 LogBuildError(programs[j]);
241                 goto exit;
242             }
243 
244             if ((error =
245                      clEnqueueNDRangeKernel(gQueue, kernels[j], 1, NULL,
246                                             &localCount, NULL, 0, NULL, NULL)))
247             {
248                 vlog_error("FAILED -- could not execute kernel\n");
249                 goto exit;
250             }
251         }
252 
253         // Get that moving
254         if ((error = clFlush(gQueue))) vlog("clFlush failed\n");
255 
256         FPU_mode_type oldMode;
257         RoundingMode oldRoundMode = kRoundToNearestEven;
258         if (isFract)
259         {
260             // Calculate the correctly rounded reference result
261             memset(&oldMode, 0, sizeof(oldMode));
262             if (ftz || relaxedMode) ForceFTZ(&oldMode);
263 
264             // Set the rounding mode to match the device
265             if (gIsInRTZMode)
266                 oldRoundMode = set_round(kRoundTowardZero, kfloat);
267         }
268 
269         // Calculate the correctly rounded reference result
270         float *r = (float *)gOut_Ref;
271         float *r2 = (float *)gOut_Ref2;
272         float *s = (float *)gIn;
273 
274         if (skipNanInf)
275         {
276             for (size_t j = 0; j < BUFFER_SIZE / sizeof(float); j++)
277             {
278                 double dd;
279                 feclearexcept(FE_OVERFLOW);
280 
281                 if (relaxedMode)
282                     r[j] = (float)f->rfunc.f_fpf(s[j], &dd);
283                 else
284                     r[j] = (float)f->func.f_fpf(s[j], &dd);
285 
286                 r2[j] = (float)dd;
287                 overflow[j] =
288                     FE_OVERFLOW == (FE_OVERFLOW & fetestexcept(FE_OVERFLOW));
289             }
290         }
291         else
292         {
293             for (size_t j = 0; j < BUFFER_SIZE / sizeof(float); j++)
294             {
295                 double dd;
296                 if (relaxedMode)
297                     r[j] = (float)f->rfunc.f_fpf(s[j], &dd);
298                 else
299                     r[j] = (float)f->func.f_fpf(s[j], &dd);
300 
301                 r2[j] = (float)dd;
302             }
303         }
304 
305         if (isFract && ftz) RestoreFPState(&oldMode);
306 
307         // Read the data back
308         for (auto j = gMinVectorSizeIndex; j < gMaxVectorSizeIndex; j++)
309         {
310             if ((error =
311                      clEnqueueReadBuffer(gQueue, gOutBuffer[j], CL_TRUE, 0,
312                                          BUFFER_SIZE, gOut[j], 0, NULL, NULL)))
313             {
314                 vlog_error("ReadArray failed %d\n", error);
315                 goto exit;
316             }
317             if ((error =
318                      clEnqueueReadBuffer(gQueue, gOutBuffer2[j], CL_TRUE, 0,
319                                          BUFFER_SIZE, gOut2[j], 0, NULL, NULL)))
320             {
321                 vlog_error("ReadArray2 failed %d\n", error);
322                 goto exit;
323             }
324         }
325 
326         if (gSkipCorrectnessTesting)
327         {
328             if (isFract && gIsInRTZMode) (void)set_round(oldRoundMode, kfloat);
329             break;
330         }
331 
332         // Verify data
333         uint32_t *t = (uint32_t *)gOut_Ref;
334         uint32_t *t2 = (uint32_t *)gOut_Ref2;
335         for (size_t j = 0; j < BUFFER_SIZE / sizeof(float); j++)
336         {
337             for (auto k = gMinVectorSizeIndex; k < gMaxVectorSizeIndex; k++)
338             {
339                 uint32_t *q = (uint32_t *)gOut[k];
340                 uint32_t *q2 = (uint32_t *)gOut2[k];
341 
342                 // If we aren't getting the correctly rounded result
343                 if (t[j] != q[j] || t2[j] != q2[j])
344                 {
345                     double correct, correct2;
346                     float err, err2;
347                     float test = ((float *)q)[j];
348                     float test2 = ((float *)q2)[j];
349 
350                     if (relaxedMode)
351                         correct = f->rfunc.f_fpf(s[j], &correct2);
352                     else
353                         correct = f->func.f_fpf(s[j], &correct2);
354 
355                     // Per section 10 paragraph 6, accept any result if an input
356                     // or output is a infinity or NaN or overflow
357                     if (relaxedMode || skipNanInf)
358                     {
359                         if (skipNanInf && overflow[j]) continue;
360                         // Note: no double rounding here.  Reference functions
361                         // calculate in single precision.
362                         if (IsFloatInfinity(correct) || IsFloatNaN(correct)
363                             || IsFloatInfinity(correct2) || IsFloatNaN(correct2)
364                             || IsFloatInfinity(s[j]) || IsFloatNaN(s[j]))
365                             continue;
366                     }
367 
368                     typedef int (*CheckForSubnormal)(
369                         double, float); // If we are in fast relaxed math, we
370                                         // have a different calculation for the
371                                         // subnormal threshold.
372                     CheckForSubnormal isFloatResultSubnormalPtr;
373                     if (relaxedMode)
374                     {
375                         err = Abs_Error(test, correct);
376                         err2 = Abs_Error(test2, correct2);
377                         isFloatResultSubnormalPtr =
378                             &IsFloatResultSubnormalAbsError;
379                     }
380                     else
381                     {
382                         err = Ulp_Error(test, correct);
383                         err2 = Ulp_Error(test2, correct2);
384                         isFloatResultSubnormalPtr = &IsFloatResultSubnormal;
385                     }
386                     int fail = !(fabsf(err) <= float_ulps
387                                  && fabsf(err2) <= float_ulps);
388 
389                     if (ftz || relaxedMode)
390                     {
391                         // retry per section 6.5.3.2
392                         if ((*isFloatResultSubnormalPtr)(correct, float_ulps))
393                         {
394                             if ((*isFloatResultSubnormalPtr)(correct2,
395                                                              float_ulps))
396                             {
397                                 fail = fail && !(test == 0.0f && test2 == 0.0f);
398                                 if (!fail)
399                                 {
400                                     err = 0.0f;
401                                     err2 = 0.0f;
402                                 }
403                             }
404                             else
405                             {
406                                 fail = fail
407                                     && !(test == 0.0f
408                                          && fabsf(err2) <= float_ulps);
409                                 if (!fail) err = 0.0f;
410                             }
411                         }
412                         else if ((*isFloatResultSubnormalPtr)(correct2,
413                                                               float_ulps))
414                         {
415                             fail = fail
416                                 && !(test2 == 0.0f && fabsf(err) <= float_ulps);
417                             if (!fail) err2 = 0.0f;
418                         }
419 
420 
421                         // retry per section 6.5.3.3
422                         if (IsFloatSubnormal(s[j]))
423                         {
424                             double correctp, correctn;
425                             double correct2p, correct2n;
426                             float errp, err2p, errn, err2n;
427 
428                             if (skipNanInf) feclearexcept(FE_OVERFLOW);
429                             if (relaxedMode)
430                             {
431                                 correctp = f->rfunc.f_fpf(0.0, &correct2p);
432                                 correctn = f->rfunc.f_fpf(-0.0, &correct2n);
433                             }
434                             else
435                             {
436                                 correctp = f->func.f_fpf(0.0, &correct2p);
437                                 correctn = f->func.f_fpf(-0.0, &correct2n);
438                             }
439 
440                             // Per section 10 paragraph 6, accept any result if
441                             // an input or output is a infinity or NaN or
442                             // overflow
443                             if (skipNanInf)
444                             {
445                                 if (fetestexcept(FE_OVERFLOW)) continue;
446 
447                                 // Note: no double rounding here.  Reference
448                                 // functions calculate in single precision.
449                                 if (IsFloatInfinity(correctp)
450                                     || IsFloatNaN(correctp)
451                                     || IsFloatInfinity(correctn)
452                                     || IsFloatNaN(correctn)
453                                     || IsFloatInfinity(correct2p)
454                                     || IsFloatNaN(correct2p)
455                                     || IsFloatInfinity(correct2n)
456                                     || IsFloatNaN(correct2n))
457                                     continue;
458                             }
459 
460                             if (relaxedMode)
461                             {
462                                 errp = Abs_Error(test, correctp);
463                                 err2p = Abs_Error(test, correct2p);
464                                 errn = Abs_Error(test, correctn);
465                                 err2n = Abs_Error(test, correct2n);
466                             }
467                             else
468                             {
469                                 errp = Ulp_Error(test, correctp);
470                                 err2p = Ulp_Error(test, correct2p);
471                                 errn = Ulp_Error(test, correctn);
472                                 err2n = Ulp_Error(test, correct2n);
473                             }
474 
475                             fail = fail
476                                 && ((!(fabsf(errp) <= float_ulps))
477                                     && (!(fabsf(err2p) <= float_ulps))
478                                     && ((!(fabsf(errn) <= float_ulps))
479                                         && (!(fabsf(err2n) <= float_ulps))));
480                             if (fabsf(errp) < fabsf(err)) err = errp;
481                             if (fabsf(errn) < fabsf(err)) err = errn;
482                             if (fabsf(err2p) < fabsf(err2)) err2 = err2p;
483                             if (fabsf(err2n) < fabsf(err2)) err2 = err2n;
484 
485                             // retry per section 6.5.3.4
486                             if ((*isFloatResultSubnormalPtr)(correctp,
487                                                              float_ulps)
488                                 || (*isFloatResultSubnormalPtr)(correctn,
489                                                                 float_ulps))
490                             {
491                                 if ((*isFloatResultSubnormalPtr)(correct2p,
492                                                                  float_ulps)
493                                     || (*isFloatResultSubnormalPtr)(correct2n,
494                                                                     float_ulps))
495                                 {
496                                     fail = fail
497                                         && !(test == 0.0f && test2 == 0.0f);
498                                     if (!fail) err = err2 = 0.0f;
499                                 }
500                                 else
501                                 {
502                                     fail = fail
503                                         && !(test == 0.0f
504                                              && fabsf(err2) <= float_ulps);
505                                     if (!fail) err = 0.0f;
506                                 }
507                             }
508                             else if ((*isFloatResultSubnormalPtr)(correct2p,
509                                                                   float_ulps)
510                                      || (*isFloatResultSubnormalPtr)(
511                                          correct2n, float_ulps))
512                             {
513                                 fail = fail
514                                     && !(test2 == 0.0f
515                                          && (fabsf(err) <= float_ulps));
516                                 if (!fail) err2 = 0.0f;
517                             }
518                         }
519                     }
520                     if (fabsf(err) > maxError0)
521                     {
522                         maxError0 = fabsf(err);
523                         maxErrorVal0 = s[j];
524                     }
525                     if (fabsf(err2) > maxError1)
526                     {
527                         maxError1 = fabsf(err2);
528                         maxErrorVal1 = s[j];
529                     }
530                     if (fail)
531                     {
532                         vlog_error("\nERROR: %s%s: {%f, %f} ulp error at %a: "
533                                    "*{%a, %a} vs. {%a, %a}\n",
534                                    f->name, sizeNames[k], err, err2,
535                                    ((float *)gIn)[j], ((float *)gOut_Ref)[j],
536                                    ((float *)gOut_Ref2)[j], test, test2);
537                         error = -1;
538                         goto exit;
539                     }
540                 }
541             }
542         }
543 
544         if (isFract && gIsInRTZMode) (void)set_round(oldRoundMode, kfloat);
545 
546         if (0 == (i & 0x0fffffff))
547         {
548             if (gVerboseBruteForce)
549             {
550                 vlog("base:%14" PRIu64 " step:%10" PRIu64
551                      "  bufferSize:%10d \n",
552                      i, step, BUFFER_SIZE);
553             }
554             else
555             {
556                 vlog(".");
557             }
558             fflush(stdout);
559         }
560     }
561 
562     if (!gSkipCorrectnessTesting)
563     {
564         if (gWimpyMode)
565             vlog("Wimp pass");
566         else
567             vlog("passed");
568 
569         vlog("\t{%8.2f, %8.2f} @ {%a, %a}", maxError0, maxError1, maxErrorVal0,
570              maxErrorVal1);
571     }
572 
573     vlog("\n");
574 
575 exit:
576     // Release
577     for (auto k = gMinVectorSizeIndex; k < gMaxVectorSizeIndex; k++)
578     {
579         clReleaseKernel(kernels[k]);
580     }
581 
582     return error;
583 }
584