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