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 <stdlib.h>
20 #include <string.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23
24
25 #include "procs.h"
26
27 const char *barrier_with_localmem_kernel_code[] = {
28 "__kernel void compute_sum_with_localmem(__global int *a, int n, __local int *tmp_sum, __global int *sum)\n"
29 "{\n"
30 " int tid = get_local_id(0);\n"
31 " int lsize = get_local_size(0);\n"
32 " int i;\n"
33 "\n"
34 " tmp_sum[tid] = 0;\n"
35 " for (i=tid; i<n; i+=lsize)\n"
36 " tmp_sum[tid] += a[i];\n"
37 "\n"
38 " if( lsize == 1 )\n"
39 " {\n"
40 " if( tid == 0 )\n"
41 " *sum = tmp_sum[0];\n"
42 " return;\n"
43 " }\n"
44 "\n"
45 " do\n"
46 " {\n"
47 " barrier(CLK_LOCAL_MEM_FENCE);\n"
48 " if (tid < lsize/2)\n"
49 " {\n"
50 " int sum = tmp_sum[tid];\n"
51 " if( (lsize & 1) && tid == 0 )\n"
52 " sum += tmp_sum[tid + lsize - 1];\n"
53 " tmp_sum[tid] = sum + tmp_sum[tid + lsize/2];\n"
54 " }\n"
55 " lsize = lsize/2; \n"
56 " }while( lsize );\n"
57 "\n"
58 " if( tid == 0 )\n"
59 " *sum = tmp_sum[0];\n"
60 "}\n",
61 "__kernel void compute_sum_with_localmem(__global int *a, int n, __global int *sum)\n"
62 "{\n"
63 " __local int tmp_sum[%d];\n"
64 " int tid = get_local_id(0);\n"
65 " int lsize = get_local_size(0);\n"
66 " int i;\n"
67 "\n"
68 " tmp_sum[tid] = 0;\n"
69 " for (i=tid; i<n; i+=lsize)\n"
70 " tmp_sum[tid] += a[i];\n"
71 "\n"
72 " if( lsize == 1 )\n"
73 " {\n"
74 " if( tid == 0 )\n"
75 " *sum = tmp_sum[0];\n"
76 " return;\n"
77 " }\n"
78 "\n"
79 " do\n"
80 " {\n"
81 " barrier(CLK_LOCAL_MEM_FENCE);\n"
82 " if (tid < lsize/2)\n"
83 " {\n"
84 " int sum = tmp_sum[tid];\n"
85 " if( (lsize & 1) && tid == 0 )\n"
86 " sum += tmp_sum[tid + lsize - 1];\n"
87 " tmp_sum[tid] = sum + tmp_sum[tid + lsize/2];\n"
88 " }\n"
89 " lsize = lsize/2; \n"
90 " }while( lsize );\n"
91 "\n"
92 " if( tid == 0 )\n"
93 " *sum = tmp_sum[0];\n"
94 "}\n"
95 };
96
97 static int
verify_sum(int * inptr,int * outptr,int n)98 verify_sum(int *inptr, int *outptr, int n)
99 {
100 int r = 0;
101 int i;
102
103 for (i=0; i<n; i++)
104 {
105 r += inptr[i];
106 }
107
108 if (r != outptr[0])
109 {
110 log_error("LOCAL test failed: *%d vs %d\n", r, outptr[0] );
111 return -1;
112 }
113
114 log_info("LOCAL test passed\n");
115 return 0;
116 }
117
test_local_arg_def(cl_device_id device,cl_context context,cl_command_queue queue,int num_elements)118 int test_local_arg_def(cl_device_id device, cl_context context, cl_command_queue queue, int num_elements)
119 {
120 cl_mem streams[2];
121 cl_program program;
122 cl_kernel kernel;
123
124 cl_int *input_ptr, *output_ptr;
125 size_t global_threads[1], local_threads[1];
126 size_t wgsize, kwgsize;
127 size_t max_local_workgroup_size[3];
128 int err, i;
129 MTdata d = init_genrand( gRandomSeed );
130
131 err = clGetDeviceInfo(device, CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof wgsize, &wgsize, NULL);
132 if (err) {
133 log_error("clGetDeviceInfo failed, %d\n\n", err);
134 return -1;
135 }
136 wgsize/=2;
137 if (wgsize < 1)
138 wgsize = 1;
139
140 size_t in_length = sizeof(cl_int) * num_elements;
141 size_t out_length = sizeof(cl_int) * wgsize;
142
143 input_ptr = (cl_int *)malloc(in_length);
144 output_ptr = (cl_int *)malloc(out_length);
145
146 streams[0] = clCreateBuffer(context, CL_MEM_READ_WRITE, in_length, NULL, NULL);
147 if (!streams[0])
148 {
149 log_error("clCreateBuffer failed\n");
150 return -1;
151 }
152 streams[1] = clCreateBuffer(context, CL_MEM_READ_WRITE, out_length, NULL, NULL);
153 if (!streams[1])
154 {
155 log_error("clCreateBuffer failed\n");
156 return -1;
157 }
158
159 for (i=0; i<num_elements; i++)
160 input_ptr[i] = (int)genrand_int32(d);
161
162 free_mtdata(d); d = NULL;
163
164 err = clEnqueueWriteBuffer(queue, streams[0], CL_TRUE, 0, in_length, input_ptr, 0, NULL, NULL);
165 if (err != CL_SUCCESS)
166 {
167 log_error("clEnqueueWriteBuffer failed\n");
168 return -1;
169 }
170
171 err = create_single_kernel_helper(context, &program, &kernel, 1, &barrier_with_localmem_kernel_code[0], "compute_sum_with_localmem" );
172 if (err)
173 return -1;
174
175 err = clGetKernelWorkGroupInfo(kernel, device, CL_KERNEL_WORK_GROUP_SIZE, sizeof kwgsize, &kwgsize, NULL);
176 test_error(err, "clGetKernelWorkGroupInfo failed for CL_KERNEL_WORK_GROUP_SIZE");
177
178 err = clGetDeviceInfo(device, CL_DEVICE_MAX_WORK_ITEM_SIZES, sizeof(max_local_workgroup_size), max_local_workgroup_size, NULL);
179 test_error(err, "clGetDeviceInfo failed for CL_DEVICE_MAX_WORK_ITEM_SIZES");
180
181 // Pick the minimum of the device and the kernel
182 if (kwgsize > max_local_workgroup_size[0])
183 kwgsize = max_local_workgroup_size[0];
184
185 // err = clSetKernelArgs(context, kernel, 4, NULL, values, sizes);
186 err = clSetKernelArg(kernel, 0, sizeof(streams[0]), &streams[0]);
187 err |= clSetKernelArg(kernel, 1, sizeof num_elements, &num_elements);
188 err |= clSetKernelArg(kernel, 2, wgsize * sizeof(cl_int), NULL);
189 err |= clSetKernelArg(kernel, 3, sizeof streams[1], &streams[1]);
190 if (err != CL_SUCCESS)
191 {
192 log_error("clSetKernelArgs failed\n");
193 return -1;
194 }
195
196 global_threads[0] = wgsize;
197 local_threads[0] = wgsize;
198
199 // Adjust the local thread size to fit and be a nice multiple.
200 if (kwgsize < wgsize) {
201 log_info("Adjusting wgsize down from %lu to %lu.\n", wgsize, kwgsize);
202 local_threads[0] = kwgsize;
203 }
204 while (global_threads[0] % local_threads[0] != 0)
205 local_threads[0]--;
206
207 err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, global_threads, local_threads, 0, NULL, NULL);
208 if (err != CL_SUCCESS)
209 {
210 log_error("clEnqueueNDRangeKernel failed\n");
211 return -1;
212 }
213
214 err = clEnqueueReadBuffer(queue, streams[1], CL_TRUE, 0, out_length, output_ptr, 0, NULL, NULL);
215 if (err != CL_SUCCESS)
216 {
217 log_error("clEnqueueReadBuffer failed\n");
218 return -1;
219 }
220
221 err = verify_sum(input_ptr, output_ptr, num_elements);
222
223 // cleanup
224 clReleaseMemObject(streams[0]);
225 clReleaseMemObject(streams[1]);
226 clReleaseKernel(kernel);
227 clReleaseProgram(program);
228 free(input_ptr);
229 free(output_ptr);
230
231 return err;
232 }
233
test_local_kernel_def(cl_device_id device,cl_context context,cl_command_queue queue,int num_elements)234 int test_local_kernel_def(cl_device_id device, cl_context context, cl_command_queue queue, int num_elements)
235 {
236 cl_mem streams[2];
237 cl_program program;
238 cl_kernel kernel;
239
240 cl_int *input_ptr, *output_ptr;
241 size_t global_threads[1], local_threads[1];
242 size_t wgsize, kwgsize;
243 int err, i;
244 char *program_source = (char*)malloc(sizeof(char)*2048);
245 MTdata d = init_genrand( gRandomSeed );
246 size_t max_local_workgroup_size[3];
247 memset(program_source, 0, 2048);
248
249 err = clGetDeviceInfo(device, CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof wgsize, &wgsize, NULL);
250 if (err) {
251 log_error("clGetDeviceInfo failed, %d\n\n", err);
252 return -1;
253 }
254 wgsize/=2;
255 if (wgsize < 1)
256 wgsize = 1;
257
258 size_t in_length = sizeof(cl_int) * num_elements;
259 size_t out_length = sizeof(cl_int) * wgsize;
260
261 input_ptr = (cl_int *)malloc(in_length);
262 output_ptr = (cl_int *)malloc(out_length);
263
264 streams[0] = clCreateBuffer(context, CL_MEM_READ_WRITE, in_length, NULL, NULL);
265 if (!streams[0])
266 {
267 log_error("clCreateBuffer failed\n");
268 return -1;
269 }
270 streams[1] = clCreateBuffer(context, CL_MEM_READ_WRITE, out_length, NULL, NULL);
271 if (!streams[1])
272 {
273 log_error("clCreateBuffer failed\n");
274 return -1;
275 }
276
277 for (i=0; i<num_elements; i++)
278 input_ptr[i] = (cl_int) genrand_int32(d);
279
280 free_mtdata(d); d = NULL;
281
282 err = clEnqueueWriteBuffer(queue, streams[0], CL_TRUE, 0, in_length, input_ptr, 0, NULL, NULL);
283 if (err != CL_SUCCESS)
284 {
285 log_error("clEnqueueWriteBuffer failed\n");
286 return -1;
287 }
288
289 // Validate that created kernel doesn't violate local memory size allowed by the device
290 cl_ulong localMemSize = 0;
291 err = clGetDeviceInfo(device, CL_DEVICE_LOCAL_MEM_SIZE, sizeof(localMemSize), &localMemSize, NULL);
292 if (err != CL_SUCCESS)
293 {
294 log_error("clGetDeviceInfo failed\n");
295 return -1;
296 }
297 if ( wgsize > (localMemSize / (sizeof(cl_int)*sizeof(cl_int))) )
298 {
299 wgsize = localMemSize / (sizeof(cl_int)*sizeof(cl_int));
300 }
301
302 sprintf(program_source, barrier_with_localmem_kernel_code[1], (int)(wgsize * sizeof(cl_int)));
303
304 err = create_single_kernel_helper(context, &program, &kernel, 1, (const char**)&program_source, "compute_sum_with_localmem" );
305 free(program_source);
306 if (err)
307 return -1;
308
309 err = clGetKernelWorkGroupInfo(kernel, device, CL_KERNEL_WORK_GROUP_SIZE, sizeof kwgsize, &kwgsize, NULL);
310 test_error(err, "clGetKernelWorkGroupInfo failed for CL_KERNEL_WORK_GROUP_SIZE");
311
312 err = clGetDeviceInfo(device, CL_DEVICE_MAX_WORK_ITEM_SIZES, sizeof(max_local_workgroup_size), max_local_workgroup_size, NULL);
313 test_error(err, "clGetDeviceInfo failed for CL_DEVICE_MAX_WORK_ITEM_SIZES");
314
315 // Pick the minimum of the device and the kernel
316 if (kwgsize > max_local_workgroup_size[0])
317 kwgsize = max_local_workgroup_size[0];
318
319 // err = clSetKernelArgs(context, kernel, 4, NULL, values, sizes);
320 err = clSetKernelArg(kernel, 0, sizeof(streams[0]), &streams[0]);
321 err |= clSetKernelArg(kernel, 1, sizeof num_elements, &num_elements);
322 err |= clSetKernelArg(kernel, 2, sizeof streams[1], &streams[1]);
323 if (err != CL_SUCCESS)
324 {
325 log_error("clSetKernelArgs failed\n");
326 return -1;
327 }
328
329 global_threads[0] = wgsize;
330 local_threads[0] = wgsize;
331
332 // Adjust the local thread size to fit and be a nice multiple.
333 if (kwgsize < wgsize) {
334 log_info("Adjusting wgsize down from %lu to %lu.\n", wgsize, kwgsize);
335 local_threads[0] = kwgsize;
336 }
337 while (global_threads[0] % local_threads[0] != 0)
338 local_threads[0]--;
339
340 err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, global_threads, local_threads, 0, NULL, NULL);
341 if (err != CL_SUCCESS)
342 {
343 log_error("clEnqueueNDRangeKernel failed\n");
344 return -1;
345 }
346
347 err = clEnqueueReadBuffer(queue, streams[1], CL_TRUE, 0, out_length, output_ptr, 0, NULL, NULL);
348 if (err != CL_SUCCESS)
349 {
350 log_error("clEnqueueReadBuffer failed\n");
351 return -1;
352 }
353
354 err = verify_sum(input_ptr, output_ptr, num_elements);
355
356 // cleanup
357 clReleaseMemObject(streams[0]);
358 clReleaseMemObject(streams[1]);
359 clReleaseKernel(kernel);
360 clReleaseProgram(program);
361 free(input_ptr);
362 free(output_ptr);
363
364 return err;
365 }
366
367
368
369