Commit 750a8ce92eb4d66cc7f67243be27f0889d7074b7
0 parents
checkin of the relevant examples of the OpenCL cookbook
Showing
11 changed files
with
521 additions
and
0 deletions
Makefile
0 → 100644
1 | +CFLAGS += -O2 -march=native -std=c99 -I/usr/local/include | |
2 | +LIBS += -lcl -L/usr/local/lib64/beignet | |
3 | +CC = cc | |
4 | + | |
5 | +BINARIES = part1 part2 part3 part4 part5 part6 part8 | |
6 | + | |
7 | +all: $(BINARIES) | |
8 | + | |
9 | +%: %.c | |
10 | + $(CC) $(CFLAGS) $(LIBS) -o $@ $< | |
11 | + strip $@ | |
12 | + | |
13 | +.PHONY: clean | |
14 | + | |
15 | +clean: | |
16 | + rm $(BINARIES) | ... | ... |
README.md
0 → 100644
part1.c
0 → 100644
1 | +#include <stdio.h> | |
2 | +#include <stdlib.h> | |
3 | +#ifdef __APPLE__ | |
4 | +#include <OpenCL/opencl.h> | |
5 | +#else | |
6 | +#include <CL/cl.h> | |
7 | +#endif | |
8 | + | |
9 | +int main() { | |
10 | + | |
11 | + int i, j; | |
12 | + char* info; | |
13 | + size_t infoSize; | |
14 | + | |
15 | + cl_uint platformCount; | |
16 | + cl_platform_id *platforms; | |
17 | + | |
18 | + const char* attributeNames[5] = { | |
19 | + "Name", "Vendor", "Version", "Profile", "Extensions" }; | |
20 | + | |
21 | + const cl_platform_info attributeTypes[5] = { | |
22 | + CL_PLATFORM_NAME, CL_PLATFORM_VENDOR, CL_PLATFORM_VERSION, | |
23 | + CL_PLATFORM_PROFILE, CL_PLATFORM_EXTENSIONS }; | |
24 | + | |
25 | + const int attributeCount = sizeof(attributeNames) / sizeof(char*); | |
26 | + | |
27 | + // get platform count | |
28 | + clGetPlatformIDs(5, NULL, &platformCount); | |
29 | + // get all platforms | |
30 | + platforms = (cl_platform_id*) malloc(sizeof(cl_platform_id) * platformCount); | |
31 | + clGetPlatformIDs(platformCount, platforms, NULL); | |
32 | + | |
33 | + // for each platform print all attributes | |
34 | + for (i=0; i<platformCount; i++) { | |
35 | + | |
36 | + printf("\n %d. Platform \n", i+1); | |
37 | + | |
38 | + for (j=0; j<attributeCount; j++) { | |
39 | + | |
40 | + // get platform attribute value size | |
41 | + clGetPlatformInfo(platforms[i], attributeTypes[j], 0, NULL, &infoSize); | |
42 | + info = (char*) malloc(infoSize); | |
43 | + | |
44 | + // get platform attribute value | |
45 | + clGetPlatformInfo(platforms[i], attributeTypes[j], infoSize, info, NULL); | |
46 | + | |
47 | + printf(" %d.%d %-11s: %s\n", i+1, j+1, attributeNames[j], info); | |
48 | + free(info); | |
49 | + | |
50 | + } | |
51 | + | |
52 | + printf("\n"); | |
53 | + | |
54 | + } | |
55 | + | |
56 | + free(platforms); | |
57 | + return 0; | |
58 | +} | |
59 | + | |
60 | +// vim: set ft=c ts=4 sw=4: | ... | ... |
part2.c
0 → 100644
1 | +#include <stdio.h> | |
2 | +#include <stdlib.h> | |
3 | +#ifdef __APPLE__ | |
4 | +#include <OpenCL/opencl.h> | |
5 | +#else | |
6 | +#include <CL/cl.h> | |
7 | +#endif | |
8 | + | |
9 | +int main() { | |
10 | + | |
11 | + int i, j; | |
12 | + char* value; | |
13 | + size_t valueSize; | |
14 | + cl_uint platformCount; | |
15 | + cl_platform_id* platforms; | |
16 | + cl_uint deviceCount; | |
17 | + cl_device_id* devices; | |
18 | + cl_uint maxComputeUnits; | |
19 | + | |
20 | + // get all platforms | |
21 | + clGetPlatformIDs(0, NULL, &platformCount); | |
22 | + platforms = (cl_platform_id*) malloc(sizeof(cl_platform_id) * platformCount); | |
23 | + clGetPlatformIDs(platformCount, platforms, NULL); | |
24 | + | |
25 | + for (i = 0; i < platformCount; i++) { | |
26 | + | |
27 | + // get all devices | |
28 | + clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, 0, NULL, &deviceCount); | |
29 | + devices = (cl_device_id*) malloc(sizeof(cl_device_id) * deviceCount); | |
30 | + clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, deviceCount, devices, NULL); | |
31 | + | |
32 | + // for each device print critical attributes | |
33 | + for (j = 0; j < deviceCount; j++) { | |
34 | + | |
35 | + // print device name | |
36 | + clGetDeviceInfo(devices[j], CL_DEVICE_NAME, 0, NULL, &valueSize); | |
37 | + value = (char*) malloc(valueSize); | |
38 | + clGetDeviceInfo(devices[j], CL_DEVICE_NAME, valueSize, value, NULL); | |
39 | + printf("%d. Device: %s\n", j+1, value); | |
40 | + free(value); | |
41 | + | |
42 | + // print hardware device version | |
43 | + clGetDeviceInfo(devices[j], CL_DEVICE_VERSION, 0, NULL, &valueSize); | |
44 | + value = (char*) malloc(valueSize); | |
45 | + clGetDeviceInfo(devices[j], CL_DEVICE_VERSION, valueSize, value, NULL); | |
46 | + printf(" %d.%d Hardware version: %s\n", j+1, 1, value); | |
47 | + free(value); | |
48 | + | |
49 | + // print software driver version | |
50 | + clGetDeviceInfo(devices[j], CL_DRIVER_VERSION, 0, NULL, &valueSize); | |
51 | + value = (char*) malloc(valueSize); | |
52 | + clGetDeviceInfo(devices[j], CL_DRIVER_VERSION, valueSize, value, NULL); | |
53 | + printf(" %d.%d Software version: %s\n", j+1, 2, value); | |
54 | + free(value); | |
55 | + | |
56 | + // print c version supported by compiler for device | |
57 | + clGetDeviceInfo(devices[j], CL_DEVICE_OPENCL_C_VERSION, 0, NULL, &valueSize); | |
58 | + value = (char*) malloc(valueSize); | |
59 | + clGetDeviceInfo(devices[j], CL_DEVICE_OPENCL_C_VERSION, valueSize, value, NULL); | |
60 | + printf(" %d.%d OpenCL C version: %s\n", j+1, 3, value); | |
61 | + free(value); | |
62 | + | |
63 | + // print parallel compute units | |
64 | + clGetDeviceInfo(devices[j], CL_DEVICE_MAX_COMPUTE_UNITS, | |
65 | + sizeof(maxComputeUnits), &maxComputeUnits, NULL); | |
66 | + printf(" %d.%d Parallel compute units: %d\n", j+1, 4, maxComputeUnits); | |
67 | + | |
68 | + } | |
69 | + | |
70 | + free(devices); | |
71 | + | |
72 | + } | |
73 | + | |
74 | + free(platforms); | |
75 | + return 0; | |
76 | +} | |
77 | + | |
78 | +// vim: set ft=c ts=4 sw=4: | ... | ... |
part3.c
0 → 100644
1 | +#include <stdio.h> | |
2 | +#include <stdlib.h> | |
3 | +#ifdef __APPLE__ | |
4 | +#include <OpenCL/opencl.h> | |
5 | +#else | |
6 | +#include <CL/cl.h> | |
7 | +#endif | |
8 | + | |
9 | +int main() { | |
10 | + | |
11 | + cl_platform_id platform; | |
12 | + cl_device_id device; | |
13 | + cl_context context; | |
14 | + cl_uint refCount; | |
15 | + | |
16 | + // get first available platform | |
17 | + clGetPlatformIDs(1, &platform, NULL); | |
18 | + | |
19 | + // get first available gpu device | |
20 | + clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL); | |
21 | + | |
22 | + // create context | |
23 | + context = clCreateContext(NULL, 1, &device, NULL, NULL, NULL); | |
24 | + | |
25 | + // get context reference count | |
26 | + clGetContextInfo(context, CL_CONTEXT_REFERENCE_COUNT, | |
27 | + sizeof(refCount), &refCount, NULL); | |
28 | + printf("Ref count: %u ", refCount); | |
29 | + | |
30 | + // increment reference count | |
31 | + clRetainContext(context); | |
32 | + clGetContextInfo(context, CL_CONTEXT_REFERENCE_COUNT, | |
33 | + sizeof(refCount), &refCount, NULL); | |
34 | + printf(">> %u ", refCount); | |
35 | + | |
36 | + // decrement reference count | |
37 | + clReleaseContext(context); | |
38 | + clGetContextInfo(context, CL_CONTEXT_REFERENCE_COUNT, | |
39 | + sizeof(refCount), &refCount, NULL); | |
40 | + printf(">> %u ", refCount); | |
41 | + | |
42 | + // finally release context | |
43 | + clReleaseContext(context); | |
44 | + printf(">> 0\n"); | |
45 | + return 0; | |
46 | + | |
47 | +} | |
48 | + | |
49 | +// vim: set ft=c ts=4 sw=4: | ... | ... |
part4.c
0 → 100644
1 | +#include <stdio.h> | |
2 | +#include <stdlib.h> | |
3 | +#include <assert.h> | |
4 | +#ifdef __APPLE__ | |
5 | +#include <OpenCL/opencl.h> | |
6 | +#else | |
7 | +#include <CL/cl.h> | |
8 | +#endif | |
9 | + | |
10 | +#define KERNEL "part4.cl" | |
11 | + | |
12 | +int main() { | |
13 | + | |
14 | + cl_platform_id platform; | |
15 | + cl_device_id device; | |
16 | + cl_context context; | |
17 | + cl_program program; | |
18 | + | |
19 | + FILE* programHandle; | |
20 | + size_t programSize, kernelSourceSize; | |
21 | + char *programBuffer, *kernelSource; | |
22 | + | |
23 | + // get first available platform and gpu and create context | |
24 | + clGetPlatformIDs(1, &platform, NULL); | |
25 | + clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL); | |
26 | + context = clCreateContext(NULL, 1, &device, NULL, NULL, NULL); | |
27 | + | |
28 | + // get size of kernel source | |
29 | + programHandle = fopen(KERNEL, "r"); | |
30 | + fseek(programHandle, 0, SEEK_END); | |
31 | + programSize = ftell(programHandle); | |
32 | + rewind(programHandle); | |
33 | + | |
34 | + // read kernel source into buffer | |
35 | + programBuffer = (char*) malloc(programSize + 1); | |
36 | + programBuffer[programSize] = '\0'; | |
37 | + assert (programSize == fread(programBuffer, sizeof(char), programSize, programHandle)); | |
38 | + fclose(programHandle); | |
39 | + | |
40 | + // create program from buffer | |
41 | + program = clCreateProgramWithSource(context, 1, | |
42 | + (const char**) &programBuffer, &programSize, NULL); | |
43 | + free(programBuffer); | |
44 | + | |
45 | + // read kernel source back in from program to check | |
46 | + clGetProgramInfo(program, CL_PROGRAM_SOURCE, 0, NULL, &kernelSourceSize); | |
47 | + kernelSource = (char*) malloc(kernelSourceSize); | |
48 | + clGetProgramInfo(program, CL_PROGRAM_SOURCE, kernelSourceSize, kernelSource, NULL); | |
49 | + printf("\nKernel source:\n\n%s\n", kernelSource); | |
50 | + free(kernelSource); | |
51 | + | |
52 | + clReleaseContext(context); | |
53 | + return 0; | |
54 | + | |
55 | +} | |
56 | + | |
57 | +// vim: set ft=c ts=4 sw=4: | ... | ... |
part4.cl
0 → 100644
1 | +__kernel void hello(__global char* string) { | |
2 | + | |
3 | + string[0] = 'H'; | |
4 | + string[1] = 'e'; | |
5 | + string[2] = 'l'; | |
6 | + string[3] = 'l'; | |
7 | + string[4] = 'o'; | |
8 | + string[5] = ','; | |
9 | + string[6] = ' '; | |
10 | + string[7] = 'W'; | |
11 | + string[8] = 'o'; | |
12 | + string[9] = 'r'; | |
13 | + string[10] = 'l'; | |
14 | + string[11] = 'd'; | |
15 | + string[12] = '!'; | |
16 | + string[13] = 0; | |
17 | + | |
18 | +} | |
19 | + | |
20 | +// vim: ft=c ts=4 sw=4: | ... | ... |
part5.c
0 → 100644
1 | +#include <stdio.h> | |
2 | +#include <stdlib.h> | |
3 | +#include <assert.h> | |
4 | +#ifdef __APPLE__ | |
5 | +#include <OpenCL/opencl.h> | |
6 | +#else | |
7 | +#include <CL/cl.h> | |
8 | +#endif | |
9 | + | |
10 | +#define KERNEL "part4.cl" | |
11 | + | |
12 | +int main() { | |
13 | + | |
14 | + cl_platform_id platform; cl_device_id device; cl_context context; | |
15 | + cl_program program; cl_int error; cl_build_status status; | |
16 | + | |
17 | + FILE* programHandle; | |
18 | + char *programBuffer; char *programLog; | |
19 | + size_t programSize; size_t logSize; | |
20 | + | |
21 | + // get first available platform and gpu and create context | |
22 | + clGetPlatformIDs(1, &platform, NULL); | |
23 | + clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL); | |
24 | + context = clCreateContext(NULL, 1, &device, NULL, NULL, NULL); | |
25 | + | |
26 | + // get size of kernel source | |
27 | + programHandle = fopen(KERNEL, "r"); | |
28 | + fseek(programHandle, 0, SEEK_END); | |
29 | + programSize = ftell(programHandle); | |
30 | + rewind(programHandle); | |
31 | + | |
32 | + // read kernel source into buffer | |
33 | + programBuffer = (char*) malloc(programSize + 1); | |
34 | + programBuffer[programSize] = '\0'; | |
35 | + assert (programSize == fread(programBuffer, sizeof(char), programSize, programHandle)); | |
36 | + | |
37 | + fclose(programHandle); | |
38 | + | |
39 | + // create program from buffer | |
40 | + program = clCreateProgramWithSource(context, 1, | |
41 | + (const char**) &programBuffer, &programSize, NULL); | |
42 | + free(programBuffer); | |
43 | + | |
44 | + // build program | |
45 | + const char options[] = "-Werror -cl-std=CL1.1"; | |
46 | + error = clBuildProgram(program, 1, &device, options, NULL, NULL); | |
47 | + | |
48 | + // build failed | |
49 | + if (error != CL_SUCCESS) { | |
50 | + | |
51 | + // check build error and build status first | |
52 | + clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_STATUS, | |
53 | + sizeof(cl_build_status), &status, NULL); | |
54 | + | |
55 | + // check build log | |
56 | + clGetProgramBuildInfo(program, device, | |
57 | + CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize); | |
58 | + programLog = (char*) calloc (logSize+1, sizeof(char)); | |
59 | + clGetProgramBuildInfo(program, device, | |
60 | + CL_PROGRAM_BUILD_LOG, logSize+1, programLog, NULL); | |
61 | + printf("Build failed; error=%d, status=%d, programLog:\n\n%s", | |
62 | + error, status, programLog); | |
63 | + free(programLog); | |
64 | + | |
65 | + } | |
66 | + | |
67 | + clReleaseContext(context); | |
68 | + return 0; | |
69 | +} | |
70 | + | |
71 | +// vim: set ts=4 sw=4: | ... | ... |
part6.c
0 → 100644
1 | +#include <stdio.h> | |
2 | +#include <stdlib.h> | |
3 | +#include <assert.h> | |
4 | +#ifdef __APPLE__ | |
5 | +#include <OpenCL/opencl.h> | |
6 | +#else | |
7 | +#include <CL/cl.h> | |
8 | +#endif | |
9 | + | |
10 | +#define KERNEL "part4.cl" | |
11 | + | |
12 | +int main() { | |
13 | + | |
14 | + cl_platform_id platform; cl_device_id device; cl_context context; | |
15 | + cl_program program; cl_kernel kernel; cl_command_queue queue; | |
16 | + cl_mem kernelBuffer; | |
17 | + | |
18 | + FILE* programHandle; char *programBuffer; char *programLog; | |
19 | + size_t programSize; char hostBuffer[32]; | |
20 | + | |
21 | + // get first available sdk and gpu and create context | |
22 | + clGetPlatformIDs(1, &platform, NULL); | |
23 | + clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL); | |
24 | + context = clCreateContext(NULL, 1, &device, NULL, NULL, NULL); | |
25 | + | |
26 | + // get size of kernel source | |
27 | + programHandle = fopen(KERNEL, "r"); | |
28 | + fseek(programHandle, 0, SEEK_END); | |
29 | + programSize = ftell(programHandle); | |
30 | + rewind(programHandle); | |
31 | + | |
32 | + // read kernel source into buffer | |
33 | + programBuffer = (char*) malloc(programSize + 1); | |
34 | + programBuffer[programSize] = '\0'; | |
35 | + assert (programSize == fread(programBuffer, sizeof(char), programSize, programHandle)); | |
36 | + | |
37 | + fclose(programHandle); | |
38 | + | |
39 | + // create and build program | |
40 | + program = clCreateProgramWithSource(context, 1, | |
41 | + (const char**) &programBuffer, &programSize, NULL); | |
42 | + free(programBuffer); | |
43 | + clBuildProgram(program, 1, &device, "-Werror -cl-std=CL1.1", NULL, NULL); | |
44 | + | |
45 | + // create kernel and command queue | |
46 | + kernel = clCreateKernel(program, "hello", NULL); | |
47 | + queue = clCreateCommandQueue(context, device, 0, NULL); | |
48 | + | |
49 | + // create kernel argument buffer and set it into kernel | |
50 | + kernelBuffer = clCreateBuffer(context, CL_MEM_WRITE_ONLY, | |
51 | + 32 * sizeof(char), NULL, NULL); | |
52 | + clSetKernelArg(kernel, 0, sizeof(cl_mem), &kernelBuffer); | |
53 | + | |
54 | + // execute kernel, read back the output and print to screen | |
55 | + clEnqueueTask(queue, kernel, 0, NULL, NULL); | |
56 | + clEnqueueReadBuffer(queue, kernelBuffer, CL_TRUE, 0, | |
57 | + 32 * sizeof(char), hostBuffer, 0, NULL, NULL); | |
58 | + puts(hostBuffer); | |
59 | + | |
60 | + clFlush(queue); | |
61 | + clFinish(queue); | |
62 | + clReleaseKernel(kernel); | |
63 | + clReleaseProgram(program); | |
64 | + clReleaseMemObject(kernelBuffer); | |
65 | + clReleaseCommandQueue(queue); | |
66 | + clReleaseContext(context); | |
67 | + | |
68 | + return 0; | |
69 | + | |
70 | +} | |
71 | + | |
72 | +// vim: set ft=c ts=4 sw=4: | ... | ... |
part8.c
0 → 100644
1 | +#include <stdio.h> | |
2 | +#include <stdlib.h> | |
3 | +#include <assert.h> | |
4 | +#ifdef __APPLE__ | |
5 | +#include <OpenCL/opencl.h> | |
6 | +#else | |
7 | +#include <CL/cl.h> | |
8 | +#endif | |
9 | + | |
10 | +#define KERNEL "part8.cl" | |
11 | + | |
12 | +void cpu_3d_loop (int x, int y, int z) { | |
13 | + for (int i = 0; i < x; i++) { | |
14 | + for (int j = 0; j < y; j++) { | |
15 | + for (int k = 0; k < z; k++) { | |
16 | + printf("CPU %d,%d,%d\n", i, j, k); | |
17 | + } | |
18 | + } | |
19 | + } | |
20 | +} | |
21 | + | |
22 | +int main() { | |
23 | + | |
24 | + cl_platform_id platform; cl_device_id device; cl_context context; | |
25 | + cl_program program; cl_kernel kernel; cl_command_queue queue; | |
26 | + cl_mem kernelBuffer; | |
27 | + | |
28 | + FILE* programHandle; char *programBuffer; char *programLog; | |
29 | + size_t programSize; char hostBuffer[32]; | |
30 | + | |
31 | + int x = 4; | |
32 | + int y = 3; | |
33 | + int z = 2; | |
34 | + | |
35 | + cpu_3d_loop(x, y, z); | |
36 | + | |
37 | + // get first available sdk and gpu and create context | |
38 | + clGetPlatformIDs(1, &platform, NULL); | |
39 | + clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL); | |
40 | + context = clCreateContext(NULL, 1, &device, NULL, NULL, NULL); | |
41 | + | |
42 | + // get size of kernel source | |
43 | + programHandle = fopen(KERNEL, "r"); | |
44 | + fseek(programHandle, 0, SEEK_END); | |
45 | + programSize = ftell(programHandle); | |
46 | + rewind(programHandle); | |
47 | + | |
48 | + // read kernel source into buffer | |
49 | + programBuffer = (char*) malloc(programSize + 1); | |
50 | + programBuffer[programSize] = '\0'; | |
51 | + assert (programSize == fread(programBuffer, sizeof(char), programSize, programHandle)); | |
52 | + | |
53 | + fclose(programHandle); | |
54 | + | |
55 | + // create and build program | |
56 | + program = clCreateProgramWithSource(context, 1, | |
57 | + (const char**) &programBuffer, &programSize, NULL); | |
58 | + free(programBuffer); | |
59 | + clBuildProgram(program, 1, &device, "-Werror -cl-std=CL1.1", NULL, NULL); | |
60 | + | |
61 | + // create kernel and command queue | |
62 | + kernel = clCreateKernel(program, "ndrange_parallelism", NULL); | |
63 | + queue = clCreateCommandQueue(context, device, 0, NULL); | |
64 | + | |
65 | + size_t globalWorkSize[3] = {x, y, z}; | |
66 | + | |
67 | + // execute kernel, read back the output and print to screen | |
68 | + clEnqueueNDRangeKernel( | |
69 | + queue, kernel, 3, NULL, globalWorkSize, NULL, 0, NULL, NULL); | |
70 | + | |
71 | + clFlush(queue); | |
72 | + clFinish(queue); | |
73 | + clReleaseKernel(kernel); | |
74 | + clReleaseProgram(program); | |
75 | + clReleaseMemObject(kernelBuffer); | |
76 | + clReleaseCommandQueue(queue); | |
77 | + clReleaseContext(context); | |
78 | + | |
79 | + return 0; | |
80 | + | |
81 | +} | |
82 | + | |
83 | +// vim: set ft=c ts=4 sw=4: | ... | ... |
Please
register
or
login
to post a comment