clutil.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #ifndef __CLUTIL_H
  2. #define __CLUTIL_H
  3. #include <stdio.h>
  4. #include <CL/cl.h>
  5. #ifndef CLUTIL_NAME_LEN
  6. #define CLUTIL_NAME_LEN 100
  7. #endif
  8. const char *getDeviceTypeString(cl_device_type type) {
  9. switch (type) {
  10. case CL_DEVICE_TYPE_CPU: return "CPU";
  11. case CL_DEVICE_TYPE_GPU: return "GPU";
  12. case CL_DEVICE_TYPE_ACCELERATOR: return "Accelerator";
  13. case CL_DEVICE_TYPE_CUSTOM: return "Custom";
  14. default: return "Unknown";
  15. }
  16. }
  17. typedef struct _device_info {
  18. cl_device_type type;
  19. char vendor[CLUTIL_NAME_LEN];
  20. char name[CLUTIL_NAME_LEN];
  21. char version[CLUTIL_NAME_LEN];
  22. cl_uint compute_units;
  23. char *info_str;
  24. } device_info;
  25. device_info *getDeviceInfo(cl_device_id id) {
  26. device_info *dev_info = (device_info *)malloc(sizeof(device_info));
  27. clGetDeviceInfo(id, CL_DEVICE_TYPE, sizeof(cl_device_type), &dev_info->type, NULL);
  28. clGetDeviceInfo(id, CL_DEVICE_VENDOR, CLUTIL_NAME_LEN, &dev_info->vendor, NULL);
  29. clGetDeviceInfo(id, CL_DEVICE_NAME, CLUTIL_NAME_LEN, &dev_info->name, NULL);
  30. clGetDeviceInfo(id, CL_DEVICE_VERSION, CLUTIL_NAME_LEN, &dev_info->version, NULL);
  31. clGetDeviceInfo(id, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(cl_uint), &dev_info->compute_units, NULL);
  32. size_t len = (CLUTIL_NAME_LEN + 1) * 3 + 20;
  33. dev_info->info_str = (char *)malloc(len);
  34. snprintf(dev_info->info_str, len, "%s %s %s %s, %u CUs",
  35. getDeviceTypeString(dev_info->type),
  36. dev_info->vendor,
  37. dev_info->name,
  38. dev_info->version,
  39. dev_info->compute_units
  40. );
  41. return dev_info;
  42. }
  43. char *getPlatformInfo(cl_platform_id id) {
  44. char vendor[CLUTIL_NAME_LEN];
  45. clGetPlatformInfo(id, CL_PLATFORM_VENDOR, CLUTIL_NAME_LEN, vendor, NULL);
  46. char name[CLUTIL_NAME_LEN];
  47. clGetPlatformInfo(id, CL_PLATFORM_NAME, CLUTIL_NAME_LEN, name, NULL);
  48. char version[CLUTIL_NAME_LEN];
  49. clGetPlatformInfo(id, CL_PLATFORM_VERSION, CLUTIL_NAME_LEN, version, NULL);
  50. char profile[CLUTIL_NAME_LEN];
  51. clGetPlatformInfo(id, CL_PLATFORM_PROFILE, CLUTIL_NAME_LEN, profile, NULL);
  52. char extensions[CLUTIL_NAME_LEN];
  53. clGetPlatformInfo(id, CL_PLATFORM_EXTENSIONS, CLUTIL_NAME_LEN, extensions, NULL);
  54. size_t len = (CLUTIL_NAME_LEN + 1) * 5 + 20;
  55. char *info = (char *)malloc(len);
  56. snprintf(info, len, "%s %s %s %s %s", vendor, name, version, profile, extensions);
  57. return info;
  58. }
  59. const char *getErrorString(cl_int error)
  60. {
  61. switch(error){
  62. // run-time and JIT compiler errors
  63. case 0: return "CL_SUCCESS";
  64. case -1: return "CL_DEVICE_NOT_FOUND";
  65. case -2: return "CL_DEVICE_NOT_AVAILABLE";
  66. case -3: return "CL_COMPILER_NOT_AVAILABLE";
  67. case -4: return "CL_MEM_OBJECT_ALLOCATION_FAILURE";
  68. case -5: return "CL_OUT_OF_RESOURCES";
  69. case -6: return "CL_OUT_OF_HOST_MEMORY";
  70. case -7: return "CL_PROFILING_INFO_NOT_AVAILABLE";
  71. case -8: return "CL_MEM_COPY_OVERLAP";
  72. case -9: return "CL_IMAGE_FORMAT_MISMATCH";
  73. case -10: return "CL_IMAGE_FORMAT_NOT_SUPPORTED";
  74. case -11: return "CL_BUILD_PROGRAM_FAILURE";
  75. case -12: return "CL_MAP_FAILURE";
  76. case -13: return "CL_MISALIGNED_SUB_BUFFER_OFFSET";
  77. case -14: return "CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST";
  78. case -15: return "CL_COMPILE_PROGRAM_FAILURE";
  79. case -16: return "CL_LINKER_NOT_AVAILABLE";
  80. case -17: return "CL_LINK_PROGRAM_FAILURE";
  81. case -18: return "CL_DEVICE_PARTITION_FAILED";
  82. case -19: return "CL_KERNEL_ARG_INFO_NOT_AVAILABLE";
  83. // compile-time errors
  84. case -30: return "CL_INVALID_VALUE";
  85. case -31: return "CL_INVALID_DEVICE_TYPE";
  86. case -32: return "CL_INVALID_PLATFORM";
  87. case -33: return "CL_INVALID_DEVICE";
  88. case -34: return "CL_INVALID_CONTEXT";
  89. case -35: return "CL_INVALID_QUEUE_PROPERTIES";
  90. case -36: return "CL_INVALID_COMMAND_QUEUE";
  91. case -37: return "CL_INVALID_HOST_PTR";
  92. case -38: return "CL_INVALID_MEM_OBJECT";
  93. case -39: return "CL_INVALID_IMAGE_FORMAT_DESCRIPTOR";
  94. case -40: return "CL_INVALID_IMAGE_SIZE";
  95. case -41: return "CL_INVALID_SAMPLER";
  96. case -42: return "CL_INVALID_BINARY";
  97. case -43: return "CL_INVALID_BUILD_OPTIONS";
  98. case -44: return "CL_INVALID_PROGRAM";
  99. case -45: return "CL_INVALID_PROGRAM_EXECUTABLE";
  100. case -46: return "CL_INVALID_KERNEL_NAME";
  101. case -47: return "CL_INVALID_KERNEL_DEFINITION";
  102. case -48: return "CL_INVALID_KERNEL";
  103. case -49: return "CL_INVALID_ARG_INDEX";
  104. case -50: return "CL_INVALID_ARG_VALUE";
  105. case -51: return "CL_INVALID_ARG_SIZE";
  106. case -52: return "CL_INVALID_KERNEL_ARGS";
  107. case -53: return "CL_INVALID_WORK_DIMENSION";
  108. case -54: return "CL_INVALID_WORK_GROUP_SIZE";
  109. case -55: return "CL_INVALID_WORK_ITEM_SIZE";
  110. case -56: return "CL_INVALID_GLOBAL_OFFSET";
  111. case -57: return "CL_INVALID_EVENT_WAIT_LIST";
  112. case -58: return "CL_INVALID_EVENT";
  113. case -59: return "CL_INVALID_OPERATION";
  114. case -60: return "CL_INVALID_GL_OBJECT";
  115. case -61: return "CL_INVALID_BUFFER_SIZE";
  116. case -62: return "CL_INVALID_MIP_LEVEL";
  117. case -63: return "CL_INVALID_GLOBAL_WORK_SIZE";
  118. case -64: return "CL_INVALID_PROPERTY";
  119. case -65: return "CL_INVALID_IMAGE_DESCRIPTOR";
  120. case -66: return "CL_INVALID_COMPILER_OPTIONS";
  121. case -67: return "CL_INVALID_LINKER_OPTIONS";
  122. case -68: return "CL_INVALID_DEVICE_PARTITION_COUNT";
  123. // extension errors
  124. case -1000: return "CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR";
  125. case -1001: return "CL_PLATFORM_NOT_FOUND_KHR";
  126. case -1002: return "CL_INVALID_D3D10_DEVICE_KHR";
  127. case -1003: return "CL_INVALID_D3D10_RESOURCE_KHR";
  128. case -1004: return "CL_D3D10_RESOURCE_ALREADY_ACQUIRED_KHR";
  129. case -1005: return "CL_D3D10_RESOURCE_NOT_ACQUIRED_KHR";
  130. default: return "Unknown OpenCL error";
  131. }
  132. }
  133. static inline void check(cl_int code, char *prefix) {
  134. if (code == CL_SUCCESS) return;
  135. fprintf(stderr, "%s: %s\n", prefix ? prefix : "An OpenCL error occured", getErrorString(code));
  136. exit(code);
  137. }
  138. static inline char *readFile(const char *name) {
  139. FILE *fp;
  140. char *source_str;
  141. size_t program_size;
  142. fp = fopen(name, "rb");
  143. if (!fp) {
  144. printf("Failed to load kernel\n");
  145. return NULL;
  146. }
  147. fseek(fp, 0, SEEK_END);
  148. program_size = ftell(fp);
  149. rewind(fp);
  150. source_str = (char*)malloc(program_size + 1);
  151. source_str[program_size] = '\0';
  152. fread(source_str, sizeof(char), program_size, fp);
  153. fclose(fp);
  154. return source_str;
  155. }
  156. static inline char *getBuildLog(cl_program program, cl_device_id device) {
  157. size_t log_size;
  158. clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
  159. char *log = (char *) malloc(log_size);
  160. clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, log_size, log, NULL);
  161. return log;
  162. }
  163. static inline char *getLog(cl_program program, cl_device_id device) {
  164. size_t log_size;
  165. clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
  166. char *log = (char *) malloc(log_size);
  167. clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, log_size, log, NULL);
  168. return log;
  169. }
  170. #endif