driver_opencl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2012 Université de Bordeaux 1
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010, 2011, 2012 Centre National de la Recherche Scientifique
  6. * Copyright (C) 2011 Télécom-SudParis
  7. *
  8. * StarPU is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * StarPU is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  18. */
  19. #include <math.h>
  20. #include <starpu.h>
  21. #include <starpu_profiling.h>
  22. #include <common/config.h>
  23. #include <common/utils.h>
  24. #include <core/debug.h>
  25. #include <starpu_opencl.h>
  26. #include <drivers/driver_common/driver_common.h>
  27. #include "driver_opencl.h"
  28. #include "driver_opencl_utils.h"
  29. #include <common/utils.h>
  30. #ifdef STARPU_SIMGRID
  31. #include <core/simgrid.h>
  32. #endif
  33. static _starpu_pthread_mutex_t big_lock = _STARPU_PTHREAD_MUTEX_INITIALIZER;
  34. static cl_context contexts[STARPU_MAXOPENCLDEVS];
  35. static cl_device_id devices[STARPU_MAXOPENCLDEVS];
  36. static cl_command_queue queues[STARPU_MAXOPENCLDEVS];
  37. static cl_command_queue transfer_queues[STARPU_MAXOPENCLDEVS];
  38. static cl_command_queue alloc_queues[STARPU_MAXOPENCLDEVS];
  39. static cl_uint nb_devices = -1;
  40. static int init_done = 0;
  41. #ifndef STARPU_SIMGRID
  42. void
  43. _starpu_opencl_discover_devices(struct _starpu_machine_config *config)
  44. {
  45. /* Discover the number of OpenCL devices. Fill the result in CONFIG. */
  46. /* As OpenCL must have been initialized before calling this function,
  47. * `nb_device' is ensured to be correctly set. */
  48. STARPU_ASSERT(init_done == 1);
  49. config->topology.nhwopenclgpus = nb_devices;
  50. }
  51. /* In case we want to cap the amount of memory available on the GPUs by the
  52. * mean of the STARPU_LIMIT_GPU_MEM, we allocate a big buffer when the driver
  53. * is launched. */
  54. static cl_mem wasted_memory[STARPU_MAXOPENCLDEVS];
  55. static void limit_gpu_mem_if_needed(int devid)
  56. {
  57. cl_int err;
  58. int limit = starpu_get_env_number("STARPU_LIMIT_GPU_MEM");
  59. if (limit == -1)
  60. {
  61. wasted_memory[devid] = NULL;
  62. return;
  63. }
  64. /* Request the size of the current device's memory */
  65. cl_ulong totalGlobalMem;
  66. err = clGetDeviceInfo(devices[devid], CL_DEVICE_GLOBAL_MEM_SIZE, sizeof(totalGlobalMem), &totalGlobalMem, NULL);
  67. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  68. STARPU_OPENCL_REPORT_ERROR(err);
  69. /* How much memory to waste ? */
  70. size_t to_waste = (size_t)totalGlobalMem - (size_t)limit*1024*1024;
  71. _STARPU_DEBUG("OpenCL device %d: Wasting %ld MB / Limit %ld MB / Total %ld MB / Remains %ld MB\n",
  72. devid, (size_t)to_waste/(1024*1024), (size_t)limit, (size_t)totalGlobalMem/(1024*1024),
  73. (size_t)(totalGlobalMem - to_waste)/(1024*1024));
  74. /* Allocate a large buffer to waste memory and constraint the amount of available memory. */
  75. wasted_memory[devid] = clCreateBuffer(contexts[devid], CL_MEM_READ_WRITE, to_waste, NULL, &err);
  76. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  77. }
  78. static void unlimit_gpu_mem_if_needed(int devid)
  79. {
  80. if (wasted_memory[devid])
  81. {
  82. cl_int err = clReleaseMemObject(wasted_memory[devid]);
  83. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  84. STARPU_OPENCL_REPORT_ERROR(err);
  85. wasted_memory[devid] = NULL;
  86. }
  87. }
  88. #endif
  89. size_t starpu_opencl_get_global_mem_size(int devid)
  90. {
  91. cl_int err;
  92. cl_ulong totalGlobalMem;
  93. /* Request the size of the current device's memory */
  94. err = clGetDeviceInfo(devices[devid], CL_DEVICE_GLOBAL_MEM_SIZE, sizeof(totalGlobalMem), &totalGlobalMem, NULL);
  95. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  96. STARPU_OPENCL_REPORT_ERROR(err);
  97. return (size_t)totalGlobalMem;
  98. }
  99. void starpu_opencl_get_context(int devid, cl_context *context)
  100. {
  101. *context = contexts[devid];
  102. }
  103. void starpu_opencl_get_device(int devid, cl_device_id *device)
  104. {
  105. *device = devices[devid];
  106. }
  107. void starpu_opencl_get_queue(int devid, cl_command_queue *queue)
  108. {
  109. *queue = queues[devid];
  110. }
  111. void starpu_opencl_get_current_queue(cl_command_queue *queue)
  112. {
  113. struct _starpu_worker *worker = _starpu_get_local_worker_key();
  114. STARPU_ASSERT(queue);
  115. *queue = queues[worker->devid];
  116. }
  117. void starpu_opencl_get_current_context(cl_context *context)
  118. {
  119. struct _starpu_worker *worker = _starpu_get_local_worker_key();
  120. STARPU_ASSERT(context);
  121. *context = contexts[worker->devid];
  122. }
  123. #ifndef STARPU_SIMGRID
  124. cl_int _starpu_opencl_init_context(int devid)
  125. {
  126. cl_int err;
  127. _STARPU_PTHREAD_MUTEX_LOCK(&big_lock);
  128. _STARPU_DEBUG("Initialising context for dev %d\n", devid);
  129. // Create a compute context
  130. err = 0;
  131. contexts[devid] = clCreateContext(NULL, 1, &devices[devid], NULL, NULL, &err);
  132. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  133. // Create execution queue for the given device
  134. queues[devid] = clCreateCommandQueue(contexts[devid], devices[devid], 0, &err);
  135. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  136. // Create transfer queue for the given device
  137. cl_command_queue_properties props;
  138. err = clGetDeviceInfo(devices[devid], CL_DEVICE_QUEUE_PROPERTIES, sizeof(props), &props, NULL);
  139. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  140. STARPU_OPENCL_REPORT_ERROR(err);
  141. props &= CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE;
  142. transfer_queues[devid] = clCreateCommandQueue(contexts[devid], devices[devid], props, &err);
  143. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  144. alloc_queues[devid] = clCreateCommandQueue(contexts[devid], devices[devid], 0, &err);
  145. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  146. _STARPU_PTHREAD_MUTEX_UNLOCK(&big_lock);
  147. limit_gpu_mem_if_needed(devid);
  148. return CL_SUCCESS;
  149. }
  150. cl_int _starpu_opencl_deinit_context(int devid)
  151. {
  152. cl_int err;
  153. _STARPU_PTHREAD_MUTEX_LOCK(&big_lock);
  154. _STARPU_DEBUG("De-initialising context for dev %d\n", devid);
  155. unlimit_gpu_mem_if_needed(devid);
  156. err = clReleaseContext(contexts[devid]);
  157. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  158. err = clReleaseCommandQueue(queues[devid]);
  159. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  160. err = clReleaseCommandQueue(transfer_queues[devid]);
  161. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  162. err = clReleaseCommandQueue(alloc_queues[devid]);
  163. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  164. contexts[devid] = NULL;
  165. _STARPU_PTHREAD_MUTEX_UNLOCK(&big_lock);
  166. return CL_SUCCESS;
  167. }
  168. #endif
  169. cl_int starpu_opencl_allocate_memory(cl_mem *mem STARPU_ATTRIBUTE_UNUSED, size_t size STARPU_ATTRIBUTE_UNUSED, cl_mem_flags flags STARPU_ATTRIBUTE_UNUSED)
  170. {
  171. #ifdef STARPU_SIMGRID
  172. STARPU_ABORT();
  173. #else
  174. cl_int err;
  175. cl_mem memory;
  176. struct _starpu_worker *worker = _starpu_get_local_worker_key();
  177. memory = clCreateBuffer(contexts[worker->devid], flags, size, NULL, &err);
  178. if (err == CL_OUT_OF_HOST_MEMORY) return err;
  179. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  180. /*
  181. * OpenCL uses lazy memory allocation: we will only know if the
  182. * allocation failed when trying to copy data onto the device. But we
  183. * want to know this __now__, so we just perform a dummy copy.
  184. */
  185. char dummy = 0;
  186. err = clEnqueueWriteBuffer(alloc_queues[worker->devid], memory, CL_TRUE,
  187. 0, sizeof(dummy), &dummy,
  188. 0, NULL, NULL);
  189. clFinish(alloc_queues[worker->devid]);
  190. if (err == CL_MEM_OBJECT_ALLOCATION_FAILURE)
  191. return err;
  192. if (err == CL_OUT_OF_RESOURCES)
  193. return err;
  194. if (err != CL_SUCCESS)
  195. STARPU_OPENCL_REPORT_ERROR(err);
  196. *mem = memory;
  197. return CL_SUCCESS;
  198. #endif
  199. }
  200. cl_int starpu_opencl_copy_ram_to_opencl(void *ptr, unsigned src_node STARPU_ATTRIBUTE_UNUSED, cl_mem buffer, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, size_t size, size_t offset, cl_event *event, int *ret)
  201. {
  202. cl_int err;
  203. struct _starpu_worker *worker = _starpu_get_local_worker_key();
  204. if (event)
  205. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  206. err = clEnqueueWriteBuffer(transfer_queues[worker->devid], buffer, CL_FALSE, offset, size, ptr, 0, NULL, event);
  207. if (event)
  208. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  209. if (STARPU_LIKELY(err == CL_SUCCESS))
  210. {
  211. if (event == NULL)
  212. {
  213. /* We want a synchronous copy, let's synchronise the queue */
  214. clFinish(transfer_queues[worker->devid]);
  215. }
  216. if (ret)
  217. {
  218. *ret = (event == NULL) ? 0 : -EAGAIN;
  219. }
  220. }
  221. return err;
  222. }
  223. cl_int starpu_opencl_copy_opencl_to_ram(cl_mem buffer, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *ptr, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, size_t size, size_t offset, cl_event *event, int *ret)
  224. {
  225. cl_int err;
  226. struct _starpu_worker *worker = _starpu_get_local_worker_key();
  227. if (event)
  228. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  229. err = clEnqueueReadBuffer(transfer_queues[worker->devid], buffer, CL_FALSE, offset, size, ptr, 0, NULL, event);
  230. if (event)
  231. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  232. if (STARPU_LIKELY(err == CL_SUCCESS))
  233. {
  234. if (event == NULL)
  235. {
  236. /* We want a synchronous copy, let's synchronise the queue */
  237. clFinish(transfer_queues[worker->devid]);
  238. }
  239. if (ret)
  240. {
  241. *ret = (event == NULL) ? 0 : -EAGAIN;
  242. }
  243. }
  244. return err;
  245. }
  246. #if 0
  247. cl_int _starpu_opencl_copy_rect_opencl_to_ram(cl_mem buffer, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *ptr, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, const size_t buffer_origin[3], const size_t host_origin[3],
  248. const size_t region[3], size_t buffer_row_pitch, size_t buffer_slice_pitch,
  249. size_t host_row_pitch, size_t host_slice_pitch, cl_event *event)
  250. {
  251. cl_int err;
  252. struct _starpu_worker *worker = _starpu_get_local_worker_key();
  253. cl_bool blocking;
  254. blocking = (event == NULL) ? CL_TRUE : CL_FALSE;
  255. if (event)
  256. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  257. err = clEnqueueReadBufferRect(transfer_queues[worker->devid], buffer, blocking, buffer_origin, host_origin, region, buffer_row_pitch,
  258. buffer_slice_pitch, host_row_pitch, host_slice_pitch, ptr, 0, NULL, event);
  259. if (event)
  260. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  261. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  262. return CL_SUCCESS;
  263. }
  264. cl_int _starpu_opencl_copy_rect_ram_to_opencl(void *ptr, unsigned src_node STARPU_ATTRIBUTE_UNUSED, cl_mem buffer, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, const size_t buffer_origin[3], const size_t host_origin[3],
  265. const size_t region[3], size_t buffer_row_pitch, size_t buffer_slice_pitch,
  266. size_t host_row_pitch, size_t host_slice_pitch, cl_event *event)
  267. {
  268. cl_int err;
  269. struct _starpu_worker *worker = _starpu_get_local_worker_key();
  270. cl_bool blocking;
  271. blocking = (event == NULL) ? CL_TRUE : CL_FALSE;
  272. if (event)
  273. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  274. err = clEnqueueWriteBufferRect(transfer_queues[worker->devid], buffer, blocking, buffer_origin, host_origin, region, buffer_row_pitch,
  275. buffer_slice_pitch, host_row_pitch, host_slice_pitch, ptr, 0, NULL, event);
  276. if (event)
  277. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  278. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  279. return CL_SUCCESS;
  280. }
  281. #endif
  282. void _starpu_opencl_init(void)
  283. {
  284. _STARPU_PTHREAD_MUTEX_LOCK(&big_lock);
  285. if (!init_done)
  286. {
  287. cl_platform_id platform_id[_STARPU_OPENCL_PLATFORM_MAX];
  288. cl_uint nb_platforms;
  289. cl_int err;
  290. unsigned int i;
  291. cl_device_type device_type = CL_DEVICE_TYPE_GPU|CL_DEVICE_TYPE_ACCELERATOR;
  292. _STARPU_DEBUG("Initialising OpenCL\n");
  293. // Get Platforms
  294. if (starpu_get_env_number("STARPU_OPENCL_ON_CPUS") > 0)
  295. device_type |= CL_DEVICE_TYPE_CPU;
  296. if (starpu_get_env_number("STARPU_OPENCL_ONLY_ON_CPUS") > 0)
  297. device_type = CL_DEVICE_TYPE_CPU;
  298. err = clGetPlatformIDs(_STARPU_OPENCL_PLATFORM_MAX, platform_id, &nb_platforms);
  299. if (STARPU_UNLIKELY(err != CL_SUCCESS)) nb_platforms=0;
  300. _STARPU_DEBUG("Platforms detected: %u\n", nb_platforms);
  301. // Get devices
  302. nb_devices = 0;
  303. {
  304. for (i=0; i<nb_platforms; i++)
  305. {
  306. cl_uint num;
  307. int platform_valid = 1;
  308. char name[1024], vendor[1024];
  309. err = clGetPlatformInfo(platform_id[i], CL_PLATFORM_NAME, 1024, name, NULL);
  310. if (err != CL_SUCCESS)
  311. {
  312. STARPU_OPENCL_REPORT_ERROR_WITH_MSG("clGetPlatformInfo NAME", err);
  313. platform_valid = 0;
  314. }
  315. else
  316. {
  317. err = clGetPlatformInfo(platform_id[i], CL_PLATFORM_VENDOR, 1024, vendor, NULL);
  318. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  319. {
  320. STARPU_OPENCL_REPORT_ERROR_WITH_MSG("clGetPlatformInfo VENDOR", err);
  321. platform_valid = 0;
  322. }
  323. }
  324. if(strcmp(name, "SOCL Platform") == 0) {
  325. platform_valid = 0;
  326. _STARPU_DEBUG("Skipping SOCL Platform\n");
  327. }
  328. #ifdef STARPU_VERBOSE
  329. if (platform_valid)
  330. _STARPU_DEBUG("Platform: %s - %s\n", name, vendor);
  331. else
  332. _STARPU_DEBUG("Platform invalid\n");
  333. #endif
  334. if (platform_valid)
  335. {
  336. err = clGetDeviceIDs(platform_id[i], device_type, STARPU_MAXOPENCLDEVS-nb_devices, &devices[nb_devices], &num);
  337. if (err == CL_DEVICE_NOT_FOUND)
  338. {
  339. _STARPU_DEBUG(" No devices detected on this platform\n");
  340. }
  341. else
  342. {
  343. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  344. _STARPU_DEBUG(" %u devices detected\n", num);
  345. nb_devices += num;
  346. }
  347. }
  348. }
  349. }
  350. // Get location of OpenCl kernel source files
  351. _starpu_opencl_program_dir = getenv("STARPU_OPENCL_PROGRAM_DIR");
  352. if (nb_devices > STARPU_MAXOPENCLDEVS)
  353. {
  354. _STARPU_DISP("# Warning: %u OpenCL devices available. Only %d enabled. Use configure option --enable-maxopencldev=xxx to update the maximum value of supported OpenCL devices?\n", nb_devices, STARPU_MAXOPENCLDEVS);
  355. nb_devices = STARPU_MAXOPENCLDEVS;
  356. }
  357. // initialise internal structures
  358. for(i=0 ; i<nb_devices ; i++)
  359. {
  360. contexts[i] = NULL;
  361. queues[i] = NULL;
  362. transfer_queues[i] = NULL;
  363. alloc_queues[i] = NULL;
  364. }
  365. init_done=1;
  366. }
  367. _STARPU_PTHREAD_MUTEX_UNLOCK(&big_lock);
  368. }
  369. #ifndef STARPU_SIMGRID
  370. static unsigned _starpu_opencl_get_device_name(int dev, char *name, int lname);
  371. #endif
  372. static int _starpu_opencl_execute_job(struct _starpu_job *j, struct _starpu_worker *args);
  373. static struct _starpu_worker*
  374. _starpu_opencl_get_worker_from_driver(struct starpu_driver *d)
  375. {
  376. int nworkers;
  377. int workers[STARPU_MAXOPENCLDEVS];
  378. nworkers = starpu_worker_get_ids_by_type(STARPU_OPENCL_WORKER, workers, STARPU_MAXOPENCLDEVS);
  379. if (nworkers == 0)
  380. return NULL;
  381. int i;
  382. for (i = 0; i < nworkers; i++)
  383. {
  384. cl_device_id device;
  385. int devid = starpu_worker_get_devid(workers[i]);
  386. starpu_opencl_get_device(devid, &device);
  387. if (device == d->id.opencl_id)
  388. break;
  389. }
  390. if (i == nworkers)
  391. return NULL;
  392. return _starpu_get_worker_struct(workers[i]);
  393. }
  394. int _starpu_opencl_driver_init(struct starpu_driver *d)
  395. {
  396. struct _starpu_worker* args;
  397. args = _starpu_opencl_get_worker_from_driver(d);
  398. STARPU_ASSERT(args);
  399. int devid = args->devid;
  400. _starpu_worker_init(args, _STARPU_FUT_OPENCL_KEY);
  401. #ifndef STARPU_SIMGRID
  402. _starpu_opencl_init_context(devid);
  403. #endif
  404. /* one more time to avoid hacks from third party lib :) */
  405. _starpu_bind_thread_on_cpu(args->config, args->bindid);
  406. args->status = STATUS_UNKNOWN;
  407. #ifdef STARPU_SIMGRID
  408. const char *devname = "Simgrid";
  409. #else
  410. /* get the device's name */
  411. char devname[128];
  412. _starpu_opencl_get_device_name(devid, devname, 128);
  413. #endif
  414. snprintf(args->name, sizeof(args->name), "OpenCL %u (%s)", devid, devname);
  415. snprintf(args->short_name, sizeof(args->short_name), "OpenCL %u", devid);
  416. _STARPU_DEBUG("OpenCL (%s) dev id %d thread is ready to run on CPU %d !\n", devname, devid, args->bindid);
  417. _STARPU_TRACE_WORKER_INIT_END
  418. /* tell the main thread that this one is ready */
  419. _STARPU_PTHREAD_MUTEX_LOCK(&args->mutex);
  420. args->worker_is_initialized = 1;
  421. _STARPU_PTHREAD_COND_SIGNAL(&args->ready_cond);
  422. _STARPU_PTHREAD_MUTEX_UNLOCK(&args->mutex);
  423. return 0;
  424. }
  425. int _starpu_opencl_driver_run_once(struct starpu_driver *d)
  426. {
  427. struct _starpu_worker* args;
  428. args = _starpu_opencl_get_worker_from_driver(d);
  429. STARPU_ASSERT(args);
  430. int workerid = args->workerid;
  431. unsigned memnode = args->memory_node;
  432. struct _starpu_job *j;
  433. struct starpu_task *task;
  434. int res;
  435. _STARPU_TRACE_START_PROGRESS(memnode);
  436. _starpu_datawizard_progress(memnode, 1);
  437. _STARPU_TRACE_END_PROGRESS(memnode);
  438. task = _starpu_get_worker_task(args, workerid, memnode);
  439. if (task == NULL)
  440. return 0;
  441. j = _starpu_get_job_associated_to_task(task);
  442. /* can OpenCL do that task ? */
  443. if (!_STARPU_OPENCL_MAY_PERFORM(j))
  444. {
  445. /* this is not a OpenCL task */
  446. _starpu_push_task(j);
  447. return 0;
  448. }
  449. _starpu_set_current_task(j->task);
  450. args->current_task = j->task;
  451. res = _starpu_opencl_execute_job(j, args);
  452. _starpu_set_current_task(NULL);
  453. args->current_task = NULL;
  454. if (res)
  455. {
  456. switch (res)
  457. {
  458. case -EAGAIN:
  459. _STARPU_DISP("ouch, put the codelet %p back ... \n", j);
  460. _starpu_push_task(j);
  461. STARPU_ABORT();
  462. return 0;
  463. default:
  464. STARPU_ABORT();
  465. }
  466. }
  467. _starpu_handle_job_termination(j);
  468. return 0;
  469. }
  470. int _starpu_opencl_driver_deinit(struct starpu_driver *d)
  471. {
  472. _STARPU_TRACE_WORKER_DEINIT_START
  473. struct _starpu_worker* args;
  474. args = _starpu_opencl_get_worker_from_driver(d);
  475. STARPU_ASSERT(args);
  476. unsigned memnode = args->memory_node;
  477. _starpu_handle_all_pending_node_data_requests(memnode);
  478. #ifndef STARPU_SIMGRID
  479. unsigned devid = args->devid;
  480. _starpu_opencl_deinit_context(devid);
  481. #endif
  482. return 0;
  483. }
  484. void *_starpu_opencl_worker(void *arg)
  485. {
  486. cl_device_id id;
  487. struct _starpu_worker* args = arg;
  488. starpu_opencl_get_device(args->devid, &id);
  489. struct starpu_driver d = {
  490. .type = STARPU_OPENCL_WORKER,
  491. .id.opencl_id = id
  492. };
  493. _starpu_opencl_driver_init(&d);
  494. while (_starpu_machine_is_running())
  495. _starpu_opencl_driver_run_once(&d);
  496. _starpu_opencl_driver_deinit(&d);
  497. return NULL;
  498. }
  499. #ifndef STARPU_SIMGRID
  500. static unsigned _starpu_opencl_get_device_name(int dev, char *name, int lname)
  501. {
  502. int err;
  503. if (!init_done)
  504. {
  505. _starpu_opencl_init();
  506. }
  507. // Get device name
  508. err = clGetDeviceInfo(devices[dev], CL_DEVICE_NAME, lname, name, NULL);
  509. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  510. _STARPU_DEBUG("Device %d : [%s]\n", dev, name);
  511. return EXIT_SUCCESS;
  512. }
  513. #endif
  514. unsigned _starpu_opencl_get_device_count(void)
  515. {
  516. if (!init_done)
  517. {
  518. _starpu_opencl_init();
  519. }
  520. return nb_devices;
  521. }
  522. cl_device_type _starpu_opencl_get_device_type(int devid)
  523. {
  524. int err;
  525. cl_device_type type;
  526. if (!init_done)
  527. _starpu_opencl_init();
  528. err = clGetDeviceInfo(devices[devid], CL_DEVICE_TYPE, sizeof(cl_device_type), &type, NULL);
  529. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  530. STARPU_OPENCL_REPORT_ERROR(err);
  531. return type;
  532. }
  533. static int _starpu_opencl_execute_job(struct _starpu_job *j, struct _starpu_worker *args)
  534. {
  535. int ret;
  536. uint32_t mask = 0;
  537. STARPU_ASSERT(j);
  538. struct starpu_task *task = j->task;
  539. int profiling = starpu_profiling_status_get();
  540. struct timespec codelet_start, codelet_end;
  541. STARPU_ASSERT(task);
  542. struct starpu_codelet *cl = task->cl;
  543. STARPU_ASSERT(cl);
  544. ret = _starpu_fetch_task_input(j, mask);
  545. if (ret != 0)
  546. {
  547. /* there was not enough memory, so the input of
  548. * the codelet cannot be fetched ... put the
  549. * codelet back, and try it later */
  550. return -EAGAIN;
  551. }
  552. _starpu_driver_start_job(args, j, &codelet_start, 0, profiling);
  553. starpu_opencl_func_t func = _starpu_task_get_opencl_nth_implementation(cl, j->nimpl);
  554. STARPU_ASSERT(func);
  555. #ifdef STARPU_SIMGRID
  556. double length = NAN;
  557. #ifdef STARPU_OPENCL_SIMULATOR
  558. func(task->interfaces, task->cl_arg);
  559. #ifndef CL_PROFILING_CLOCK_CYCLE_COUNT
  560. #ifdef CL_PROFILING_COMMAND_SHAVE_CYCLE_COUNT
  561. #define CL_PROFILING_CLOCK_CYCLE_COUNT CL_PROFILING_COMMAND_SHAVE_CYCLE_COUNT
  562. #else
  563. #error The OpenCL simulator must provide CL_PROFILING_CLOCK_CYCLE_COUNT
  564. #endif
  565. #endif
  566. struct starpu_task_profiling_info *profiling_info = task->profiling_info;
  567. STARPU_ASSERT_MSG(profiling_info->used_cycles, "Application kernel must call starpu_opencl_collect_stats to collect simulated time");
  568. length = ((double) profiling_info->used_cycles)/MSG_get_host_speed(MSG_host_self());
  569. #endif
  570. _starpu_simgrid_execute_job(j, args->perf_arch, length);
  571. #else
  572. func(task->interfaces, task->cl_arg);
  573. #endif
  574. _starpu_driver_end_job(args, j, args->perf_arch, &codelet_end, 0, profiling);
  575. _starpu_driver_update_job_feedback(j, args, args->perf_arch,
  576. &codelet_start, &codelet_end, profiling);
  577. _starpu_push_task_output(j, mask);
  578. return EXIT_SUCCESS;
  579. }
  580. int _starpu_run_opencl(struct starpu_driver *d)
  581. {
  582. STARPU_ASSERT(d && d->type == STARPU_OPENCL_WORKER);
  583. int nworkers;
  584. int workers[STARPU_MAXOPENCLDEVS];
  585. nworkers = starpu_worker_get_ids_by_type(STARPU_OPENCL_WORKER, workers, STARPU_MAXOPENCLDEVS);
  586. if (nworkers == 0)
  587. return -ENODEV;
  588. int i;
  589. for (i = 0; i < nworkers; i++)
  590. {
  591. cl_device_id device;
  592. int devid = starpu_worker_get_devid(workers[i]);
  593. starpu_opencl_get_device(devid, &device);
  594. if (device == d->id.opencl_id)
  595. break;
  596. }
  597. if (i == nworkers)
  598. return -ENODEV;
  599. struct _starpu_worker *workerarg = _starpu_get_worker_struct(i);
  600. _STARPU_DEBUG("Running OpenCL %u from the application\n", workerarg->devid);
  601. workerarg->set = NULL;
  602. workerarg->worker_is_initialized = 0;
  603. /* Let's go ! */
  604. _starpu_opencl_worker(workerarg);
  605. /* XXX: Should we wait for the driver to be ready, as it is done when
  606. * launching it the usual way ? Cf. the end of _starpu_launch_drivers()
  607. */
  608. return 0;
  609. }