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 "testBase.h"
17 #include "harness/conversions.h"
18
19 const char * atomic_index_source =
20 "#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable\n"
21 "// Counter keeps track of which index in counts we are using.\n"
22 "// We get that value, increment it, and then set that index in counts to our thread ID.\n"
23 "// At the end of this we should have all thread IDs in some random location in counts\n"
24 "// exactly once. If atom_add failed then we will write over various thread IDs and we\n"
25 "// will be missing some.\n"
26 "\n"
27 "__kernel void add_index_test(__global int *counter, __global int *counts) {\n"
28 " int tid = get_global_id(0);\n"
29 " \n"
30 " int counter_to_use = atom_add(counter, 1);\n"
31 " counts[counter_to_use] = tid;\n"
32 "}";
33
test_atomic_add_index(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)34 int test_atomic_add_index(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements)
35 {
36 clProgramWrapper program;
37 clKernelWrapper kernel;
38 clMemWrapper counter, counters;
39 size_t numGlobalThreads, numLocalThreads;
40 int fail = 0, succeed = 0, err;
41
42 /* Check if atomics are supported. */
43 if (!is_extension_available(deviceID, "cl_khr_global_int32_base_atomics")) {
44 log_info("Base atomics not supported (cl_khr_global_int32_base_atomics). Skipping test.\n");
45 return 0;
46 }
47
48 //===== add_index test
49 // The index test replicates what particles does.
50 // It uses one memory location to keep track of the current index and then each thread
51 // does an atomic add to it to get its new location. The threads then write to their
52 // assigned location. At the end we check to make sure that each thread's ID shows up
53 // exactly once in the output.
54
55 numGlobalThreads = 2048;
56
57 if( create_single_kernel_helper( context, &program, &kernel, 1, &atomic_index_source, "add_index_test" ) )
58 return -1;
59
60 if( get_max_common_work_group_size( context, kernel, numGlobalThreads, &numLocalThreads ) )
61 return -1;
62
63 log_info("Execute global_threads:%d local_threads:%d\n",
64 (int)numGlobalThreads, (int)numLocalThreads);
65
66 // Create the counter that will keep track of where each thread writes.
67 counter = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(cl_int) * 1,
68 NULL, NULL);
69 // Create the counters that will hold the results of each thread writing
70 // its ID into a (hopefully) unique location.
71 counters = clCreateBuffer(context, CL_MEM_READ_WRITE,
72 sizeof(cl_int) * numGlobalThreads, NULL, NULL);
73
74 // Reset all those locations to -1 to indciate they have not been used.
75 cl_int *values = (cl_int*) malloc(sizeof(cl_int)*numGlobalThreads);
76 if (values == NULL) {
77 log_error("add_index_test FAILED to allocate memory for initial values.\n");
78 fail = 1; succeed = -1;
79 } else {
80 memset(values, -1, numLocalThreads);
81 unsigned int i=0;
82 for (i=0; i<numGlobalThreads; i++)
83 values[i] = -1;
84 int init=0;
85 err = clEnqueueWriteBuffer(queue, counters, true, 0, numGlobalThreads*sizeof(cl_int), values, 0, NULL, NULL);
86 err |= clEnqueueWriteBuffer(queue, counter, true, 0,1*sizeof(cl_int), &init, 0, NULL, NULL);
87 if (err) {
88 log_error("add_index_test FAILED to write initial values to arrays: %d\n", err);
89 fail=1; succeed=-1;
90 } else {
91 err = clSetKernelArg(kernel, 0, sizeof(counter), &counter);
92 err |= clSetKernelArg(kernel, 1, sizeof(counters), &counters);
93 if (err) {
94 log_error("add_index_test FAILED to set kernel arguments: %d\n", err);
95 fail=1; succeed=-1;
96 } else {
97 err = clEnqueueNDRangeKernel( queue, kernel, 1, NULL, &numGlobalThreads, &numLocalThreads, 0, NULL, NULL );
98 if (err) {
99 log_error("add_index_test FAILED to execute kernel: %d\n", err);
100 fail=1; succeed=-1;
101 } else {
102 err = clEnqueueReadBuffer( queue, counters, true, 0, sizeof(cl_int)*numGlobalThreads, values, 0, NULL, NULL );
103 if (err) {
104 log_error("add_index_test FAILED to read back results: %d\n", err);
105 fail = 1; succeed=-1;
106 } else {
107 unsigned int looking_for, index;
108 for (looking_for=0; looking_for<numGlobalThreads; looking_for++) {
109 int instances_found=0;
110 for (index=0; index<numGlobalThreads; index++) {
111 if (values[index]==(int)looking_for)
112 instances_found++;
113 }
114 if (instances_found != 1) {
115 log_error("add_index_test FAILED: wrong number of instances (%d!=1) for counter %d.\n", instances_found, looking_for);
116 fail = 1; succeed=-1;
117 }
118 }
119 }
120 }
121 }
122 }
123 if (!fail) {
124 log_info("add_index_test passed. Each thread used exactly one index.\n");
125 }
126 free(values);
127 }
128 return fail;
129 }
130
131 const char *add_index_bin_kernel[] = {
132 "#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable\n"
133 "// This test assigns a bunch of values to bins and then tries to put them in the bins in parallel\n"
134 "// using an atomic add to keep track of the current location to write into in each bin.\n"
135 "// This is the same as the memory update for the particles demo.\n"
136 "\n"
137 "__kernel void add_index_bin_test(__global int *bin_counters, __global int *bins, __global int *bin_assignments, int max_counts_per_bin) {\n"
138 " int tid = get_global_id(0);\n"
139 "\n"
140 " int location = bin_assignments[tid];\n"
141 " int counter = atom_add(&bin_counters[location], 1);\n"
142 " bins[location*max_counts_per_bin + counter] = tid;\n"
143 "}" };
144
145 // This test assigns a bunch of values to bins and then tries to put them in the bins in parallel
146 // using an atomic add to keep track of the current location to write into in each bin.
147 // This is the same as the memory update for the particles demo.
add_index_bin_test(size_t * global_threads,cl_command_queue queue,cl_context context,MTdata d)148 int add_index_bin_test(size_t *global_threads, cl_command_queue queue, cl_context context, MTdata d)
149 {
150 int number_of_items = (int)global_threads[0];
151 size_t local_threads[1];
152 int divisor = 12;
153 int number_of_bins = number_of_items/divisor;
154 int max_counts_per_bin = divisor*2;
155
156 int fail = 0;
157 int succeed = 0;
158 int err;
159
160 clProgramWrapper program;
161 clKernelWrapper kernel;
162
163 // log_info("add_index_bin_test: %d items, into %d bins, with a max of %d items per bin (bins is %d long).\n",
164 // number_of_items, number_of_bins, max_counts_per_bin, number_of_bins*max_counts_per_bin);
165
166 //===== add_index_bin test
167 // The index test replicates what particles does.
168 err = create_single_kernel_helper(context, &program, &kernel, 1, add_index_bin_kernel, "add_index_bin_test" );
169 test_error( err, "Unable to create testing kernel" );
170
171 if( get_max_common_work_group_size( context, kernel, global_threads[0], &local_threads[0] ) )
172 return -1;
173
174 log_info("Execute global_threads:%d local_threads:%d\n",
175 (int)global_threads[0], (int)local_threads[0]);
176
177 // Allocate our storage
178 cl_mem bin_counters =
179 clCreateBuffer(context, CL_MEM_READ_WRITE,
180 sizeof(cl_int) * number_of_bins, NULL, NULL);
181 cl_mem bins = clCreateBuffer(
182 context, CL_MEM_READ_WRITE,
183 sizeof(cl_int) * number_of_bins * max_counts_per_bin, NULL, NULL);
184 cl_mem bin_assignments =
185 clCreateBuffer(context, CL_MEM_READ_ONLY,
186 sizeof(cl_int) * number_of_items, NULL, NULL);
187
188 if (bin_counters == NULL) {
189 log_error("add_index_bin_test FAILED to allocate bin_counters.\n");
190 return -1;
191 }
192 if (bins == NULL) {
193 log_error("add_index_bin_test FAILED to allocate bins.\n");
194 return -1;
195 }
196 if (bin_assignments == NULL) {
197 log_error("add_index_bin_test FAILED to allocate bin_assignments.\n");
198 return -1;
199 }
200
201 // Initialize our storage
202 cl_int *l_bin_counts = (cl_int*)malloc(sizeof(cl_int)*number_of_bins);
203 if (!l_bin_counts) {
204 log_error("add_index_bin_test FAILED to allocate initial values for bin_counters.\n");
205 return -1;
206 }
207 int i;
208 for (i=0; i<number_of_bins; i++)
209 l_bin_counts[i] = 0;
210 err = clEnqueueWriteBuffer(queue, bin_counters, true, 0, sizeof(cl_int)*number_of_bins, l_bin_counts, 0, NULL, NULL);
211 if (err) {
212 log_error("add_index_bin_test FAILED to set initial values for bin_counters: %d\n", err);
213 return -1;
214 }
215
216 cl_int *values = (cl_int*)malloc(sizeof(cl_int)*number_of_bins*max_counts_per_bin);
217 if (!values) {
218 log_error("add_index_bin_test FAILED to allocate initial values for bins.\n");
219 return -1;
220 }
221 for (i=0; i<number_of_bins*max_counts_per_bin; i++)
222 values[i] = -1;
223 err = clEnqueueWriteBuffer(queue, bins, true, 0, sizeof(cl_int)*number_of_bins*max_counts_per_bin, values, 0, NULL, NULL);
224 if (err) {
225 log_error("add_index_bin_test FAILED to set initial values for bins: %d\n", err);
226 return -1;
227 }
228 free(values);
229
230 cl_int *l_bin_assignments = (cl_int*)malloc(sizeof(cl_int)*number_of_items);
231 if (!l_bin_assignments) {
232 log_error("add_index_bin_test FAILED to allocate initial values for l_bin_assignments.\n");
233 return -1;
234 }
235 for (i=0; i<number_of_items; i++) {
236 int bin = random_in_range(0, number_of_bins-1, d);
237 while (l_bin_counts[bin] >= max_counts_per_bin) {
238 bin = random_in_range(0, number_of_bins-1, d);
239 }
240 if (bin >= number_of_bins)
241 log_error("add_index_bin_test internal error generating bin assignments: bin %d >= number_of_bins %d.\n", bin, number_of_bins);
242 if (l_bin_counts[bin]+1 > max_counts_per_bin)
243 log_error("add_index_bin_test internal error generating bin assignments: bin %d has more entries (%d) than max_counts_per_bin (%d).\n", bin, l_bin_counts[bin], max_counts_per_bin);
244 l_bin_counts[bin]++;
245 l_bin_assignments[i] = bin;
246 // log_info("item %d assigned to bin %d (%d items)\n", i, bin, l_bin_counts[bin]);
247 }
248 err = clEnqueueWriteBuffer(queue, bin_assignments, true, 0, sizeof(cl_int)*number_of_items, l_bin_assignments, 0, NULL, NULL);
249 if (err) {
250 log_error("add_index_bin_test FAILED to set initial values for bin_assignments: %d\n", err);
251 return -1;
252 }
253 // Setup the kernel
254 err = clSetKernelArg(kernel, 0, sizeof(bin_counters), &bin_counters);
255 err |= clSetKernelArg(kernel, 1, sizeof(bins), &bins);
256 err |= clSetKernelArg(kernel, 2, sizeof(bin_assignments), &bin_assignments);
257 err |= clSetKernelArg(kernel, 3, sizeof(max_counts_per_bin), &max_counts_per_bin);
258 if (err) {
259 log_error("add_index_bin_test FAILED to set kernel arguments: %d\n", err);
260 fail=1; succeed=-1;
261 return -1;
262 }
263
264 err = clEnqueueNDRangeKernel( queue, kernel, 1, NULL, global_threads, local_threads, 0, NULL, NULL );
265 if (err) {
266 log_error("add_index_bin_test FAILED to execute kernel: %d\n", err);
267 fail=1; succeed=-1;
268 }
269
270 cl_int *final_bin_assignments = (cl_int*)malloc(sizeof(cl_int)*number_of_bins*max_counts_per_bin);
271 if (!final_bin_assignments) {
272 log_error("add_index_bin_test FAILED to allocate initial values for final_bin_assignments.\n");
273 return -1;
274 }
275 err = clEnqueueReadBuffer( queue, bins, true, 0, sizeof(cl_int)*number_of_bins*max_counts_per_bin, final_bin_assignments, 0, NULL, NULL );
276 if (err) {
277 log_error("add_index_bin_test FAILED to read back bins: %d\n", err);
278 fail = 1; succeed=-1;
279 }
280
281 cl_int *final_bin_counts = (cl_int*)malloc(sizeof(cl_int)*number_of_bins);
282 if (!final_bin_counts) {
283 log_error("add_index_bin_test FAILED to allocate initial values for final_bin_counts.\n");
284 return -1;
285 }
286 err = clEnqueueReadBuffer( queue, bin_counters, true, 0, sizeof(cl_int)*number_of_bins, final_bin_counts, 0, NULL, NULL );
287 if (err) {
288 log_error("add_index_bin_test FAILED to read back bin_counters: %d\n", err);
289 fail = 1; succeed=-1;
290 }
291
292 // Verification.
293 int errors=0;
294 int current_bin;
295 int search;
296 // Print out all the contents of the bins.
297 // for (current_bin=0; current_bin<number_of_bins; current_bin++)
298 // for (search=0; search<max_counts_per_bin; search++)
299 // log_info("[bin %d, entry %d] = %d\n", current_bin, search, final_bin_assignments[current_bin*max_counts_per_bin+search]);
300
301 // First verify that there are the correct number in each bin.
302 for (current_bin=0; current_bin<number_of_bins; current_bin++) {
303 int expected_number = l_bin_counts[current_bin];
304 int actual_number = final_bin_counts[current_bin];
305 if (expected_number != actual_number) {
306 log_error("add_index_bin_test FAILED: bin %d reported %d entries when %d were expected.\n", current_bin, actual_number, expected_number);
307 errors++;
308 }
309 for (search=0; search<expected_number; search++) {
310 if (final_bin_assignments[current_bin*max_counts_per_bin+search] == -1) {
311 log_error("add_index_bin_test FAILED: bin %d had no entry at position %d when it should have had %d entries.\n", current_bin, search, expected_number);
312 errors++;
313 }
314 }
315 for (search=expected_number; search<max_counts_per_bin; search++) {
316 if (final_bin_assignments[current_bin*max_counts_per_bin+search] != -1) {
317 log_error("add_index_bin_test FAILED: bin %d had an extra entry at position %d when it should have had only %d entries.\n", current_bin, search, expected_number);
318 errors++;
319 }
320 }
321 }
322 // Now verify that the correct ones are in each bin
323 int index;
324 for (index=0; index<number_of_items; index++) {
325 int expected_bin = l_bin_assignments[index];
326 int found_it = 0;
327 for (search=0; search<l_bin_counts[expected_bin]; search++) {
328 if (final_bin_assignments[expected_bin*max_counts_per_bin+search] == index) {
329 found_it = 1;
330 }
331 }
332 if (found_it == 0) {
333 log_error("add_index_bin_test FAILED: did not find item %d in bin %d.\n", index, expected_bin);
334 errors++;
335 }
336 }
337 free(l_bin_counts);
338 free(l_bin_assignments);
339 free(final_bin_assignments);
340 free(final_bin_counts);
341 clReleaseMemObject(bin_counters);
342 clReleaseMemObject(bins);
343 clReleaseMemObject(bin_assignments);
344 if (errors == 0) {
345 log_info("add_index_bin_test passed. Each item was put in the correct bin in parallel.\n");
346 return 0;
347 } else {
348 log_error("add_index_bin_test FAILED: %d errors.\n", errors);
349 return -1;
350 }
351 }
352
test_atomic_add_index_bin(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)353 int test_atomic_add_index_bin(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements)
354 {
355 //===== add_index_bin test
356 size_t numGlobalThreads = 2048;
357 int iteration=0;
358 int err, failed = 0;
359 MTdata d = init_genrand( gRandomSeed );
360
361 /* Check if atomics are supported. */
362 if (!is_extension_available(deviceID, "cl_khr_global_int32_base_atomics")) {
363 log_info("Base atomics not supported (cl_khr_global_int32_base_atomics). Skipping test.\n");
364 free_mtdata( d );
365 return 0;
366 }
367
368 for(iteration=0; iteration<10; iteration++) {
369 log_info("add_index_bin_test with %d elements:\n", (int)numGlobalThreads);
370 err = add_index_bin_test(&numGlobalThreads, queue, context, d);
371 if (err) {
372 failed++;
373 break;
374 }
375 numGlobalThreads*=2;
376 }
377 free_mtdata( d );
378 return failed;
379 }
380
381
382