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 <cstring>
22
BuildKernel(const char * name,int vectorSize,cl_kernel * k,cl_program * p,bool relaxedMode)23 static int BuildKernel(const char *name, int vectorSize, cl_kernel *k,
24 cl_program *p, bool relaxedMode)
25 {
26 const char *c[] = { "__kernel void math_kernel",
27 sizeNames[vectorSize],
28 "( __global float",
29 sizeNames[vectorSize],
30 "* out, __global float",
31 sizeNames[vectorSize],
32 "* in1, __global float",
33 sizeNames[vectorSize],
34 "* in2, __global float",
35 sizeNames[vectorSize],
36 "* in3 )\n"
37 "{\n"
38 " size_t i = get_global_id(0);\n"
39 " out[i] = ",
40 name,
41 "( in1[i], in2[i], in3[i] );\n"
42 "}\n" };
43
44 const char *c3[] = {
45 "__kernel void math_kernel",
46 sizeNames[vectorSize],
47 "( __global float* out, __global float* in, __global float* in2, "
48 "__global float* in3)\n"
49 "{\n"
50 " size_t i = get_global_id(0);\n"
51 " if( i + 1 < get_global_size(0) )\n"
52 " {\n"
53 " float3 f0 = vload3( 0, in + 3 * i );\n"
54 " float3 f1 = vload3( 0, in2 + 3 * i );\n"
55 " float3 f2 = vload3( 0, in3 + 3 * i );\n"
56 " f0 = ",
57 name,
58 "( f0, f1, f2 );\n"
59 " vstore3( f0, 0, out + 3*i );\n"
60 " }\n"
61 " else\n"
62 " {\n"
63 " size_t parity = i & 1; // Figure out how many elements are "
64 "left over after BUFFER_SIZE % (3*sizeof(float)). Assume power of two "
65 "buffer size \n"
66 " float3 f0;\n"
67 " float3 f1;\n"
68 " float3 f2;\n"
69 " switch( parity )\n"
70 " {\n"
71 " case 1:\n"
72 " f0 = (float3)( in[3*i], NAN, NAN ); \n"
73 " f1 = (float3)( in2[3*i], NAN, NAN ); \n"
74 " f2 = (float3)( in3[3*i], NAN, NAN ); \n"
75 " break;\n"
76 " case 0:\n"
77 " f0 = (float3)( in[3*i], in[3*i+1], NAN ); \n"
78 " f1 = (float3)( in2[3*i], in2[3*i+1], NAN ); \n"
79 " f2 = (float3)( in3[3*i], in3[3*i+1], NAN ); \n"
80 " break;\n"
81 " }\n"
82 " f0 = ",
83 name,
84 "( f0, f1, f2 );\n"
85 " switch( parity )\n"
86 " {\n"
87 " case 0:\n"
88 " out[3*i+1] = f0.y; \n"
89 " // fall through\n"
90 " case 1:\n"
91 " out[3*i] = f0.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 typedef struct BuildKernelInfo
115 {
116 cl_uint offset; // the first vector size to build
117 cl_kernel *kernels;
118 cl_program *programs;
119 const char *nameInCode;
120 bool relaxedMode; // Whether to build with -cl-fast-relaxed-math.
121 } BuildKernelInfo;
122
BuildKernelFn(cl_uint job_id,cl_uint thread_id UNUSED,void * p)123 static cl_int BuildKernelFn(cl_uint job_id, cl_uint thread_id UNUSED, void *p)
124 {
125 BuildKernelInfo *info = (BuildKernelInfo *)p;
126 cl_uint i = info->offset + job_id;
127 return BuildKernel(info->nameInCode, i, info->kernels + i,
128 info->programs + i, info->relaxedMode);
129 }
130
TestFunc_mad_Float(const Func * f,MTdata d,bool relaxedMode)131 int TestFunc_mad_Float(const Func *f, MTdata d, bool relaxedMode)
132 {
133 int error;
134
135 logFunctionInfo(f->name, sizeof(cl_float), relaxedMode);
136
137 cl_program programs[VECTOR_SIZE_COUNT];
138 cl_kernel kernels[VECTOR_SIZE_COUNT];
139 float maxError = 0.0f;
140 float maxErrorVal = 0.0f;
141 float maxErrorVal2 = 0.0f;
142 float maxErrorVal3 = 0.0f;
143 uint64_t step = getTestStep(sizeof(float), BUFFER_SIZE);
144
145 // Init the kernels
146 {
147 BuildKernelInfo build_info = { gMinVectorSizeIndex, kernels, programs,
148 f->nameInCode, relaxedMode };
149 if ((error = ThreadPool_Do(BuildKernelFn,
150 gMaxVectorSizeIndex - gMinVectorSizeIndex,
151 &build_info)))
152 return error;
153 }
154
155 for (uint64_t i = 0; i < (1ULL << 32); i += step)
156 {
157 // Init input array
158 cl_uint *p = (cl_uint *)gIn;
159 cl_uint *p2 = (cl_uint *)gIn2;
160 cl_uint *p3 = (cl_uint *)gIn3;
161 for (size_t j = 0; j < BUFFER_SIZE / sizeof(float); j++)
162 {
163 p[j] = genrand_int32(d);
164 p2[j] = genrand_int32(d);
165 p3[j] = genrand_int32(d);
166 }
167
168 if ((error = clEnqueueWriteBuffer(gQueue, gInBuffer, CL_FALSE, 0,
169 BUFFER_SIZE, gIn, 0, NULL, NULL)))
170 {
171 vlog_error("\n*** Error %d in clEnqueueWriteBuffer ***\n", error);
172 return error;
173 }
174
175 if ((error = clEnqueueWriteBuffer(gQueue, gInBuffer2, CL_FALSE, 0,
176 BUFFER_SIZE, gIn2, 0, NULL, NULL)))
177 {
178 vlog_error("\n*** Error %d in clEnqueueWriteBuffer2 ***\n", error);
179 return error;
180 }
181
182 if ((error = clEnqueueWriteBuffer(gQueue, gInBuffer3, CL_FALSE, 0,
183 BUFFER_SIZE, gIn3, 0, NULL, NULL)))
184 {
185 vlog_error("\n*** Error %d in clEnqueueWriteBuffer3 ***\n", error);
186 return error;
187 }
188
189 // write garbage into output arrays
190 for (auto j = gMinVectorSizeIndex; j < gMaxVectorSizeIndex; j++)
191 {
192 uint32_t pattern = 0xffffdead;
193 memset_pattern4(gOut[j], &pattern, BUFFER_SIZE);
194 if ((error =
195 clEnqueueWriteBuffer(gQueue, gOutBuffer[j], CL_FALSE, 0,
196 BUFFER_SIZE, gOut[j], 0, NULL, NULL)))
197 {
198 vlog_error("\n*** Error %d in clEnqueueWriteBuffer2(%d) ***\n",
199 error, j);
200 goto exit;
201 }
202 }
203
204 // Run the kernels
205 for (auto j = gMinVectorSizeIndex; j < gMaxVectorSizeIndex; j++)
206 {
207 size_t vectorSize = sizeof(cl_float) * sizeValues[j];
208 size_t localCount = (BUFFER_SIZE + vectorSize - 1)
209 / vectorSize; // BUFFER_SIZE / vectorSize rounded up
210 if ((error = clSetKernelArg(kernels[j], 0, sizeof(gOutBuffer[j]),
211 &gOutBuffer[j])))
212 {
213 LogBuildError(programs[j]);
214 goto exit;
215 }
216 if ((error = clSetKernelArg(kernels[j], 1, sizeof(gInBuffer),
217 &gInBuffer)))
218 {
219 LogBuildError(programs[j]);
220 goto exit;
221 }
222 if ((error = clSetKernelArg(kernels[j], 2, sizeof(gInBuffer2),
223 &gInBuffer2)))
224 {
225 LogBuildError(programs[j]);
226 goto exit;
227 }
228 if ((error = clSetKernelArg(kernels[j], 3, sizeof(gInBuffer3),
229 &gInBuffer3)))
230 {
231 LogBuildError(programs[j]);
232 goto exit;
233 }
234
235 if ((error =
236 clEnqueueNDRangeKernel(gQueue, kernels[j], 1, NULL,
237 &localCount, NULL, 0, NULL, NULL)))
238 {
239 vlog_error("FAILED -- could not execute kernel\n");
240 goto exit;
241 }
242 }
243
244 // Get that moving
245 if ((error = clFlush(gQueue))) vlog("clFlush failed\n");
246
247 // Calculate the correctly rounded reference result
248 float *r = (float *)gOut_Ref;
249 float *s = (float *)gIn;
250 float *s2 = (float *)gIn2;
251 float *s3 = (float *)gIn3;
252 for (size_t j = 0; j < BUFFER_SIZE / sizeof(float); j++)
253 r[j] = (float)f->func.f_fff(s[j], s2[j], s3[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 }
266
267 if (gSkipCorrectnessTesting) break;
268
269 // Verify data -- No verification possible.
270 // MAD is a random number generator.
271 if (0 == (i & 0x0fffffff))
272 {
273 vlog(".");
274 fflush(stdout);
275 }
276 }
277
278 if (!gSkipCorrectnessTesting)
279 {
280 if (gWimpyMode)
281 vlog("Wimp pass");
282 else
283 vlog("passed");
284
285 vlog("\t%8.2f @ {%a, %a, %a}", maxError, maxErrorVal, maxErrorVal2,
286 maxErrorVal3);
287 }
288
289 vlog("\n");
290
291 exit:
292 // Release
293 for (auto k = gMinVectorSizeIndex; k < gMaxVectorSizeIndex; k++)
294 {
295 clReleaseKernel(kernels[k]);
296 clReleaseProgram(programs[k]);
297 }
298
299 return error;
300 }
301