driver_opencl_utils.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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, char *located_dir_name)
  37. {
  38. int ret = EXIT_FAILURE;
  39. _STARPU_DEBUG("Trying to locate <%s>\n", source_file_name);
  40. if (access(source_file_name, R_OK) == 0)
  41. {
  42. strcpy(located_file_name, source_file_name);
  43. ret = EXIT_SUCCESS;
  44. }
  45. if (ret == EXIT_FAILURE && _starpu_opencl_program_dir)
  46. {
  47. sprintf(located_file_name, "%s/%s", _starpu_opencl_program_dir, source_file_name);
  48. _STARPU_DEBUG("Trying to locate <%s>\n", located_file_name);
  49. if (access(located_file_name, R_OK) == 0) ret = EXIT_SUCCESS;
  50. }
  51. if (ret == EXIT_FAILURE)
  52. {
  53. sprintf(located_file_name, "%s/%s", _STARPU_STRINGIFY(STARPU_OPENCL_DATADIR), source_file_name);
  54. _STARPU_DEBUG("Trying to locate <%s>\n", located_file_name);
  55. if (access(located_file_name, R_OK) == 0) ret = EXIT_SUCCESS;
  56. }
  57. if (ret == EXIT_FAILURE)
  58. {
  59. sprintf(located_file_name, "%s/%s", STARPU_SRC_DIR, source_file_name);
  60. _STARPU_DEBUG("Trying to locate <%s>\n", located_file_name);
  61. if (access(located_file_name, R_OK) == 0) ret = EXIT_SUCCESS;
  62. }
  63. if (ret == EXIT_FAILURE)
  64. {
  65. strcpy(located_file_name, "");
  66. strcpy(located_dir_name, "");
  67. _STARPU_ERROR("Cannot locate file <%s>\n", source_file_name);
  68. }
  69. else
  70. {
  71. char *last = strrchr(located_file_name, '/');
  72. if (!last) strcpy(located_dir_name, "");
  73. else
  74. {
  75. sprintf(located_dir_name, "%s", located_file_name);
  76. located_dir_name[strlen(located_file_name)-strlen(last)+1] = '\0';
  77. }
  78. }
  79. return ret;
  80. }
  81. cl_int starpu_opencl_load_kernel(cl_kernel *kernel, cl_command_queue *queue, struct starpu_opencl_program *opencl_programs,
  82. const char *kernel_name, int devid)
  83. {
  84. cl_int err;
  85. cl_device_id device;
  86. cl_context context;
  87. cl_program program;
  88. starpu_opencl_get_device(devid, &device);
  89. starpu_opencl_get_context(devid, &context);
  90. starpu_opencl_get_queue(devid, queue);
  91. program = opencl_programs->programs[devid];
  92. if (!program)
  93. {
  94. _STARPU_DISP("Program not available\n");
  95. return CL_INVALID_PROGRAM;
  96. }
  97. // Create the compute kernel in the program we wish to run
  98. *kernel = clCreateKernel(program, kernel_name, &err);
  99. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  100. return CL_SUCCESS;
  101. }
  102. cl_int starpu_opencl_release_kernel(cl_kernel kernel)
  103. {
  104. cl_int err;
  105. err = clReleaseKernel(kernel);
  106. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  107. return CL_SUCCESS;
  108. }
  109. static
  110. char *_starpu_opencl_load_program_source(const char *filename)
  111. {
  112. struct stat statbuf;
  113. FILE *fh;
  114. char *source;
  115. int x;
  116. char c;
  117. fh = fopen(filename, "r");
  118. if (fh == 0)
  119. return NULL;
  120. stat(filename, &statbuf);
  121. source = (char *) malloc(statbuf.st_size + 1);
  122. for(c=fgetc(fh), x=0 ; c != EOF ; c = fgetc(fh), x++)
  123. {
  124. source[x] = c;
  125. }
  126. source[x] = '\0';
  127. _STARPU_DEBUG("OpenCL kernel <%s>\n", source);
  128. fclose(fh);
  129. return source;
  130. }
  131. int starpu_opencl_load_opencl_from_string(const char *opencl_program_source, struct starpu_opencl_program *opencl_programs,
  132. const char* build_options)
  133. {
  134. unsigned int dev;
  135. unsigned int nb_devices;
  136. nb_devices = _starpu_opencl_get_device_count();
  137. // Iterate over each device
  138. for(dev = 0; dev < nb_devices; dev ++)
  139. {
  140. cl_device_id device;
  141. cl_context context;
  142. cl_program program;
  143. cl_int err;
  144. starpu_opencl_get_device(dev, &device);
  145. starpu_opencl_get_context(dev, &context);
  146. if (context == NULL)
  147. {
  148. _STARPU_DEBUG("[%d] is not a valid OpenCL context\n", dev);
  149. continue;
  150. }
  151. opencl_programs->programs[dev] = NULL;
  152. if (context == NULL) continue;
  153. // Create the compute program from the source buffer
  154. program = clCreateProgramWithSource(context, 1, (const char **) &opencl_program_source, NULL, &err);
  155. if (!program || err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  156. // Build the program executable
  157. err = clBuildProgram(program, 1, &device, build_options, NULL, NULL);
  158. if (err != CL_SUCCESS)
  159. {
  160. size_t len;
  161. static char buffer[4096];
  162. _STARPU_DISP("Error: Failed to build program executable!\n");
  163. clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &len);
  164. _STARPU_DISP("<%s>\n", buffer);
  165. return EXIT_FAILURE;
  166. }
  167. // Store program
  168. opencl_programs->programs[dev] = program;
  169. }
  170. return EXIT_SUCCESS;
  171. }
  172. int starpu_opencl_load_opencl_from_file(const char *source_file_name, struct starpu_opencl_program *opencl_programs,
  173. const char* build_options)
  174. {
  175. int nb_devices;
  176. char located_file_name[1024];
  177. char located_dir_name[1024];
  178. char new_build_options[1024];
  179. // Do not try to load and compile the file if there is no devices
  180. nb_devices = starpu_opencl_worker_get_count();
  181. if (nb_devices == 0) return EXIT_SUCCESS;
  182. // Locate source file
  183. _starpu_opencl_locate_file(source_file_name, located_file_name, located_dir_name);
  184. _STARPU_DEBUG("Source file name : <%s>\n", located_file_name);
  185. _STARPU_DEBUG("Source directory name : <%s>\n", located_dir_name);
  186. // Load the compute program from disk into a cstring buffer
  187. char *opencl_program_source = _starpu_opencl_load_program_source(located_file_name);
  188. if(!opencl_program_source)
  189. _STARPU_ERROR("Failed to load compute program from file <%s>!\n", located_file_name);
  190. if (!strcmp(located_dir_name, ""))
  191. strcpy(new_build_options, build_options);
  192. else if (build_options)
  193. sprintf(new_build_options, "-I %s %s", located_dir_name, build_options);
  194. else
  195. sprintf(new_build_options, "-I %s", located_dir_name);
  196. _STARPU_DEBUG("Build options: <%s>\n", new_build_options);
  197. return starpu_opencl_load_opencl_from_string(opencl_program_source, opencl_programs, new_build_options);
  198. }
  199. cl_int starpu_opencl_unload_opencl(struct starpu_opencl_program *opencl_programs)
  200. {
  201. unsigned int dev;
  202. unsigned int nb_devices;
  203. if (!starpu_opencl_worker_get_count())
  204. return CL_SUCCESS;
  205. nb_devices = _starpu_opencl_get_device_count();
  206. // Iterate over each device
  207. for(dev = 0; dev < nb_devices; dev ++)
  208. {
  209. if (opencl_programs->programs[dev])
  210. clReleaseProgram(opencl_programs->programs[dev]);
  211. }
  212. return CL_SUCCESS;
  213. }
  214. int starpu_opencl_collect_stats(cl_event event STARPU_ATTRIBUTE_UNUSED)
  215. {
  216. #if defined(CL_PROFILING_CLOCK_CYCLE_COUNT)||defined(CL_PROFILING_STALL_CYCLE_COUNT)||defined(CL_PROFILING_POWER_CONSUMED)
  217. struct starpu_task *task = starpu_get_current_task();
  218. struct starpu_task_profiling_info *info = task->profiling_info;
  219. #endif
  220. #ifdef CL_PROFILING_CLOCK_CYCLE_COUNT
  221. if (starpu_profiling_status_get() && info)
  222. {
  223. cl_int err;
  224. unsigned int clock_cycle_count;
  225. size_t size;
  226. err = clGetEventProfilingInfo(event, CL_PROFILING_CLOCK_CYCLE_COUNT, sizeof(clock_cycle_count), &clock_cycle_count, &size);
  227. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  228. STARPU_ASSERT(size == sizeof(clock_cycle_count));
  229. info->used_cycles += clock_cycle_count;
  230. }
  231. #endif
  232. #ifdef CL_PROFILING_STALL_CYCLE_COUNT
  233. if (starpu_profiling_status_get() && info)
  234. {
  235. cl_int err;
  236. unsigned int stall_cycle_count;
  237. size_t size;
  238. err = clGetEventProfilingInfo(event, CL_PROFILING_STALL_CYCLE_COUNT, sizeof(stall_cycle_count), &stall_cycle_count, &size);
  239. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  240. STARPU_ASSERT(size == sizeof(stall_cycle_count));
  241. info->stall_cycles += stall_cycle_count;
  242. }
  243. #endif
  244. #ifdef CL_PROFILING_POWER_CONSUMED
  245. if (info && (starpu_profiling_status_get() || (task->cl && task->cl->power_model && task->cl->power_model->benchmarking)))
  246. {
  247. cl_int err;
  248. double power_consumed;
  249. size_t size;
  250. err = clGetEventProfilingInfo(event, CL_PROFILING_POWER_CONSUMED, sizeof(power_consumed), &power_consumed, &size);
  251. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  252. STARPU_ASSERT(size == sizeof(power_consumed));
  253. info->power_consumed += power_consumed;
  254. }
  255. #endif
  256. return 0;
  257. }
  258. void starpu_opencl_display_error(const char *func, const char *file, int line, const char* msg, cl_int status)
  259. {
  260. const char *errormsg;
  261. switch (status)
  262. {
  263. case CL_SUCCESS:
  264. errormsg = "success";
  265. break;
  266. case CL_DEVICE_NOT_FOUND:
  267. errormsg = "Device not found";
  268. break;
  269. case CL_DEVICE_NOT_AVAILABLE:
  270. errormsg = "Device not available";
  271. break;
  272. case CL_COMPILER_NOT_AVAILABLE:
  273. errormsg = "Compiler not available";
  274. break;
  275. case CL_MEM_OBJECT_ALLOCATION_FAILURE:
  276. errormsg = "Memory object allocation failure";
  277. break;
  278. case CL_OUT_OF_RESOURCES:
  279. errormsg = "Out of resources";
  280. break;
  281. case CL_OUT_OF_HOST_MEMORY:
  282. errormsg = "Out of host memory";
  283. break;
  284. case CL_PROFILING_INFO_NOT_AVAILABLE:
  285. errormsg = "Profiling info not available";
  286. break;
  287. case CL_MEM_COPY_OVERLAP:
  288. errormsg = "Memory copy overlap";
  289. break;
  290. case CL_IMAGE_FORMAT_MISMATCH:
  291. errormsg = "Image format mismatch";
  292. break;
  293. case CL_IMAGE_FORMAT_NOT_SUPPORTED:
  294. errormsg = "Image format not supported";
  295. break;
  296. case CL_BUILD_PROGRAM_FAILURE:
  297. errormsg = "Build program failure";
  298. break;
  299. case CL_MAP_FAILURE:
  300. errormsg = "Map failure";
  301. break;
  302. case CL_INVALID_VALUE:
  303. errormsg = "Invalid value";
  304. break;
  305. case CL_INVALID_DEVICE_TYPE:
  306. errormsg = "Invalid device type";
  307. break;
  308. case CL_INVALID_PLATFORM:
  309. errormsg = "Invalid platform";
  310. break;
  311. case CL_INVALID_DEVICE:
  312. errormsg = "Invalid device";
  313. break;
  314. case CL_INVALID_CONTEXT:
  315. errormsg = "Invalid context";
  316. break;
  317. case CL_INVALID_QUEUE_PROPERTIES:
  318. errormsg = "Invalid queue properties";
  319. break;
  320. case CL_INVALID_COMMAND_QUEUE:
  321. errormsg = "Invalid command queue";
  322. break;
  323. case CL_INVALID_HOST_PTR:
  324. errormsg = "Invalid host pointer";
  325. break;
  326. case CL_INVALID_MEM_OBJECT:
  327. errormsg = "Invalid memory object";
  328. break;
  329. case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR:
  330. errormsg = "Invalid image format descriptor";
  331. break;
  332. case CL_INVALID_IMAGE_SIZE:
  333. errormsg = "Invalid image size";
  334. break;
  335. case CL_INVALID_SAMPLER:
  336. errormsg = "Invalid sampler";
  337. break;
  338. case CL_INVALID_BINARY:
  339. errormsg = "Invalid binary";
  340. break;
  341. case CL_INVALID_BUILD_OPTIONS:
  342. errormsg = "Invalid build options";
  343. break;
  344. case CL_INVALID_PROGRAM:
  345. errormsg = "Invalid program";
  346. break;
  347. case CL_INVALID_PROGRAM_EXECUTABLE:
  348. errormsg = "Invalid program executable";
  349. break;
  350. case CL_INVALID_KERNEL_NAME:
  351. errormsg = "Invalid kernel name";
  352. break;
  353. case CL_INVALID_KERNEL_DEFINITION:
  354. errormsg = "Invalid kernel definition";
  355. break;
  356. case CL_INVALID_KERNEL:
  357. errormsg = "Invalid kernel";
  358. break;
  359. case CL_INVALID_ARG_INDEX:
  360. errormsg = "Invalid argument index";
  361. break;
  362. case CL_INVALID_ARG_VALUE:
  363. errormsg = "Invalid argument value";
  364. break;
  365. case CL_INVALID_ARG_SIZE:
  366. errormsg = "Invalid argument size";
  367. break;
  368. case CL_INVALID_KERNEL_ARGS:
  369. errormsg = "Invalid kernel arguments";
  370. break;
  371. case CL_INVALID_WORK_DIMENSION:
  372. errormsg = "Invalid work dimension";
  373. break;
  374. case CL_INVALID_WORK_GROUP_SIZE:
  375. errormsg = "Invalid work group size";
  376. break;
  377. case CL_INVALID_WORK_ITEM_SIZE:
  378. errormsg = "Invalid work item size";
  379. break;
  380. case CL_INVALID_GLOBAL_OFFSET:
  381. errormsg = "Invalid global offset";
  382. break;
  383. case CL_INVALID_EVENT_WAIT_LIST:
  384. errormsg = "Invalid event wait list";
  385. break;
  386. case CL_INVALID_EVENT:
  387. errormsg = "Invalid event";
  388. break;
  389. case CL_INVALID_OPERATION:
  390. errormsg = "Invalid operation";
  391. break;
  392. case CL_INVALID_GL_OBJECT:
  393. errormsg = "Invalid GL object";
  394. break;
  395. case CL_INVALID_BUFFER_SIZE:
  396. errormsg = "Invalid buffer size";
  397. break;
  398. case CL_INVALID_MIP_LEVEL:
  399. errormsg = "Invalid MIP level";
  400. break;
  401. #ifdef CL_PLATFORM_NOT_FOUND_KHR
  402. case CL_PLATFORM_NOT_FOUND_KHR:
  403. errormsg = "Platform not found";
  404. break;
  405. #endif
  406. default:
  407. errormsg = "unknown error";
  408. break;
  409. }
  410. if (msg)
  411. printf("oops in %s (%s:%d) (%s) ... <%s> (%d) \n", func, file, line, msg, errormsg, status);
  412. else
  413. printf("oops in %s (%s:%d) ... <%s> (%d) \n", func, file, line, errormsg, status);
  414. }
  415. int starpu_opencl_set_kernel_args(cl_int *error, cl_kernel *kernel, ...)
  416. {
  417. int i;
  418. va_list ap;
  419. va_start(ap, kernel);
  420. for (i = 0; ; i++)
  421. {
  422. int size = va_arg(ap, int);
  423. if (size == 0)
  424. break;
  425. cl_mem *ptr = va_arg(ap, cl_mem *);
  426. int err = clSetKernelArg(*kernel, i, size, ptr);
  427. if (err != CL_SUCCESS)
  428. {
  429. *error = err;
  430. break;
  431. }
  432. }
  433. va_end(ap);
  434. return i;
  435. }