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_kernel * k,cl_program * p,bool relaxedMode)26 int BuildKernel(const char *name, int vectorSize, cl_kernel *k, cl_program *p,
27 bool relaxedMode)
28 {
29 auto kernel_name = GetKernelName(vectorSize);
30 auto source = GetTernaryKernel(kernel_name, name, ParameterType::Double,
31 ParameterType::Double, ParameterType::Double,
32 ParameterType::Double, vectorSize);
33 std::array<const char *, 1> sources{ source.c_str() };
34 return MakeKernel(sources.data(), sources.size(), kernel_name.c_str(), k, p,
35 relaxedMode);
36 }
37
38 struct BuildKernelInfo2
39 {
40 cl_kernel *kernels;
41 Programs &programs;
42 const char *nameInCode;
43 bool relaxedMode; // Whether to build with -cl-fast-relaxed-math.
44 };
45
BuildKernelFn(cl_uint job_id,cl_uint thread_id UNUSED,void * p)46 cl_int BuildKernelFn(cl_uint job_id, cl_uint thread_id UNUSED, void *p)
47 {
48 BuildKernelInfo2 *info = (BuildKernelInfo2 *)p;
49 cl_uint vectorSize = gMinVectorSizeIndex + job_id;
50 return BuildKernel(info->nameInCode, vectorSize, info->kernels + vectorSize,
51 &(info->programs[vectorSize]), info->relaxedMode);
52 }
53
54 } // anonymous namespace
55
TestFunc_mad_Double(const Func * f,MTdata d,bool relaxedMode)56 int TestFunc_mad_Double(const Func *f, MTdata d, bool relaxedMode)
57 {
58 int error;
59 Programs programs;
60 cl_kernel kernels[VECTOR_SIZE_COUNT];
61 float maxError = 0.0f;
62 double maxErrorVal = 0.0f;
63 double maxErrorVal2 = 0.0f;
64 double maxErrorVal3 = 0.0f;
65 uint64_t step = getTestStep(sizeof(double), BUFFER_SIZE);
66
67 logFunctionInfo(f->name, sizeof(cl_double), relaxedMode);
68
69 // Init the kernels
70 {
71 BuildKernelInfo2 build_info{ kernels, programs, f->nameInCode,
72 relaxedMode };
73 if ((error = ThreadPool_Do(BuildKernelFn,
74 gMaxVectorSizeIndex - gMinVectorSizeIndex,
75 &build_info)))
76 return error;
77 }
78
79 for (uint64_t i = 0; i < (1ULL << 32); i += step)
80 {
81 // Init input array
82 double *p = (double *)gIn;
83 double *p2 = (double *)gIn2;
84 double *p3 = (double *)gIn3;
85 for (size_t j = 0; j < BUFFER_SIZE / sizeof(double); j++)
86 {
87 p[j] = DoubleFromUInt32(genrand_int32(d));
88 p2[j] = DoubleFromUInt32(genrand_int32(d));
89 p3[j] = DoubleFromUInt32(genrand_int32(d));
90 }
91
92 if ((error = clEnqueueWriteBuffer(gQueue, gInBuffer, CL_FALSE, 0,
93 BUFFER_SIZE, gIn, 0, NULL, NULL)))
94 {
95 vlog_error("\n*** Error %d in clEnqueueWriteBuffer ***\n", error);
96 return error;
97 }
98
99 if ((error = clEnqueueWriteBuffer(gQueue, gInBuffer2, CL_FALSE, 0,
100 BUFFER_SIZE, gIn2, 0, NULL, NULL)))
101 {
102 vlog_error("\n*** Error %d in clEnqueueWriteBuffer2 ***\n", error);
103 return error;
104 }
105
106 if ((error = clEnqueueWriteBuffer(gQueue, gInBuffer3, CL_FALSE, 0,
107 BUFFER_SIZE, gIn3, 0, NULL, NULL)))
108 {
109 vlog_error("\n*** Error %d in clEnqueueWriteBuffer3 ***\n", error);
110 return error;
111 }
112
113 // write garbage into output arrays
114 for (auto j = gMinVectorSizeIndex; j < gMaxVectorSizeIndex; j++)
115 {
116 uint32_t pattern = 0xffffdead;
117 memset_pattern4(gOut[j], &pattern, BUFFER_SIZE);
118 if ((error =
119 clEnqueueWriteBuffer(gQueue, gOutBuffer[j], CL_FALSE, 0,
120 BUFFER_SIZE, gOut[j], 0, NULL, NULL)))
121 {
122 vlog_error("\n*** Error %d in clEnqueueWriteBuffer2(%d) ***\n",
123 error, j);
124 goto exit;
125 }
126 }
127
128 // Run the kernels
129 for (auto j = gMinVectorSizeIndex; j < gMaxVectorSizeIndex; j++)
130 {
131 size_t vectorSize = sizeof(cl_double) * sizeValues[j];
132 size_t localCount = (BUFFER_SIZE + vectorSize - 1)
133 / vectorSize; // BUFFER_SIZE / vectorSize rounded up
134 if ((error = clSetKernelArg(kernels[j], 0, sizeof(gOutBuffer[j]),
135 &gOutBuffer[j])))
136 {
137 LogBuildError(programs[j]);
138 goto exit;
139 }
140 if ((error = clSetKernelArg(kernels[j], 1, sizeof(gInBuffer),
141 &gInBuffer)))
142 {
143 LogBuildError(programs[j]);
144 goto exit;
145 }
146 if ((error = clSetKernelArg(kernels[j], 2, sizeof(gInBuffer2),
147 &gInBuffer2)))
148 {
149 LogBuildError(programs[j]);
150 goto exit;
151 }
152 if ((error = clSetKernelArg(kernels[j], 3, sizeof(gInBuffer3),
153 &gInBuffer3)))
154 {
155 LogBuildError(programs[j]);
156 goto exit;
157 }
158
159 if ((error =
160 clEnqueueNDRangeKernel(gQueue, kernels[j], 1, NULL,
161 &localCount, NULL, 0, NULL, NULL)))
162 {
163 vlog_error("FAILED -- could not execute kernel\n");
164 goto exit;
165 }
166 }
167
168 // Get that moving
169 if ((error = clFlush(gQueue))) vlog("clFlush failed\n");
170
171 // Calculate the correctly rounded reference result
172 double *r = (double *)gOut_Ref;
173 double *s = (double *)gIn;
174 double *s2 = (double *)gIn2;
175 double *s3 = (double *)gIn3;
176 for (size_t j = 0; j < BUFFER_SIZE / sizeof(double); j++)
177 r[j] = (double)f->dfunc.f_fff(s[j], s2[j], s3[j]);
178
179 // Read the data back
180 for (auto j = gMinVectorSizeIndex; j < gMaxVectorSizeIndex; j++)
181 {
182 if ((error =
183 clEnqueueReadBuffer(gQueue, gOutBuffer[j], CL_TRUE, 0,
184 BUFFER_SIZE, gOut[j], 0, NULL, NULL)))
185 {
186 vlog_error("ReadArray failed %d\n", error);
187 goto exit;
188 }
189 }
190
191 if (gSkipCorrectnessTesting) break;
192
193 // Verify data -- No verification possible.
194 // MAD is a random number generator.
195 if (0 == (i & 0x0fffffff))
196 {
197 vlog(".");
198 fflush(stdout);
199 }
200 }
201
202 if (!gSkipCorrectnessTesting)
203 {
204 if (gWimpyMode)
205 vlog("Wimp pass");
206 else
207 vlog("passed");
208
209 vlog("\t%8.2f @ {%a, %a, %a}", maxError, maxErrorVal, maxErrorVal2,
210 maxErrorVal3);
211 }
212
213 vlog("\n");
214
215 exit:
216 // Release
217 for (auto k = gMinVectorSizeIndex; k < gMaxVectorSizeIndex; k++)
218 {
219 clReleaseKernel(kernels[k]);
220 }
221
222 return error;
223 }
224