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