driver_opencl_utils.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011, 2012, 2014, 2015, 2016 CNRS
  4. * Copyright (C) 2010-2016 Université de Bordeaux
  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 <sys/types.h>
  23. #include <common/config.h>
  24. #ifdef HAVE_UNISTD_H
  25. #include <unistd.h>
  26. #endif
  27. #include <starpu_opencl.h>
  28. #include <starpu_profiling.h>
  29. #include <core/workers.h>
  30. #include <common/utils.h>
  31. #include "driver_opencl_utils.h"
  32. #include "driver_opencl.h"
  33. #ifdef HAVE_CL_CL_EXT_H
  34. #include <CL/cl_ext.h>
  35. #endif
  36. char *_starpu_opencl_program_dir;
  37. #define _STARPU_STRINGIFY_(x) #x
  38. #define _STARPU_STRINGIFY(x) _STARPU_STRINGIFY_(x)
  39. static
  40. int _starpu_opencl_locate_file(const char *source_file_name, char *located_file_name, char *located_dir_name)
  41. {
  42. int ret = EXIT_FAILURE;
  43. _STARPU_DEBUG("Trying to locate <%s>\n", source_file_name);
  44. if (access(source_file_name, R_OK) == 0)
  45. {
  46. strcpy(located_file_name, source_file_name);
  47. ret = EXIT_SUCCESS;
  48. }
  49. if (ret == EXIT_FAILURE && _starpu_opencl_program_dir)
  50. {
  51. sprintf(located_file_name, "%s/%s", _starpu_opencl_program_dir, source_file_name);
  52. _STARPU_DEBUG("Trying to locate <%s>\n", located_file_name);
  53. if (access(located_file_name, R_OK) == 0)
  54. ret = EXIT_SUCCESS;
  55. }
  56. if (ret == EXIT_FAILURE)
  57. {
  58. sprintf(located_file_name, "%s/%s", STARPU_SRC_DIR, source_file_name);
  59. _STARPU_DEBUG("Trying to locate <%s>\n", located_file_name);
  60. if (access(located_file_name, R_OK) == 0)
  61. ret = EXIT_SUCCESS;
  62. }
  63. if (ret == EXIT_FAILURE)
  64. {
  65. sprintf(located_file_name, "%s/%s", _STARPU_STRINGIFY(STARPU_OPENCL_DATADIR), source_file_name);
  66. _STARPU_DEBUG("Trying to locate <%s>\n", located_file_name);
  67. if (access(located_file_name, R_OK) == 0)
  68. ret = EXIT_SUCCESS;
  69. }
  70. if (ret == EXIT_FAILURE)
  71. {
  72. strcpy(located_file_name, "");
  73. strcpy(located_dir_name, "");
  74. _STARPU_ERROR("Cannot locate file <%s>\n", source_file_name);
  75. }
  76. else
  77. {
  78. char *last = strrchr(located_file_name, '/');
  79. if (!last)
  80. {
  81. strcpy(located_dir_name, "");
  82. }
  83. else
  84. {
  85. sprintf(located_dir_name, "%s", located_file_name);
  86. located_dir_name[strlen(located_file_name)-strlen(last)+1] = '\0';
  87. }
  88. }
  89. return ret;
  90. }
  91. cl_int starpu_opencl_load_kernel(cl_kernel *kernel, cl_command_queue *queue, struct starpu_opencl_program *opencl_programs,
  92. const char *kernel_name, int devid)
  93. {
  94. cl_int err;
  95. cl_device_id device;
  96. cl_program program;
  97. starpu_opencl_get_device(devid, &device);
  98. starpu_opencl_get_queue(devid, queue);
  99. program = opencl_programs->programs[devid];
  100. if (!program)
  101. {
  102. _STARPU_DISP("Program not available for device <%d>\n", devid);
  103. return CL_INVALID_PROGRAM;
  104. }
  105. // Create the compute kernel in the program we wish to run
  106. *kernel = clCreateKernel(program, kernel_name, &err);
  107. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  108. STARPU_OPENCL_REPORT_ERROR(err);
  109. return CL_SUCCESS;
  110. }
  111. cl_int starpu_opencl_release_kernel(cl_kernel kernel)
  112. {
  113. cl_int err;
  114. err = clReleaseKernel(kernel);
  115. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  116. STARPU_OPENCL_REPORT_ERROR(err);
  117. return CL_SUCCESS;
  118. }
  119. static
  120. char *_starpu_opencl_load_program_source(const char *filename)
  121. {
  122. struct stat statbuf;
  123. FILE *fh;
  124. char *source;
  125. int x;
  126. char c;
  127. int err;
  128. fh = fopen(filename, "r");
  129. if (!fh)
  130. return NULL;
  131. err = stat(filename, &statbuf);
  132. STARPU_ASSERT_MSG(err == 0, "could not open file %s\n", filename);
  133. source = (char *) malloc(statbuf.st_size + 1);
  134. for(c=(char)fgetc(fh), x=0 ; c != EOF ; c =(char)fgetc(fh), x++)
  135. {
  136. source[x] = c;
  137. }
  138. source[x] = '\0';
  139. _STARPU_DEBUG("OpenCL kernel <%s>\n", source);
  140. fclose(fh);
  141. return source;
  142. }
  143. static
  144. char *_starpu_opencl_load_program_binary(const char *filename, size_t *len)
  145. {
  146. struct stat statbuf;
  147. FILE *fh;
  148. char *binary;
  149. int err;
  150. fh = fopen(filename, "r");
  151. if (fh == 0)
  152. return NULL;
  153. err = stat(filename, &statbuf);
  154. STARPU_ASSERT_MSG(err == 0, "could not open file %s\n", filename);
  155. binary = (char *) malloc(statbuf.st_size);
  156. if (!binary)
  157. {
  158. fclose(fh);
  159. return binary;
  160. }
  161. err = fread(binary, statbuf.st_size, 1, fh);
  162. STARPU_ASSERT_MSG(err == 1, "could not read from file %s\n", filename);
  163. fclose(fh);
  164. *len = statbuf.st_size;
  165. return binary;
  166. }
  167. static
  168. void _starpu_opencl_create_binary_directory(char *path, size_t maxlen)
  169. {
  170. static int _directory_created = 0;
  171. snprintf(path, maxlen, "%s/.starpu/opencl/", _starpu_get_home_path());
  172. if (_directory_created == 0)
  173. {
  174. _STARPU_DEBUG("Creating directory %s\n", path);
  175. _starpu_mkpath_and_check(path, S_IRWXU);
  176. _directory_created = 1;
  177. }
  178. }
  179. char *_starpu_opencl_get_device_type_as_string(int id)
  180. {
  181. cl_device_type type;
  182. type = _starpu_opencl_get_device_type(id);
  183. switch (type)
  184. {
  185. case CL_DEVICE_TYPE_GPU: return "gpu";
  186. case CL_DEVICE_TYPE_ACCELERATOR: return "acc";
  187. case CL_DEVICE_TYPE_CPU: return "cpu";
  188. default: return "unk";
  189. }
  190. }
  191. static
  192. int _starpu_opencl_get_binary_name(char *binary_file_name, size_t maxlen, const char *source_file_name, int dev, cl_device_id device)
  193. {
  194. char binary_directory[1024];
  195. char *p;
  196. cl_int err;
  197. cl_uint vendor_id;
  198. _starpu_opencl_create_binary_directory(binary_directory, 1024);
  199. p = strrchr(source_file_name, '/');
  200. snprintf(binary_file_name, maxlen, "%s/%s", binary_directory, p?p:source_file_name);
  201. p = strstr(binary_file_name, ".cl");
  202. if (p == NULL) p=binary_file_name + strlen(binary_file_name);
  203. err = clGetDeviceInfo(device, CL_DEVICE_VENDOR_ID, sizeof(vendor_id), &vendor_id, NULL);
  204. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  205. sprintf(p, ".%s.vendor_id_%d_device_id_%d", _starpu_opencl_get_device_type_as_string(dev), (int)vendor_id, dev);
  206. return CL_SUCCESS;
  207. }
  208. static
  209. int _starpu_opencl_compile_or_load_opencl_from_string(const char *opencl_program_source, const char* build_options,
  210. struct starpu_opencl_program *opencl_programs, const char* source_file_name)
  211. {
  212. unsigned int dev;
  213. unsigned int nb_devices;
  214. nb_devices = _starpu_opencl_get_device_count();
  215. // Iterate over each device
  216. for(dev = 0; dev < nb_devices; dev ++)
  217. {
  218. cl_device_id device;
  219. cl_context context;
  220. cl_program program;
  221. cl_int err;
  222. if (opencl_programs)
  223. opencl_programs->programs[dev] = NULL;
  224. starpu_opencl_get_device(dev, &device);
  225. starpu_opencl_get_context(dev, &context);
  226. if (context == NULL)
  227. {
  228. _STARPU_DEBUG("[%u] is not a valid OpenCL context\n", dev);
  229. continue;
  230. }
  231. // Create the compute program from the source buffer
  232. program = clCreateProgramWithSource(context, 1, (const char **) &opencl_program_source, NULL, &err);
  233. if (!program || err != CL_SUCCESS)
  234. {
  235. _STARPU_DISP("Error: Failed to load program source with options %s!\n", build_options);
  236. return EXIT_FAILURE;
  237. }
  238. // Build the program executable
  239. err = clBuildProgram(program, 1, &device, build_options, NULL, NULL);
  240. // Get the status
  241. {
  242. cl_build_status status;
  243. size_t len;
  244. clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 0, NULL, &len);
  245. if (len > 2)
  246. {
  247. char *buffer = malloc(len);
  248. clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, len, buffer, &len);
  249. _STARPU_DISP("Compilation output\n%s\n", buffer);
  250. free(buffer);
  251. }
  252. clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_STATUS, sizeof(status), &status, NULL);
  253. if (err != CL_SUCCESS || status != CL_BUILD_SUCCESS)
  254. {
  255. _STARPU_DISP("Error: Failed to build program executable!\n");
  256. _STARPU_DISP("clBuildProgram: %d - clGetProgramBuildInfo: %d\n", err, status);
  257. return EXIT_FAILURE;
  258. }
  259. }
  260. // Store program
  261. if (opencl_programs)
  262. opencl_programs->programs[dev] = program;
  263. else
  264. {
  265. char binary_file_name[1024];
  266. char *binary;
  267. size_t binary_len;
  268. FILE *fh;
  269. err = _starpu_opencl_get_binary_name(binary_file_name, 1024, source_file_name, dev, device);
  270. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  271. err = clGetProgramInfo(program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t), &binary_len, NULL);
  272. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  273. binary = malloc(binary_len);
  274. err = clGetProgramInfo(program, CL_PROGRAM_BINARIES, sizeof(binary), &binary, NULL);
  275. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  276. fh = fopen(binary_file_name, "w");
  277. if (fh == NULL)
  278. {
  279. _STARPU_DISP("Error: Failed to open file <%s>\n", binary_file_name);
  280. perror("fopen");
  281. return EXIT_FAILURE;
  282. }
  283. fwrite(binary, binary_len, 1, fh);
  284. fclose(fh);
  285. free(binary);
  286. _STARPU_DEBUG("File <%s> created\n", binary_file_name);
  287. }
  288. }
  289. return EXIT_SUCCESS;
  290. }
  291. void starpu_opencl_load_program_source(const char *source_file_name, char *located_file_name, char *located_dir_name, char *opencl_program_source)
  292. {
  293. // Locate source file
  294. _starpu_opencl_locate_file(source_file_name, located_file_name, located_dir_name);
  295. _STARPU_DEBUG("Source file name : <%s>\n", located_file_name);
  296. _STARPU_DEBUG("Source directory name : <%s>\n", located_dir_name);
  297. // Load the compute program from disk into a char *
  298. char *source = _starpu_opencl_load_program_source(located_file_name);
  299. if(!source)
  300. _STARPU_ERROR("Failed to load compute program from file <%s>!\n", located_file_name);
  301. sprintf(opencl_program_source, "%s", source);
  302. free(source);
  303. }
  304. static
  305. int _starpu_opencl_compile_or_load_opencl_from_file(const char *source_file_name, struct starpu_opencl_program *opencl_programs, const char* build_options)
  306. {
  307. int nb_devices;
  308. char located_file_name[1024];
  309. char located_dir_name[1024];
  310. char new_build_options[1024];
  311. #ifdef STARPU_DEVEL
  312. #warning Use dynamic allocation
  313. #endif
  314. char opencl_program_source[256*1024];
  315. // Do not try to load and compile the file if there is no devices
  316. nb_devices = starpu_opencl_worker_get_count();
  317. if (nb_devices == 0) return EXIT_SUCCESS;
  318. starpu_opencl_load_program_source(source_file_name, located_file_name, located_dir_name, opencl_program_source);
  319. if (!build_options)
  320. build_options = "";
  321. if (!strcmp(located_dir_name, ""))
  322. strcpy(new_build_options, build_options);
  323. else
  324. sprintf(new_build_options, "-I %s %s", located_dir_name, build_options);
  325. _STARPU_DEBUG("Build options: <%s>\n", new_build_options);
  326. return _starpu_opencl_compile_or_load_opencl_from_string(opencl_program_source, new_build_options, opencl_programs, source_file_name);
  327. }
  328. int starpu_opencl_compile_opencl_from_file(const char *source_file_name, const char* build_options)
  329. {
  330. return _starpu_opencl_compile_or_load_opencl_from_file(source_file_name, NULL, build_options);
  331. }
  332. int starpu_opencl_compile_opencl_from_string(const char *opencl_program_source, const char *file_name, const char* build_options)
  333. {
  334. return _starpu_opencl_compile_or_load_opencl_from_string(opencl_program_source, build_options, NULL, file_name);
  335. }
  336. int starpu_opencl_load_opencl_from_string(const char *opencl_program_source, struct starpu_opencl_program *opencl_programs,
  337. const char* build_options)
  338. {
  339. return _starpu_opencl_compile_or_load_opencl_from_string(opencl_program_source, build_options, opencl_programs, NULL);
  340. }
  341. int starpu_opencl_load_opencl_from_file(const char *source_file_name, struct starpu_opencl_program *opencl_programs,
  342. const char* build_options)
  343. {
  344. return _starpu_opencl_compile_or_load_opencl_from_file(source_file_name, opencl_programs, build_options);
  345. }
  346. int starpu_opencl_load_binary_opencl(const char *kernel_id, struct starpu_opencl_program *opencl_programs)
  347. {
  348. unsigned int dev;
  349. unsigned int nb_devices;
  350. nb_devices = _starpu_opencl_get_device_count();
  351. // Iterate over each device
  352. for(dev = 0; dev < nb_devices; dev ++)
  353. {
  354. cl_device_id device;
  355. cl_context context;
  356. cl_program program;
  357. cl_int err;
  358. char *binary;
  359. char binary_file_name[1024];
  360. size_t length;
  361. cl_int binary_status;
  362. opencl_programs->programs[dev] = NULL;
  363. starpu_opencl_get_device(dev, &device);
  364. starpu_opencl_get_context(dev, &context);
  365. if (context == NULL)
  366. {
  367. _STARPU_DEBUG("[%u] is not a valid OpenCL context\n", dev);
  368. continue;
  369. }
  370. // Load the binary buffer
  371. err = _starpu_opencl_get_binary_name(binary_file_name, 1024, kernel_id, dev, device);
  372. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  373. binary = _starpu_opencl_load_program_binary(binary_file_name, &length);
  374. // Create the compute program from the binary buffer
  375. program = clCreateProgramWithBinary(context, 1, &device, &length, (const unsigned char **) &binary, &binary_status, &err);
  376. if (!program || err != CL_SUCCESS)
  377. {
  378. _STARPU_DISP("Error: Failed to load program binary!\n");
  379. return EXIT_FAILURE;
  380. }
  381. // Build the program executable
  382. err = clBuildProgram(program, 1, &device, NULL, NULL, NULL);
  383. // Get the status
  384. {
  385. cl_build_status status;
  386. size_t len;
  387. static char buffer[4096] = "";
  388. clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &len);
  389. if (len > 2)
  390. _STARPU_DISP("Compilation output\n%s\n", buffer);
  391. clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_STATUS, sizeof(status), &status, NULL);
  392. if (err != CL_SUCCESS || status != CL_BUILD_SUCCESS)
  393. {
  394. _STARPU_DISP("Error: Failed to build program executable!\n");
  395. _STARPU_DISP("clBuildProgram: %d - clGetProgramBuildInfo: %d\n", err, status);
  396. return EXIT_FAILURE;
  397. }
  398. }
  399. // Store program
  400. opencl_programs->programs[dev] = program;
  401. }
  402. return 0;
  403. }
  404. int starpu_opencl_unload_opencl(struct starpu_opencl_program *opencl_programs)
  405. {
  406. unsigned int dev;
  407. unsigned int nb_devices;
  408. if (!starpu_opencl_worker_get_count())
  409. return 0;
  410. nb_devices = _starpu_opencl_get_device_count();
  411. // Iterate over each device
  412. for(dev = 0; dev < nb_devices; dev ++)
  413. {
  414. if (opencl_programs->programs[dev])
  415. {
  416. cl_int err;
  417. err = clReleaseProgram(opencl_programs->programs[dev]);
  418. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  419. STARPU_OPENCL_REPORT_ERROR(err);
  420. }
  421. }
  422. return 0;
  423. }
  424. int starpu_opencl_collect_stats(cl_event event STARPU_ATTRIBUTE_UNUSED)
  425. {
  426. #if defined(CL_PROFILING_CLOCK_CYCLE_COUNT)||defined(CL_PROFILING_STALL_CYCLE_COUNT)||defined(CL_PROFILING_POWER_CONSUMED)
  427. struct starpu_task *task = starpu_task_get_current();
  428. struct starpu_profiling_task_info *info = task->profiling_info;
  429. #endif
  430. #ifdef CL_PROFILING_CLOCK_CYCLE_COUNT
  431. if (starpu_profiling_status_get() && info)
  432. {
  433. cl_int err;
  434. unsigned int clock_cycle_count;
  435. size_t size;
  436. err = clGetEventProfilingInfo(event, CL_PROFILING_CLOCK_CYCLE_COUNT, sizeof(clock_cycle_count), &clock_cycle_count, &size);
  437. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  438. STARPU_ASSERT(size == sizeof(clock_cycle_count));
  439. info->used_cycles += clock_cycle_count;
  440. }
  441. #endif
  442. #ifdef CL_PROFILING_STALL_CYCLE_COUNT
  443. if (starpu_profiling_status_get() && info)
  444. {
  445. cl_int err;
  446. unsigned int stall_cycle_count;
  447. size_t size;
  448. err = clGetEventProfilingInfo(event, CL_PROFILING_STALL_CYCLE_COUNT, sizeof(stall_cycle_count), &stall_cycle_count, &size);
  449. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  450. STARPU_ASSERT(size == sizeof(stall_cycle_count));
  451. info->stall_cycles += stall_cycle_count;
  452. }
  453. #endif
  454. #ifdef CL_PROFILING_POWER_CONSUMED
  455. if (info && (starpu_profiling_status_get() || (task->cl && task->cl->power_model && task->cl->power_model->benchmarking)))
  456. {
  457. cl_int err;
  458. double power_consumed;
  459. size_t size;
  460. err = clGetEventProfilingInfo(event, CL_PROFILING_POWER_CONSUMED, sizeof(power_consumed), &power_consumed, &size);
  461. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  462. STARPU_ASSERT(size == sizeof(power_consumed));
  463. info->power_consumed += power_consumed;
  464. }
  465. #endif
  466. return 0;
  467. }
  468. const char *starpu_opencl_error_string(cl_int status)
  469. {
  470. const char *errormsg;
  471. switch (status)
  472. {
  473. case CL_SUCCESS:
  474. errormsg = "Success";
  475. break;
  476. case CL_DEVICE_NOT_FOUND:
  477. errormsg = "Device not found";
  478. break;
  479. case CL_DEVICE_NOT_AVAILABLE:
  480. errormsg = "Device not available";
  481. break;
  482. case CL_COMPILER_NOT_AVAILABLE:
  483. errormsg = "Compiler not available";
  484. break;
  485. case CL_MEM_OBJECT_ALLOCATION_FAILURE:
  486. errormsg = "Memory object allocation failure";
  487. break;
  488. case CL_OUT_OF_RESOURCES:
  489. errormsg = "Out of resources";
  490. break;
  491. case CL_OUT_OF_HOST_MEMORY:
  492. errormsg = "Out of host memory";
  493. break;
  494. case CL_PROFILING_INFO_NOT_AVAILABLE:
  495. errormsg = "Profiling info not available";
  496. break;
  497. case CL_MEM_COPY_OVERLAP:
  498. errormsg = "Memory copy overlap";
  499. break;
  500. case CL_IMAGE_FORMAT_MISMATCH:
  501. errormsg = "Image format mismatch";
  502. break;
  503. case CL_IMAGE_FORMAT_NOT_SUPPORTED:
  504. errormsg = "Image format not supported";
  505. break;
  506. case CL_BUILD_PROGRAM_FAILURE:
  507. errormsg = "Build program failure";
  508. break;
  509. case CL_MAP_FAILURE:
  510. errormsg = "Map failure";
  511. break;
  512. case CL_INVALID_VALUE:
  513. errormsg = "Invalid value";
  514. break;
  515. case CL_INVALID_DEVICE_TYPE:
  516. errormsg = "Invalid device type";
  517. break;
  518. case CL_INVALID_PLATFORM:
  519. errormsg = "Invalid platform";
  520. break;
  521. case CL_INVALID_DEVICE:
  522. errormsg = "Invalid device";
  523. break;
  524. case CL_INVALID_CONTEXT:
  525. errormsg = "Invalid context";
  526. break;
  527. case CL_INVALID_QUEUE_PROPERTIES:
  528. errormsg = "Invalid queue properties";
  529. break;
  530. case CL_INVALID_COMMAND_QUEUE:
  531. errormsg = "Invalid command queue";
  532. break;
  533. case CL_INVALID_HOST_PTR:
  534. errormsg = "Invalid host pointer";
  535. break;
  536. case CL_INVALID_MEM_OBJECT:
  537. errormsg = "Invalid memory object";
  538. break;
  539. case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR:
  540. errormsg = "Invalid image format descriptor";
  541. break;
  542. case CL_INVALID_IMAGE_SIZE:
  543. errormsg = "Invalid image size";
  544. break;
  545. case CL_INVALID_SAMPLER:
  546. errormsg = "Invalid sampler";
  547. break;
  548. case CL_INVALID_BINARY:
  549. errormsg = "Invalid binary";
  550. break;
  551. case CL_INVALID_BUILD_OPTIONS:
  552. errormsg = "Invalid build options";
  553. break;
  554. case CL_INVALID_PROGRAM:
  555. errormsg = "Invalid program";
  556. break;
  557. case CL_INVALID_PROGRAM_EXECUTABLE:
  558. errormsg = "Invalid program executable";
  559. break;
  560. case CL_INVALID_KERNEL_NAME:
  561. errormsg = "Invalid kernel name";
  562. break;
  563. case CL_INVALID_KERNEL_DEFINITION:
  564. errormsg = "Invalid kernel definition";
  565. break;
  566. case CL_INVALID_KERNEL:
  567. errormsg = "Invalid kernel";
  568. break;
  569. case CL_INVALID_ARG_INDEX:
  570. errormsg = "Invalid argument index";
  571. break;
  572. case CL_INVALID_ARG_VALUE:
  573. errormsg = "Invalid argument value";
  574. break;
  575. case CL_INVALID_ARG_SIZE:
  576. errormsg = "Invalid argument size";
  577. break;
  578. case CL_INVALID_KERNEL_ARGS:
  579. errormsg = "Invalid kernel arguments";
  580. break;
  581. case CL_INVALID_WORK_DIMENSION:
  582. errormsg = "Invalid work dimension";
  583. break;
  584. case CL_INVALID_WORK_GROUP_SIZE:
  585. errormsg = "Invalid work group size";
  586. break;
  587. case CL_INVALID_WORK_ITEM_SIZE:
  588. errormsg = "Invalid work item size";
  589. break;
  590. case CL_INVALID_GLOBAL_OFFSET:
  591. errormsg = "Invalid global offset";
  592. break;
  593. case CL_INVALID_EVENT_WAIT_LIST:
  594. errormsg = "Invalid event wait list";
  595. break;
  596. case CL_INVALID_EVENT:
  597. errormsg = "Invalid event";
  598. break;
  599. case CL_INVALID_OPERATION:
  600. errormsg = "Invalid operation";
  601. break;
  602. case CL_INVALID_GL_OBJECT:
  603. errormsg = "Invalid GL object";
  604. break;
  605. case CL_INVALID_BUFFER_SIZE:
  606. errormsg = "Invalid buffer size";
  607. break;
  608. case CL_INVALID_MIP_LEVEL:
  609. errormsg = "Invalid MIP level";
  610. break;
  611. #ifdef CL_PLATFORM_NOT_FOUND_KHR
  612. case CL_PLATFORM_NOT_FOUND_KHR:
  613. errormsg = "Platform not found";
  614. break;
  615. #endif
  616. default:
  617. errormsg = "unknown OpenCL error";
  618. break;
  619. }
  620. return errormsg;
  621. }
  622. void starpu_opencl_display_error(const char *func, const char *file, int line, const char* msg, cl_int status)
  623. {
  624. printf("oops in %s (%s:%d) (%s) ... <%s> (%d) \n", func, file, line, msg,
  625. starpu_opencl_error_string (status), status);
  626. }
  627. int starpu_opencl_set_kernel_args(cl_int *error, cl_kernel *kernel, ...)
  628. {
  629. int i;
  630. va_list ap;
  631. va_start(ap, kernel);
  632. for (i = 0; ; i++)
  633. {
  634. int size = va_arg(ap, int);
  635. if (size == 0)
  636. break;
  637. cl_mem *ptr = va_arg(ap, cl_mem *);
  638. int err = clSetKernelArg(*kernel, i, size, ptr);
  639. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  640. {
  641. *error = err;
  642. break;
  643. }
  644. }
  645. va_end(ap);
  646. return i;
  647. }