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 *fmax_kernel_code =
26 "__kernel void test_fmax(__global float *srcA, __global float *srcB, __global float *dst)\n"
27 "{\n"
28 " int tid = get_global_id(0);\n"
29 " dst[tid] = fmax(srcA[tid], srcB[tid]);\n"
30 "}\n";
31
32 static const char *fmax2_kernel_code =
33 "__kernel void test_fmax2(__global float2 *srcA, __global float *srcB, __global float2 *dst)\n"
34 "{\n"
35 " int tid = get_global_id(0);\n"
36 " dst[tid] = fmax(srcA[tid], srcB[tid]);\n"
37 "}\n";
38
39 static const char *fmax4_kernel_code =
40 "__kernel void test_fmax4(__global float4 *srcA, __global float *srcB, __global float4 *dst)\n"
41 "{\n"
42 " int tid = get_global_id(0);\n"
43 " dst[tid] = fmax(srcA[tid], srcB[tid]);\n"
44 "}\n";
45
46 static const char *fmax8_kernel_code =
47 "__kernel void test_fmax8(__global float8 *srcA, __global float *srcB, __global float8 *dst)\n"
48 "{\n"
49 " int tid = get_global_id(0);\n"
50 " dst[tid] = fmax(srcA[tid], srcB[tid]);\n"
51 "}\n";
52
53 static const char *fmax16_kernel_code =
54 "__kernel void test_fmax16(__global float16 *srcA, __global float *srcB, __global float16 *dst)\n"
55 "{\n"
56 " int tid = get_global_id(0);\n"
57 " dst[tid] = fmax(srcA[tid], srcB[tid]);\n"
58 "}\n";
59
60 static const char *fmax3_kernel_code =
61 "__kernel void test_fmax3(__global float *srcA, __global float *srcB, __global float *dst)\n"
62 "{\n"
63 " int tid = get_global_id(0);\n"
64 " vstore3(fmax(vload3(tid,srcA), srcB[tid]),tid,dst);\n"
65 "}\n";
66
67 static int
verify_fmax(float * inptrA,float * inptrB,float * outptr,int n,int veclen)68 verify_fmax(float *inptrA, float *inptrB, float *outptr, int n, int veclen)
69 {
70 float r;
71 int i, j;
72
73 for (i=0; i<n; ) {
74 int ii = i/veclen;
75 for (j=0; j<veclen && i<n; ++j, ++i) {
76 r = (inptrA[i] >= inptrB[ii]) ? inptrA[i] : inptrB[ii];
77 if (r != outptr[i]) {
78 log_info("Verify noted discrepancy at %d (of %d) (vec %d, pos %d)\n",
79 i,n,ii,j);
80 log_info("SHould be %f, is %f\n", r, outptr[i]);
81 log_info("Taking max of (%f,%f)\n", inptrA[i], inptrB[i]);
82 return -1;
83 }
84 }
85 }
86
87 return 0;
88 }
89
90 int
test_fmaxf(cl_device_id device,cl_context context,cl_command_queue queue,int n_elems)91 test_fmaxf(cl_device_id device, cl_context context, cl_command_queue queue, int n_elems)
92 {
93 cl_mem streams[3];
94 cl_float *input_ptr[2], *output_ptr, *p;
95 cl_program *program;
96 cl_kernel *kernel;
97 void *values[3];
98 size_t threads[1];
99 int num_elements;
100 int err;
101 int i;
102 MTdata d;
103
104 program = (cl_program*)malloc(sizeof(cl_program)*kTotalVecCount);
105 kernel = (cl_kernel*)malloc(sizeof(cl_kernel)*kTotalVecCount);
106
107 num_elements = n_elems * (1 << (kTotalVecCount-1));
108
109 input_ptr[0] = (cl_float*)malloc(sizeof(cl_float) * num_elements);
110 input_ptr[1] = (cl_float*)malloc(sizeof(cl_float) * num_elements);
111 output_ptr = (cl_float*)malloc(sizeof(cl_float) * num_elements);
112 streams[0] = clCreateBuffer(context, CL_MEM_READ_WRITE,
113 sizeof(cl_float) * num_elements, NULL, NULL);
114 if (!streams[0])
115 {
116 log_error("clCreateBuffer failed\n");
117 return -1;
118 }
119 streams[1] =
120 clCreateBuffer(context, CL_MEM_READ_WRITE,
121 sizeof(cl_float) * num_elements, NULL, NULL);
122 if (!streams[1])
123 {
124 log_error("clCreateBuffer failed\n");
125 return -1;
126 }
127 streams[2] =
128 clCreateBuffer(context, CL_MEM_READ_WRITE,
129 sizeof(cl_float) * num_elements, NULL, NULL);
130 if (!streams[2])
131 {
132 log_error("clCreateBuffer failed\n");
133 return -1;
134 }
135
136 d = init_genrand( gRandomSeed );
137 p = input_ptr[0];
138 for (i=0; i<num_elements; i++)
139 {
140 p[i] = get_random_float(-0x20000000, 0x20000000, d);
141 }
142 p = input_ptr[1];
143 for (i=0; i<num_elements; i++)
144 {
145 p[i] = get_random_float(-0x20000000, 0x20000000, d);
146 }
147 free_mtdata(d); d = NULL;
148
149 err = clEnqueueWriteBuffer( queue, streams[0], true, 0, sizeof(cl_float)*num_elements,
150 (void *)input_ptr[0], 0, NULL, NULL );
151 if (err != CL_SUCCESS)
152 {
153 log_error("clWriteArray failed\n");
154 return -1;
155 }
156 err = clEnqueueWriteBuffer( queue, streams[1], true, 0, sizeof(cl_float)*num_elements,
157 (void *)input_ptr[1], 0, NULL, NULL );
158 if (err != CL_SUCCESS)
159 {
160 log_error("clWriteArray failed\n");
161 return -1;
162 }
163
164 err = create_single_kernel_helper( context, &program[0], &kernel[0], 1, &fmax_kernel_code, "test_fmax" );
165 if (err)
166 return -1;
167 err = create_single_kernel_helper( context, &program[1], &kernel[1], 1, &fmax2_kernel_code, "test_fmax2" );
168 if (err)
169 return -1;
170 err = create_single_kernel_helper( context, &program[2], &kernel[2], 1, &fmax4_kernel_code, "test_fmax4" );
171 if (err)
172 return -1;
173 err = create_single_kernel_helper( context, &program[3], &kernel[3], 1, &fmax8_kernel_code, "test_fmax8" );
174 if (err)
175 return -1;
176 err = create_single_kernel_helper( context, &program[4], &kernel[4], 1, &fmax16_kernel_code, "test_fmax16" );
177 if (err)
178 return -1;
179 err = create_single_kernel_helper( context, &program[5], &kernel[5], 1, &fmax3_kernel_code, "test_fmax3" );
180 if (err)
181 return -1;
182
183 values[0] = streams[0];
184 values[1] = streams[1];
185 values[2] = streams[2];
186 for (i=0; i < kTotalVecCount; i++)
187 {
188 err = clSetKernelArg(kernel[i], 0, sizeof streams[0], &streams[0] );
189 err |= clSetKernelArg(kernel[i], 1, sizeof streams[1], &streams[1] );
190 err |= clSetKernelArg(kernel[i], 2, sizeof streams[2], &streams[2] );
191 if (err != CL_SUCCESS)
192 {
193 log_error("clSetKernelArgs failed\n");
194 return -1;
195 }
196 }
197
198 threads[0] = (size_t)n_elems;
199 for (i=0; i < kTotalVecCount; i++)
200 {
201 err = clEnqueueNDRangeKernel( queue, kernel[i], 1, NULL, threads, NULL, 0, NULL, NULL );
202 if (err != CL_SUCCESS)
203 {
204 log_error("clEnqueueNDRangeKernel failed\n");
205 return -1;
206 }
207
208 err = clEnqueueReadBuffer(queue, streams[2], true, 0, sizeof(cl_float)*num_elements,
209 output_ptr, 0, NULL, NULL);
210 if (err != CL_SUCCESS)
211 {
212 log_error("clEnqueueReadBuffer failed\n");
213 return -1;
214 }
215
216 if (verify_fmax(input_ptr[0], input_ptr[1], output_ptr, n_elems*((g_arrVecSizes[i])), (g_arrVecSizes[i])))
217 {
218 log_error("FMAX float%d,float test failed\n", (g_arrVecSizes[i]));
219 err = -1;
220 }
221 else
222 {
223 log_info("FMAX float%d,float test passed\n", (g_arrVecSizes[i]));
224 err = 0;
225 }
226
227 if (err)
228 break;
229 }
230
231 clReleaseMemObject(streams[0]);
232 clReleaseMemObject(streams[1]);
233 clReleaseMemObject(streams[2]);
234 for (i=0; i < kTotalVecCount; i++)
235 {
236 clReleaseKernel(kernel[i]);
237 clReleaseProgram(program[i]);
238 }
239 free(program);
240 free(kernel);
241 free(input_ptr[0]);
242 free(input_ptr[1]);
243 free(output_ptr);
244
245 return err;
246 }
247
248
249