Skip to content

Commit 7fd12b8

Browse files
committed
Merge branch 'master' of github.com:cirosantilli/cpp-cheat
2 parents ce5193c + 4a96135 commit 7fd12b8

12 files changed

Lines changed: 127 additions & 85 deletions

‎opencl/README.md‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
1. [Getting started](getting-started.md)
44
1. Examples
5-
1. [increment](inc.c)
6-
1. [Build error](build_error.c)
5+
1. [Increment](inc.c)
6+
1. [Increment vector](inc_vector.c)
77
1. [Pass by value](pass_by_value.c)
88
1. [Work item built-ins](work_item_builtin.c)
9-
1. [Increment vector](inc_vector.c)
109
1. [Vector built-in](vector_builtin.c)
1110
1. Tools
1211
1. [clinfo](clinfo.md)

‎opencl/build_error.c‎

Lines changed: 0 additions & 42 deletions
This file was deleted.

‎opencl/clinfo.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
`glxinfo` for OpenCL:
44

55
sudo clinfo
6+
7+
Upstream: <https://github.com/Oblomov/clinfo>

‎opencl/common.h‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef COMMON_H
2+
#define COMMON_H
3+
4+
#include <assert.h>
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
8+
#include <CL/cl.h>
9+
10+
void common_check_build(
11+
cl_int ret,
12+
cl_device_id device,
13+
cl_program program
14+
) {
15+
char *err;
16+
size_t err_len;
17+
if (CL_SUCCESS != ret) {
18+
clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 0, NULL, &err_len);
19+
err = malloc(err_len);
20+
clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, err_len, err, NULL);
21+
printf("Kernel build error:\n%s\n", err);
22+
free(err);
23+
exit(EXIT_FAILURE);
24+
}
25+
}
26+
27+
#endif

‎opencl/implementations.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ Threads:
3333
- <http://stackoverflow.com/questions/3271243/clcreatesubbuffer-not-found-oo>
3434
- <https://devtalk.nvidia.com/default/topic/486564/nvidia-39-s-opencl-1-1-and-clcreatesubbuffer/>
3535

36+
No OpenCL 2 planned as of 2016:
37+
38+
- <http://stackoverflow.com/questions/29219307/opencl-2-0-on-nvidia-graphics-cards>
39+
- <https://devtalk.nvidia.com/default/topic/954622/opencl-2-x-support-plans-/>
40+
41+
but hardware support will very likely be / is already there because of Vulkan / OpenCL 2 convergence.
42+
43+
Linux dependencies for 340.65:
44+
45+
- `libdl.so.2`
46+
- `libpthread.so.0`
47+
- `libc.so.6`
48+
3649
## Intel
3750

3851
<http://stackoverflow.com/questions/19141272/opencl-implementation-for-intel-core-i5-linux>

‎opencl/inc.c‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/*
22
Pass an int by reference and increment it.
3+
4+
This is our OpenCL hello world. No error checking for simplicity.
35
*/
46

57
#include <assert.h>
6-
#include <stdlib.h>
78
#include <stdio.h>
89

910
#include <CL/cl.h>
@@ -24,21 +25,30 @@ int main(void) {
2425
cl_platform_id platform;
2526
cl_program program;
2627

28+
/* Run kernel. */
2729
clGetPlatformIDs(1, &platform, NULL);
2830
clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 1, &device, NULL);
2931
context = clCreateContext(NULL, 1, &device, NULL, NULL, NULL);
30-
command_queue = clCreateCommandQueue(context, device, 0, NULL);
31-
buffer = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, sizeof(cl_int), &input, NULL);
3232
program = clCreateProgramWithSource(context, 1, &source, NULL, NULL);
33-
clBuildProgram(program, 1, &device, "", NULL, NULL);
33+
clBuildProgram(program, 1, &device, "", NULL, NULL);
3434
/* The name of the kernel function we want to call. */
3535
kernel = clCreateKernel(program, "increment", NULL);
36-
clSetKernelArg(kernel, 0, sizeof(cl_mem), &buffer);
36+
buffer = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, sizeof(cl_int), &input, NULL);
37+
clSetKernelArg(kernel, 0, sizeof(buffer), &buffer);
38+
command_queue = clCreateCommandQueue(context, device, 0, NULL);
3739
clEnqueueTask(command_queue, kernel, 0, NULL, NULL);
3840
clFlush(command_queue);
3941
clFinish(command_queue);
40-
clEnqueueReadBuffer(command_queue, buffer, CL_TRUE, 0, sizeof(cl_int), &input, 0, NULL, NULL);
42+
clEnqueueReadBuffer(command_queue, buffer, CL_TRUE, 0, sizeof(input), &input, 0, NULL, NULL);
4143

44+
/* Asserts. */
4245
assert(input == 2);
46+
47+
/* Cleanup. */
48+
clReleaseKernel(kernel);
49+
clReleaseProgram(program);
50+
clReleaseCommandQueue(command_queue);
51+
clReleaseContext(context);
52+
clReleaseMemObject(buffer);
4353
return EXIT_SUCCESS;
4454
}

‎opencl/inc_vector.c‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ it's just a clEnqueueNDRangeKernel + get_global_id hello world.
99
- http://stackoverflow.com/questions/15161575/reduction-for-sum-of-vector-when-size-is-not-power-of-2
1010
*/
1111

12-
#include <assert.h>
13-
#include <stdlib.h>
14-
15-
#include <CL/cl.h>
12+
#include "common.h"
1613

1714
int main(void) {
1815
const char *source =
@@ -22,28 +19,38 @@ int main(void) {
2219
cl_command_queue command_queue;
2320
cl_context context;
2421
cl_device_id device;
25-
cl_int input[] = {1, 2};
22+
cl_int input[] = {1, 2}, ret;
2623
const size_t global_work_size = sizeof(input) / sizeof(cl_int);
2724
cl_kernel kernel;
2825
cl_mem buffer;
2926
cl_platform_id platform;
3027
cl_program program;
3128

29+
/* Run kernel. */
3230
clGetPlatformIDs(1, &platform, NULL);
3331
clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 1, &device, NULL);
3432
context = clCreateContext(NULL, 1, &device, NULL, NULL, NULL);
35-
command_queue = clCreateCommandQueue(context, device, 0, NULL);
3633
buffer = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, sizeof(input), &input, NULL);
3734
program = clCreateProgramWithSource(context, 1, &source, NULL, NULL);
38-
clBuildProgram(program, 1, &device, "", NULL, NULL);
35+
ret = clBuildProgram(program, 1, &device, "", NULL, NULL);
36+
common_check_build(ret, device, program);
3937
kernel = clCreateKernel(program, "main", NULL);
4038
clSetKernelArg(kernel, 0, sizeof(cl_mem), &buffer);
39+
command_queue = clCreateCommandQueue(context, device, 0, NULL);
4140
clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, &global_work_size, NULL, 0, NULL, NULL);
4241
clFlush(command_queue);
4342
clFinish(command_queue);
4443
clEnqueueReadBuffer(command_queue, buffer, CL_TRUE, 0, sizeof(input), &input, 0, NULL, NULL);
4544

45+
/* Assertions. */
4646
assert(input[0] == 2);
4747
assert(input[1] == 3);
48+
49+
/* Cleanup. */
50+
clReleaseKernel(kernel);
51+
clReleaseProgram(program);
52+
clReleaseCommandQueue(command_queue);
53+
clReleaseContext(context);
54+
clReleaseMemObject(buffer);
4855
return EXIT_SUCCESS;
4956
}

‎opencl/pass_by_value.c‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
/*
2+
Kernel takes an integer value instead of a pointer.
3+
4+
cl_int is passed directly to clSetKernelArg instead of using
5+
a buffer obtained from clCreateBuffer.
6+
27
Increment a vector. It is useless to do this on a GPU, not enough work / IO.
38
*/
49

5-
#include <assert.h>
6-
#include <stdlib.h>
7-
#include <stdio.h>
8-
9-
#include <CL/cl.h>
10+
#include "common.h"
1011

1112
int main(void) {
1213
const char *source =
@@ -16,27 +17,37 @@ int main(void) {
1617
cl_command_queue command_queue;
1718
cl_context context;
1819
cl_device_id device;
19-
cl_int input = 1;
20+
cl_int input = 1, ret;
2021
cl_kernel kernel;
2122
cl_mem buffer;
2223
cl_platform_id platform;
2324
cl_program program;
2425

26+
/* Run kernel. */
2527
clGetPlatformIDs(1, &platform, NULL);
2628
clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 1, &device, NULL);
2729
context = clCreateContext(NULL, 1, &device, NULL, NULL, NULL);
28-
command_queue = clCreateCommandQueue(context, device, 0, NULL);
2930
buffer = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(cl_int), NULL, NULL);
3031
program = clCreateProgramWithSource(context, 1, &source, NULL, NULL);
31-
clBuildProgram(program, 1, &device, "", NULL, NULL);
32+
ret = clBuildProgram(program, 1, &device, "", NULL, NULL);
33+
common_check_build(ret, device, program);
3234
kernel = clCreateKernel(program, "main", NULL);
3335
clSetKernelArg(kernel, 0, sizeof(cl_int), &input);
3436
clSetKernelArg(kernel, 1, sizeof(cl_mem), &buffer);
37+
command_queue = clCreateCommandQueue(context, device, 0, NULL);
3538
clEnqueueTask(command_queue, kernel, 0, NULL, NULL);
3639
clFlush(command_queue);
3740
clFinish(command_queue);
3841
clEnqueueReadBuffer(command_queue, buffer, CL_TRUE, 0, sizeof(cl_int), &input, 0, NULL, NULL);
3942

43+
/* Assertions. */
4044
assert(input == 2);
45+
46+
/* Cleanup. */
47+
clReleaseKernel(kernel);
48+
clReleaseProgram(program);
49+
clReleaseCommandQueue(command_queue);
50+
clReleaseContext(context);
51+
clReleaseMemObject(buffer);
4152
return EXIT_SUCCESS;
4253
}

‎opencl/vector_builtin.c‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
/*
22
Increment a vector using a vector built-in type.
33
4-
TODO why does it not work?
4+
TODO not working.
55
66
http://stackoverflow.com/questions/13118228/how-to-pass-vector-paramater-to-opencl-kernel-in-c
77
*/
88

9-
#include <assert.h>
10-
#include <stdlib.h>
11-
12-
#include <CL/cl.h>
9+
#include "common.h"
1310

1411
int main(void) {
1512
const char *source =
@@ -19,30 +16,40 @@ int main(void) {
1916
cl_command_queue command_queue;
2017
cl_context context;
2118
cl_device_id device;
22-
cl_int input[] = {0, 1, 2, 3};
19+
cl_int input[] = {0, 1, 2, 3}, ret;
2320
const size_t global_work_size = sizeof(input) / sizeof(cl_int2);
2421
cl_kernel kernel;
2522
cl_mem buffer;
2623
cl_platform_id platform;
2724
cl_program program;
2825

26+
/* Run kernel. */
2927
clGetPlatformIDs(1, &platform, NULL);
3028
clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 1, &device, NULL);
3129
context = clCreateContext(NULL, 1, &device, NULL, NULL, NULL);
32-
command_queue = clCreateCommandQueue(context, device, 0, NULL);
3330
buffer = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, sizeof(input), &input, NULL);
3431
program = clCreateProgramWithSource(context, 1, &source, NULL, NULL);
35-
clBuildProgram(program, 1, &device, "", NULL, NULL);
32+
ret = clBuildProgram(program, 1, &device, "", NULL, NULL);
33+
common_check_build(ret, device, program);
3634
kernel = clCreateKernel(program, "main", NULL);
3735
clSetKernelArg(kernel, 0, sizeof(cl_mem), &buffer);
36+
command_queue = clCreateCommandQueue(context, device, 0, NULL);
3837
clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, &global_work_size, NULL, 0, NULL, NULL);
3938
clFlush(command_queue);
4039
clFinish(command_queue);
4140
clEnqueueReadBuffer(command_queue, buffer, CL_TRUE, 0, sizeof(input), &input, 0, NULL, NULL);
4241

42+
/* Assertions. */
4343
assert(input[0] == 1);
4444
assert(input[1] == 2);
4545
assert(input[2] == 3);
4646
assert(input[3] == 4);
47+
48+
/* Cleanup. */
49+
clReleaseKernel(kernel);
50+
clReleaseProgram(program);
51+
clReleaseCommandQueue(command_queue);
52+
clReleaseContext(context);
53+
clReleaseMemObject(buffer);
4754
return EXIT_SUCCESS;
4855
}

‎opencl/work_item_builtin.c‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
https://www.khronos.org/registry/cl/sdk/2.1/docs/man/xhtml/workItemFunctions.html
33
*/
44

5-
#include <assert.h>
6-
#include <stdlib.h>
7-
#include <stdio.h>
8-
9-
#include <CL/cl.h>
5+
#include "common.h"
106

117
#define NUM_FUNCTIONS (8)
128
/* Play around with those parameters. */
@@ -36,26 +32,30 @@ int main(void) {
3632
cl_command_queue command_queue;
3733
cl_context context;
3834
cl_device_id device;
39-
cl_uint output[NUM_FUNCTIONS];
35+
cl_int ret;
4036
cl_kernel kernel;
4137
cl_mem buffer;
4238
cl_platform_id platform;
4339
cl_program program;
40+
cl_uint output[NUM_FUNCTIONS];
4441

42+
/* Run kernel. */
4543
clGetPlatformIDs(1, &platform, NULL);
4644
clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 1, &device, NULL);
4745
context = clCreateContext(NULL, 1, &device, NULL, NULL, NULL);
48-
command_queue = clCreateCommandQueue(context, device, 0, NULL);
4946
buffer = clCreateBuffer(context, CL_MEM_WRITE_ONLY, NUM_FUNCTIONS * sizeof(cl_uint), NULL, NULL);
5047
program = clCreateProgramWithSource(context, 1, &source, NULL, NULL);
51-
clBuildProgram(program, 1, &device, "", NULL, NULL);
48+
ret = clBuildProgram(program, 1, &device, "", NULL, NULL);
49+
common_check_build(ret, device, program);
5250
kernel = clCreateKernel(program, "main", NULL);
5351
clSetKernelArg(kernel, 0, sizeof(cl_mem), &buffer);
52+
command_queue = clCreateCommandQueue(context, device, 0, NULL);
5453
clEnqueueNDRangeKernel(command_queue, kernel, 1, &offset, &global, &local, 0, NULL, NULL);
5554
clFlush(command_queue);
5655
clFinish(command_queue);
5756
clEnqueueReadBuffer(command_queue, buffer, CL_TRUE, 0, NUM_FUNCTIONS * sizeof(cl_uint), &output, 0, NULL, NULL);
5857

58+
/* Check the values. */
5959
printf("work_dim = %d\n", output[0]);
6060
printf("global_size = %d\n", output[1]);
6161
printf("global_id = %d\n", output[2]);
@@ -64,5 +64,12 @@ int main(void) {
6464
printf("num_groups = %d\n", output[5]);
6565
printf("group_id = %d\n", output[6]);
6666
printf("global_offset = %d\n", output[7]);
67+
68+
/* Cleanup. */
69+
clReleaseKernel(kernel);
70+
clReleaseProgram(program);
71+
clReleaseCommandQueue(command_queue);
72+
clReleaseContext(context);
73+
clReleaseMemObject(buffer);
6774
return EXIT_SUCCESS;
6875
}

0 commit comments

Comments
 (0)