• 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 <cstring>
23 
24 namespace {
25 
BuildKernel(const char * name,int vectorSize,cl_uint kernel_count,cl_kernel * k,cl_program * p,bool relaxedMode)26 int BuildKernel(const char *name, int vectorSize, cl_uint kernel_count,
27                 cl_kernel *k, cl_program *p, bool relaxedMode)
28 {
29     const char *c[] = { "__kernel void math_kernel",
30                         sizeNames[vectorSize],
31                         "( __global int",
32                         sizeNames[vectorSize],
33                         "* out, __global float",
34                         sizeNames[vectorSize],
35                         "* in )\n"
36                         "{\n"
37                         "   size_t i = get_global_id(0);\n"
38                         "   out[i] = ",
39                         name,
40                         "( in[i] );\n"
41                         "}\n" };
42 
43     const char *c3[] = {
44         "__kernel void math_kernel",
45         sizeNames[vectorSize],
46         "( __global int* out, __global float* in)\n"
47         "{\n"
48         "   size_t i = get_global_id(0);\n"
49         "   if( i + 1 < get_global_size(0) )\n"
50         "   {\n"
51         "       float3 f0 = vload3( 0, in + 3 * i );\n"
52         "       int3 i0 = ",
53         name,
54         "( f0 );\n"
55         "       vstore3( i0, 0, out + 3*i );\n"
56         "   }\n"
57         "   else\n"
58         "   {\n"
59         "       size_t parity = i & 1;   // Figure out how many elements are "
60         "left over after BUFFER_SIZE % (3*sizeof(float)). Assume power of two "
61         "buffer size \n"
62         "       int3 i0;\n"
63         "       float3 f0;\n"
64         "       switch( parity )\n"
65         "       {\n"
66         "           case 1:\n"
67         "               f0 = (float3)( in[3*i], 0xdead, 0xdead ); \n"
68         "               break;\n"
69         "           case 0:\n"
70         "               f0 = (float3)( in[3*i], in[3*i+1], 0xdead ); \n"
71         "               break;\n"
72         "       }\n"
73         "       i0 = ",
74         name,
75         "( f0 );\n"
76         "       switch( parity )\n"
77         "       {\n"
78         "           case 0:\n"
79         "               out[3*i+1] = i0.y; \n"
80         "               // fall through\n"
81         "           case 1:\n"
82         "               out[3*i] = i0.x; \n"
83         "               break;\n"
84         "       }\n"
85         "   }\n"
86         "}\n"
87     };
88 
89     const char **kern = c;
90     size_t kernSize = sizeof(c) / sizeof(c[0]);
91 
92     if (sizeValues[vectorSize] == 3)
93     {
94         kern = c3;
95         kernSize = sizeof(c3) / sizeof(c3[0]);
96     }
97 
98     char testName[32];
99     snprintf(testName, sizeof(testName) - 1, "math_kernel%s",
100              sizeNames[vectorSize]);
101 
102     return MakeKernels(kern, (cl_uint)kernSize, testName, kernel_count, k, p,
103                        relaxedMode);
104 }
105 
BuildKernelFn(cl_uint job_id,cl_uint thread_id UNUSED,void * p)106 cl_int BuildKernelFn(cl_uint job_id, cl_uint thread_id UNUSED, void *p)
107 {
108     BuildKernelInfo *info = (BuildKernelInfo *)p;
109     cl_uint vectorSize = gMinVectorSizeIndex + job_id;
110     return BuildKernel(info->nameInCode, vectorSize, info->threadCount,
111                        info->kernels[vectorSize].data(),
112                        &(info->programs[vectorSize]), info->relaxedMode);
113 }
114 
115 // Thread specific data for a worker thread
116 struct ThreadInfo
117 {
118     // Input and output buffers for the thread
119     clMemWrapper inBuf;
120     Buffers outBuf;
121 
122     // Per thread command queue to improve performance
123     clCommandQueueWrapper tQueue;
124 };
125 
126 struct TestInfo
127 {
128     size_t subBufferSize; // Size of the sub-buffer in elements
129     const Func *f; // A pointer to the function info
130 
131     // Programs for various vector sizes.
132     Programs programs;
133 
134     // Thread-specific kernels for each vector size:
135     // k[vector_size][thread_id]
136     KernelMatrix k;
137 
138     // Array of thread specific information
139     std::vector<ThreadInfo> tinfo;
140 
141     cl_uint threadCount; // Number of worker threads
142     cl_uint jobCount; // Number of jobs
143     cl_uint step; // step between each chunk and the next.
144     cl_uint scale; // stride between individual test values
145     int ftz; // non-zero if running in flush to zero mode
146     bool relaxedMode; // True if test is running in relaxed mode, false
147                       // otherwise.
148 };
149 
Test(cl_uint job_id,cl_uint thread_id,void * data)150 cl_int Test(cl_uint job_id, cl_uint thread_id, void *data)
151 {
152     TestInfo *job = (TestInfo *)data;
153     size_t buffer_elements = job->subBufferSize;
154     size_t buffer_size = buffer_elements * sizeof(cl_float);
155     cl_uint scale = job->scale;
156     cl_uint base = job_id * (cl_uint)job->step;
157     ThreadInfo *tinfo = &(job->tinfo[thread_id]);
158     fptr func = job->f->func;
159     int ftz = job->ftz;
160     bool relaxedMode = job->relaxedMode;
161     cl_int error = CL_SUCCESS;
162     cl_int ret = CL_SUCCESS;
163     const char *name = job->f->name;
164 
165     int signbit_test = 0;
166     if (!strcmp(name, "signbit")) signbit_test = 1;
167 
168 #define ref_func(s) (signbit_test ? func.i_f_f(s) : func.i_f(s))
169 
170     // start the map of the output arrays
171     cl_event e[VECTOR_SIZE_COUNT];
172     cl_int *out[VECTOR_SIZE_COUNT];
173     for (auto j = gMinVectorSizeIndex; j < gMaxVectorSizeIndex; j++)
174     {
175         out[j] = (cl_int *)clEnqueueMapBuffer(
176             tinfo->tQueue, tinfo->outBuf[j], CL_FALSE, CL_MAP_WRITE, 0,
177             buffer_size, 0, NULL, e + j, &error);
178         if (error || NULL == out[j])
179         {
180             vlog_error("Error: clEnqueueMapBuffer %d failed! err: %d\n", j,
181                        error);
182             return error;
183         }
184     }
185 
186     // Get that moving
187     if ((error = clFlush(tinfo->tQueue))) vlog("clFlush failed\n");
188 
189     // Init input array
190     cl_uint *p = (cl_uint *)gIn + thread_id * buffer_elements;
191     for (size_t j = 0; j < buffer_elements; j++) p[j] = base + j * scale;
192 
193     if ((error = clEnqueueWriteBuffer(tinfo->tQueue, tinfo->inBuf, CL_FALSE, 0,
194                                       buffer_size, p, 0, NULL, NULL)))
195     {
196         vlog_error("Error: clEnqueueWriteBuffer failed! err: %d\n", error);
197         return error;
198     }
199 
200     for (auto j = gMinVectorSizeIndex; j < gMaxVectorSizeIndex; j++)
201     {
202         // Wait for the map to finish
203         if ((error = clWaitForEvents(1, e + j)))
204         {
205             vlog_error("Error: clWaitForEvents failed! err: %d\n", error);
206             return error;
207         }
208         if ((error = clReleaseEvent(e[j])))
209         {
210             vlog_error("Error: clReleaseEvent failed! err: %d\n", error);
211             return error;
212         }
213 
214         // Fill the result buffer with garbage, so that old results don't carry
215         // over
216         uint32_t pattern = 0xffffdead;
217         memset_pattern4(out[j], &pattern, buffer_size);
218         if ((error = clEnqueueUnmapMemObject(tinfo->tQueue, tinfo->outBuf[j],
219                                              out[j], 0, NULL, NULL)))
220         {
221             vlog_error("Error: clEnqueueUnmapMemObject failed! err: %d\n",
222                        error);
223             return error;
224         }
225 
226         // run the kernel
227         size_t vectorCount =
228             (buffer_elements + sizeValues[j] - 1) / sizeValues[j];
229         cl_kernel kernel = job->k[j][thread_id]; // each worker thread has its
230                                                  // own copy of the cl_kernel
231         cl_program program = job->programs[j];
232 
233         if ((error = clSetKernelArg(kernel, 0, sizeof(tinfo->outBuf[j]),
234                                     &tinfo->outBuf[j])))
235         {
236             LogBuildError(program);
237             return error;
238         }
239         if ((error = clSetKernelArg(kernel, 1, sizeof(tinfo->inBuf),
240                                     &tinfo->inBuf)))
241         {
242             LogBuildError(program);
243             return error;
244         }
245 
246         if ((error = clEnqueueNDRangeKernel(tinfo->tQueue, kernel, 1, NULL,
247                                             &vectorCount, NULL, 0, NULL, NULL)))
248         {
249             vlog_error("FAILED -- could not execute kernel\n");
250             return error;
251         }
252     }
253 
254     // Get that moving
255     if ((error = clFlush(tinfo->tQueue))) vlog("clFlush 2 failed\n");
256 
257     if (gSkipCorrectnessTesting) return CL_SUCCESS;
258 
259     // Calculate the correctly rounded reference result
260     cl_int *r = (cl_int *)gOut_Ref + thread_id * buffer_elements;
261     float *s = (float *)p;
262     for (size_t j = 0; j < buffer_elements; j++) r[j] = ref_func(s[j]);
263 
264     // Read the data back -- no need to wait for the first N-1 buffers but wait
265     // for the last buffer. This is an in order queue.
266     for (auto j = gMinVectorSizeIndex; j < gMaxVectorSizeIndex; j++)
267     {
268         cl_bool blocking = (j + 1 < gMaxVectorSizeIndex) ? CL_FALSE : CL_TRUE;
269         out[j] = (cl_int *)clEnqueueMapBuffer(
270             tinfo->tQueue, tinfo->outBuf[j], blocking, CL_MAP_READ, 0,
271             buffer_size, 0, NULL, NULL, &error);
272         if (error || NULL == out[j])
273         {
274             vlog_error("Error: clEnqueueMapBuffer %d failed! err: %d\n", j,
275                        error);
276             return error;
277         }
278     }
279 
280     // Verify data
281     cl_int *t = (cl_int *)r;
282     for (size_t j = 0; j < buffer_elements; j++)
283     {
284         for (auto k = gMinVectorSizeIndex; k < gMaxVectorSizeIndex; k++)
285         {
286             cl_int *q = out[0];
287 
288             // If we aren't getting the correctly rounded result
289             if (gMinVectorSizeIndex == 0 && t[j] != q[j])
290             {
291                 // If we aren't getting the correctly rounded result
292                 if (ftz || relaxedMode)
293                 {
294                     if (IsFloatSubnormal(s[j]))
295                     {
296                         int correct = ref_func(+0.0f);
297                         int correct2 = ref_func(-0.0f);
298                         if (correct == q[j] || correct2 == q[j]) continue;
299                     }
300                 }
301 
302                 uint32_t err = t[j] - q[j];
303                 if (q[j] > t[j]) err = q[j] - t[j];
304                 vlog_error("\nERROR: %s: %d ulp error at %a: *%d vs. %d\n",
305                            name, err, ((float *)s)[j], t[j], q[j]);
306                 error = -1;
307                 goto exit;
308             }
309 
310 
311             for (auto k = std::max(1U, gMinVectorSizeIndex);
312                  k < gMaxVectorSizeIndex; k++)
313             {
314                 q = out[k];
315                 // If we aren't getting the correctly rounded result
316                 if (-t[j] != q[j])
317                 {
318                     if (ftz || relaxedMode)
319                     {
320                         if (IsFloatSubnormal(s[j]))
321                         {
322                             int correct = -ref_func(+0.0f);
323                             int correct2 = -ref_func(-0.0f);
324                             if (correct == q[j] || correct2 == q[j]) continue;
325                         }
326                     }
327 
328                     uint32_t err = -t[j] - q[j];
329                     if (q[j] > -t[j]) err = q[j] + t[j];
330                     vlog_error(
331                         "\nERROR: %s%s: %d ulp error at %a: *%d vs. %d\n", name,
332                         sizeNames[k], err, ((float *)s)[j], -t[j], q[j]);
333                     error = -1;
334                     goto exit;
335                 }
336             }
337         }
338     }
339 
340 exit:
341     ret = error;
342     for (auto j = gMinVectorSizeIndex; j < gMaxVectorSizeIndex; j++)
343     {
344         if ((error = clEnqueueUnmapMemObject(tinfo->tQueue, tinfo->outBuf[j],
345                                              out[j], 0, NULL, NULL)))
346         {
347             vlog_error("Error: clEnqueueUnmapMemObject %d failed 2! err: %d\n",
348                        j, error);
349             return error;
350         }
351     }
352 
353     if ((error = clFlush(tinfo->tQueue)))
354     {
355         vlog("clFlush 3 failed\n");
356         return error;
357     }
358 
359 
360     if (0 == (base & 0x0fffffff))
361     {
362         if (gVerboseBruteForce)
363         {
364             vlog("base:%14u step:%10u scale:%10u buf_elements:%10zd "
365                  "ThreadCount:%2u\n",
366                  base, job->step, job->scale, buffer_elements,
367                  job->threadCount);
368         }
369         else
370         {
371             vlog(".");
372         }
373         fflush(stdout);
374     }
375 
376     return ret;
377 }
378 
379 } // anonymous namespace
380 
TestMacro_Int_Float(const Func * f,MTdata d,bool relaxedMode)381 int TestMacro_Int_Float(const Func *f, MTdata d, bool relaxedMode)
382 {
383     TestInfo test_info{};
384     cl_int error;
385 
386     logFunctionInfo(f->name, sizeof(cl_float), relaxedMode);
387 
388     // Init test_info
389     test_info.threadCount = GetThreadCount();
390     test_info.subBufferSize = BUFFER_SIZE
391         / (sizeof(cl_float) * RoundUpToNextPowerOfTwo(test_info.threadCount));
392     test_info.scale = getTestScale(sizeof(cl_float));
393 
394     test_info.step = (cl_uint)test_info.subBufferSize * test_info.scale;
395     if (test_info.step / test_info.subBufferSize != test_info.scale)
396     {
397         // there was overflow
398         test_info.jobCount = 1;
399     }
400     else
401     {
402         test_info.jobCount = (cl_uint)((1ULL << 32) / test_info.step);
403     }
404 
405     test_info.f = f;
406     test_info.ftz =
407         f->ftz || gForceFTZ || 0 == (CL_FP_DENORM & gFloatCapabilities);
408     test_info.relaxedMode = relaxedMode;
409 
410     // cl_kernels aren't thread safe, so we make one for each vector size for
411     // every thread
412     for (auto i = gMinVectorSizeIndex; i < gMaxVectorSizeIndex; i++)
413     {
414         test_info.k[i].resize(test_info.threadCount, nullptr);
415     }
416 
417     test_info.tinfo.resize(test_info.threadCount);
418     for (cl_uint i = 0; i < test_info.threadCount; i++)
419     {
420         cl_buffer_region region = {
421             i * test_info.subBufferSize * sizeof(cl_float),
422             test_info.subBufferSize * sizeof(cl_float)
423         };
424         test_info.tinfo[i].inBuf =
425             clCreateSubBuffer(gInBuffer, CL_MEM_READ_ONLY,
426                               CL_BUFFER_CREATE_TYPE_REGION, &region, &error);
427         if (error || NULL == test_info.tinfo[i].inBuf)
428         {
429             vlog_error("Error: Unable to create sub-buffer of gInBuffer for "
430                        "region {%zd, %zd}\n",
431                        region.origin, region.size);
432             goto exit;
433         }
434 
435         for (auto j = gMinVectorSizeIndex; j < gMaxVectorSizeIndex; j++)
436         {
437             test_info.tinfo[i].outBuf[j] = clCreateSubBuffer(
438                 gOutBuffer[j], CL_MEM_WRITE_ONLY, CL_BUFFER_CREATE_TYPE_REGION,
439                 &region, &error);
440             if (error || NULL == test_info.tinfo[i].outBuf[j])
441             {
442                 vlog_error("Error: Unable to create sub-buffer of "
443                            "gOutBuffer[%d] for region {%zd, %zd}\n",
444                            (int)j, region.origin, region.size);
445                 goto exit;
446             }
447         }
448         test_info.tinfo[i].tQueue =
449             clCreateCommandQueue(gContext, gDevice, 0, &error);
450         if (NULL == test_info.tinfo[i].tQueue || error)
451         {
452             vlog_error("clCreateCommandQueue failed. (%d)\n", error);
453             goto exit;
454         }
455     }
456 
457     // Init the kernels
458     {
459         BuildKernelInfo build_info{ test_info.threadCount, test_info.k,
460                                     test_info.programs, f->nameInCode,
461                                     relaxedMode };
462         if ((error = ThreadPool_Do(BuildKernelFn,
463                                    gMaxVectorSizeIndex - gMinVectorSizeIndex,
464                                    &build_info)))
465             goto exit;
466     }
467 
468     // Run the kernels
469     if (!gSkipCorrectnessTesting)
470     {
471         error = ThreadPool_Do(Test, test_info.jobCount, &test_info);
472 
473         if (error) goto exit;
474 
475         if (gWimpyMode)
476             vlog("Wimp pass");
477         else
478             vlog("passed");
479     }
480 
481     vlog("\n");
482 
483 exit:
484     // Release
485     for (auto i = gMinVectorSizeIndex; i < gMaxVectorSizeIndex; i++)
486     {
487         for (auto &kernel : test_info.k[i])
488         {
489             clReleaseKernel(kernel);
490         }
491     }
492 
493     return error;
494 }
495