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