• 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 #include "harness/compat.h"
17 
18 #include <stdio.h>
19 #include <string.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 
23 #include "procs.h"
24 
25 static const char *smoothstep_kernel_code =
26 "__kernel void test_smoothstep(__global float *edge0, __global float *edge1, __global float *x, __global float *dst)\n"
27 "{\n"
28 "    int  tid = get_global_id(0);\n"
29 "\n"
30 "    dst[tid] = smoothstep(edge0[tid], edge1[tid], x[tid]);\n"
31 "}\n";
32 
33 static const char *smoothstep2_kernel_code =
34 "__kernel void test_smoothstep2(__global float2 *edge0, __global float2 *edge1, __global float2 *x, __global float2 *dst)\n"
35 "{\n"
36 "    int  tid = get_global_id(0);\n"
37 "\n"
38 "    dst[tid] = smoothstep(edge0[tid], edge1[tid], x[tid]);\n"
39 "}\n";
40 
41 static const char *smoothstep4_kernel_code =
42 "__kernel void test_smoothstep4(__global float4 *edge0, __global float4 *edge1, __global float4 *x, __global float4 *dst)\n"
43 "{\n"
44 "    int  tid = get_global_id(0);\n"
45 "\n"
46 "    dst[tid] = smoothstep(edge0[tid], edge1[tid], x[tid]);\n"
47 "}\n";
48 
49 static const char *smoothstep8_kernel_code =
50 "__kernel void test_smoothstep8(__global float8 *edge0, __global float8 *edge1, __global float8 *x, __global float8 *dst)\n"
51 "{\n"
52 "    int  tid = get_global_id(0);\n"
53 "\n"
54 "    dst[tid] = smoothstep(edge0[tid], edge1[tid], x[tid]);\n"
55 "}\n";
56 
57 static const char *smoothstep16_kernel_code =
58 "__kernel void test_smoothstep16(__global float16 *edge0, __global float16 *edge1, __global float16 *x, __global float16 *dst)\n"
59 "{\n"
60 "    int  tid = get_global_id(0);\n"
61 "\n"
62 "    dst[tid] = smoothstep(edge0[tid], edge1[tid], x[tid]);\n"
63 "}\n";
64 
65 static const char *smoothstep3_kernel_code =
66 "__kernel void test_smoothstep3(__global float *edge0, __global float *edge1, __global float *x, __global float *dst)\n"
67 "{\n"
68 "    int  tid = get_global_id(0);\n"
69 "\n"
70 "    vstore3(smoothstep(vload3(tid,edge0),vload3(tid,edge1),vload3(tid,x)), tid, dst);\n"
71 "}\n";
72 
73 #define MAX_ERR (1e-5f)
74 
75 static float
verify_smoothstep(float * edge0,float * edge1,float * x,float * outptr,int n)76 verify_smoothstep(float *edge0, float *edge1, float *x, float *outptr, int n)
77 {
78   float       r, t, delta, max_err = 0.0f;
79   int         i;
80 
81   for (i=0; i<n; i++)
82   {
83     t = (x[i] - edge0[i]) / (edge1[i] - edge0[i]);
84     if (t < 0.0f)
85       t = 0.0f;
86     else if (t > 1.0f)
87       t = 1.0f;
88     r = t * t * (3.0f - 2.0f * t);
89     delta = (float)fabs(r - outptr[i]);
90     if (delta > max_err)
91       max_err = delta;
92   }
93 
94   return max_err;
95 }
96 
97 const static char *fn_names[] = { "SMOOTHSTEP float", "SMOOTHSTEP float2", "SMOOTHSTEP float4", "SMOOTHSTEP float8", "SMOOTHSTEP float16", "SMOOTHSTEP float3" };
98 
99 int
test_smoothstep(cl_device_id device,cl_context context,cl_command_queue queue,int n_elems)100 test_smoothstep(cl_device_id device, cl_context context, cl_command_queue queue, int n_elems)
101 {
102   cl_mem      streams[4];
103   cl_float    *input_ptr[3], *output_ptr, *p, *p_edge0;
104   cl_program  program[kTotalVecCount];
105   cl_kernel   kernel[kTotalVecCount];
106   size_t  threads[1];
107   float max_err;
108   int num_elements;
109   int err;
110   int i;
111   MTdata d;
112 
113   num_elements = n_elems * 16;
114 
115   input_ptr[0] = (cl_float*)malloc(sizeof(cl_float) * num_elements);
116   input_ptr[1] = (cl_float*)malloc(sizeof(cl_float) * num_elements);
117   input_ptr[2] = (cl_float*)malloc(sizeof(cl_float) * num_elements);
118   output_ptr = (cl_float*)malloc(sizeof(cl_float) * num_elements);
119   streams[0] = clCreateBuffer(context, CL_MEM_READ_WRITE,
120                               sizeof(cl_float) * num_elements, NULL, NULL);
121   if (!streams[0])
122   {
123     log_error("clCreateBuffer failed\n");
124     return -1;
125   }
126   streams[1] = clCreateBuffer(context, CL_MEM_READ_WRITE,
127                               sizeof(cl_float) * num_elements, NULL, NULL);
128   if (!streams[1])
129   {
130     log_error("clCreateBuffer failed\n");
131     return -1;
132   }
133   streams[2] = clCreateBuffer(context, CL_MEM_READ_WRITE,
134                               sizeof(cl_float) * num_elements, NULL, NULL);
135   if (!streams[2])
136   {
137     log_error("clCreateBuffer failed\n");
138     return -1;
139   }
140 
141   streams[3] = clCreateBuffer(context, CL_MEM_READ_WRITE,
142                               sizeof(cl_float) * num_elements, NULL, NULL);
143   if (!streams[3])
144   {
145     log_error("clCreateBuffer failed\n");
146     return -1;
147   }
148 
149   p = input_ptr[0];
150   d = init_genrand( gRandomSeed );
151   for (i=0; i<num_elements; i++)
152   {
153     p[i] = get_random_float(-0x00400000, 0x00400000, d);
154   }
155 
156   p = input_ptr[1];
157   p_edge0 = input_ptr[0];
158   for (i=0; i<num_elements; i++)
159   {
160     float edge0 = p_edge0[i];
161     float edge1;
162     do {
163       edge1 = get_random_float(-0x00400000, 0x00400000, d);
164       if (edge0 < edge1)
165         break;
166     } while (1);
167     p[i] = edge1;
168   }
169 
170   p = input_ptr[2];
171   for (i=0; i<num_elements; i++)
172   {
173     p[i] = get_random_float(-0x00400000, 0x00400000, d);
174   }
175   free_mtdata(d);
176   d = NULL;
177 
178   err = clEnqueueWriteBuffer( queue, streams[0], true, 0, sizeof(cl_float)*num_elements, (void *)input_ptr[0], 0, NULL, NULL );
179   if (err != CL_SUCCESS)
180   {
181     log_error("clWriteArray failed\n");
182     return -1;
183   }
184   err = clEnqueueWriteBuffer( queue, streams[1], true, 0, sizeof(cl_float)*num_elements, (void *)input_ptr[1], 0, NULL, NULL );
185   if (err != CL_SUCCESS)
186   {
187     log_error("clWriteArray failed\n");
188     return -1;
189   }
190   err = clEnqueueWriteBuffer( queue, streams[2], true, 0, sizeof(cl_float)*num_elements, (void *)input_ptr[2], 0, NULL, NULL );
191   if (err != CL_SUCCESS)
192   {
193     log_error("clWriteArray failed\n");
194     return -1;
195   }
196 
197   err = create_single_kernel_helper( context, &program[0], &kernel[0], 1, &smoothstep_kernel_code, "test_smoothstep" );
198   if (err)
199     return -1;
200   err = create_single_kernel_helper( context, &program[1], &kernel[1], 1, &smoothstep2_kernel_code, "test_smoothstep2" );
201   if (err)
202     return -1;
203   err = create_single_kernel_helper( context, &program[2], &kernel[2], 1, &smoothstep4_kernel_code, "test_smoothstep4" );
204   if (err)
205     return -1;
206   err = create_single_kernel_helper( context, &program[3], &kernel[3], 1, &smoothstep8_kernel_code, "test_smoothstep8" );
207   if (err)
208     return -1;
209   err = create_single_kernel_helper( context, &program[4], &kernel[4], 1, &smoothstep16_kernel_code, "test_smoothstep16" );
210   if (err)
211     return -1;
212   err = create_single_kernel_helper( context, &program[5], &kernel[5], 1, &smoothstep3_kernel_code, "test_smoothstep3" );
213   if (err)
214     return -1;
215 
216   for (i=0; i<kTotalVecCount; i++)
217   {
218       err = clSetKernelArg(kernel[i], 0, sizeof streams[0], &streams[0] );
219       err |= clSetKernelArg(kernel[i], 1, sizeof streams[1], &streams[1] );
220       err |= clSetKernelArg(kernel[i], 2, sizeof streams[2], &streams[2] );
221       err |= clSetKernelArg(kernel[i], 3, sizeof streams[3], &streams[3] );
222       if (err != CL_SUCCESS)
223     {
224       log_error("clSetKernelArgs failed\n");
225       return -1;
226     }
227   }
228 
229 
230   threads[0] = (size_t)n_elems;
231   for (i=0; i<kTotalVecCount; i++)
232   {
233     err = clEnqueueNDRangeKernel( queue, kernel[i], 1, NULL, threads, NULL, 0, NULL, NULL );
234     if (err != CL_SUCCESS)
235     {
236       log_error("clEnqueueNDRangeKernel failed\n");
237       return -1;
238     }
239 
240 
241     err = clEnqueueReadBuffer( queue, streams[3], true, 0, sizeof(cl_float)*num_elements, (void *)output_ptr, 0, NULL, NULL );
242     if (err != CL_SUCCESS)
243     {
244       log_error("clEnqueueReadBuffer failed\n");
245       return -1;
246     }
247 
248     max_err = verify_smoothstep(input_ptr[0], input_ptr[1], input_ptr[2], output_ptr, n_elems * g_arrVecSizes[i]);
249 
250     if (max_err > MAX_ERR)
251     {
252       log_error("%s test failed %g max err\n", fn_names[i], max_err);
253       err = -1;
254     }
255     else
256     {
257       log_info("%s test passed %g max err\n", fn_names[i], max_err);
258       err = 0;
259     }
260 
261     if (err)
262       break;
263   }
264 
265   clReleaseMemObject(streams[0]);
266   clReleaseMemObject(streams[1]);
267   clReleaseMemObject(streams[2]);
268   clReleaseMemObject(streams[3]);
269   for (i=0; i<kTotalVecCount; i++)
270   {
271     clReleaseKernel(kernel[i]);
272     clReleaseProgram(program[i]);
273   }
274   free(input_ptr[0]);
275   free(input_ptr[1]);
276   free(input_ptr[2]);
277   free(output_ptr);
278 
279   return err;
280 }
281 
282 
283