driver_opencl_utils.c 20 KB

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