-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathEasyCL.cpp
624 lines (565 loc) · 21.6 KB
/
EasyCL.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
// Copyright Hugh Perkins 2013,2014 hughperkins at gmail
//
// This Source Code Form is subject to the terms of the Mozilla Public License,
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
#include <cstring>
#include <stdexcept>
using namespace std;
#ifdef USE_CLEW
#include "clew.h"
#else
#if defined(__APPLE__) || defined(__MACOSX)
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif
#endif
#include "EasyCL.h"
#include "CLArrayFloat.h"
#include "CLArrayInt.h"
#include "CLIntWrapper.h"
#include "CLFloatWrapperConst.h"
#include "CLWrapper.h"
#include "CLKernel.h"
#include "util/easycl_stringhelper.h"
namespace easycl {
CLQueue::CLQueue(EasyCL *cl) : cl(cl) {
cl_int err;
this->queue = clCreateCommandQueue(*cl->context, cl->device, 0, &err);
cl->checkError(err);
}
CLQueue::CLQueue(EasyCL *cl, cl_command_queue queue) :
cl(cl), queue(queue) {
// takes ownership of this queue. releases it at the end (if we delete this CLQueue object)
}
CLQueue::~CLQueue() {
cl_int err = clReleaseCommandQueue(this->queue);
cl->checkError(err);
}
EasyCL::EasyCL(int gpu, bool verbose) {
init(gpu, verbose);
}
EasyCL::EasyCL(int gpu) {
init(gpu, true);
}
EasyCL::EasyCL(bool verbose) {
init(0, verbose);
}
EasyCL::EasyCL() {
init(0, true);
}
void EasyCL::commonConstructor(cl_platform_id platform_id, cl_device_id device, bool verbose) {
this->verbose = verbose;
queue = 0;
context = 0;
profilingOn = false;
#ifdef USE_CLEW
bool clpresent = 0 == clewInit();
if(!clpresent) {
throw std::runtime_error("OpenCL library not found");
}
#endif
this->platform_id = platform_id;
this->device = device;
if(verbose) {
// std::cout << "Using " << getPlatformInfoString(platform_id, CL_PLATFORM_VENDOR) << ", OpenCL platform: " << getPlatformInfoString(platform_id, CL_PLATFORM_NAME) << std::endl;
std::cout << "OpenCL platform: " << getPlatformInfoString(platform_id, CL_PLATFORM_NAME) << std::endl;
// std::cout << "Using " << getPlatformInfoString(platform_id, CL_PLATFORM_NAME) << std::endl;
// std::cout << "Using OpenCL device: " << getDeviceInfoString(device, CL_DEVICE_NAME) << std::endl;
std::cout << "OpenCL device: " << getDeviceInfoString(device, CL_DEVICE_NAME) << std::endl;
}
// Context
context = new cl_context();
*context = clCreateContext(0, 1, &device, NULL, NULL, &error);
if (error != CL_SUCCESS) {
throw std::runtime_error("Error creating OpenCL context, OpenCL errocode: " + errorMessage(error));
}
// Command-queue
queue = new cl_command_queue;
*queue = clCreateCommandQueue(*context, device, 0, &error);
if (error != CL_SUCCESS) {
throw std::runtime_error("Error creating OpenCL command queue, OpenCL errorcode: " + errorMessage(error));
}
default_queue = new CLQueue(this, *queue);
}
EasyCL::EasyCL(cl_platform_id platform_id, cl_device_id device, bool verbose) {
commonConstructor(platform_id, device, verbose);
}
EasyCL::EasyCL(cl_platform_id platform_id, cl_device_id device) {
commonConstructor(platform_id, device, true);
}
EasyCL *EasyCL::createForFirstGpu() {
// cout << "createForFirstgpu" << endl;
return createForIndexedGpu(0, true);
}
EasyCL *EasyCL::createForFirstGpu(bool verbose) {
// cout << "createForFirstgpu verbose=" << verbose << endl;
return createForIndexedGpu(0, verbose);
}
EasyCL *EasyCL::createForFirstGpuOtherwiseCpu() {
return createForFirstGpuOtherwiseCpu(true);
}
EasyCL *EasyCL::createForFirstGpuOtherwiseCpu(bool verbose) {
try {
return createForIndexedGpu(0, verbose);
} catch(std::runtime_error error) {
cout << "Couldnt find OpenCL-enabled GPU: " << error.what() << endl;
cout << "Trying for OpenCL-enabled CPU" << endl;
}
return createForPlatformDeviceIndexes(0, 0);
}
EasyCL *EasyCL::createForIndexedGpu(int gpu) {
// cout << "createForIndexedgpu gpu=" << gpu << endl;
return createForIndexedGpu(gpu, true);
}
EasyCL *EasyCL::createForIndexedDevice(int device) {
return createForIndexedDevice(device, true);
}
EasyCL *EasyCL::createForIndexedGpu(int gpu, bool verbose) {
// cout << "createForindexedgpu gpu=" << gpu << " verbose=" << verbose << endl;
#ifdef USE_CLEW
bool clpresent = 0 == clewInit();
if(!clpresent) {
throw std::runtime_error("OpenCL library not found");
}
#endif
char *gpuOffsetStr = getenv("CL_GPUOFFSET");
// int gpuOffset = 0;
if(gpuOffsetStr != 0) {
int gpuOffset = atoi(gpuOffsetStr);
if(gpuOffset != 0) {
int newGpu = gpu + gpuOffset;
// cout << "CL_GPUOFFSET var detected, changing gpu offset from " << gpu << " to " << newGpu << endl;
gpu = newGpu;
}
}
cl_int error;
int currentGpuIndex = 0;
cl_platform_id platform_ids[10];
cl_uint num_platforms;
error = clGetPlatformIDs(10, platform_ids, &num_platforms);
if (error != CL_SUCCESS) {
throw std::runtime_error("Error getting OpenCL platforms ids, OpenCL errorcode: " + errorMessage(error));
}
if(num_platforms == 0) {
throw std::runtime_error("Error: no OpenCL platforms available");
}
for(int platform = 0; platform < (int)num_platforms; platform++) {
cl_platform_id platform_id = platform_ids[platform];
// cout << "checking platform id " << platform_id << endl;
cl_device_id device_ids[100];
cl_uint num_devices;
error = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU | CL_DEVICE_TYPE_ACCELERATOR , 100, device_ids, &num_devices);
if (error != CL_SUCCESS) {
continue;
// throw std::runtime_error("Error getting device ids for platform " + toString(platform) + ": " + errorMessage(error));
}
// cout << "gpu=" << gpu << " currentGpuIndex=" << currentGpuIndex << " num_devices=" << num_devices << endl;
if(( gpu - currentGpuIndex) < (int)num_devices) {
return new EasyCL(platform_id, device_ids[(gpu - currentGpuIndex)], verbose);
} else {
currentGpuIndex += num_devices;
}
}
if(gpu == 0) {
throw std::runtime_error("No OpenCL-enabled GPUs found");
} else {
throw std::runtime_error("Not enough OpenCL-enabled GPUs found to satisfy gpu index: " + toString(gpu) );
}
}
EasyCL *EasyCL::createForIndexedDevice(int device, bool verbose) {
#ifdef USE_CLEW
bool clpresent = 0 == clewInit();
if(!clpresent) {
throw std::runtime_error("OpenCL library not found");
}
#endif
cl_int error;
int currentGpuIndex = 0;
cl_platform_id platform_ids[10];
cl_uint num_platforms;
error = clGetPlatformIDs(10, platform_ids, &num_platforms);
if (error != CL_SUCCESS) {
throw std::runtime_error("Error getting OpenCL platforms ids, OpenCL errorcode: " + errorMessage(error));
}
if(num_platforms == 0) {
throw std::runtime_error("Error: no OpenCL platforms available");
}
for(int platform = 0; platform < (int)num_platforms; platform++) {
cl_platform_id platform_id = platform_ids[platform];
// cout << "checking platform id " << platform_id << endl;
cl_device_id device_ids[100];
cl_uint num_devices;
error = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_ALL , 100, device_ids, &num_devices);
if (error != CL_SUCCESS) {
continue;
// throw std::runtime_error("Error getting device ids for platform " + toString(platform) + ": " + errorMessage(error));
}
if(( device - currentGpuIndex) < (int)num_devices) {
return new EasyCL(platform_id, device_ids[(device - currentGpuIndex)], verbose);
} else {
currentGpuIndex += num_devices;
}
}
if(device == 0) {
throw std::runtime_error("No OpenCL devices found");
} else {
throw std::runtime_error("Not enough OpenCL devices found to satisfy gpu index: " + toString(device) );
}
}
EasyCL *EasyCL::createForPlatformDeviceIndexes(int platformIndex, int deviceIndex) {
#ifdef USE_CLEW
bool clpresent = 0 == clewInit();
if(!clpresent) {
throw std::runtime_error("OpenCL library not found");
}
#endif
cl_int error;
// int currentGpuIndex = 0;
cl_platform_id platform_ids[10];
cl_uint num_platforms;
error = clGetPlatformIDs(10, platform_ids, &num_platforms);
if (error != CL_SUCCESS) {
throw std::runtime_error("Error getting OpenCL platforms ids, OpenCL errorcode: " + errorMessage(error));
}
if(num_platforms == 0) {
throw std::runtime_error("Error: no OpenCL platforms available");
}
if(platformIndex >= (int)num_platforms) {
throw std::runtime_error("Error: OpenCL platform index " + toString(platformIndex) + " not available. There are only: " + toString(num_platforms) + " platforms available");
}
cl_platform_id platform_id = platform_ids[platformIndex];
cl_device_id device_ids[100];
cl_uint num_devices;
error = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_ALL, 100, device_ids, &num_devices);
if (error != CL_SUCCESS) {
throw std::runtime_error("Error getting OpenCL device ids for platform index " + toString(platformIndex) + ": OpenCL errorcode: " + errorMessage(error));
}
if(num_devices == 0) {
throw std::runtime_error("Error: no OpenCL devices available for platform index " + toString(platformIndex) );
}
if(deviceIndex >= (int)num_devices) {
throw std::runtime_error("Error: OpenCL device index " + toString(deviceIndex) + " goes beyond the available devices on platform index " + toString(platformIndex) + ", which has " + toString(num_devices) + " devices");
}
return new EasyCL(platform_id, device_ids[deviceIndex]);
}
EasyCL *EasyCL::createForPlatformDeviceIds(cl_platform_id platformId, cl_device_id deviceId) {
return new EasyCL(platformId, deviceId);
}
void EasyCL::init(int gpuIndex, bool verbose) {
#ifdef USE_CLEW
bool clpresent = 0 == clewInit();
if(!clpresent) {
throw std::runtime_error("OpenCL library not found");
}
#endif
// std::cout << "this: " << this << std::endl;
// this->gpuIndex = gpuindex;
error = 0;
queue = 0;
context = 0;
// Platform
cl_uint num_platforms;
error = clGetPlatformIDs(1, &platform_id, &num_platforms);
// std::cout << "num platforms: " << num_platforms << std::endl;
// assert (num_platforms == 1);
if (error != CL_SUCCESS) {
throw std::runtime_error("Error getting OpenCL platforms ids: " + errorMessage(error));
}
if(num_platforms == 0) {
throw std::runtime_error("Error: no OpenCL platforms available");
}
cl_uint num_devices;
error = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU | CL_DEVICE_TYPE_ACCELERATOR , 0, 0, &num_devices);
if (error != CL_SUCCESS) {
throw std::runtime_error("Error getting OpenCL device ids: " + errorMessage(error));
}
// std::cout << "num devices: " << num_devices << std::endl;
cl_device_id *device_ids = new cl_device_id[num_devices];
error = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU | CL_DEVICE_TYPE_ACCELERATOR, num_devices, device_ids, &num_devices);
if (error != CL_SUCCESS) {
throw std::runtime_error("Error getting OpenCL device ids: " + errorMessage(error));
}
if(gpuIndex >= static_cast<int>(num_devices) ) {
throw std::runtime_error("requested gpuindex " + toString(gpuIndex) + " goes beyond number of available device " + toString(num_devices) );
}
device = device_ids[gpuIndex];
delete[] device_ids;
// Context
context = new cl_context();
*context = clCreateContext(0, 1, &device, NULL, NULL, &error);
if (error != CL_SUCCESS) {
throw std::runtime_error("Error creating OpenCL context, OpenCL errorcode: " + errorMessage(error));
}
// Command-queue
queue = new cl_command_queue;
*queue = clCreateCommandQueue(*context, device, 0, &error);
if (error != CL_SUCCESS) {
throw std::runtime_error("Error creating OpenCL command queue, OpenCL errorcode: " + errorMessage(error));
}
}
EasyCL::~EasyCL() {
for(map< string, bool >::iterator it = kernelOwnedByName.begin(); it != kernelOwnedByName.end(); it++) {
if(it->second) {
delete kernelByName[ it->first ];
}
}
for(vector< cl_event * >::iterator it = profilingEvents.begin(); it != profilingEvents.end(); it++) {
clReleaseEvent(**it);
delete *it;
}
// clReleaseProgram(program);
if(queue != 0) {
// cout << "releasing OpenCL command queue" << endl;
clReleaseCommandQueue(*queue);
delete queue;
}
if(context != 0) {
// cout << "releasing OpenCL context" << endl;
clReleaseContext(*context);
delete context;
}
}
CLQueue *EasyCL::newQueue() {
return new CLQueue(this);
}
CLArrayFloat *EasyCL::arrayFloat(int N) {
return new CLArrayFloat(N, this);
}
CLArrayInt *EasyCL::arrayInt(int N) {
return new CLArrayInt(N, this);
}
CLIntWrapper *EasyCL::wrap(int N, int *source) {
return new CLIntWrapper(N, source, this);
}
CLUCharWrapper *EasyCL::wrap(int N, unsigned char*source) {
return new CLUCharWrapper(N, source, this);
}
CLFloatWrapper *EasyCL::wrap(int N, float *source) {
return new CLFloatWrapper(N, source, this);
}
CLFloatWrapperConst *EasyCL::wrap(int N, float const*source) {
return new CLFloatWrapperConst(N, source, this);
}
CLKernel *EasyCL::buildKernel(string kernelfilepath, string kernelname, bool quiet) {
return buildKernel(kernelfilepath, kernelname, "", quiet);
}
CLKernel *EasyCL::buildKernel(string kernelfilepath, string kernelname, string options, bool quiet) {
std::string path = kernelfilepath.c_str();
std::string source = getFileContents(path);
return buildKernelFromString(source, kernelname, options, kernelfilepath, quiet);
}
CLKernel *EasyCL::buildKernelFromString(string source, string kernelname, string options, string sourcefilename, bool quiet) {
size_t src_size = 0;
const char *source_char = source.c_str();
src_size = strlen(source_char);
// cl_program program = new cl_program();
cl_program program = clCreateProgramWithSource(*context, 1, &source_char, &src_size, &error);
checkError(error);
// error = clBuildProgram(program, 1, &device, "-cl-opt-disable", NULL, NULL);
// std::cout << "options: [" << options.c_str() << "]" << std::endl;
error = clBuildProgram(program, 1, &device, options.c_str(), NULL, NULL);
char* build_log;
size_t log_size;
error = clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
checkError(error);
build_log = new char[log_size+1];
error = clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, log_size, build_log, NULL);
checkError(error);
build_log[log_size] = '\0';
string buildLogMessage = "";
if(log_size > 2) {
buildLogMessage = sourcefilename + " build log: " + "\n" + build_log;
if(!quiet) {
cout << buildLogMessage << endl;
}
}
delete[] build_log;
checkError(error);
cl_kernel kernel = clCreateKernel(program, kernelname.c_str(), &error);
if(error != CL_SUCCESS) {
std::string exceptionMessage = "";
switch(error) {
case -46:
exceptionMessage = "Invalid kernel name, code -46, kernel " + kernelname + "\n" + buildLogMessage;
break;
default:
exceptionMessage = "Something went wrong with clCreateKernel, OpenCL error code " + toString(error) + "\n" + buildLogMessage;
break;
}
if(quiet) {
cout << buildLogMessage << std::endl;
}
cout << "kernel build error:\n" << exceptionMessage << endl;
cout << "storing failed kernel into: easycl-failedkernel.cl" << endl;
exceptionMessage += "storing failed kernel into: easycl-failedkernel.cl\n";
ofstream f;
f.open("easycl-failedkernel.cl", ios_base::out);
f << source << endl;
f.close();
throw std::runtime_error(exceptionMessage);
}
checkError(error);
// clReleaseProgram(program);
CLKernel *newkernel = new CLKernel(this, sourcefilename, kernelname, source, program, kernel);
newkernel->buildLog = buildLogMessage;
return newkernel;
}
bool EasyCL::isOpenCLAvailable() {
#ifdef USE_CLEW
return 0 == clewInit();
#else
return true; // I guess?
#endif
}
int EasyCL::getPower2Upperbound(int value) {
int upperbound = 1;
while(upperbound < value) {
upperbound <<= 1;
}
return upperbound;
}
int EasyCL::roundUp(int quantization, int minimum) {
int size = (minimum / quantization) * quantization;
if(size < minimum) {
size += quantization;
}
return size;
}
// accidentally created 2 funcftions that do the same thing :-P but wont remove either,
// in case someone's using that one
int EasyCL::getNextPower2(int value){
return getPower2Upperbound(value);
} // eg pass in 320, it will return: 512
void EasyCL::gpu(int gpuIndex) {
if(queue != 0) {
clReleaseCommandQueue(*queue);
delete queue;
}
if(context != 0) {
clReleaseContext(*context);
delete context;
}
init(gpuIndex, this->verbose);
}
void EasyCL::finish() {
error = clFinish(*queue);
switch(error) {
case CL_SUCCESS:
break;
case -36:
throw std::runtime_error("Invalid command queue: often indicates out of bounds memory access within kernel");
default:
checkError(error);
}
}
void EasyCL::setProfiling(bool profiling) {
finish();
clReleaseCommandQueue(*queue);
// delete queue;
// queue = new cl_command_queue;
*queue = clCreateCommandQueue(*context, device, profiling ? CL_QUEUE_PROFILING_ENABLE : 0, &error);
if (error != CL_SUCCESS) {
throw std::runtime_error("Error creating command queue: " + errorMessage(error));
}
this->profilingOn = profiling;
}
void EasyCL::pushEvent(std::string name, cl_event *event) {
profilingNames.push_back(name);
profilingEvents.push_back(event);
}
void EasyCL::dumpProfiling() {
map< string, double > timeByKernel;
for(int i = 0; i < (int)profilingEvents.size(); i++) {
string name = profilingNames[i];
cl_event *event = profilingEvents[i];
clWaitForEvents(1, event);
cl_ulong start, end;
clGetEventProfilingInfo(*event, CL_PROFILING_COMMAND_START, sizeof(start), &start, NULL);
clGetEventProfilingInfo(*event, CL_PROFILING_COMMAND_END, sizeof(end), &end, NULL);
cl_ulong time_nanos = end - start;
double time_millis = time_nanos / 1000000.0;
timeByKernel[name] += time_millis;
clReleaseEvent(*event);
delete event;
}
for(map< string, double >::iterator it = timeByKernel.begin(); it != timeByKernel.end(); it++) {
cout << it->first << " " << it->second << "ms" << endl;
}
profilingNames.clear();
profilingEvents.clear();
}
int EasyCL::getComputeUnits() {
return (int)this->getDeviceInfoInt64(CL_DEVICE_MAX_COMPUTE_UNITS);
}
int EasyCL::getLocalMemorySize() {
return (int)this->getDeviceInfoInt64(CL_DEVICE_LOCAL_MEM_SIZE);
}
int EasyCL::getLocalMemorySizeKB() {
return (int)(this->getDeviceInfoInt64(CL_DEVICE_LOCAL_MEM_SIZE) / 1024);
}
int EasyCL::getMaxWorkgroupSize() {
return (int)this->getDeviceInfoInt64(CL_DEVICE_MAX_WORK_GROUP_SIZE);
}
int EasyCL::getMaxAllocSizeMB() {
return (int)(this->getDeviceInfoInt64(CL_DEVICE_MAX_MEM_ALLOC_SIZE) / 1024 / 1024);
}
std::string EasyCL::errorMessage(cl_int error) {
return toString(error);
}
void EasyCL::checkError(cl_int error) {
if(error != CL_SUCCESS) {
std::string message = toString(error);
switch(error) {
case CL_MEM_OBJECT_ALLOCATION_FAILURE:
message = "CL_MEM_OBJECT_ALLOCATION_FAILURE";
break;
case CL_INVALID_ARG_SIZE:
message = "CL_INVALID_ARG_SIZE";
break;
case CL_INVALID_BUFFER_SIZE:
message = "CL_INVALID_BUFFER_SIZE";
break;
}
cout << "opencl execution error, code " << error << " " << message << endl;
throw std::runtime_error(std::string("OpenCL error, code: ") + message);
}
}
std::string EasyCL::getFileContents(std::string filename) {
std::ifstream t(filename.c_str());
std::stringstream buffer;
buffer << t.rdbuf();
return buffer.str();
}
//long EasyCL::getDeviceInfoInt(cl_device_info name) {
// cl_ulong value = 0;
// clGetDeviceInfo(device, name, sizeof(cl_ulong), &value, 0);
// return static_cast<long>(value);
//}
int64_t EasyCL::getDeviceInfoInt64(cl_device_info name) {
cl_ulong value = 0;
clGetDeviceInfo(device, name, sizeof(cl_ulong), &value, 0);
return static_cast<int64_t>(value);
}
void EasyCL::storeKernel(std::string name, CLKernel *kernel) {
storeKernel(name, kernel, false);
}
// note that storing same name twice is an error, for now
// you can use name-mangling, or request I add a parameter 'bool overwrite'
// if deleteWithCl is true, then when this EasyCL object is deleted, this kernel
// will be deleted too
void EasyCL::storeKernel(std::string name, CLKernel *kernel, bool deleteWithCl) {
if(kernelByName.count(name) != 0) {
throw runtime_error("error: kernel for " + name + " already stored.");
}
kernelByName[ name ] = kernel;
kernelOwnedByName[ name ] = deleteWithCl;
}
CLKernel *EasyCL::getKernel(std::string name) {
return kernelByName[ name ];
}
bool EasyCL::kernelExists(std::string name) {
return kernelByName.count(name) != 0;
}
}