part8.c
2.12 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#ifdef __APPLE__
#include <OpenCL/opencl.h>
#else
#include <CL/cl.h>
#endif
#define KERNEL "part8.cl"
void cpu_3d_loop (int x, int y, int z) {
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
for (int k = 0; k < z; k++) {
printf("CPU %d,%d,%d\n", i, j, k);
}
}
}
}
int main() {
cl_platform_id platform; cl_device_id device; cl_context context;
cl_program program; cl_kernel kernel; cl_command_queue queue;
cl_mem kernelBuffer;
FILE* programHandle; char *programBuffer; char *programLog;
size_t programSize; char hostBuffer[32];
int x = 4;
int y = 3;
int z = 2;
cpu_3d_loop(x, y, z);
// get first available sdk and gpu and create context
clGetPlatformIDs(1, &platform, NULL);
clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL);
context = clCreateContext(NULL, 1, &device, NULL, NULL, NULL);
// get size of kernel source
programHandle = fopen(KERNEL, "r");
fseek(programHandle, 0, SEEK_END);
programSize = ftell(programHandle);
rewind(programHandle);
// read kernel source into buffer
programBuffer = (char*) malloc(programSize + 1);
programBuffer[programSize] = '\0';
assert (programSize == fread(programBuffer, sizeof(char), programSize, programHandle));
fclose(programHandle);
// create and build program
program = clCreateProgramWithSource(context, 1,
(const char**) &programBuffer, &programSize, NULL);
free(programBuffer);
clBuildProgram(program, 1, &device, "-Werror -cl-std=CL1.1", NULL, NULL);
// create kernel and command queue
kernel = clCreateKernel(program, "ndrange_parallelism", NULL);
queue = clCreateCommandQueue(context, device, 0, NULL);
size_t globalWorkSize[3] = {x, y, z};
// execute kernel, read back the output and print to screen
clEnqueueNDRangeKernel(
queue, kernel, 3, NULL, globalWorkSize, NULL, 0, NULL, NULL);
clFlush(queue);
clFinish(queue);
clReleaseKernel(kernel);
clReleaseProgram(program);
clReleaseMemObject(kernelBuffer);
clReleaseCommandQueue(queue);
clReleaseContext(context);
return 0;
}
// vim: set ft=c ts=4 sw=4: