driver_opencl_utils.c 14 KB

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