-
Notifications
You must be signed in to change notification settings - Fork 0
/
opencl.h
178 lines (139 loc) · 4.88 KB
/
opencl.h
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
#ifndef OPENCL_H
#define OPENCL_H
#define CL_USE_DEPRECATED_OPENCL_2_0_APIS 1
#include <CL/cl.h>
#include <stdio.h>
#include "shared.h"
#include "engine_platform.h"
void checkErr(cl_int err, const char * name) {
if (err != CL_SUCCESS) {
fprintf(stderr, "ERROR: %s %d\n", name, err);
exit(EXIT_FAILURE);
assert(false);
}
}
struct OpenclState
{
cl_context context;
cl_uint deviceIdCount;
cl_device_id* deviceIds;
} OpenclState;
cl_kernel clCreteKernel(struct OpenclState* clstate, const char* kernelFile)
{
cl_int error;
PlatformFileHandle fh = Platform.openFile(kernelFile);
if(fh.noErrors)
{
size_t size = (size_t)Platform.getFileSize(&fh);
void* fmem = malloc(size);
Platform.readFromFile(&fh, 0, size, fmem);
// build kernel
cl_program program = clCreateProgramWithSource (clstate->context, 1, (const char**)&fmem, (size_t*)&size, &error);
checkErr(error,"clCreateProgramWithSource");
checkErr(clBuildProgram (program, clstate->deviceIdCount, clstate->deviceIds, 0, 0, 0), "cBuildProgram");
cl_kernel kernel = clCreateKernel (program, "SAXPY", &error);
checkErr (error, "clCreateKernel");
free(fmem);
printf("Kernel file loaded!\n");
return kernel;
}
else
{
printf("Failed to open kernel file %s\n",kernelFile);
INVALID_CODE_PATH
cl_kernel kernel;
return kernel;
}
}
u32 inticl(void)
{
struct OpenclState clstate;
cl_uint platformIdCount = 0;
clGetPlatformIDs (0, 0, &platformIdCount);
cl_platform_id* platformIds = (cl_platform_id*)malloc(sizeof(cl_platform_id)*platformIdCount);
clGetPlatformIDs (platformIdCount, platformIds, 0);
clGetDeviceIDs (platformIds [0], CL_DEVICE_TYPE_ALL, 0, 0, &clstate.deviceIdCount);
clstate.deviceIds = (cl_device_id*) malloc(sizeof(cl_device_id)*clstate.deviceIdCount);
clGetDeviceIDs (platformIds [0], CL_DEVICE_TYPE_ALL, clstate.deviceIdCount, clstate.deviceIds, 0);
const cl_context_properties contextProperties [] =
{
CL_CONTEXT_PLATFORM,
(cl_context_properties)(platformIds [0]),
0, 0
};
cl_int error;
clstate.context = clCreateContext (contextProperties, clstate.deviceIdCount, clstate.deviceIds, 0, 0, &error);
checkErr(error, "create context");
for(int i = 0; i < platformIdCount; i++)
{
size_t size = 0;
clGetPlatformInfo (platformIds[i], CL_PLATFORM_NAME, 0, 0, &size);
char* result = (char*)malloc(size);
clGetPlatformInfo (platformIds[i], CL_PLATFORM_NAME, size, result, 0);
printf("platform %s\n", result);
free(result);
}
for(int i = 0; i < clstate.deviceIdCount; i++)
{
size_t size;
clGetDeviceInfo(clstate.deviceIds[i], CL_DEVICE_NAME, 0, 0, &size);
char* result = (char*)malloc(size);
clGetDeviceInfo(clstate.deviceIds[i], CL_DEVICE_NAME, size, result, 0);
printf("device %s\n", result);
free(result);
}
size_t testDataSize = 1 << 10;
float a[testDataSize]; // c99 heheeh xd
float b[testDataSize];
for (int i = 0; i < testDataSize; ++i) {
a [i] = (r32) (23 ^ i);
b [i] = (r32) (42 ^ i);
}
cl_mem aBuffer = clCreateBuffer (clstate.context,
CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
sizeof(float) * testDataSize,
a, &error);
checkErr(error, "clCreateBuffer");
cl_mem bBuffer = clCreateBuffer (clstate.context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
sizeof (float) * testDataSize,
b, &error);
checkErr (error, "clCreateBuffer");
char* kernelFile = "kernels/saxpy.cl";
cl_kernel kernel = clCreteKernel(&clstate, kernelFile);
clSetKernelArg (kernel, 0, sizeof (cl_mem), &aBuffer);
clSetKernelArg (kernel, 1, sizeof (cl_mem), &bBuffer);
static const float two = 2.0f;
clSetKernelArg (kernel, 2, sizeof (float), &two);
const size_t globalWorkSize [] = { testDataSize, 0, 0 };
cl_command_queue queue = clCreateCommandQueue (clstate.context, clstate.deviceIds [0],
0, &error);
checkErr (error,"clCreateCommandQueue");
for(int i = 0; i < testDataSize; i++)
{
printf("before: %f \n", b[i]);
}
clEnqueueNDRangeKernel (queue, kernel,
1, // One dimension
0,
globalWorkSize,
0,
0, 0, 0);
clEnqueueReadBuffer (queue, bBuffer, CL_TRUE, 0,
sizeof (float) * testDataSize,
b,
0, 0, 0);
for(int i = 0; i < testDataSize; i++)
{
printf("after: %f \n", b[i]);
}
clReleaseCommandQueue (queue);
clReleaseMemObject (bBuffer);
clReleaseMemObject (aBuffer);
clReleaseKernel (kernel);
//clReleaseProgram (program); // TODO
clReleaseContext (clstate.context);
free(platformIds);
free(clstate.deviceIds);
return 0;
}
#endif // OPENCL_H