driver_opencl_utils.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  4. * Copyright (C) 2010, 2011 Université de Bordeaux 1
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <starpu.h>
  18. #include <sys/stat.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <sys/types.h>
  24. #include <starpu_opencl.h>
  25. #include <starpu_profiling.h>
  26. #include <core/workers.h>
  27. #include "driver_opencl_utils.h"
  28. #include "driver_opencl.h"
  29. #ifdef HAVE_CL_CL_EXT_H
  30. #include <CL/cl_ext.h>
  31. #endif
  32. char *_starpu_opencl_program_dir;
  33. #define _STARPU_STRINGIFY_(x) #x
  34. #define _STARPU_STRINGIFY(x) _STARPU_STRINGIFY_(x)
  35. static
  36. int _starpu_opencl_locate_file(const char *source_file_name, char *located_file_name) {
  37. _STARPU_DEBUG("Trying to locate <%s>\n", source_file_name);
  38. if (access(source_file_name, R_OK) == 0) {
  39. strcpy(located_file_name, source_file_name);
  40. return EXIT_SUCCESS;
  41. }
  42. if (_starpu_opencl_program_dir) {
  43. sprintf(located_file_name, "%s/%s", _starpu_opencl_program_dir, source_file_name);
  44. _STARPU_DEBUG("Trying to locate <%s>\n", located_file_name);
  45. if (access(located_file_name, R_OK) == 0) return EXIT_SUCCESS;
  46. }
  47. sprintf(located_file_name, "%s/%s", _STARPU_STRINGIFY(STARPU_OPENCL_DATADIR), source_file_name);
  48. _STARPU_DEBUG("Trying to locate <%s>\n", located_file_name);
  49. if (access(located_file_name, R_OK) == 0) return EXIT_SUCCESS;
  50. sprintf(located_file_name, "%s/%s", STARPU_SRC_DIR, source_file_name);
  51. _STARPU_DEBUG("Trying to locate <%s>\n", located_file_name);
  52. if (access(located_file_name, R_OK) == 0) return EXIT_SUCCESS;
  53. strcpy(located_file_name, "");
  54. _STARPU_ERROR("Cannot locate file <%s>\n", source_file_name);
  55. return EXIT_FAILURE;
  56. }
  57. cl_int starpu_opencl_load_kernel(cl_kernel *kernel, cl_command_queue *queue, struct starpu_opencl_program *opencl_programs,
  58. const char *kernel_name, int devid)
  59. {
  60. cl_int err;
  61. cl_device_id device;
  62. cl_context context;
  63. cl_program program;
  64. starpu_opencl_get_device(devid, &device);
  65. starpu_opencl_get_context(devid, &context);
  66. starpu_opencl_get_queue(devid, queue);
  67. program = opencl_programs->programs[devid];
  68. if (!program) {
  69. _STARPU_DISP("Program not available\n");
  70. return CL_INVALID_PROGRAM;
  71. }
  72. // Create the compute kernel in the program we wish to run
  73. *kernel = clCreateKernel(program, kernel_name, &err);
  74. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  75. return CL_SUCCESS;
  76. }
  77. cl_int starpu_opencl_release_kernel(cl_kernel kernel) {
  78. cl_int err;
  79. err = clReleaseKernel(kernel);
  80. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  81. return CL_SUCCESS;
  82. }
  83. static
  84. char *_starpu_opencl_load_program_source(const char *filename)
  85. {
  86. struct stat statbuf;
  87. FILE *fh;
  88. char *source;
  89. int x;
  90. char c;
  91. fh = fopen(filename, "r");
  92. if (fh == 0)
  93. return NULL;
  94. stat(filename, &statbuf);
  95. source = (char *) malloc(statbuf.st_size + 1);
  96. for(c=fgetc(fh), x=0 ; c != EOF ; c = fgetc(fh), x++) {
  97. source[x] = c;
  98. }
  99. source[x] = '\0';
  100. _STARPU_DEBUG("OpenCL kernel <%s>\n", source);
  101. fclose(fh);
  102. return source;
  103. }
  104. int starpu_opencl_load_opencl_from_string(const char *opencl_program_source, struct starpu_opencl_program *opencl_programs,
  105. const char* build_options)
  106. {
  107. unsigned int dev;
  108. unsigned int nb_devices;
  109. nb_devices = _starpu_opencl_get_device_count();
  110. // Iterate over each device
  111. for(dev = 0; dev < nb_devices; dev ++) {
  112. cl_device_id device;
  113. cl_context context;
  114. cl_program program;
  115. cl_int err;
  116. starpu_opencl_get_device(dev, &device);
  117. starpu_opencl_get_context(dev, &context);
  118. if (context == NULL) {
  119. _STARPU_DEBUG("[%d] is not a valid OpenCL context\n", dev);
  120. continue;
  121. }
  122. opencl_programs->programs[dev] = NULL;
  123. if (context == NULL) continue;
  124. // Create the compute program from the source buffer
  125. program = clCreateProgramWithSource(context, 1, (const char **) &opencl_program_source, NULL, &err);
  126. if (!program || err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  127. // Build the program executable
  128. err = clBuildProgram(program, 1, &device, build_options, NULL, NULL);
  129. if (err != CL_SUCCESS) {
  130. size_t len;
  131. static char buffer[4096];
  132. _STARPU_DISP("Error: Failed to build program executable!\n");
  133. clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &len);
  134. _STARPU_DISP("<%s>\n", buffer);
  135. return EXIT_FAILURE;
  136. }
  137. // Store program
  138. opencl_programs->programs[dev] = program;
  139. }
  140. return EXIT_SUCCESS;
  141. }
  142. int starpu_opencl_load_opencl_from_file(const char *source_file_name, struct starpu_opencl_program *opencl_programs,
  143. const char* build_options)
  144. {
  145. int nb_devices;
  146. char located_file_name[1024];
  147. // Do not try to load and compile the file if there is no devices
  148. nb_devices = _starpu_opencl_get_device_count();
  149. if (nb_devices == 0) return EXIT_SUCCESS;
  150. // Locate source file
  151. _starpu_opencl_locate_file(source_file_name, located_file_name);
  152. _STARPU_DEBUG("Source file name : <%s>\n", located_file_name);
  153. // Load the compute program from disk into a cstring buffer
  154. char *opencl_program_source = _starpu_opencl_load_program_source(located_file_name);
  155. if(!opencl_program_source)
  156. _STARPU_ERROR("Failed to load compute program from file <%s>!\n", located_file_name);
  157. return starpu_opencl_load_opencl_from_string(opencl_program_source, opencl_programs, build_options);
  158. }
  159. cl_int starpu_opencl_unload_opencl(struct starpu_opencl_program *opencl_programs)
  160. {
  161. unsigned int dev;
  162. unsigned int nb_devices;
  163. nb_devices = _starpu_opencl_get_device_count();
  164. // Iterate over each device
  165. for(dev = 0; dev < nb_devices; dev ++) {
  166. if (opencl_programs->programs[dev])
  167. clReleaseProgram(opencl_programs->programs[dev]);
  168. }
  169. return CL_SUCCESS;
  170. }
  171. int starpu_opencl_collect_stats(cl_event event STARPU_ATTRIBUTE_UNUSED)
  172. {
  173. #if defined(CL_PROFILING_CLOCK_CYCLE_COUNT)||defined(CL_PROFILING_STALL_CYCLE_COUNT)||defined(CL_PROFILING_POWER_CONSUMED)
  174. struct starpu_task *task = starpu_get_current_task();
  175. struct starpu_task_profiling_info *info = task->profiling_info;
  176. #endif
  177. #ifdef CL_PROFILING_CLOCK_CYCLE_COUNT
  178. if (starpu_profiling_status_get() && info) {
  179. cl_int err;
  180. unsigned int clock_cycle_count;
  181. size_t size;
  182. err = clGetEventProfilingInfo(event, CL_PROFILING_CLOCK_CYCLE_COUNT, sizeof(clock_cycle_count), &clock_cycle_count, &size);
  183. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  184. STARPU_ASSERT(size == sizeof(clock_cycle_count));
  185. info->used_cycles += clock_cycle_count;
  186. }
  187. #endif
  188. #ifdef CL_PROFILING_STALL_CYCLE_COUNT
  189. if (starpu_profiling_status_get() && info) {
  190. cl_int err;
  191. unsigned int stall_cycle_count;
  192. size_t size;
  193. err = clGetEventProfilingInfo(event, CL_PROFILING_STALL_CYCLE_COUNT, sizeof(stall_cycle_count), &stall_cycle_count, &size);
  194. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  195. STARPU_ASSERT(size == sizeof(stall_cycle_count));
  196. info->stall_cycles += stall_cycle_count;
  197. }
  198. #endif
  199. #ifdef CL_PROFILING_POWER_CONSUMED
  200. if (info && (starpu_profiling_status_get() || (task->cl && task->cl->power_model && task->cl->power_model->benchmarking))) {
  201. cl_int err;
  202. double power_consumed;
  203. size_t size;
  204. err = clGetEventProfilingInfo(event, CL_PROFILING_POWER_CONSUMED, sizeof(power_consumed), &power_consumed, &size);
  205. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  206. STARPU_ASSERT(size == sizeof(power_consumed));
  207. info->power_consumed += power_consumed;
  208. }
  209. #endif
  210. return 0;
  211. }
  212. void starpu_opencl_display_error(const char *func, const char* msg, cl_int status)
  213. {
  214. const char *errormsg;
  215. switch (status) {
  216. case CL_SUCCESS:
  217. errormsg = "success";
  218. break;
  219. case CL_DEVICE_NOT_FOUND:
  220. errormsg = "Device not found";
  221. break;
  222. case CL_DEVICE_NOT_AVAILABLE:
  223. errormsg = "Device not available";
  224. break;
  225. case CL_COMPILER_NOT_AVAILABLE:
  226. errormsg = "Compiler not available";
  227. break;
  228. case CL_MEM_OBJECT_ALLOCATION_FAILURE:
  229. errormsg = "Memory object allocation failure";
  230. break;
  231. case CL_OUT_OF_RESOURCES:
  232. errormsg = "Out of resources";
  233. break;
  234. case CL_OUT_OF_HOST_MEMORY:
  235. errormsg = "Out of host memory";
  236. break;
  237. case CL_PROFILING_INFO_NOT_AVAILABLE:
  238. errormsg = "Profiling info not available";
  239. break;
  240. case CL_MEM_COPY_OVERLAP:
  241. errormsg = "Memory copy overlap";
  242. break;
  243. case CL_IMAGE_FORMAT_MISMATCH:
  244. errormsg = "Image format mismatch";
  245. break;
  246. case CL_IMAGE_FORMAT_NOT_SUPPORTED:
  247. errormsg = "Image format not supported";
  248. break;
  249. case CL_BUILD_PROGRAM_FAILURE:
  250. errormsg = "Build program failure";
  251. break;
  252. case CL_MAP_FAILURE:
  253. errormsg = "Map failure";
  254. break;
  255. case CL_INVALID_VALUE:
  256. errormsg = "Invalid value";
  257. break;
  258. case CL_INVALID_DEVICE_TYPE:
  259. errormsg = "Invalid device type";
  260. break;
  261. case CL_INVALID_PLATFORM:
  262. errormsg = "Invalid platform";
  263. break;
  264. case CL_INVALID_DEVICE:
  265. errormsg = "Invalid device";
  266. break;
  267. case CL_INVALID_CONTEXT:
  268. errormsg = "Invalid context";
  269. break;
  270. case CL_INVALID_QUEUE_PROPERTIES:
  271. errormsg = "Invalid queue properties";
  272. break;
  273. case CL_INVALID_COMMAND_QUEUE:
  274. errormsg = "Invalid command queue";
  275. break;
  276. case CL_INVALID_HOST_PTR:
  277. errormsg = "Invalid host pointer";
  278. break;
  279. case CL_INVALID_MEM_OBJECT:
  280. errormsg = "Invalid memory object";
  281. break;
  282. case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR:
  283. errormsg = "Invalid image format descriptor";
  284. break;
  285. case CL_INVALID_IMAGE_SIZE:
  286. errormsg = "Invalid image size";
  287. break;
  288. case CL_INVALID_SAMPLER:
  289. errormsg = "Invalid sampler";
  290. break;
  291. case CL_INVALID_BINARY:
  292. errormsg = "Invalid binary";
  293. break;
  294. case CL_INVALID_BUILD_OPTIONS:
  295. errormsg = "Invalid build options";
  296. break;
  297. case CL_INVALID_PROGRAM:
  298. errormsg = "Invalid program";
  299. break;
  300. case CL_INVALID_PROGRAM_EXECUTABLE:
  301. errormsg = "Invalid program executable";
  302. break;
  303. case CL_INVALID_KERNEL_NAME:
  304. errormsg = "Invalid kernel name";
  305. break;
  306. case CL_INVALID_KERNEL_DEFINITION:
  307. errormsg = "Invalid kernel definition";
  308. break;
  309. case CL_INVALID_KERNEL:
  310. errormsg = "Invalid kernel";
  311. break;
  312. case CL_INVALID_ARG_INDEX:
  313. errormsg = "Invalid argument index";
  314. break;
  315. case CL_INVALID_ARG_VALUE:
  316. errormsg = "Invalid argument value";
  317. break;
  318. case CL_INVALID_ARG_SIZE:
  319. errormsg = "Invalid argument size";
  320. break;
  321. case CL_INVALID_KERNEL_ARGS:
  322. errormsg = "Invalid kernel arguments";
  323. break;
  324. case CL_INVALID_WORK_DIMENSION:
  325. errormsg = "Invalid work dimension";
  326. break;
  327. case CL_INVALID_WORK_GROUP_SIZE:
  328. errormsg = "Invalid work group size";
  329. break;
  330. case CL_INVALID_WORK_ITEM_SIZE:
  331. errormsg = "Invalid work item size";
  332. break;
  333. case CL_INVALID_GLOBAL_OFFSET:
  334. errormsg = "Invalid global offset";
  335. break;
  336. case CL_INVALID_EVENT_WAIT_LIST:
  337. errormsg = "Invalid event wait list";
  338. break;
  339. case CL_INVALID_EVENT:
  340. errormsg = "Invalid event";
  341. break;
  342. case CL_INVALID_OPERATION:
  343. errormsg = "Invalid operation";
  344. break;
  345. case CL_INVALID_GL_OBJECT:
  346. errormsg = "Invalid GL object";
  347. break;
  348. case CL_INVALID_BUFFER_SIZE:
  349. errormsg = "Invalid buffer size";
  350. break;
  351. case CL_INVALID_MIP_LEVEL:
  352. errormsg = "Invalid MIP level";
  353. break;
  354. #ifdef CL_PLATFORM_NOT_FOUND_KHR
  355. case CL_PLATFORM_NOT_FOUND_KHR:
  356. errormsg = "Platform not found";
  357. break;
  358. #endif
  359. default:
  360. errormsg = "unknown error";
  361. break;
  362. }
  363. if (msg)
  364. printf("oops in %s (%s) ... <%s> (%d) \n", func, msg, errormsg, status);
  365. else
  366. printf("oops in %s ... <%s> (%d) \n", func, errormsg, status);
  367. }