driver_cuda.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2015 Université de Bordeaux
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010, 2011, 2012, 2013, 2014 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 <starpu.h>
  20. #include <starpu_cuda.h>
  21. #include <starpu_profiling.h>
  22. #include <common/utils.h>
  23. #include <common/config.h>
  24. #include <core/debug.h>
  25. #include <drivers/driver_common/driver_common.h>
  26. #include "driver_cuda.h"
  27. #include <core/sched_policy.h>
  28. #ifdef HAVE_CUDA_GL_INTEROP_H
  29. #include <cuda_gl_interop.h>
  30. #endif
  31. #include <datawizard/memory_manager.h>
  32. #include <datawizard/malloc.h>
  33. #ifdef STARPU_SIMGRID
  34. #include <core/simgrid.h>
  35. #endif
  36. /* the number of CUDA devices */
  37. static unsigned ncudagpus;
  38. static size_t global_mem[STARPU_MAXCUDADEVS];
  39. #ifdef STARPU_USE_CUDA
  40. static cudaStream_t streams[STARPU_NMAXWORKERS];
  41. static cudaStream_t out_transfer_streams[STARPU_MAXCUDADEVS];
  42. static cudaStream_t in_transfer_streams[STARPU_MAXCUDADEVS];
  43. /* Note: streams are not thread-safe, so we define them for each CUDA worker
  44. * emitting a GPU-GPU transfer */
  45. static cudaStream_t in_peer_transfer_streams[STARPU_MAXCUDADEVS][STARPU_MAXCUDADEVS];
  46. static cudaStream_t out_peer_transfer_streams[STARPU_MAXCUDADEVS][STARPU_MAXCUDADEVS];
  47. static struct cudaDeviceProp props[STARPU_MAXCUDADEVS];
  48. #ifndef STARPU_SIMGRID
  49. static cudaEvent_t task_events[STARPU_NMAXWORKERS][STARPU_MAX_PIPELINE];
  50. #endif
  51. #endif /* STARPU_USE_CUDA */
  52. #ifdef STARPU_SIMGRID
  53. static unsigned task_finished[STARPU_NMAXWORKERS][STARPU_MAX_PIPELINE];
  54. static starpu_pthread_mutex_t task_mutex[STARPU_NMAXWORKERS][STARPU_MAX_PIPELINE];
  55. static starpu_pthread_cond_t task_cond[STARPU_NMAXWORKERS][STARPU_MAX_PIPELINE];
  56. #endif /* STARPU_SIMGRID */
  57. void
  58. _starpu_cuda_discover_devices (struct _starpu_machine_config *config)
  59. {
  60. /* Discover the number of CUDA devices. Fill the result in CONFIG. */
  61. #ifdef STARPU_SIMGRID
  62. config->topology.nhwcudagpus = _starpu_simgrid_get_nbhosts("CUDA");
  63. #else
  64. int cnt;
  65. cudaError_t cures;
  66. cures = cudaGetDeviceCount (&cnt);
  67. if (STARPU_UNLIKELY(cures != cudaSuccess))
  68. cnt = 0;
  69. config->topology.nhwcudagpus = cnt;
  70. #endif
  71. }
  72. /* In case we want to cap the amount of memory available on the GPUs by the
  73. * mean of the STARPU_LIMIT_CUDA_MEM, we decrease the value of
  74. * global_mem[devid] which is the value returned by
  75. * _starpu_cuda_get_global_mem_size() to indicate how much memory can
  76. * be allocated on the device
  77. */
  78. static void _starpu_cuda_limit_gpu_mem_if_needed(unsigned devid)
  79. {
  80. starpu_ssize_t limit;
  81. size_t STARPU_ATTRIBUTE_UNUSED totalGlobalMem = 0;
  82. size_t STARPU_ATTRIBUTE_UNUSED to_waste = 0;
  83. char name[30];
  84. #ifdef STARPU_SIMGRID
  85. totalGlobalMem = _starpu_simgrid_get_memsize("CUDA", devid);
  86. #elif defined(STARPU_USE_CUDA)
  87. /* Find the size of the memory on the device */
  88. totalGlobalMem = props[devid].totalGlobalMem;
  89. #endif
  90. limit = starpu_get_env_number("STARPU_LIMIT_CUDA_MEM");
  91. if (limit == -1)
  92. {
  93. sprintf(name, "STARPU_LIMIT_CUDA_%u_MEM", devid);
  94. limit = starpu_get_env_number(name);
  95. }
  96. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  97. if (limit == -1)
  98. {
  99. /* Use 90% of the available memory by default. */
  100. limit = totalGlobalMem / (1024*1024) * 0.9;
  101. }
  102. #endif
  103. global_mem[devid] = limit * 1024*1024;
  104. #ifdef STARPU_USE_CUDA
  105. /* How much memory to waste ? */
  106. to_waste = totalGlobalMem - global_mem[devid];
  107. props[devid].totalGlobalMem -= to_waste;
  108. #endif /* STARPU_USE_CUDA */
  109. _STARPU_DEBUG("CUDA device %u: Wasting %ld MB / Limit %ld MB / Total %ld MB / Remains %ld MB\n",
  110. devid, (long) to_waste/(1024*1024), (long) limit, (long) totalGlobalMem/(1024*1024),
  111. (long) (totalGlobalMem - to_waste)/(1024*1024));
  112. }
  113. #ifdef STARPU_USE_CUDA
  114. cudaStream_t starpu_cuda_get_local_in_transfer_stream()
  115. {
  116. int worker = starpu_worker_get_id();
  117. int devid = starpu_worker_get_devid(worker);
  118. cudaStream_t stream;
  119. stream = in_transfer_streams[devid];
  120. STARPU_ASSERT(stream);
  121. return stream;
  122. }
  123. cudaStream_t starpu_cuda_get_local_out_transfer_stream()
  124. {
  125. int worker = starpu_worker_get_id();
  126. int devid = starpu_worker_get_devid(worker);
  127. cudaStream_t stream;
  128. stream = out_transfer_streams[devid];
  129. STARPU_ASSERT(stream);
  130. return stream;
  131. }
  132. cudaStream_t starpu_cuda_get_peer_transfer_stream(unsigned src_node, unsigned dst_node)
  133. {
  134. int worker = starpu_worker_get_id();
  135. int devid = starpu_worker_get_devid(worker);
  136. int src_devid = _starpu_memory_node_get_devid(src_node);
  137. int dst_devid = _starpu_memory_node_get_devid(dst_node);
  138. cudaStream_t stream;
  139. STARPU_ASSERT(devid == src_devid || devid == dst_devid);
  140. if (devid == dst_devid)
  141. stream = in_peer_transfer_streams[src_devid][dst_devid];
  142. else
  143. stream = out_peer_transfer_streams[src_devid][dst_devid];
  144. STARPU_ASSERT(stream);
  145. return stream;
  146. }
  147. cudaStream_t starpu_cuda_get_local_stream(void)
  148. {
  149. int worker = starpu_worker_get_id();
  150. return streams[worker];
  151. }
  152. const struct cudaDeviceProp *starpu_cuda_get_device_properties(unsigned workerid)
  153. {
  154. struct _starpu_machine_config *config = _starpu_get_machine_config();
  155. unsigned devid = config->workers[workerid].devid;
  156. return &props[devid];
  157. }
  158. #endif /* STARPU_USE_CUDA */
  159. void starpu_cuda_set_device(unsigned devid STARPU_ATTRIBUTE_UNUSED)
  160. {
  161. #ifdef STARPU_SIMGRID
  162. STARPU_ABORT();
  163. #else
  164. cudaError_t cures;
  165. struct starpu_conf *conf = _starpu_get_machine_config()->conf;
  166. #if !defined(HAVE_CUDA_MEMCPY_PEER) && defined(HAVE_CUDA_GL_INTEROP_H)
  167. unsigned i;
  168. #endif
  169. #ifdef HAVE_CUDA_MEMCPY_PEER
  170. if (conf->n_cuda_opengl_interoperability)
  171. {
  172. fprintf(stderr, "OpenGL interoperability was requested, but StarPU was built with multithread GPU control support, please reconfigure with --disable-cuda-memcpy-peer but that will disable the memcpy-peer optimizations\n");
  173. STARPU_ABORT();
  174. }
  175. #elif !defined(HAVE_CUDA_GL_INTEROP_H)
  176. if (conf->n_cuda_opengl_interoperability)
  177. {
  178. fprintf(stderr,"OpenGL interoperability was requested, but cuda_gl_interop.h could not be compiled, please make sure that OpenGL headers were available before ./configure run.");
  179. STARPU_ABORT();
  180. }
  181. #else
  182. for (i = 0; i < conf->n_cuda_opengl_interoperability; i++)
  183. if (conf->cuda_opengl_interoperability[i] == devid)
  184. {
  185. cures = cudaGLSetGLDevice(devid);
  186. goto done;
  187. }
  188. #endif
  189. cures = cudaSetDevice(devid);
  190. #if !defined(HAVE_CUDA_MEMCPY_PEER) && defined(HAVE_CUDA_GL_INTEROP_H)
  191. done:
  192. #endif
  193. if (STARPU_UNLIKELY(cures
  194. #ifdef STARPU_OPENMP
  195. /* When StarPU is used as Open Runtime support,
  196. * starpu_omp_shutdown() will usually be called from a
  197. * destructor, in which case cudaThreadExit() reports a
  198. * cudaErrorCudartUnloading here. There should not
  199. * be any remaining tasks running at this point so
  200. * we can probably ignore it without much consequences. */
  201. && cures != cudaErrorCudartUnloading
  202. #endif /* STARPU_OPENMP */
  203. ))
  204. STARPU_CUDA_REPORT_ERROR(cures);
  205. #endif
  206. }
  207. #ifndef STARPU_SIMGRID
  208. static void init_device_context(unsigned devid)
  209. {
  210. int workerid;
  211. unsigned i;
  212. cudaError_t cures;
  213. /* TODO: cudaSetDeviceFlag(cudaDeviceMapHost) */
  214. starpu_cuda_set_device(devid);
  215. #ifdef HAVE_CUDA_MEMCPY_PEER
  216. if (starpu_get_env_number("STARPU_ENABLE_CUDA_GPU_GPU_DIRECT") != 0)
  217. {
  218. int nworkers = starpu_worker_get_count();
  219. for (workerid = 0; workerid < nworkers; workerid++)
  220. {
  221. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  222. if (worker->arch == STARPU_CUDA_WORKER && worker->devid != devid)
  223. {
  224. int can;
  225. cures = cudaDeviceCanAccessPeer(&can, devid, worker->devid);
  226. if (!cures && can)
  227. {
  228. cures = cudaDeviceEnablePeerAccess(worker->devid, 0);
  229. if (!cures)
  230. _STARPU_DEBUG("Enabled GPU-Direct %d -> %d\n", worker->devid, devid);
  231. }
  232. }
  233. }
  234. }
  235. #endif
  236. /* force CUDA to initialize the context for real */
  237. cures = cudaFree(0);
  238. if (STARPU_UNLIKELY(cures))
  239. {
  240. if (cures == cudaErrorDevicesUnavailable)
  241. {
  242. fprintf(stderr,"All CUDA-capable devices are busy or unavailable\n");
  243. exit(77);
  244. }
  245. STARPU_CUDA_REPORT_ERROR(cures);
  246. }
  247. cures = cudaGetDeviceProperties(&props[devid], devid);
  248. if (STARPU_UNLIKELY(cures))
  249. STARPU_CUDA_REPORT_ERROR(cures);
  250. #ifdef HAVE_CUDA_MEMCPY_PEER
  251. if (props[devid].computeMode == cudaComputeModeExclusive)
  252. {
  253. fprintf(stderr, "CUDA is in EXCLUSIVE-THREAD mode, but StarPU was built with multithread GPU control support, please either ask your administrator to use EXCLUSIVE-PROCESS mode (which should really be fine), or reconfigure with --disable-cuda-memcpy-peer but that will disable the memcpy-peer optimizations\n");
  254. STARPU_ABORT();
  255. }
  256. #endif
  257. cures = cudaStreamCreate(&in_transfer_streams[devid]);
  258. if (STARPU_UNLIKELY(cures))
  259. STARPU_CUDA_REPORT_ERROR(cures);
  260. cures = cudaStreamCreate(&out_transfer_streams[devid]);
  261. if (STARPU_UNLIKELY(cures))
  262. STARPU_CUDA_REPORT_ERROR(cures);
  263. for (i = 0; i < ncudagpus; i++)
  264. {
  265. cures = cudaStreamCreate(&in_peer_transfer_streams[i][devid]);
  266. if (STARPU_UNLIKELY(cures))
  267. STARPU_CUDA_REPORT_ERROR(cures);
  268. cures = cudaStreamCreate(&out_peer_transfer_streams[devid][i]);
  269. if (STARPU_UNLIKELY(cures))
  270. STARPU_CUDA_REPORT_ERROR(cures);
  271. }
  272. }
  273. #endif /* !STARPU_SIMGRID */
  274. static void init_worker_context(unsigned workerid)
  275. {
  276. int j;
  277. #ifdef STARPU_SIMGRID
  278. for (j = 0; j < STARPU_MAX_PIPELINE; j++)
  279. {
  280. task_finished[workerid][j] = 0;
  281. STARPU_PTHREAD_MUTEX_INIT(&task_mutex[workerid][j], NULL);
  282. STARPU_PTHREAD_COND_INIT(&task_cond[workerid][j], NULL);
  283. }
  284. #else /* !STARPU_SIMGRID */
  285. cudaError_t cures;
  286. for (j = 0; j < STARPU_MAX_PIPELINE; j++)
  287. {
  288. cures = cudaEventCreateWithFlags(&task_events[workerid][j], cudaEventDisableTiming);
  289. if (STARPU_UNLIKELY(cures))
  290. STARPU_CUDA_REPORT_ERROR(cures);
  291. }
  292. cures = cudaStreamCreate(&streams[workerid]);
  293. if (STARPU_UNLIKELY(cures))
  294. STARPU_CUDA_REPORT_ERROR(cures);
  295. #endif /* !STARPU_SIMGRID */
  296. }
  297. #ifndef STARPU_SIMGRID
  298. static void deinit_device_context(unsigned devid)
  299. {
  300. unsigned i;
  301. cudaStreamDestroy(in_transfer_streams[devid]);
  302. cudaStreamDestroy(out_transfer_streams[devid]);
  303. for (i = 0; i < ncudagpus; i++)
  304. {
  305. cudaStreamDestroy(in_peer_transfer_streams[i][devid]);
  306. cudaStreamDestroy(out_peer_transfer_streams[devid][i]);
  307. }
  308. }
  309. #endif /* !STARPU_SIMGRID */
  310. static void deinit_worker_context(unsigned workerid)
  311. {
  312. unsigned j;
  313. #ifdef STARPU_SIMGRID
  314. for (j = 0; j < STARPU_MAX_PIPELINE; j++)
  315. {
  316. STARPU_PTHREAD_MUTEX_DESTROY(&task_mutex[workerid][j]);
  317. STARPU_PTHREAD_COND_DESTROY(&task_cond[workerid][j]);
  318. }
  319. #else /* STARPU_SIMGRID */
  320. for (j = 0; j < STARPU_MAX_PIPELINE; j++)
  321. cudaEventDestroy(task_events[workerid][j]);
  322. cudaStreamDestroy(streams[workerid]);
  323. #endif /* STARPU_SIMGRID */
  324. }
  325. static size_t _starpu_cuda_get_global_mem_size(unsigned devid)
  326. {
  327. return global_mem[devid];
  328. }
  329. /* Return the number of devices usable in the system.
  330. * The value returned cannot be greater than MAXCUDADEVS */
  331. unsigned _starpu_get_cuda_device_count(void)
  332. {
  333. int cnt;
  334. #ifdef STARPU_SIMGRID
  335. cnt = _starpu_simgrid_get_nbhosts("CUDA");
  336. #else
  337. cudaError_t cures;
  338. cures = cudaGetDeviceCount(&cnt);
  339. if (STARPU_UNLIKELY(cures))
  340. return 0;
  341. #endif
  342. if (cnt > STARPU_MAXCUDADEVS)
  343. {
  344. fprintf(stderr, "# Warning: %d CUDA devices available. Only %d enabled. Use configure option --enable-maxcudadev=xxx to update the maximum value of supported CUDA devices.\n", cnt, STARPU_MAXCUDADEVS);
  345. cnt = STARPU_MAXCUDADEVS;
  346. }
  347. return (unsigned)cnt;
  348. }
  349. void _starpu_init_cuda(void)
  350. {
  351. ncudagpus = _starpu_get_cuda_device_count();
  352. STARPU_ASSERT(ncudagpus <= STARPU_MAXCUDADEVS);
  353. }
  354. static int start_job_on_cuda(struct _starpu_job *j, struct _starpu_worker *worker, unsigned char pipeline_idx STARPU_ATTRIBUTE_UNUSED)
  355. {
  356. int ret;
  357. STARPU_ASSERT(j);
  358. struct starpu_task *task = j->task;
  359. int profiling = starpu_profiling_status_get();
  360. STARPU_ASSERT(task);
  361. struct starpu_codelet *cl = task->cl;
  362. STARPU_ASSERT(cl);
  363. _starpu_set_current_task(task);
  364. ret = _starpu_fetch_task_input(j);
  365. if (ret != 0)
  366. {
  367. /* there was not enough memory, so the input of
  368. * the codelet cannot be fetched ... put the
  369. * codelet back, and try it later */
  370. return -EAGAIN;
  371. }
  372. if (worker->ntasks == 1)
  373. {
  374. /* We are alone in the pipeline, the kernel will start now, record it */
  375. _starpu_driver_start_job(worker, j, &worker->perf_arch, &j->cl_start, 0, profiling);
  376. }
  377. #if defined(HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID)
  378. /* We make sure we do manipulate the proper device */
  379. starpu_cuda_set_device(worker->devid);
  380. #endif
  381. starpu_cuda_func_t func = _starpu_task_get_cuda_nth_implementation(cl, j->nimpl);
  382. STARPU_ASSERT_MSG(func, "when STARPU_CUDA is defined in 'where', cuda_func or cuda_funcs has to be defined");
  383. if (starpu_get_env_number("STARPU_DISABLE_KERNELS") <= 0)
  384. {
  385. _STARPU_TRACE_START_EXECUTING();
  386. #ifdef STARPU_SIMGRID
  387. int async = task->cl->cuda_flags[j->nimpl] & STARPU_CUDA_ASYNC;
  388. unsigned workerid = worker->workerid;
  389. _starpu_simgrid_submit_job(workerid, j, &worker->perf_arch, NAN,
  390. async ? &task_finished[workerid][pipeline_idx] : NULL,
  391. async ? &task_mutex[workerid][pipeline_idx] : NULL,
  392. async ? &task_cond[workerid][pipeline_idx] : NULL);
  393. #else
  394. func(_STARPU_TASK_GET_INTERFACES(task), task->cl_arg);
  395. #endif
  396. _STARPU_TRACE_END_EXECUTING();
  397. }
  398. return 0;
  399. }
  400. static void finish_job_on_cuda(struct _starpu_job *j, struct _starpu_worker *worker)
  401. {
  402. struct timespec codelet_end;
  403. int profiling = starpu_profiling_status_get();
  404. _starpu_set_current_task(NULL);
  405. if (worker->pipeline_length)
  406. worker->current_tasks[worker->first_task] = NULL;
  407. else
  408. worker->current_task = NULL;
  409. worker->first_task = (worker->first_task + 1) % STARPU_MAX_PIPELINE;
  410. worker->ntasks--;
  411. _starpu_driver_end_job(worker, j, &worker->perf_arch, &codelet_end, 0, profiling);
  412. struct _starpu_sched_ctx *sched_ctx = _starpu_sched_ctx_get_sched_ctx_for_worker_and_job(worker, j);
  413. if(!sched_ctx)
  414. sched_ctx = _starpu_get_sched_ctx_struct(j->task->sched_ctx);
  415. if(!sched_ctx->sched_policy)
  416. _starpu_driver_update_job_feedback(j, worker, &sched_ctx->perf_arch, &j->cl_start, &codelet_end, profiling);
  417. else
  418. _starpu_driver_update_job_feedback(j, worker, &worker->perf_arch, &j->cl_start, &codelet_end, profiling);
  419. _starpu_push_task_output(j);
  420. _starpu_handle_job_termination(j);
  421. }
  422. /* Execute a job, up to completion for synchronous jobs */
  423. static void execute_job_on_cuda(struct starpu_task *task, struct _starpu_worker *worker)
  424. {
  425. int workerid = worker->workerid;
  426. int res;
  427. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  428. unsigned char pipeline_idx = (worker->first_task + worker->ntasks - 1)%STARPU_MAX_PIPELINE;
  429. res = start_job_on_cuda(j, worker, pipeline_idx);
  430. if (res)
  431. {
  432. switch (res)
  433. {
  434. case -EAGAIN:
  435. _STARPU_DISP("ouch, CUDA could not actually run task %p, putting it back...\n", task);
  436. _starpu_push_task_to_workers(task);
  437. STARPU_ABORT();
  438. default:
  439. STARPU_ABORT();
  440. }
  441. }
  442. if (task->cl->cuda_flags[j->nimpl] & STARPU_CUDA_ASYNC)
  443. {
  444. if (worker->pipeline_length == 0)
  445. {
  446. #ifdef STARPU_SIMGRID
  447. _starpu_simgrid_wait_tasks(workerid);
  448. #else
  449. /* Forced synchronous execution */
  450. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  451. #endif
  452. finish_job_on_cuda(j, worker);
  453. }
  454. else
  455. {
  456. #ifndef STARPU_SIMGRID
  457. /* Record event to synchronize with task termination later */
  458. cudaEventRecord(task_events[workerid][pipeline_idx], starpu_cuda_get_local_stream());
  459. #endif
  460. #ifdef STARPU_USE_FXT
  461. int k;
  462. for (k = 0; k < (int) worker->set->nworkers; k++)
  463. if (worker->set->workers[k].ntasks == worker->set->workers[k].pipeline_length)
  464. break;
  465. if (k == (int) worker->set->nworkers)
  466. /* Everybody busy */
  467. _STARPU_TRACE_START_EXECUTING();
  468. #endif
  469. }
  470. }
  471. else
  472. /* Synchronous execution */
  473. {
  474. #if !defined(STARPU_SIMGRID)
  475. STARPU_ASSERT_MSG(cudaStreamQuery(starpu_cuda_get_local_stream()) == cudaSuccess, "Unless when using the STARPU_CUDA_ASYNC flag, CUDA codelets have to wait for termination of their kernels on the starpu_cuda_get_local_stream() stream");
  476. #endif
  477. finish_job_on_cuda(j, worker);
  478. }
  479. }
  480. /* XXX Should this be merged with _starpu_init_cuda ? */
  481. int _starpu_cuda_driver_init(struct _starpu_worker_set *worker_set)
  482. {
  483. struct _starpu_worker *worker0 = &worker_set->workers[0];
  484. int lastdevid = -1;
  485. unsigned i;
  486. _starpu_driver_start(worker0, _STARPU_FUT_CUDA_KEY, 0);
  487. _starpu_set_local_worker_set_key(worker_set);
  488. #ifdef STARPU_USE_FXT
  489. for (i = 1; i < worker_set->nworkers; i++)
  490. _starpu_worker_start(&worker_set->workers[i], _STARPU_FUT_CUDA_KEY, 0);
  491. #endif
  492. for (i = 0; i < worker_set->nworkers; i++)
  493. {
  494. struct _starpu_worker *worker = &worker_set->workers[i];
  495. unsigned devid = worker->devid;
  496. unsigned memnode = worker->memory_node;
  497. if ((int) devid == lastdevid)
  498. /* Already initialized */
  499. continue;
  500. lastdevid = devid;
  501. #ifndef STARPU_SIMGRID
  502. init_device_context(devid);
  503. #endif
  504. #ifdef STARPU_SIMGRID
  505. STARPU_ASSERT_MSG(worker_set->nworkers == 1, "Simgrid mode does not support concurrent kernel execution yet\n");
  506. #else /* !STARPU_SIMGRID */
  507. if (worker_set->nworkers > 1 && props[devid].concurrentKernels == 0)
  508. _STARPU_DISP("Warning: STARPU_NWORKER_PER_CUDA is %u, but the device does not support concurrent kernel execution!\n", worker_set->nworkers);
  509. #endif /* !STARPU_SIMGRID */
  510. _starpu_cuda_limit_gpu_mem_if_needed(devid);
  511. _starpu_memory_manager_set_global_memory_size(memnode, _starpu_cuda_get_global_mem_size(devid));
  512. _starpu_malloc_init(memnode);
  513. }
  514. /* one more time to avoid hacks from third party lib :) */
  515. _starpu_bind_thread_on_cpu(worker0->config, worker0->bindid);
  516. for (i = 0; i < worker_set->nworkers; i++)
  517. {
  518. struct _starpu_worker *worker = &worker_set->workers[i];
  519. unsigned devid = worker->devid;
  520. unsigned workerid = worker->workerid;
  521. float size = (float) global_mem[devid] / (1<<30);
  522. #ifdef STARPU_SIMGRID
  523. const char *devname = "Simgrid";
  524. #else
  525. /* get the device's name */
  526. char devname[128];
  527. strncpy(devname, props[devid].name, 128);
  528. #endif
  529. #if defined(STARPU_HAVE_BUSID) && !defined(STARPU_SIMGRID)
  530. #if defined(STARPU_HAVE_DOMAINID) && !defined(STARPU_SIMGRID)
  531. if (props[devid].pciDomainID)
  532. snprintf(worker->name, sizeof(worker->name), "CUDA %u.%u (%s %.1f GiB %04x:%02x:%02x.0)", devid, i, devname, size, props[devid].pciDomainID, props[devid].pciBusID, props[devid].pciDeviceID);
  533. else
  534. #endif
  535. snprintf(worker->name, sizeof(worker->name), "CUDA %u.%u (%s %.1f GiB %02x:%02x.0)", devid, i, devname, size, props[devid].pciBusID, props[devid].pciDeviceID);
  536. #else
  537. snprintf(worker->name, sizeof(worker->name), "CUDA %u.%u (%s %.1f GiB)", devid, i, devname, size);
  538. #endif
  539. snprintf(worker->short_name, sizeof(worker->short_name), "CUDA %u.%u", devid, i);
  540. _STARPU_DEBUG("cuda (%s) dev id %u worker %u thread is ready to run on CPU %d !\n", devname, devid, i, worker->bindid);
  541. worker->pipeline_length = starpu_get_env_number_default("STARPU_CUDA_PIPELINE", 2);
  542. if (worker->pipeline_length > STARPU_MAX_PIPELINE)
  543. {
  544. _STARPU_DISP("Warning: STARPU_CUDA_PIPELINE is %u, but STARPU_MAX_PIPELINE is only %u", worker->pipeline_length, STARPU_MAX_PIPELINE);
  545. worker->pipeline_length = STARPU_MAX_PIPELINE;
  546. }
  547. #if defined(STARPU_SIMGRID) && defined(STARPU_NON_BLOCKING_DRIVERS)
  548. if (worker->pipeline_length >= 1)
  549. {
  550. /* We need blocking drivers, otherwise idle drivers
  551. * would keep consuming real CPU time while just
  552. * polling for task termination */
  553. _STARPU_DISP("Warning: reducing STARPU_CUDA_PIPELINE to 0 because simgrid is enabled and blocking drivers are not enabled\n");
  554. worker->pipeline_length = 0;
  555. }
  556. #endif
  557. #if !defined(STARPU_SIMGRID) && !defined(STARPU_NON_BLOCKING_DRIVERS)
  558. if (worker->pipeline_length >= 1)
  559. {
  560. /* We need non-blocking drivers, to poll for CUDA task
  561. * termination */
  562. _STARPU_DISP("Warning: reducing STARPU_CUDA_PIPELINE to 0 because blocking drivers are enabled (and simgrid is not enabled)\n");
  563. worker->pipeline_length = 0;
  564. }
  565. #endif
  566. init_worker_context(workerid);
  567. _STARPU_TRACE_WORKER_INIT_END(workerid);
  568. }
  569. /* tell the main thread that this one is ready */
  570. STARPU_PTHREAD_MUTEX_LOCK(&worker0->mutex);
  571. worker0->status = STATUS_UNKNOWN;
  572. worker0->worker_is_initialized = 1;
  573. STARPU_PTHREAD_COND_SIGNAL(&worker0->ready_cond);
  574. STARPU_PTHREAD_MUTEX_UNLOCK(&worker0->mutex);
  575. /* tell the main thread that this one is ready */
  576. STARPU_PTHREAD_MUTEX_LOCK(&worker_set->mutex);
  577. worker_set->set_is_initialized = 1;
  578. STARPU_PTHREAD_COND_SIGNAL(&worker_set->ready_cond);
  579. STARPU_PTHREAD_MUTEX_UNLOCK(&worker_set->mutex);
  580. return 0;
  581. }
  582. int _starpu_cuda_driver_run_once(struct _starpu_worker_set *worker_set)
  583. {
  584. struct _starpu_worker *worker0 = &worker_set->workers[0];
  585. unsigned memnode = worker0->memory_node;
  586. struct starpu_task *tasks[worker_set->nworkers], *task;
  587. struct _starpu_job *j;
  588. int i, res;
  589. int idle;
  590. /* First poll for completed jobs */
  591. idle = 0;
  592. for (i = 0; i < (int) worker_set->nworkers; i++)
  593. {
  594. struct _starpu_worker *worker = &worker_set->workers[i];
  595. int workerid = worker->workerid;
  596. if (!worker->ntasks)
  597. {
  598. idle++;
  599. /* Even nothing to test */
  600. continue;
  601. }
  602. task = worker->current_tasks[worker->first_task];
  603. /* On-going asynchronous task, check for its termination first */
  604. #ifdef STARPU_SIMGRID
  605. if (task_finished[workerid][worker->first_task])
  606. #else /* !STARPU_SIMGRID */
  607. cudaError_t cures = cudaEventQuery(task_events[workerid][worker->first_task]);
  608. if (cures != cudaSuccess)
  609. {
  610. STARPU_ASSERT_MSG(cures == cudaErrorNotReady, "CUDA error on task %p, codelet %p (%s): %s (%d)", task, task->cl, _starpu_codelet_get_model_name(task->cl), cudaGetErrorString(cures), cures);
  611. }
  612. else
  613. #endif /* !STARPU_SIMGRID */
  614. {
  615. /* Asynchronous task completed! */
  616. _starpu_set_local_worker_key(worker);
  617. finish_job_on_cuda(_starpu_get_job_associated_to_task(task), worker);
  618. /* See next task if any */
  619. if (worker->ntasks)
  620. {
  621. task = worker->current_tasks[worker->first_task];
  622. j = _starpu_get_job_associated_to_task(task);
  623. if (task->cl->cuda_flags[j->nimpl] & STARPU_CUDA_ASYNC)
  624. {
  625. /* An asynchronous task, it was already
  626. * queued, it's now running, record its start time. */
  627. _starpu_driver_start_job(worker, j, &worker->perf_arch, &j->cl_start, 0, starpu_profiling_status_get());
  628. }
  629. else
  630. {
  631. /* A synchronous task, we have finished
  632. * flushing the pipeline, we can now at
  633. * last execute it. */
  634. _STARPU_TRACE_END_PROGRESS(memnode);
  635. _STARPU_TRACE_EVENT("sync_task");
  636. execute_job_on_cuda(task, worker);
  637. _STARPU_TRACE_EVENT("end_sync_task");
  638. _STARPU_TRACE_START_PROGRESS(memnode);
  639. worker->pipeline_stuck = 0;
  640. }
  641. }
  642. #ifdef STARPU_USE_FXT
  643. int k;
  644. for (k = 0; k < (int) worker_set->nworkers; k++)
  645. if (worker_set->workers[k].ntasks)
  646. break;
  647. if (k == (int) worker_set->nworkers)
  648. /* Everybody busy */
  649. _STARPU_TRACE_END_EXECUTING()
  650. #endif
  651. }
  652. if (worker->ntasks < worker->pipeline_length)
  653. idle++;
  654. }
  655. #ifdef STARPU_NON_BLOCKING_DRIVERS
  656. if (!idle)
  657. {
  658. /* Nothing ready yet, no better thing to do than waiting */
  659. __starpu_datawizard_progress(memnode, 1, 0);
  660. return 0;
  661. }
  662. #endif
  663. /* Something done, make some progress */
  664. __starpu_datawizard_progress(memnode, 1, 1);
  665. /* And pull tasks */
  666. res = _starpu_get_multi_worker_task(worker_set->workers, tasks, worker_set->nworkers, memnode);
  667. if (!res)
  668. return 0;
  669. for (i = 0; i < (int) worker_set->nworkers; i++)
  670. {
  671. struct _starpu_worker *worker = &worker_set->workers[i];
  672. task = tasks[i];
  673. if (!task)
  674. continue;
  675. j = _starpu_get_job_associated_to_task(task);
  676. /* can CUDA do that task ? */
  677. if (!_STARPU_CUDA_MAY_PERFORM(j))
  678. {
  679. /* this is neither a cuda or a cublas task */
  680. worker->ntasks--;
  681. _starpu_push_task_to_workers(task);
  682. continue;
  683. }
  684. if (worker->ntasks > 1 && !(task->cl->cuda_flags[j->nimpl] & STARPU_CUDA_ASYNC))
  685. {
  686. /* We have to execute a non-asynchronous task but we
  687. * still have tasks in the pipeline... Record it to
  688. * prevent more tasks from coming, and do it later */
  689. worker->pipeline_stuck = 1;
  690. continue;
  691. }
  692. _starpu_set_local_worker_key(worker);
  693. _STARPU_TRACE_END_PROGRESS(memnode);
  694. execute_job_on_cuda(task, worker);
  695. _STARPU_TRACE_START_PROGRESS(memnode);
  696. }
  697. return 0;
  698. }
  699. int _starpu_cuda_driver_deinit(struct _starpu_worker_set *worker_set)
  700. {
  701. int lastdevid = -1;
  702. unsigned i;
  703. _STARPU_TRACE_WORKER_DEINIT_START;
  704. for (i = 0; i < worker_set->nworkers; i++)
  705. {
  706. struct _starpu_worker *worker = &worker_set->workers[i];
  707. unsigned devid = worker->devid;
  708. unsigned memnode = worker->memory_node;
  709. if ((int) devid == lastdevid)
  710. /* Already initialized */
  711. continue;
  712. lastdevid = devid;
  713. _starpu_handle_all_pending_node_data_requests(memnode);
  714. /* In case there remains some memory that was automatically
  715. * allocated by StarPU, we release it now. Note that data
  716. * coherency is not maintained anymore at that point ! */
  717. _starpu_free_all_automatically_allocated_buffers(memnode);
  718. _starpu_malloc_shutdown(memnode);
  719. #ifndef STARPU_SIMGRID
  720. deinit_device_context(devid);
  721. #endif /* !STARPU_SIMGRID */
  722. }
  723. for (i = 0; i < worker_set->nworkers; i++)
  724. {
  725. unsigned workerid = worker_set->workers[i].workerid;
  726. deinit_worker_context(workerid);
  727. }
  728. _STARPU_TRACE_WORKER_DEINIT_END(_STARPU_FUT_CUDA_KEY);
  729. return 0;
  730. }
  731. void *_starpu_cuda_worker(void *_arg)
  732. {
  733. struct _starpu_worker_set* worker = _arg;
  734. _starpu_cuda_driver_init(worker);
  735. _STARPU_TRACE_START_PROGRESS(memnode);
  736. while (_starpu_machine_is_running())
  737. {
  738. _starpu_may_pause();
  739. _starpu_cuda_driver_run_once(worker);
  740. }
  741. _STARPU_TRACE_END_PROGRESS(memnode);
  742. _starpu_cuda_driver_deinit(worker);
  743. return NULL;
  744. }
  745. #ifdef STARPU_USE_CUDA
  746. void starpu_cublas_report_error(const char *func, const char *file, int line, int status)
  747. {
  748. char *errormsg;
  749. switch (status)
  750. {
  751. case CUBLAS_STATUS_SUCCESS:
  752. errormsg = "success";
  753. break;
  754. case CUBLAS_STATUS_NOT_INITIALIZED:
  755. errormsg = "not initialized";
  756. break;
  757. case CUBLAS_STATUS_ALLOC_FAILED:
  758. errormsg = "alloc failed";
  759. break;
  760. case CUBLAS_STATUS_INVALID_VALUE:
  761. errormsg = "invalid value";
  762. break;
  763. case CUBLAS_STATUS_ARCH_MISMATCH:
  764. errormsg = "arch mismatch";
  765. break;
  766. case CUBLAS_STATUS_EXECUTION_FAILED:
  767. errormsg = "execution failed";
  768. break;
  769. case CUBLAS_STATUS_INTERNAL_ERROR:
  770. errormsg = "internal error";
  771. break;
  772. default:
  773. errormsg = "unknown error";
  774. break;
  775. }
  776. fprintf(stderr, "oops in %s (%s:%d)... %d: %s \n", func, file, line, status, errormsg);
  777. STARPU_ABORT();
  778. }
  779. void starpu_cuda_report_error(const char *func, const char *file, int line, cudaError_t status)
  780. {
  781. const char *errormsg = cudaGetErrorString(status);
  782. printf("oops in %s (%s:%d)... %d: %s \n", func, file, line, status, errormsg);
  783. STARPU_ABORT();
  784. }
  785. #endif /* STARPU_USE_CUDA */
  786. #ifdef STARPU_USE_CUDA
  787. int
  788. starpu_cuda_copy_async_sync(void *src_ptr, unsigned src_node,
  789. void *dst_ptr, unsigned dst_node,
  790. size_t ssize, cudaStream_t stream,
  791. enum cudaMemcpyKind kind)
  792. {
  793. #ifdef HAVE_CUDA_MEMCPY_PEER
  794. int peer_copy = 0;
  795. int src_dev = -1, dst_dev = -1;
  796. #endif
  797. cudaError_t cures = 0;
  798. if (kind == cudaMemcpyDeviceToDevice && src_node != dst_node)
  799. {
  800. #ifdef HAVE_CUDA_MEMCPY_PEER
  801. peer_copy = 1;
  802. src_dev = _starpu_memory_node_get_devid(src_node);
  803. dst_dev = _starpu_memory_node_get_devid(dst_node);
  804. #else
  805. STARPU_ABORT();
  806. #endif
  807. }
  808. if (stream)
  809. {
  810. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  811. #ifdef HAVE_CUDA_MEMCPY_PEER
  812. if (peer_copy)
  813. {
  814. cures = cudaMemcpyPeerAsync((char *) dst_ptr, dst_dev,
  815. (char *) src_ptr, src_dev,
  816. ssize, stream);
  817. }
  818. else
  819. #endif
  820. {
  821. cures = cudaMemcpyAsync((char *)dst_ptr, (char *)src_ptr, ssize, kind, stream);
  822. }
  823. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  824. }
  825. /* Test if the asynchronous copy has failed or if the caller only asked for a synchronous copy */
  826. if (stream == NULL || cures)
  827. {
  828. /* do it in a synchronous fashion */
  829. #ifdef HAVE_CUDA_MEMCPY_PEER
  830. if (peer_copy)
  831. {
  832. cures = cudaMemcpyPeer((char *) dst_ptr, dst_dev,
  833. (char *) src_ptr, src_dev,
  834. ssize);
  835. }
  836. else
  837. #endif
  838. {
  839. cures = cudaMemcpy((char *)dst_ptr, (char *)src_ptr, ssize, kind);
  840. }
  841. if (STARPU_UNLIKELY(cures))
  842. STARPU_CUDA_REPORT_ERROR(cures);
  843. return 0;
  844. }
  845. return -EAGAIN;
  846. }
  847. #endif /* STARPU_USE_CUDA */
  848. int _starpu_run_cuda(struct _starpu_worker_set *workerarg)
  849. {
  850. /* Let's go ! */
  851. _starpu_cuda_worker(workerarg);
  852. return 0;
  853. }