-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOpenCL.cpp
410 lines (343 loc) · 13.8 KB
/
OpenCL.cpp
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
#include "OpenCL.hpp"
using std::cout;
using std::cerr;
using std::endl;
OpenCLBackend::opencl_alloc_t::~opencl_alloc_t()
{}
OpenCLBackend &OpenCL = OpenCLBackend::get();
OpenCLBackend& OpenCLBackend::get()
{
static OpenCLBackend opencl;
return opencl;
}
const char *openCLErrorToString(const cl_int error)
{
switch (error) {
case CL_SUCCESS: return "Success";
case CL_DEVICE_NOT_FOUND: return "Device not found";
case CL_DEVICE_NOT_AVAILABLE: return "Device not available";
case CL_COMPILER_NOT_AVAILABLE: return "Compiler not availabe";
case CL_MEM_OBJECT_ALLOCATION_FAILURE:
return "Memory object allocation failure";
case CL_OUT_OF_RESOURCES: return "Out of resources";
case CL_OUT_OF_HOST_MEMORY: return "Out of host memory";
case CL_PROFILING_INFO_NOT_AVAILABLE:
return "Profiling info not available";
case CL_MEM_COPY_OVERLAP: return "Memory copy overlap";
case CL_IMAGE_FORMAT_MISMATCH: return "Image format mismatch";
case CL_IMAGE_FORMAT_NOT_SUPPORTED:
return "Image format not supported";
case CL_BUILD_PROGRAM_FAILURE: return "Program build failure";
case CL_MAP_FAILURE: return "Image mapping failure";
case CL_MISALIGNED_SUB_BUFFER_OFFSET:
return "Misaligned sub-buffer offeset";
case CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST:
return "Execution status error for events in wait list";
#ifdef CL_VERSION_1_2
case CL_COMPILE_PROGRAM_FAILURE: return "Program compilation failure";
case CL_LINKER_NOT_AVAILABLE: return "Linker not available";
case CL_LINK_PROGRAM_FAILURE: return "Program linking failure";
case CL_DEVICE_PARTITION_FAILED: return "Device partition failed";
case CL_KERNEL_ARG_INFO_NOT_AVAILABLE:
return "Kernel argument info not available";
#endif
case CL_INVALID_VALUE: return "Invalid value";
case CL_INVALID_DEVICE_TYPE: return "Invalid device type";
case CL_INVALID_PLATFORM: return "Invalid platform";
case CL_INVALID_DEVICE: return "Invalid device";
case CL_INVALID_CONTEXT: return "Invalid context";
case CL_INVALID_QUEUE_PROPERTIES:
return "Invalid command queue properties";
case CL_INVALID_COMMAND_QUEUE: return "Invalid command queue";
case CL_INVALID_HOST_PTR: return "Invalid host pointer";
case CL_INVALID_MEM_OBJECT: return "Invalid memory object";
case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR:
return "Invalid image format descriptor";
case CL_INVALID_IMAGE_SIZE: return "Invalid image size";
case CL_INVALID_SAMPLER: return "Invalid sampler";
case CL_INVALID_BINARY: return "Invalid binary";
case CL_INVALID_BUILD_OPTIONS: return "Invalid build options";
case CL_INVALID_PROGRAM: return "Invalid program";
case CL_INVALID_PROGRAM_EXECUTABLE:
return "Invalid program executable";
case CL_INVALID_KERNEL_NAME: return "Invalid kernel name";
case CL_INVALID_KERNEL_DEFINITION: return "Invalid kernel definition";
case CL_INVALID_KERNEL: return "Invalid kernel";
case CL_INVALID_ARG_INDEX: return "Invalid argument index";
case CL_INVALID_ARG_VALUE: return "Invalid argument value";
case CL_INVALID_ARG_SIZE: return "Invalid argument size";
case CL_INVALID_KERNEL_ARGS: return "Invalid kernel arguments";
case CL_INVALID_WORK_DIMENSION: return "Invalid work dimension";
case CL_INVALID_WORK_GROUP_SIZE: return "Invalid work group size";
case CL_INVALID_WORK_ITEM_SIZE: return "Invalid work item size";
case CL_INVALID_GLOBAL_OFFSET: return "Invalid global offset";
case CL_INVALID_EVENT_WAIT_LIST: return "Invalid event wait list";
case CL_INVALID_EVENT: return "Invalid event";
case CL_INVALID_OPERATION: return "Invalid operation";
case CL_INVALID_GL_OBJECT: return "Invalid GL object";
case CL_INVALID_BUFFER_SIZE: return "Invalid buffer size";
case CL_INVALID_MIP_LEVEL: return "Invalid MIP level";
case CL_INVALID_GLOBAL_WORK_SIZE: return "Invalid global work size";
case CL_INVALID_PROPERTY: return "Invalid property";
#ifdef CL_VERSION_1_2
case CL_INVALID_IMAGE_DESCRIPTOR: return "Invalid image descriptor";
case CL_INVALID_COMPILER_OPTIONS: return "Invalid compiler options";
case CL_INVALID_LINKER_OPTIONS: return "Invalid linker options";
case CL_INVALID_DEVICE_PARTITION_COUNT:
return "Invalid device partition count";
#endif
default:
return "Unknown error";
}
}
template<typename T>
static void oclQueryPlatform(cl_platform_id platform, cl_platform_info info, T **result)
{
size_t size;
OPENCL_CHK(clGetPlatformInfo(platform, info, 0, nullptr, &size));
*result = new T[size / sizeof (T)];
OPENCL_CHK(clGetPlatformInfo(platform, info, size, *result, nullptr));
}
template<typename T>
static void oclQueryDevice(cl_device_id device, cl_device_info info, T **result)
{
size_t size;
OPENCL_CHK(clGetDeviceInfo(device, info, 0, nullptr, &size));
*result = new T[size / sizeof (T)];
OPENCL_CHK(clGetDeviceInfo(device, info, size, *result, nullptr));
}
template<typename T>
static void oclQueryKernel(cl_kernel kernel, cl_device_id dev, cl_kernel_work_group_info info, T **result)
{
size_t size;
OPENCL_CHK(clGetKernelWorkGroupInfo(kernel, dev, info, 0, nullptr, &size));
*result = new T[size];
OPENCL_CHK(clGetKernelWorkGroupInfo(kernel, dev, info, size, *result, nullptr));
}
static void printSpaceSeperated(char *str, bool deepIndent)
{
char *start = str;
while (*str != '\0') {
if (*str == ' ') {
*str = '\0';
if (deepIndent) cout << "\t " << start << endl;
else cout << "\t" << start << endl;
start = str + 1;
}
str++;
}
}
void OpenCLBackend::queryPlatform(size_t platform, bool verbose)
{
char *info;
if (platform >= devicesPerPlatform.size()) {
cerr << "Non-existent platform #"
<< platform
<< ", platform count is "
<< devicesPerPlatform.size()
<< endl;
exit(EXIT_FAILURE);
}
cout << "Platform: " << platform << endl;
oclQueryPlatform(platforms[platform], CL_PLATFORM_PROFILE, &info);
cout << " Profile: " << info << endl;
delete[] info;
oclQueryPlatform(platforms[platform], CL_PLATFORM_VERSION, &info);
cout << " Version: " << info << endl;
delete[] info;
oclQueryPlatform(platforms[platform], CL_PLATFORM_NAME, &info);
cout << " Name: " << info << endl;
delete[] info;
oclQueryPlatform(platforms[platform], CL_PLATFORM_VENDOR, &info);
cout << " Vendor: " << info << endl;
delete[] info;
oclQueryPlatform(platforms[platform], CL_PLATFORM_EXTENSIONS, &info);
cout << " Extensions:" << endl;
printSpaceSeperated(info, false);
delete[] info;
cout << endl;
listDevices(platform, verbose);
}
void OpenCLBackend::queryDevice(size_t platform, int dev, bool verbose)
{
char *info;
size_t *sizeVal;
cl_uint *uintVal;
cl_ulong *longVal;
size_t idx = static_cast<size_t>(dev);
if (platform >= devicesPerPlatform.size()) {
cerr << "Non-existent platform #"
<< platform
<< ", platform count is "
<< devicesPerPlatform.size()
<< endl;
exit(EXIT_FAILURE);
} else if (dev >= devicesPerPlatform[platform]) {
cerr << "Non-existent device #"
<< dev
<< ", device count for platform "
<< platform
<< " is "
<< devicesPerPlatform[idx]
<< endl;
exit(EXIT_FAILURE);
}
cout << " Device Number: " << dev << endl;
oclQueryDevice(devices[platform][idx], CL_DEVICE_NAME, &info);
cout << "\tDevice Name: " << info << endl;
delete[] info;
oclQueryDevice(devices[platform][idx], CL_DEVICE_VENDOR, &info);
cout << "\tVendor Name: " << info << endl;
delete[] info;
oclQueryDevice(devices[platform][idx], CL_DRIVER_VERSION, &info);
cout << "\tDriver Version: " << info << endl;
delete[] info;
oclQueryDevice(devices[platform][idx], CL_DEVICE_VERSION, &info);
cout << "\tOpenCL Version: " << info << endl;
delete[] info;
oclQueryDevice(devices[platform][idx], CL_DEVICE_OPENCL_C_VERSION, &info);
cout << "\tOpenCL C Version: " << info << endl;
delete[] info;
cout << endl;
if (!verbose) return;
cl_device_type *devType;
oclQueryDevice(devices[platform][idx], CL_DEVICE_TYPE, &devType);
/* FIXME */
delete[] devType;
oclQueryDevice(devices[platform][idx], CL_DEVICE_MAX_COMPUTE_UNITS, &uintVal);
cout << "\tMax Compute Units: " << *uintVal << endl;
delete[] uintVal;
oclQueryDevice(devices[platform][idx], CL_DEVICE_MAX_WORK_GROUP_SIZE, &sizeVal);
cout << "\tMax Workgroup Size: " << *sizeVal << endl;
delete[] sizeVal;
cout << endl;
oclQueryDevice(devices[platform][idx], CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS, &uintVal);
oclQueryDevice(devices[platform][idx], CL_DEVICE_MAX_WORK_ITEM_SIZES, &sizeVal);
cout << "\tMax Work Item Dimensions: " << sizeVal[0];
for (cl_uint i = 1; i < *uintVal; i++) {
cout << " x " << sizeVal[i];
}
cout << endl;
delete[] sizeVal;
delete[] uintVal;
cout << endl;
oclQueryDevice(devices[platform][idx], CL_DEVICE_GLOBAL_MEM_SIZE, &longVal);
cout << "\tGlobal Mem: " << *longVal << endl;
delete[] longVal;
cl_device_local_mem_type *memType;
oclQueryDevice(devices[platform][idx], CL_DEVICE_LOCAL_MEM_TYPE, &memType);
/* FIXME */
delete[] memType;
oclQueryDevice(devices[platform][idx], CL_DEVICE_LOCAL_MEM_SIZE, &longVal);
cout << "\tMax Local Mem: " << *longVal << endl;
delete[] longVal;
oclQueryDevice(devices[platform][idx], CL_DEVICE_MAX_MEM_ALLOC_SIZE, &longVal);
cout << "\tMax Mem Allocation: " << *longVal << endl;
delete[] longVal;
cout << endl;
oclQueryDevice(devices[platform][idx], CL_DEVICE_EXTENSIONS, &info);
cout << "\tExtensions:" << endl;
printSpaceSeperated(info, true);
delete[] info;
cout << endl;
}
void OpenCLBackend::setDevice(size_t platform, int device)
{
cl_int ret;
cl_uint *uintVal;
cl_ulong *longVal;
size_t *sizeVal;
size_t idx = static_cast<size_t>(device);
if (platform >= devicesPerPlatform.size()) {
cerr << "Non-existent platform #"
<< platform
<< ", platform count is "
<< devicesPerPlatform.size()
<< endl;
exit(EXIT_FAILURE);
} else if (device >= devicesPerPlatform[platform]) {
cerr << "Non-existent device #"
<< device
<< ", device count for platform "
<< platform
<< " is "
<< devicesPerPlatform[platform]
<< endl;
exit(EXIT_FAILURE);
}
clFinish(queue);
clReleaseCommandQueue(queue);
clReleaseContext(ctxt);
ctxt = clCreateContext(nullptr, static_cast<cl_uint>(devicesPerPlatform[platform]),
devices[platform].data(), opencl_error_callback,
nullptr, &ret);
OPENCL_CHK(ret);
queue = clCreateCommandQueue(ctxt, devices[platform][idx], 0, &ret);
OPENCL_CHK(ret);
activePlatform = platforms[platform];
activeDevice = devices[platform][idx];
oclQueryDevice(devices[platform][idx], CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS, &uintVal);
maxDims_ = static_cast<size_t>(*uintVal);
delete[] uintVal;
oclQueryDevice(devices[platform][idx], CL_DEVICE_MAX_COMPUTE_UNITS, &uintVal);
numComputeUnits_ = static_cast<size_t>(*uintVal);
delete[] uintVal;
oclQueryDevice(devices[platform][idx], CL_DEVICE_MAX_WORK_GROUP_SIZE, &sizeVal);
maxThreadsPerBlock_ = static_cast<size_t>(*sizeVal);
delete[] sizeVal;
oclQueryDevice(devices[platform][idx], CL_DEVICE_LOCAL_MEM_SIZE, &longVal);
maxSharedMem_ = static_cast<size_t>(*longVal);
delete[] longVal;
oclQueryDevice(devices[platform][idx], CL_DEVICE_MAX_WORK_ITEM_SIZES, &sizeVal);
maxBlockSizes_.reserve(maxDims);
for (size_t i = 0; i < maxDims; i++) {
maxBlockSizes_.push_back(sizeVal[i]);
maxGridSizes_.push_back(maxDims);
}
delete[] sizeVal;
}
void OpenCLBackend::runKernel(cl_kernel kernel) const
{
cl_event evt;
cl_uint *computeUnits;
size_t globalSize;
size_t *workgroupSize;
oclQueryDevice(activeDevice, CL_DEVICE_MAX_COMPUTE_UNITS, &computeUnits);
oclQueryKernel(kernel, activeDevice, CL_KERNEL_WORK_GROUP_SIZE, &workgroupSize);
globalSize = *computeUnits * *workgroupSize;
OPENCL_CHK(clEnqueueNDRangeKernel(queue, kernel, 1, nullptr, &globalSize, workgroupSize, 0, nullptr, &evt));
OPENCL_CHK(clWaitForEvents(1, &evt));
delete[] workgroupSize;
delete[] computeUnits;
}
void OpenCLBackend::setWorkSizes
( size_t dims
, std::vector<size_t> blockSizes
, std::vector<size_t> gridSizes
, size_t)
{
if (dims < 1 || dims > 3) {
cerr << "Invalid number of dimensions: " << dims << endl;
exit(EXIT_FAILURE);
}
if (blockSizes.size() != dims) {
cerr << "Number of block sizes ("
<< blockSizes.size()
<< ") don't match specified number of dimensions ("
<< dims
<< ")"
<< endl;
exit(EXIT_FAILURE);
}
if (gridSizes.size() != dims) {
cerr << "Number of grid sizes ("
<< gridSizes.size()
<< ") don't match specified number of dimensions ("
<< dims
<< ")"
<< endl;
exit(EXIT_FAILURE);
}
/* FIXME */
exit(EXIT_FAILURE);
}