20 cl_ulong queuedTime = 0;
21 if (!
checkSuccess(clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_QUEUED,
sizeof(cl_ulong), &queuedTime, NULL)))
23 cerr <<
"Retrieving CL_PROFILING_COMMAND_QUEUED OpenCL profiling information failed. " << __FILE__ <<
":"<< __LINE__ << endl;
27 cl_ulong submittedTime = 0;
28 if (!
checkSuccess(clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_SUBMIT,
sizeof(cl_ulong), &submittedTime, NULL)))
30 cerr <<
"Retrieving CL_PROFILING_COMMAND_SUBMIT OpenCL profiling information failed. " << __FILE__ <<
":"<< __LINE__ << endl;
34 cl_ulong startTime = 0;
35 if (!
checkSuccess(clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_START,
sizeof(cl_ulong), &startTime, NULL)))
37 cerr <<
"Retrieving CL_PROFILING_COMMAND_START OpenCL profiling information failed. " << __FILE__ <<
":"<< __LINE__ << endl;
42 if (!
checkSuccess(clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_END,
sizeof(cl_ulong), &endTime, NULL)))
44 cerr <<
"Retrieving CL_PROFILING_COMMAND_END OpenCL profiling information failed. " << __FILE__ <<
":"<< __LINE__ << endl;
48 cout <<
"Profiling information:\n";
50 cout <<
"Queued time: \t" << (submittedTime - queuedTime) / 1000000.0 <<
"ms\n";
51 cout <<
"Wait time: \t" << (startTime - submittedTime) / 1000000.0 <<
"ms\n";
52 cout <<
"Run time: \t" << (endTime - startTime) / 1000000.0 <<
"ms" << endl;
60 cl_uint numberOfImageFormats;
61 if (!
checkSuccess(clGetSupportedImageFormats(context, 0, CL_MEM_OBJECT_IMAGE2D, 0, NULL, &numberOfImageFormats)))
63 cerr <<
"Getting the number of supported 2D image formats failed. " << __FILE__ <<
":"<< __LINE__ << endl;
68 cl_image_format* imageFormats =
new cl_image_format[numberOfImageFormats];
69 if (!
checkSuccess(clGetSupportedImageFormats(context, 0, CL_MEM_OBJECT_IMAGE3D, numberOfImageFormats, imageFormats, NULL)))
71 cerr <<
"Getting the list of supported 2D image formats failed. " << __FILE__ <<
":"<< __LINE__ << endl;
75 cout << numberOfImageFormats <<
" Image formats supported";
77 if (numberOfImageFormats > 0)
79 cout <<
" (channel order, channel data type):" << endl;
86 for (
unsigned int i = 0; i < numberOfImageFormats; i++)
91 delete[] imageFormats;
117 return "CL_INTENSITY";
119 return "CL_LUMINANCE";
127 return "Unknown image channel order";
133 switch (channelDataType)
136 return "CL_SNORM_INT8";
138 return "CL_SNORM_INT16";
140 return "CL_UNORM_INT8";
142 return "CL_UNORM_INT16";
143 case CL_UNORM_SHORT_565:
144 return "CL_UNORM_SHORT_565";
145 case CL_UNORM_SHORT_555:
146 return "CL_UNORM_SHORT_555";
147 case CL_UNORM_INT_101010:
148 return "CL_UNORM_INT_101010";
150 return "CL_SIGNED_INT8";
151 case CL_SIGNED_INT16:
152 return "CL_SIGNED_INT16";
153 case CL_SIGNED_INT32:
154 return "CL_SIGNED_INT32";
155 case CL_UNSIGNED_INT8:
156 return "CL_UNSIGNED_INT8";
157 case CL_UNSIGNED_INT16:
158 return "CL_UNSIGNED_INT16";
159 case CL_UNSIGNED_INT32:
160 return "CL_UNSIGNED_INT32";
162 return "CL_HALF_FLOAT";
166 return "Unknown image channel data type";
170 bool cleanUpOpenCL(cl_context context, cl_command_queue commandQueue, cl_program program, cl_kernel kernel, cl_mem* memoryObjects,
int numberOfMemoryObjects)
172 bool returnValue =
true;
177 cerr <<
"Releasing the OpenCL context failed. " << __FILE__ <<
":"<< __LINE__ << endl;
182 if (commandQueue != 0)
186 cerr <<
"Releasing the OpenCL command queue failed. " << __FILE__ <<
":"<< __LINE__ << endl;
195 cerr <<
"Releasing the OpenCL kernel failed. " << __FILE__ <<
":"<< __LINE__ << endl;
204 cerr <<
"Releasing the OpenCL program failed. " << __FILE__ <<
":"<< __LINE__ << endl;
209 for (
int index = 0; index < numberOfMemoryObjects; index++)
211 if (memoryObjects[index] != 0)
213 if (!
checkSuccess(clReleaseMemObject(memoryObjects[index])))
215 cerr <<
"Releasing the OpenCL memory object " << index <<
" failed. " << __FILE__ <<
":"<< __LINE__ << endl;
226 cl_int errorNumber = 0;
227 cl_uint numberOfPlatforms = 0;
228 cl_platform_id firstPlatformID = 0;
231 if (!
checkSuccess(clGetPlatformIDs(1, &firstPlatformID, &numberOfPlatforms)))
233 cerr <<
"Retrieving OpenCL platforms failed. " << __FILE__ <<
":"<< __LINE__ << endl;
237 if (numberOfPlatforms <= 0)
239 cerr <<
"No OpenCL platforms found. " << __FILE__ <<
":"<< __LINE__ << endl;
244 cl_context_properties contextProperties [] = {CL_CONTEXT_PLATFORM, (cl_context_properties)firstPlatformID, 0};
245 *context = clCreateContextFromType(contextProperties, CL_DEVICE_TYPE_GPU, NULL, NULL, &errorNumber);
248 cerr <<
"Creating an OpenCL context failed. " << __FILE__ <<
":"<< __LINE__ << endl;
257 cl_int errorNumber = 0;
258 cl_device_id* devices = NULL;
259 size_t deviceBufferSize = -1;
262 if (!
checkSuccess(clGetContextInfo(context, CL_CONTEXT_DEVICES, 0, NULL, &deviceBufferSize)))
264 cerr <<
"Failed to get OpenCL context information. " << __FILE__ <<
":"<< __LINE__ << endl;
268 if(deviceBufferSize == 0)
270 cerr <<
"No OpenCL devices found. " << __FILE__ <<
":"<< __LINE__ << endl;
275 devices =
new cl_device_id[deviceBufferSize /
sizeof(cl_device_id)];
276 if (!
checkSuccess(clGetContextInfo(context, CL_CONTEXT_DEVICES, deviceBufferSize, devices, NULL)))
278 cerr <<
"Failed to get the OpenCL context information. " << __FILE__ <<
":"<< __LINE__ << endl;
284 *device = devices[0];
288 *commandQueue = clCreateCommandQueue(context, *device, CL_QUEUE_PROFILING_ENABLE, &errorNumber);
291 cerr <<
"Failed to create the OpenCL command queue. " << __FILE__ <<
":"<< __LINE__ << endl;
298 bool createProgram(cl_context context, cl_device_id device,
string filename, cl_program* program)
300 cl_int errorNumber = 0;
301 ifstream kernelFile(filename.c_str(), ios::in);
303 if(!kernelFile.is_open())
305 cerr <<
"Unable to open " << filename <<
". " << __FILE__ <<
":"<< __LINE__ << endl;
313 ostringstream outputStringStream;
314 outputStringStream << kernelFile.rdbuf();
315 string srcStdStr = outputStringStream.str();
316 const char* charSource = srcStdStr.c_str();
318 *program = clCreateProgramWithSource(context, 1, &charSource, NULL, &errorNumber);
321 cerr <<
"Failed to create OpenCL program. " << __FILE__ <<
":"<< __LINE__ << endl;
326 bool buildSuccess =
checkSuccess(clBuildProgram(*program, 0, NULL, NULL, NULL, NULL));
330 clGetProgramBuildInfo(*program, device, CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
338 char* log =
new char[logSize];
339 clGetProgramBuildInfo(*program, device, CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
341 string* stringChars =
new string(log, logSize);
342 cerr <<
"Build log:\n " << *stringChars << endl;
350 clReleaseProgram(*program);
351 cerr <<
"Failed to build OpenCL program. " << __FILE__ <<
":"<< __LINE__ << endl;
360 if (errorNumber != CL_SUCCESS)
374 case CL_DEVICE_NOT_FOUND:
375 return "CL_DEVICE_NOT_FOUND";
376 case CL_DEVICE_NOT_AVAILABLE:
377 return "CL_DEVICE_NOT_AVAILABLE";
378 case CL_COMPILER_NOT_AVAILABLE:
379 return "CL_COMPILER_NOT_AVAILABLE";
380 case CL_MEM_OBJECT_ALLOCATION_FAILURE:
381 return "CL_MEM_OBJECT_ALLOCATION_FAILURE";
382 case CL_OUT_OF_RESOURCES:
383 return "CL_OUT_OF_RESOURCES";
384 case CL_OUT_OF_HOST_MEMORY:
385 return "CL_OUT_OF_HOST_MEMORY";
386 case CL_PROFILING_INFO_NOT_AVAILABLE:
387 return "CL_PROFILING_INFO_NOT_AVAILABLE";
388 case CL_MEM_COPY_OVERLAP:
389 return "CL_MEM_COPY_OVERLAP";
390 case CL_IMAGE_FORMAT_MISMATCH:
391 return "CL_IMAGE_FORMAT_MISMATCH";
392 case CL_IMAGE_FORMAT_NOT_SUPPORTED:
393 return "CL_IMAGE_FORMAT_NOT_SUPPORTED";
394 case CL_BUILD_PROGRAM_FAILURE:
395 return "CL_BUILD_PROGRAM_FAILURE";
397 return "CL_MAP_FAILURE";
398 case CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST:
399 return "CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST";
400 case CL_INVALID_VALUE:
401 return "CL_INVALID_VALUE";
402 case CL_INVALID_DEVICE_TYPE:
403 return "CL_INVALID_DEVICE_TYPE";
404 case CL_INVALID_PLATFORM:
405 return "CL_INVALID_PLATFORM";
406 case CL_INVALID_DEVICE:
407 return "CL_INVALID_DEVICE";
408 case CL_INVALID_CONTEXT:
409 return "CL_INVALID_CONTEXT";
410 case CL_INVALID_QUEUE_PROPERTIES:
411 return "CL_INVALID_QUEUE_PROPERTIES";
412 case CL_INVALID_COMMAND_QUEUE:
413 return "CL_INVALID_COMMAND_QUEUE";
414 case CL_INVALID_HOST_PTR:
415 return "CL_INVALID_HOST_PTR";
416 case CL_INVALID_MEM_OBJECT:
417 return "CL_INVALID_MEM_OBJECT";
418 case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR:
419 return "CL_INVALID_IMAGE_FORMAT_DESCRIPTOR";
420 case CL_INVALID_IMAGE_SIZE:
421 return "CL_INVALID_IMAGE_SIZE";
422 case CL_INVALID_SAMPLER:
423 return "CL_INVALID_SAMPLER";
424 case CL_INVALID_BINARY:
425 return "CL_INVALID_BINARY";
426 case CL_INVALID_BUILD_OPTIONS:
427 return "CL_INVALID_BUILD_OPTIONS";
428 case CL_INVALID_PROGRAM:
429 return "CL_INVALID_PROGRAM";
430 case CL_INVALID_PROGRAM_EXECUTABLE:
431 return "CL_INVALID_PROGRAM_EXECUTABLE";
432 case CL_INVALID_KERNEL_NAME:
433 return "CL_INVALID_KERNEL_NAME";
434 case CL_INVALID_KERNEL_DEFINITION:
435 return "CL_INVALID_KERNEL_DEFINITION";
436 case CL_INVALID_KERNEL:
437 return "CL_INVALID_KERNEL";
438 case CL_INVALID_ARG_INDEX:
439 return "CL_INVALID_ARG_INDEX";
440 case CL_INVALID_ARG_VALUE:
441 return "CL_INVALID_ARG_VALUE";
442 case CL_INVALID_ARG_SIZE:
443 return "CL_INVALID_ARG_SIZE";
444 case CL_INVALID_KERNEL_ARGS:
445 return "CL_INVALID_KERNEL_ARGS";
446 case CL_INVALID_WORK_DIMENSION:
447 return "CL_INVALID_WORK_DIMENSION";
448 case CL_INVALID_WORK_GROUP_SIZE:
449 return "CL_INVALID_WORK_GROUP_SIZE";
450 case CL_INVALID_WORK_ITEM_SIZE:
451 return "CL_INVALID_WORK_ITEM_SIZE";
452 case CL_INVALID_GLOBAL_OFFSET:
453 return "CL_INVALID_GLOBAL_OFFSET";
454 case CL_INVALID_EVENT_WAIT_LIST:
455 return "CL_INVALID_EVENT_WAIT_LIST";
456 case CL_INVALID_EVENT:
457 return "CL_INVALID_EVENT";
458 case CL_INVALID_OPERATION:
459 return "CL_INVALID_OPERATION";
460 case CL_INVALID_GL_OBJECT:
461 return "CL_INVALID_GL_OBJECT";
462 case CL_INVALID_BUFFER_SIZE:
463 return "CL_INVALID_BUFFER_SIZE";
464 case CL_INVALID_MIP_LEVEL:
465 return "CL_INVALID_MIP_LEVEL";
467 return "Unknown error";
473 if (extension.empty())
479 size_t extensionsReturnSize = 0;
480 if (!
checkSuccess(clGetDeviceInfo(device, CL_DEVICE_EXTENSIONS, 0, NULL, &extensionsReturnSize)))
482 cerr <<
"Failed to get return size from clGetDeviceInfo for parameter CL_DEVICE_EXTENSIONS. " << __FILE__ <<
":"<< __LINE__ << endl;
487 char* extensions =
new char[extensionsReturnSize];
490 if (!
checkSuccess(clGetDeviceInfo(device, CL_DEVICE_EXTENSIONS, extensionsReturnSize, extensions, NULL)))
492 cerr <<
"Failed to get data from clGetDeviceInfo for parameter CL_DEVICE_EXTENSIONS. " << __FILE__ <<
":"<< __LINE__ << endl;
493 delete [] extensions;
498 string* extensionsString =
new string(extensions);
499 bool returnResult =
false;
500 if (extensionsString->find(extension) != string::npos)
505 delete [] extensions;
506 delete extensionsString;