/
histogram.cpp
678 lines (579 loc) · 23.3 KB
/
histogram.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
//
// Book: OpenCL(R) Programming Guide
// Authors: Aaftab Munshi, Benedict Gaster, Timothy Mattson, James Fung, Dan Ginsburg
// ISBN-10: 0-321-74964-2
// ISBN-13: 978-0-321-74964-2
// Publisher: Addison-Wesley Professional
// URLs: http://safari.informit.com/9780132488006/
// http://www.openclprogrammingguide.com
//
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#ifdef __APPLE__
#include <OpenCL/opencl.h>
#else
#include <CL/cl.h>
#endif
const char cl_kernel_histogram_filename[] = "histogram_image.cl";
const int num_pixels_per_work_item = 32;
static int num_iterations = 1000;
// fill an image of w x h pixels with 4-channels / pixel with random data
// each channel is an unisgned 8-bit value
//
static void *
create_image_data_unorm8(int w, int h)
{
unsigned char *p = (unsigned char *)malloc(w * h * 4);
int i;
for (i=0; i<w*h*4; i++)
p[i] = (unsigned char)(rand() & 0xFF);
return (void *)p;
}
// generate the reference results for unsigned 8-bit RGBA image.
// this reference result will be compared with histogram results generated by the OpenCL device.
//
static void *
generate_reference_histogram_results_unorm8(void *image_data, int w, int h)
{
unsigned int *ref_histogram_results = (unsigned int *)malloc(256 * 3 * sizeof(unsigned int));
unsigned char *img = (unsigned char *)image_data;
unsigned int *ptr = ref_histogram_results;
int i;
memset(ref_histogram_results, 0x0, 256 * 3 * sizeof(unsigned int));
for (i=0; i<w*h*4; i+=4)
{
int indx = img[i];
ptr[indx]++;
}
ptr += 256;
for (i=1; i<w*h*4; i+=4)
{
int indx = img[i];
ptr[indx]++;
}
ptr += 256;
for (i=2; i<w*h*4; i+=4)
{
int indx = img[i];
ptr[indx]++;
}
return ref_histogram_results;
}
// fill an image of w x h pixels with 4-channels / pixel with random data
// each channel is a single precision floating-point value
//
static void *
create_image_data_fp32(int w, int h)
{
float *p = (float *)malloc(w * h * 4 * sizeof(float));
int i;
for (i=0; i<w*h*4; i++)
p[i] = (float)rand() / (float)RAND_MAX;
return (void *)p;
}
// generate the reference results for floating-point RGBA image.
// this reference result will be compared with histogram results generated by the OpenCL device.
//
static void *
generate_reference_histogram_results_fp32(void *image_data, int w, int h)
{
unsigned int *ref_histogram_results = (unsigned int *)malloc(257 * 3 * sizeof(unsigned int));
float *img = (float *)image_data;
unsigned int *ptr = ref_histogram_results;
int i;
memset(ref_histogram_results, 0x0, 257 * 3 * sizeof(unsigned int));
for (i=0; i<w*h*4; i+=4)
{
float f = img[i];
unsigned int indx;
if (f > 1.0f)
f = 1.0f;
f *= 256.0f;
indx = (unsigned int)f;
ptr[indx]++;
}
ptr += 257;
for (i=1; i<w*h*4; i+=4)
{
float f = img[i];
unsigned int indx;
if (f > 1.0f)
f = 1.0f;
f *= 256.0f;
indx = (unsigned int)f;
ptr[indx]++;
}
ptr += 257;
for (i=2; i<w*h*4; i+=4)
{
float f = img[i];
unsigned int indx;
if (f > 1.0f)
f = 1.0f;
f *= 256.0f;
indx = (unsigned int)f;
ptr[indx]++;
}
return ref_histogram_results;
}
static int
verify_histogram_results(const char *str, unsigned int *histogram_results, unsigned int *ref_histogram_results, int num_entries)
{
int i;
for (i=0; i<num_entries; i++)
{
if (histogram_results[i] != ref_histogram_results[i])
{
printf("%s: verify_histogram_results failed for indx = %d, gpu result = %d, expected result = %d\n",
str, i, histogram_results[i], ref_histogram_results[i]);
return -1;
}
}
printf("%s: VERIFIED\n", str);
return 0;
}
static int
read_kernel_from_file(const char *filename, char **source, size_t *len)
{
struct stat statbuf;
FILE *fh;
size_t file_len;
fh = fopen(filename, "r");
if (fh == 0)
return -1;
stat(filename, &statbuf);
file_len = (size_t)statbuf.st_size;
*len = file_len;
*source = (char *) malloc(file_len+1);
fread(*source, file_len, 1, fh);
(*source)[file_len] = '\0';
fclose(fh);
return 0;
}
int
test_histogram(cl_context context, cl_command_queue queue, cl_device_id device)
{
cl_program program;
cl_kernel histogram_rgba_unorm8;
cl_kernel histogram_rgba_fp;
cl_kernel histogram_sum_partial_results_unorm8;
cl_kernel histogram_sum_partial_results_fp;
cl_image_format image_format;
int image_width = 1920;
int image_height = 1080;
size_t global_work_size[2];
size_t local_work_size[2];
size_t partial_global_work_size[2];
size_t partial_local_work_size[2];
size_t workgroup_size;
size_t num_groups;
unsigned int *ref_histogram_results, *histogram_results;
void *image_data_unorm8;
cl_mem input_image_unorm8;
void *image_data_fp32;
cl_mem input_image_fp32;
cl_mem histogram_buffer;
cl_mem partial_histogram_buffer;
cl_event events[2];
cl_ulong time_start, time_end;
size_t src_len[1];
char *source[1];
int i, err;
srand(0);
err = read_kernel_from_file(cl_kernel_histogram_filename, &source[0], &src_len[0]);
if(err)
{
printf("read_kernel_from_file() failed. (%s) file not found\n", cl_kernel_histogram_filename);
return EXIT_FAILURE;
}
program = clCreateProgramWithSource(context, 1, (const char **)source, (size_t *)src_len, &err);
if(!program || err)
{
printf("clCreateProgramWithSource() failed. (%d)\n", err);
return EXIT_FAILURE;
}
free(source[0]);
err = clBuildProgram(program, 1, &device, NULL, NULL, NULL);
if(err != CL_SUCCESS)
{
char buffer[2048] = "";
printf("clBuildProgram() failed.\n");
clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, NULL);
printf("Log:\n%s\n", buffer);
return EXIT_FAILURE;
}
histogram_rgba_unorm8 = clCreateKernel(program, "histogram_image_rgba_unorm8", &err);
if(!histogram_rgba_unorm8 || err)
{
printf("clCreateKernel() failed creating kernel void histogram_rgba_unorm8(). (%d)\n", err);
return EXIT_FAILURE;
}
histogram_rgba_fp = clCreateKernel(program, "histogram_image_rgba_fp", &err);
if(!histogram_rgba_fp || err)
{
printf("clCreateKernel() failed creating kernel void histogram_image_rgba_fp(). (%d)\n", err);
return EXIT_FAILURE;
}
histogram_sum_partial_results_unorm8 = clCreateKernel(program, "histogram_sum_partial_results_unorm8", &err);
if(!histogram_sum_partial_results_unorm8 || err)
{
printf("clCreateKernel() failed creating kernel void histogram_sum_partial_results_unorm8(). (%d)\n", err);
return EXIT_FAILURE;
}
histogram_sum_partial_results_fp = clCreateKernel(program, "histogram_sum_partial_results_fp", &err);
if(!histogram_sum_partial_results_fp || err)
{
printf("clCreateKernel() failed creating kernel void histogram_sum_partial_results_fp(). (%d)\n", err);
return EXIT_FAILURE;
}
histogram_buffer = clCreateBuffer(context, CL_MEM_WRITE_ONLY, 257*3*sizeof(unsigned int), NULL, &err);
if (!histogram_buffer || err)
{
printf("clCreateBuffer() failed. (%d)\n", err);
return EXIT_FAILURE;
}
image_format.image_channel_order = CL_RGBA;
image_format.image_channel_data_type = CL_UNORM_INT8;
image_data_unorm8 = create_image_data_unorm8(image_width, image_height);
input_image_unorm8 = clCreateImage2D(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
&image_format, image_width, image_height, 0, image_data_unorm8, &err);
if (!input_image_unorm8 || err)
{
printf("clCreateImage2D() failed. (%d)\n", err);
return EXIT_FAILURE;
}
image_format.image_channel_order = CL_RGBA;
image_format.image_channel_data_type = CL_FLOAT;
image_data_fp32 = create_image_data_fp32(image_width, image_height);
input_image_fp32 = clCreateImage2D(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
&image_format, image_width, image_height, 0, image_data_fp32, &err);
if (!input_image_fp32 || err)
{
printf("clCreateImage2D() failed. (%d)\n", err);
return EXIT_FAILURE;
}
/************ Testing RGBA 8-bit histogram **********/
clGetKernelWorkGroupInfo(histogram_rgba_unorm8, device, CL_KERNEL_WORK_GROUP_SIZE, sizeof(size_t), &workgroup_size, NULL);
{
size_t gsize[2];
int w;
if (workgroup_size <= 256)
{
gsize[0] = 16;
gsize[1] = workgroup_size / 16;
}
else if (workgroup_size <= 1024)
{
gsize[0] = workgroup_size / 16;
gsize[1] = 16;
}
else
{
gsize[0] = workgroup_size / 32;
gsize[1] = 32;
}
local_work_size[0] = gsize[0];
local_work_size[1] = gsize[1];
w = (image_width + num_pixels_per_work_item - 1) / num_pixels_per_work_item;
global_work_size[0] = ((w + gsize[0] - 1) / gsize[0]);
global_work_size[1] = ((image_height + gsize[1] - 1) / gsize[1]);
num_groups = global_work_size[0] * global_work_size[1];
global_work_size[0] *= gsize[0];
global_work_size[1] *= gsize[1];
}
partial_histogram_buffer = clCreateBuffer(context, CL_MEM_READ_WRITE, num_groups*257*3*sizeof(unsigned int), NULL, &err);
if (!partial_histogram_buffer || err)
{
printf("clCreateBuffer() failed. (%d)\n", err);
return EXIT_FAILURE;
}
clSetKernelArg(histogram_rgba_unorm8, 0, sizeof(cl_mem), &input_image_unorm8);
clSetKernelArg(histogram_rgba_unorm8, 1, sizeof(int), &num_pixels_per_work_item);
clSetKernelArg(histogram_rgba_unorm8, 2, sizeof(cl_mem), &partial_histogram_buffer);
clSetKernelArg(histogram_sum_partial_results_unorm8, 0, sizeof(cl_mem), &partial_histogram_buffer);
clSetKernelArg(histogram_sum_partial_results_unorm8, 1, sizeof(int), &num_groups);
clSetKernelArg(histogram_sum_partial_results_unorm8, 2, sizeof(cl_mem), &histogram_buffer);
// verify that the kernel works correctly. also acts as a warmup
err = clEnqueueNDRangeKernel(queue, histogram_rgba_unorm8, 2, NULL, global_work_size, local_work_size, 0, NULL, NULL);
if (err)
{
printf("clEnqueueNDRangeKernel() failed for histogram_rgba_unorm8 kernel. (%d)\n", err);
return EXIT_FAILURE;
}
// verify that the kernel works correctly. also acts as a warmup
clGetKernelWorkGroupInfo(histogram_sum_partial_results_unorm8, device, CL_KERNEL_WORK_GROUP_SIZE, sizeof(size_t), &workgroup_size, NULL);
if (workgroup_size < 256)
{
printf("A min. of 256 work-items in work-group is needed for histogram_sum_partial_results_unorm8 kernel. (%d)\n", (int)workgroup_size);
return EXIT_FAILURE;
}
partial_global_work_size[0] = 256*3;
partial_local_work_size[0] = (workgroup_size > 256) ? 256 : workgroup_size;
err = clEnqueueNDRangeKernel(queue, histogram_sum_partial_results_unorm8, 1, NULL, partial_global_work_size, partial_local_work_size, 0, NULL, NULL);
if (err)
{
printf("clEnqueueNDRangeKernel() failed for histogram_sum_partial_results_unorm8 kernel. (%d)\n", err);
return EXIT_FAILURE;
}
ref_histogram_results = (unsigned int *)generate_reference_histogram_results_unorm8(image_data_unorm8, image_width, image_height);
histogram_results = (unsigned int *)malloc(257*3*sizeof(unsigned int));
err = clEnqueueReadBuffer(queue, histogram_buffer, CL_TRUE, 0, 256*3*sizeof(unsigned int), histogram_results, 0, NULL, NULL);
if (err)
{
printf("clEnqueueReadBuffer() failed. (%d)\n", err);
return EXIT_FAILURE;
}
verify_histogram_results("Image Histogram for image type = CL_RGBA, CL_UNORM_INT8", histogram_results, ref_histogram_results, 256*3);
// now measure performance
err = clEnqueueMarker(queue, &events[0]);
if (err)
{
printf("clEnqeueMarker() failed for histogram_rgba_unorm8 kernel. (%d)\n", err);
return EXIT_FAILURE;
}
for (i=0; i<num_iterations; i++)
{
err = clEnqueueNDRangeKernel(queue, histogram_rgba_unorm8, 2, NULL, global_work_size, local_work_size, 0, NULL, NULL);
if (err)
{
printf("clEnqueueNDRangeKernel() failed for histogram_rgba_unorm8 kernel. (%d)\n", err);
return EXIT_FAILURE;
}
err = clEnqueueNDRangeKernel(queue, histogram_sum_partial_results_unorm8, 1, NULL, partial_global_work_size, partial_local_work_size, 0, NULL, NULL);
if (err)
{
printf("clEnqueueNDRangeKernel() failed for histogram_sum_partial_results_unorm8 kernel. (%d)\n", err);
return EXIT_FAILURE;
}
}
err = clEnqueueMarker(queue, &events[1]);
if (err)
{
printf("clEnqeueMarker() failed for histogram_rgba_unorm8 kernel. (%d)\n", err);
return EXIT_FAILURE;
}
err = clWaitForEvents(1, &events[1]);
if (err)
{
printf("clWaitForEvents() failed for histogram_rgba_unorm8 kernel. (%d)\n", err);
return EXIT_FAILURE;
}
err = clGetEventProfilingInfo(events[0], CL_PROFILING_COMMAND_QUEUED, sizeof(cl_long), &time_start, NULL);
err |= clGetEventProfilingInfo(events[1], CL_PROFILING_COMMAND_END, sizeof(cl_long), &time_end, NULL);
if (err)
{
printf("clGetEventProfilingInfo() failed for histogram_rgba_unorm8 kernel. (%d)\n", err);
return EXIT_FAILURE;
}
printf("Image dimensions: %d x %d pixels, Image type = CL_RGBA, CL_UNORM_INT8\n", image_width, image_height);
printf("Time to compute histogram = %g ms\n", (double)(time_end - time_start) * 1e-9 * 1000.0 / (double)num_iterations);
clReleaseEvent(events[0]);
clReleaseEvent(events[1]);
/************ Testing RGBA 32-bit fp histogram **********/
clGetKernelWorkGroupInfo(histogram_rgba_fp, device, CL_KERNEL_WORK_GROUP_SIZE, sizeof(size_t), &workgroup_size, NULL);
{
size_t gsize[2];
int w;
if (workgroup_size <= 256)
{
gsize[0] = 16;
gsize[1] = workgroup_size / 16;
}
else if (workgroup_size <= 1024)
{
gsize[0] = workgroup_size / 16;
gsize[1] = 16;
}
else
{
gsize[0] = workgroup_size / 32;
gsize[1] = 32;
}
local_work_size[0] = gsize[0];
local_work_size[1] = gsize[1];
w = (image_width + num_pixels_per_work_item - 1) / num_pixels_per_work_item;
global_work_size[0] = ((w + gsize[0] - 1) / gsize[0]);
global_work_size[1] = ((image_height + gsize[1] - 1) / gsize[1]);
num_groups = global_work_size[0] * global_work_size[1];
global_work_size[0] *= gsize[0];
global_work_size[1] *= gsize[1];
}
partial_histogram_buffer = clCreateBuffer(context, CL_MEM_READ_WRITE, num_groups*257*3*sizeof(unsigned int), NULL, &err);
if (!partial_histogram_buffer || err)
{
printf("clCreateBuffer() failed. (%d)\n", err);
return EXIT_FAILURE;
}
clSetKernelArg(histogram_rgba_fp, 0, sizeof(cl_mem), &input_image_fp32);
clSetKernelArg(histogram_rgba_fp, 1, sizeof(int), &num_pixels_per_work_item);
clSetKernelArg(histogram_rgba_fp, 2, sizeof(cl_mem), &partial_histogram_buffer);
clSetKernelArg(histogram_sum_partial_results_fp, 0, sizeof(cl_mem), &partial_histogram_buffer);
clSetKernelArg(histogram_sum_partial_results_fp, 1, sizeof(int), &num_groups);
clSetKernelArg(histogram_sum_partial_results_fp, 2, sizeof(cl_mem), &histogram_buffer);
// verify that the kernel works correctly. also acts as a warmup
err = clEnqueueNDRangeKernel(queue, histogram_rgba_fp, 2, NULL, global_work_size, local_work_size, 0, NULL, NULL);
if (err)
{
printf("clEnqueueNDRangeKernel() failed for histogram_rgba_fp kernel. (%d)\n", err);
return EXIT_FAILURE;
}
// verify that the kernel works correctly. also acts as a warmup
clGetKernelWorkGroupInfo(histogram_sum_partial_results_fp, device, CL_KERNEL_WORK_GROUP_SIZE, sizeof(size_t), &workgroup_size, NULL);
if (workgroup_size < 256)
{
printf("A min. of 256 work-items in work-group is needed for histogram_sum_partial_results_fp kernel. (%d)\n", (int)workgroup_size);
return EXIT_FAILURE;
}
partial_global_work_size[0] = 256*3;
partial_local_work_size[0] = (workgroup_size > 256) ? 256 : workgroup_size;
err = clEnqueueNDRangeKernel(queue, histogram_sum_partial_results_fp, 1, NULL, partial_global_work_size, partial_local_work_size, 0, NULL, NULL);
if (err)
{
printf("clEnqueueNDRangeKernel() failed for histogram_sum_partial_results_fp kernel. (%d)\n", err);
return EXIT_FAILURE;
}
ref_histogram_results = (unsigned int *)generate_reference_histogram_results_fp32(image_data_fp32, image_width, image_height);
err = clEnqueueReadBuffer(queue, histogram_buffer, CL_TRUE, 0, 257*3*sizeof(unsigned int), histogram_results, 0, NULL, NULL);
if (err)
{
printf("clEnqueueReadBuffer() failed. (%d)\n", err);
return EXIT_FAILURE;
}
verify_histogram_results("Image Histogram for image type = CL_RGBA, CL_FLOAT", histogram_results, ref_histogram_results, 257*3);
// now measure performance
err = clEnqueueMarker(queue, &events[0]);
if (err)
{
printf("clEnqeueMarker() failed for histogram_rgba_fp kernel. (%d)\n", err);
return EXIT_FAILURE;
}
for (i=0; i<num_iterations; i++)
{
err = clEnqueueNDRangeKernel(queue, histogram_rgba_fp, 2, NULL, global_work_size, local_work_size, 0, NULL, NULL);
if (err)
{
printf("clEnqueueNDRangeKernel() failed for histogram_rgba_fp kernel. (%d)\n", err);
return EXIT_FAILURE;
}
err = clEnqueueNDRangeKernel(queue, histogram_sum_partial_results_fp, 1, NULL, partial_global_work_size, partial_local_work_size, 0, NULL, NULL);
if (err)
{
printf("clEnqueueNDRangeKernel() failed for histogram_sum_partial_results_fp kernel. (%d)\n", err);
return EXIT_FAILURE;
}
}
err = clEnqueueMarker(queue, &events[1]);
if (err)
{
printf("clEnqeueMarker() failed for histogram_rgba_fp kernel. (%d)\n", err);
return EXIT_FAILURE;
}
err = clWaitForEvents(1, &events[1]);
if (err)
{
printf("clWaitForEvents() failed for histogram_rgba_fp kernel. (%d)\n", err);
return EXIT_FAILURE;
}
err = clGetEventProfilingInfo(events[0], CL_PROFILING_COMMAND_QUEUED, sizeof(cl_long), &time_start, NULL);
err |= clGetEventProfilingInfo(events[1], CL_PROFILING_COMMAND_END, sizeof(cl_long), &time_end, NULL);
if (err)
{
printf("clGetEventProfilingInfo() failed for histogram_rgba_fp kernel. (%d)\n", err);
return EXIT_FAILURE;
}
printf("Image dimensions: %d x %d pixels, Image type = CL_RGBA, CL_FLOAT\n", image_width, image_height);
printf("Time to compute histogram = %g ms\n", (double)(time_end - time_start) * 1e-9 * 1000.0 / (double)num_iterations);
clReleaseEvent(events[0]);
clReleaseEvent(events[1]);
free(ref_histogram_results);
free(histogram_results);
free(image_data_unorm8);
free(image_data_fp32);
clReleaseKernel(histogram_rgba_unorm8);
clReleaseKernel(histogram_rgba_fp);
clReleaseKernel(histogram_sum_partial_results_unorm8);
clReleaseKernel(histogram_sum_partial_results_fp);
clReleaseProgram(program);
clReleaseMemObject(partial_histogram_buffer);
clReleaseMemObject(histogram_buffer);
clReleaseMemObject(input_image_unorm8);
clReleaseMemObject(input_image_fp32);
return EXIT_SUCCESS;
}
int
main(int argc, char **argv)
{
cl_device_id device;
cl_context context;
cl_command_queue queue;
int err;
cl_device_type device_type = CL_DEVICE_TYPE_GPU;
#if (__APPLE__) || defined(__MACOSX)
cl_platform_id platform = NULL;
#else
cl_platform_id platform = NULL;
err = clGetPlatformIDs(1, &platform, NULL);
if(err != CL_SUCCESS)
{
printf("clGetPlatformIDs() failed. (%d)\n", err);
return EXIT_FAILURE;
}
#endif
err = clGetDeviceIDs(platform, device_type, 1, &device, NULL);
if(err != CL_SUCCESS)
{
printf("clGetDeviceIDs() failed. (%d)\n", err);
return EXIT_FAILURE;
}
// Dump device information
char deviceName[ 512 ];
char deviceVendor[ 512 ];
char deviceVersion[ 512 ];
err = clGetDeviceInfo(device, CL_DEVICE_VENDOR, sizeof( deviceVendor ),
deviceVendor, NULL);
err |= clGetDeviceInfo(device, CL_DEVICE_NAME, sizeof( deviceName ),
deviceName, NULL);
err |= clGetDeviceInfo(device, CL_DEVICE_VERSION, sizeof( deviceVersion ),
deviceVersion, NULL);
printf("OpenCL Device Vendor = %s, OpenCL Device Name = %s, OpenCL Device Version = %s\n", deviceVendor, deviceName, deviceVersion);
size_t ext_size = 0;
err |= clGetDeviceInfo(device, CL_DEVICE_EXTENSIONS, 0, NULL, &ext_size);
if (err) {
printf("clGetDeviceInfo() failed. (%d)\n", err);
return EXIT_FAILURE;
}
// Check if 32 bit local atomics are supported
char* ext_string = (char*) malloc(ext_size+1);
clGetDeviceInfo(device, CL_DEVICE_EXTENSIONS, ext_size+1, ext_string, NULL);
if (ext_string == NULL) {
printf("clGetDeviceInfo() failed. (%d)\n", err);
return EXIT_FAILURE;
}
if (!strstr(ext_string, "cl_khr_local_int32_base_atomics")) {
free(ext_string);
printf("Skipping: histogram requires local atomics support\n");
return EXIT_SUCCESS;
}
free(ext_string);
context = clCreateContext( 0, 1, &device, NULL, NULL, &err);
if (!context || err)
{
printf("clCreateContext() failed. (%d)\n", err);
return EXIT_FAILURE;
}
queue = clCreateCommandQueue( context, device, CL_QUEUE_PROFILING_ENABLE, &err);
if(!queue || err)
{
printf("clCreateCommandQueue() failed. (%d)\n", err);
return EXIT_FAILURE;
}
if (test_histogram(context, queue, device) == EXIT_FAILURE)
return EXIT_FAILURE;
clReleaseCommandQueue(queue);
clReleaseContext(context);
return EXIT_SUCCESS;
}