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