-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcl_util.c
423 lines (321 loc) · 12.4 KB
/
cl_util.c
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
#include "cl_util.h"
///////////////////////////////////////////////////////////////////////////////
// Prints out the name of the input platform.
void PrintPlatformName(cl_platform_id platform)
{
cl_uint clError = 0;
size_t nameSize = 0;
char* platformName = NULL;
clError = clGetPlatformInfo(platform, CL_PLATFORM_NAME, 0, NULL, &nameSize);
CHECK_OCL_ERR("clGetPlatformInfo", clError);
platformName = (char*)malloc(nameSize * sizeof(char));
CHECK_NULL(platformName);
clError = clGetPlatformInfo(platform, CL_PLATFORM_NAME, nameSize, platformName, NULL);
CHECK_OCL_ERR("clGetPlatformInfo", clError);
printf("%s", platformName);
free(platformName);
}
///////////////////////////////////////////////////////////////////////////////
// Prints out the name of the input device.
void PrintDeviceName(cl_device_id device)
{
cl_uint clError = 0;
size_t nameSize = 0;
char* deviceName = NULL;
clError = clGetDeviceInfo(device, CL_DEVICE_NAME, 0, NULL, &nameSize);
CHECK_OCL_ERR("clGetDeviceInfo", clError);
deviceName = (char*)malloc(nameSize * sizeof(char));
CHECK_NULL(deviceName);
clError = clGetDeviceInfo(device, CL_DEVICE_NAME, nameSize, deviceName, NULL);
CHECK_OCL_ERR("clGetDeviceInfo", clError);
printf("%s", deviceName);
free(deviceName);
}
///////////////////////////////////////////////////////////////////////////////
// Prints out detailed information about OpenCL.
int PrintOpenCLInfo()
{
cl_uint numPlatforms = 0;
cl_platform_id *platforms = 0;
cl_int clError = 0;
clError = clGetPlatformIDs(0, NULL, &numPlatforms);
CHECK_OCL_ERR("clGetPlatformIDs", clError);
if (0 >= numPlatforms)
return 0;
printf("\nOpenCL platforms detected: %d", numPlatforms);
platforms = (cl_platform_id*)malloc(numPlatforms * sizeof(cl_platform_id));
CHECK_NULL(platforms);
clError = clGetPlatformIDs(numPlatforms, platforms, NULL);
CHECK_OCL_ERR("clGetPlatformIDs", clError);
for (cl_uint i = 0; i < numPlatforms; i++)
{
cl_uint numDevices = 0;
cl_device_id *devices = NULL;
printf("\n%d. Platform: ", i + 1);
PrintPlatformName(platforms[i]);
clError = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, 0, NULL, &numDevices);
CHECK_OCL_ERR("clGetDeviceIDs", clError);
printf("\n\tNumber of devices: %d", numDevices);
devices = (cl_device_id*)malloc(numDevices * sizeof(cl_device_id));
CHECK_NULL(devices);
clError = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, numDevices, devices, NULL);
CHECK_OCL_ERR("clGetDeviceIDs", clError);
for (cl_uint j = 0; j < numDevices; j++)
{
printf("\n\t%d. Device: ", j + 1);
PrintDeviceName(devices[j]);
cl_device_type deviceType;
clError = clGetDeviceInfo(devices[j], CL_DEVICE_TYPE, sizeof(deviceType), &deviceType, NULL);
CHECK_OCL_ERR("clGetDeviceInfo", clError);
switch (deviceType)
{
case CL_DEVICE_TYPE_CPU:
printf("\n\t\tType: CPU");
break;
case CL_DEVICE_TYPE_GPU:
printf("\n\t\tType: GPU");
break;
case CL_DEVICE_TYPE_ACCELERATOR:
printf("\n\t\tType: ACCELERATOR");
break;
default:
printf("\n\t\tType: Unknown");
break;
}
cl_uint cuCnt = 0;
clError = clGetDeviceInfo(devices[j], CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(cuCnt), &cuCnt, NULL);
CHECK_OCL_ERR("clGetDeviceInfo", clError);
printf("\n\t\tNumber of CUs: %d", cuCnt);
}
if (devices)
free(devices);
}
if (platforms)
free(platforms);
return numPlatforms;
}
///////////////////////////////////////////////////////////////////////////////
// Returns a platform and device id as selected by the user.
void SelectOpenCLPlatformAndDevice(cl_platform_id* pPlatform, cl_device_id* pDevice)
{
cl_uint numPlatforms = 0;
cl_uint numDevices = 0;
cl_uint clError = 0;
int platformIndex = 0;
int deviceIndex = 0;
cl_platform_id *platforms = 0;
cl_device_id* devices = 0;
CHECK_NULL(pPlatform);
CHECK_NULL(pDevice);
platforms = NULL;
devices = NULL;
clError = clGetPlatformIDs(0, NULL, &numPlatforms);
CHECK_OCL_ERR("clGetPlatformIDs", clError);
platforms = (cl_platform_id*)malloc(numPlatforms * sizeof(cl_platform_id));
CHECK_NULL(platforms);
clError = clGetPlatformIDs(numPlatforms, platforms, NULL);
CHECK_OCL_ERR("clGetPlatformIDs", clError);
printf("\n\nSelect platform to use [%d-%d]:", 1, numPlatforms);
scanf("%d", &platformIndex);
platformIndex--;
*pPlatform = platforms[platformIndex];
clError = clGetDeviceIDs(platforms[platformIndex], CL_DEVICE_TYPE_ALL, 0, NULL, &numDevices);
CHECK_OCL_ERR("clGetDeviceIDs", clError);
devices = (cl_device_id*)malloc(numDevices * sizeof(cl_device_id));
CHECK_NULL(devices);
clError = clGetDeviceIDs(platforms[platformIndex], CL_DEVICE_TYPE_ALL, numDevices, devices, NULL);
CHECK_OCL_ERR("clGetDeviceIDs", clError);
printf("Select device to use [%d-%d]:", 1, numDevices);
scanf("%d", &deviceIndex);
deviceIndex--;
*pDevice = devices[deviceIndex];
if (platforms)
free(platforms);
if (devices)
free(devices);
}
///////////////////////////////////////////////////////////////////////////////
// Creates an OpenCL context for the given device and platform.
cl_context CreateOpenCLContext(cl_platform_id platform, cl_device_id device)
{
cl_int clError;
cl_context context;
cl_context_properties contextProperties[] =
{
CL_CONTEXT_PLATFORM,
(cl_context_properties)platform,
0
};
context = clCreateContext(contextProperties, 1, &device, NULL, NULL, &clError);
CHECK_OCL_ERR("clCreateContext", clError);
return context;
}
///////////////////////////////////////////////////////////////////////////////
// Releases the input OpenCL context.
void ReleaseOpenCLContext(cl_context *pContext)
{
cl_int clError;
CHECK_NULL(pContext);
if (*pContext)
{
clError = clReleaseContext(*pContext);
CHECK_OCL_ERR("clReleaseContext", clError);
*pContext = 0;
}
}
///////////////////////////////////////////////////////////////////////////////
// Creates an OpenCL queue for the given device and context.
cl_command_queue CreateOpenCLQueue(cl_device_id device, cl_context context)
{
cl_int clError;
cl_command_queue queue;
queue = clCreateCommandQueue(context, device, 0, &clError);
CHECK_OCL_ERR("clCreateCommandQueue", clError);
return queue;
}
///////////////////////////////////////////////////////////////////////////////
// Releases the input OpenCL queue.
void ReleaseOpenCLQueue(cl_command_queue *pQueue)
{
cl_int clError;
CHECK_NULL(pQueue);
if (*pQueue)
{
clError = clReleaseCommandQueue(*pQueue);
CHECK_OCL_ERR("clReleaseCommandQueue", clError);
*pQueue = 0;
}
}
///////////////////////////////////////////////////////////////////////////////
// Creates an OpenCL buffer in the given context.
cl_mem CreateDeviceBuffer(cl_context context, size_t sizeInBytes)
{
cl_int clError;
cl_mem buffer = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeInBytes, NULL, &clError);
CHECK_OCL_ERR("clCreateBuffer", clError);
return buffer;
}
///////////////////////////////////////////////////////////////////////////////
// Releases the input OpenCL memory buffer.
void ReleaseDeviceBuffer(cl_mem *pDeviceBuffer)
{
cl_int clError;
CHECK_NULL(pDeviceBuffer);
if (*pDeviceBuffer)
{
clError = clReleaseMemObject(*pDeviceBuffer);
CHECK_OCL_ERR("clReleaseMemObject", clError);
*pDeviceBuffer = 0;
}
}
///////////////////////////////////////////////////////////////////////////////
// Copies data from a host buffer to an OpenCL device buffer.
void CopyHostToDevice(void* hostBuffer, cl_mem deviceBuffer, size_t sizeInBytes, cl_command_queue queue, cl_bool blocking)
{
cl_int clError;
clError = clEnqueueWriteBuffer(queue, deviceBuffer, blocking, 0, sizeInBytes, hostBuffer, 0, NULL, NULL);
CHECK_OCL_ERR("clEnqueueWriteBuffer", clError);
}
///////////////////////////////////////////////////////////////////////////////
// Copies data from a device buffer back to host.
void CopyDeviceToHost(cl_mem deviceBuffer, void* hostBuffer, size_t sizeInBytes, cl_command_queue queue, cl_bool blocking)
{
cl_int clError;
clError = clEnqueueReadBuffer(queue, deviceBuffer, blocking, 0, sizeInBytes, hostBuffer, 0, NULL, NULL);
CHECK_OCL_ERR("clEnqueueReadBuffer", clError);
}
///////////////////////////////////////////////////////////////////////////////
// Loads the OpenCL code from the input file.
char* LoadOpenCLSourceFromFile(char* filePath, size_t *pSourceLength)
{
FILE* fileHandle;
char* sourceCode;
fileHandle = fopen(filePath, "rb");
CHECK_NULL(fileHandle);
fseek(fileHandle, 0, SEEK_END);
*pSourceLength = ftell(fileHandle);
sourceCode = (char*)malloc((*pSourceLength) + 1);
CHECK_NULL(sourceCode);
fseek(fileHandle, 0, SEEK_SET);
fread(sourceCode, *pSourceLength, 1, fileHandle);
sourceCode[(*pSourceLength)] = 0;
*pSourceLength = (*pSourceLength) + 1;
fclose(fileHandle);
return sourceCode;
}
///////////////////////////////////////////////////////////////////////////////
// Creates and builds an OpenCL program with the input source code for
// the given context and source code string.
cl_program CreateAndBuildProgram(cl_context context, char* sourceCode, size_t sourceCodeLength)
{
cl_program program;
cl_int clError;
char *buildLog;
size_t buildLogSize;
cl_device_id device;
program = clCreateProgramWithSource(context, 1, (const char**)(&sourceCode), &sourceCodeLength, &clError);
CHECK_OCL_ERR("clCreateProgramWithSource", clError);
clError = clGetContextInfo(context, CL_CONTEXT_DEVICES, sizeof(cl_device_id), &device, NULL);
CHECK_OCL_ERR("clGetContextInfo", clError);
clError = clBuildProgram(program, 1, &device, NULL, NULL, NULL);
if (CL_SUCCESS != clError)
{
printf("\nOpenCL error %d at line %d in file %s\n", clError, __LINE__, __FILE__);
print_cl_error("clBuildProgram", clError);
buildLog = NULL;
buildLogSize = 0;
clError = clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 0, NULL, &buildLogSize);
CHECK_OCL_ERR("clGetProgramBuildInfo", clError);
if (buildLogSize)
{
// Allocate memory to fit the build log - it can be very large in case of errors
buildLog = (char*)malloc(buildLogSize);
if (buildLog)
{
clError = clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, buildLogSize, buildLog, NULL);
CHECK_OCL_ERR("clGetProgramBuildInfo", clError);
printf("\nOpenCL program build info: \n%s\n", buildLog);
// Free buildLog buffer
free(buildLog);
}
}
exit(EXIT_FAILURE);
}
return program;
}
///////////////////////////////////////////////////////////////////////////////
// Releases an OpenCL program object.
void ReleaseProgram(cl_program *pProgram)
{
cl_int clError;
CHECK_NULL(pProgram);
if (*pProgram)
{
clError = clReleaseProgram(*pProgram);
CHECK_OCL_ERR("clReleaseProgram", clError);
*pProgram = 0;
}
}
///////////////////////////////////////////////////////////////////////////////
// Creates an OpenCL kernel object for the kernel with the given name.
cl_kernel CreateKernel(cl_program program, char* kernelName)
{
cl_int clError;
cl_kernel kernel;
kernel = clCreateKernel(program, kernelName, &clError);
CHECK_OCL_ERR("clCreateKernel", clError);
return kernel;
}
///////////////////////////////////////////////////////////////////////////////
// Releases an OpenCL kernel object.
void ReleaseKernel(cl_kernel *pKernel)
{
cl_int clError;
CHECK_NULL(pKernel);
if (*pKernel)
{
clError = clReleaseKernel(*pKernel);
CHECK_OCL_ERR("clReleaseKernel", clError);
*pKernel = 0;
}
}