35 #define CL_USE_DEPRECATED_OPENCL_1_1_APIS 37 #include <OpenCL/opencl.h> 55 unsigned int m_platform;
56 unsigned int m_device;
58 cl_command_queue m_command_queue;
59 cl_platform_id * m_platforms;
60 cl_device_id * m_devices;
65 OclEngine(
unsigned int platform,
unsigned int device);
73 std::string
getTypeName(
float dummy) { dummy=dummy;
return std::string(
"float"); }
75 std::string
getTypeName(
double dummy) { dummy=dummy;
return std::string(
"double"); }
78 std::string
getTypeName(int8_t dummy) { dummy=dummy;
return std::string(
"char"); }
80 std::string
getTypeName(uint8_t dummy) { dummy=dummy;
return std::string(
"unsigned char"); }
83 std::string
getTypeName(int16_t dummy) { dummy=dummy;
return std::string(
"short"); }
85 std::string
getTypeName(uint16_t dummy) { dummy=dummy;
return std::string(
"unsigned short"); }
88 std::string
getTypeName(int32_t dummy) { dummy=dummy;
return std::string(
"int"); }
90 std::string
getTypeName(uint32_t dummy) { dummy=dummy;
return std::string(
"unsigned int"); }
93 std::string
getTypeName(int64_t dummy) { dummy=dummy;
return std::string(
"long"); }
95 std::string
getTypeName(uint64_t dummy) { dummy=dummy;
return std::string(
"unsigned long"); }
98 static void trimWS(std::string & str);
104 OclEngine<T>::OclEngine(
unsigned int platform,
unsigned int device): m_platform(platform), m_device(device), m_platforms(NULL), m_devices(NULL) {
107 cl_uint ret_num_platforms;
108 cl_int ret = clGetPlatformIDs(0, NULL, &ret_num_platforms);
109 if (ret)
throw RuntimeException(
"Couldn't get number of platforms available", ret);
110 if (ret_num_platforms == 0)
throw RuntimeException(
"No OpenCL platforms found");
112 if (platform >= ret_num_platforms)
throw RuntimeException(
"No such OpenCL platform found");
115 m_platforms = (cl_platform_id*)malloc(ret_num_platforms *
sizeof(cl_platform_id));
119 ret = clGetPlatformIDs(ret_num_platforms, m_platforms, NULL);
126 cl_uint ret_num_devices;
127 ret = clGetDeviceIDs(m_platforms[platform], CL_DEVICE_TYPE_ALL, 0, NULL, &ret_num_devices);
133 if (device >= ret_num_devices) {
139 m_devices = (cl_device_id*)malloc(ret_num_devices *
sizeof(cl_device_id));
140 if (m_devices == NULL) {
146 ret = clGetDeviceIDs(m_platforms[platform], CL_DEVICE_TYPE_ALL, ret_num_devices, m_devices, &ret_num_devices);
154 m_context = clCreateContext(NULL, 1, &(m_devices[device]), NULL, NULL, &ret);
162 m_command_queue = clCreateCommandQueue(m_context, m_devices[device], 0, &ret);
164 ret = clReleaseContext(m_context);
178 ret = clReleaseCommandQueue(m_command_queue);
179 ret = clReleaseContext(m_context);
189 const std::string whitespaces =
"\t\n\v\f\r ";
190 str.erase(0, str.find_first_not_of(whitespaces));
191 str.erase(str.find_last_not_of(whitespaces) + 1);
198 std::string devicesRet;
199 char textBuffer[1024];
200 std::string textBufferStr;
203 cl_uint ret_num_platforms;
204 cl_int ret = clGetPlatformIDs(0, NULL, &ret_num_platforms);
205 if (ret)
throw RuntimeException(
"Couldn't get number of platforms available", ret);
207 if (ret_num_platforms == 0) {
208 return std::string(
" No OpenCL platforms found!\n");
212 cl_platform_id *platforms = NULL;
213 platforms = (cl_platform_id*)malloc(ret_num_platforms *
sizeof(cl_platform_id));
217 ret = clGetPlatformIDs(ret_num_platforms, platforms, NULL);
224 for (
unsigned int i = 0; i < ret_num_platforms; i++) {
227 ret = clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME,
sizeof(textBuffer), textBuffer, NULL);
233 textBufferStr = textBuffer;
234 trimWS(textBufferStr);
235 devicesRet.append(
" * Platform ID: '").append(std::to_string(i)).append(
"', name: '").append(textBufferStr).append(
"'");
238 ret = clGetPlatformInfo(platforms[i], CL_PLATFORM_VERSION,
sizeof(textBuffer), textBuffer, NULL);
244 devicesRet.append(
" (").append(textBuffer).append(
")\n");
247 cl_uint ret_num_devices;
248 ret = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, 0, NULL, &ret_num_devices);
254 if (ret_num_devices == 0) {
255 devicesRet.append(
"No OpenCL devices found\n\n");
260 cl_device_id *devices = NULL;
261 devices = (cl_device_id*)malloc(ret_num_devices *
sizeof(cl_device_id));
262 if (devices == NULL) {
268 ret = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, ret_num_devices, devices, &ret_num_devices);
276 for (
unsigned int k = 0; k < ret_num_devices; k++) {
279 ret = clGetDeviceInfo(devices[k], CL_DEVICE_NAME,
sizeof(textBuffer), &textBuffer, NULL);
286 textBufferStr = textBuffer;
287 trimWS(textBufferStr);
288 devicesRet.append(
" * Device ID: '").append(std::to_string(k)).append(
"', name: '").append(textBufferStr).append(
"'\n");
292 devicesRet.append(
"\n");
std::string getTypeName(uint8_t dummy)
Returns the data type name.
Definition: oclengine.hpp:80
std::string getTypeName(uint32_t dummy)
Returns the data type name.
Definition: oclengine.hpp:90
std::string getTypeName(int16_t dummy)
Returns the data type name.
Definition: oclengine.hpp:83
std::string getTypeName(double dummy)
Returns the data type name.
Definition: oclengine.hpp:75
This header file contains exceptions.
std::string getTypeName(int64_t dummy)
Returns the data type name.
Definition: oclengine.hpp:93
std::string getTypeName(uint16_t dummy)
Returns the data type name.
Definition: oclengine.hpp:85
std::string getTypeName(float dummy)
Returns the data type name.
Definition: oclengine.hpp:73
std::string getTypeName(uint64_t dummy)
Returns the data type name.
Definition: oclengine.hpp:95
OpenCL base class template used in other SICAK plugins.
Definition: oclengine.hpp:51
static void trimWS(std::string &str)
Trims white space from string.
Definition: oclengine.hpp:187
std::string getTypeName(int8_t dummy)
Returns the data type name.
Definition: oclengine.hpp:78
OclEngine(unsigned int platform, unsigned int device)
Initializes the specified OpenCL device and creates an OpenCL command queue.
Definition: oclengine.hpp:104
std::string getTypeName(int32_t dummy)
Returns the data type name.
Definition: oclengine.hpp:88
An exception which cannot be directly influenced by the user, or predicted beforehand.
Definition: exceptions.hpp:76
static std::string queryDevices()
Query available platforms and devices.
Definition: oclengine.hpp:196