driver_opencl_utils.c 20 KB

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