driver_cuda.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010, 2011, 2012, 2013 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. #ifdef STARPU_SIMGRID
  33. #include <core/simgrid.h>
  34. #endif
  35. /* the number of CUDA devices */
  36. static int ncudagpus;
  37. static size_t global_mem[STARPU_NMAXWORKERS];
  38. #ifdef STARPU_USE_CUDA
  39. static cudaStream_t streams[STARPU_NMAXWORKERS];
  40. static cudaStream_t out_transfer_streams[STARPU_NMAXWORKERS];
  41. static cudaStream_t in_transfer_streams[STARPU_NMAXWORKERS];
  42. static cudaStream_t peer_transfer_streams[STARPU_NMAXWORKERS];
  43. static struct cudaDeviceProp props[STARPU_MAXCUDADEVS];
  44. #endif /* STARPU_USE_CUDA */
  45. void
  46. _starpu_cuda_discover_devices (struct _starpu_machine_config *config)
  47. {
  48. /* Discover the number of CUDA devices. Fill the result in CONFIG. */
  49. #ifdef STARPU_SIMGRID
  50. config->topology.nhwcudagpus = _starpu_simgrid_get_nbhosts("CUDA");
  51. #else
  52. int cnt;
  53. cudaError_t cures;
  54. cures = cudaGetDeviceCount (&cnt);
  55. if (STARPU_UNLIKELY(cures != cudaSuccess))
  56. cnt = 0;
  57. config->topology.nhwcudagpus = cnt;
  58. #endif
  59. }
  60. /* In case we want to cap the amount of memory available on the GPUs by the
  61. * mean of the STARPU_LIMIT_CUDA_MEM, we decrease the value of
  62. * global_mem[devid] which is the value returned by
  63. * _starpu_cuda_get_global_mem_size() to indicate how much memory can
  64. * be allocated on the device
  65. */
  66. static void _starpu_cuda_limit_gpu_mem_if_needed(unsigned devid)
  67. {
  68. starpu_ssize_t limit;
  69. size_t STARPU_ATTRIBUTE_UNUSED totalGlobalMem = 0;
  70. size_t STARPU_ATTRIBUTE_UNUSED to_waste = 0;
  71. char name[30];
  72. #ifdef STARPU_USE_CUDA
  73. global_mem[devid] = props[devid].totalGlobalMem;
  74. #endif
  75. limit = starpu_get_env_number("STARPU_LIMIT_CUDA_MEM");
  76. if (limit == -1)
  77. {
  78. sprintf(name, "STARPU_LIMIT_CUDA_%u_MEM", devid);
  79. limit = starpu_get_env_number(name);
  80. }
  81. if (limit == -1)
  82. {
  83. return;
  84. }
  85. global_mem[devid] = limit * 1024*1024;
  86. #ifdef STARPU_USE_CUDA
  87. /* Find the size of the memory on the device */
  88. totalGlobalMem = props[devid].totalGlobalMem;
  89. /* How much memory to waste ? */
  90. to_waste = totalGlobalMem - global_mem[devid];
  91. props[devid].totalGlobalMem -= to_waste;
  92. #endif /* STARPU_USE_CUDA */
  93. _STARPU_DEBUG("CUDA device %u: Wasting %ld MB / Limit %ld MB / Total %ld MB / Remains %ld MB\n",
  94. devid, (long) to_waste/(1024*1024), (long) limit, (long) totalGlobalMem/(1024*1024),
  95. (long) (totalGlobalMem - to_waste)/(1024*1024));
  96. }
  97. #ifdef STARPU_USE_CUDA
  98. cudaStream_t starpu_cuda_get_local_in_transfer_stream(void)
  99. {
  100. int worker = starpu_worker_get_id();
  101. return in_transfer_streams[worker];
  102. }
  103. cudaStream_t starpu_cuda_get_local_out_transfer_stream(void)
  104. {
  105. int worker = starpu_worker_get_id();
  106. return out_transfer_streams[worker];
  107. }
  108. cudaStream_t starpu_cuda_get_local_peer_transfer_stream(void)
  109. {
  110. int worker = starpu_worker_get_id();
  111. return peer_transfer_streams[worker];
  112. }
  113. cudaStream_t starpu_cuda_get_local_stream(void)
  114. {
  115. int worker = starpu_worker_get_id();
  116. return streams[worker];
  117. }
  118. const struct cudaDeviceProp *starpu_cuda_get_device_properties(unsigned workerid)
  119. {
  120. struct _starpu_machine_config *config = _starpu_get_machine_config();
  121. unsigned devid = config->workers[workerid].devid;
  122. return &props[devid];
  123. }
  124. #endif /* STARPU_USE_CUDA */
  125. void starpu_cuda_set_device(unsigned devid STARPU_ATTRIBUTE_UNUSED)
  126. {
  127. #ifdef STARPU_SIMGRID
  128. STARPU_ABORT();
  129. #else
  130. cudaError_t cures;
  131. struct starpu_conf *conf = _starpu_get_machine_config()->conf;
  132. #if !defined(HAVE_CUDA_MEMCPY_PEER) && defined(HAVE_CUDA_GL_INTEROP_H)
  133. unsigned i;
  134. #endif
  135. #ifdef HAVE_CUDA_MEMCPY_PEER
  136. if (conf->n_cuda_opengl_interoperability)
  137. {
  138. 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");
  139. STARPU_ABORT();
  140. }
  141. #elif !defined(HAVE_CUDA_GL_INTEROP_H)
  142. if (conf->n_cuda_opengl_interoperability)
  143. {
  144. 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.");
  145. STARPU_ABORT();
  146. }
  147. #else
  148. for (i = 0; i < conf->n_cuda_opengl_interoperability; i++)
  149. if (conf->cuda_opengl_interoperability[i] == devid)
  150. {
  151. cures = cudaGLSetGLDevice(devid);
  152. goto done;
  153. }
  154. #endif
  155. cures = cudaSetDevice(devid);
  156. #if !defined(HAVE_CUDA_MEMCPY_PEER) && defined(HAVE_CUDA_GL_INTEROP_H)
  157. done:
  158. #endif
  159. if (STARPU_UNLIKELY(cures))
  160. STARPU_CUDA_REPORT_ERROR(cures);
  161. #endif
  162. }
  163. #ifndef STARPU_SIMGRID
  164. static void init_context(unsigned devid)
  165. {
  166. cudaError_t cures;
  167. int workerid;
  168. /* TODO: cudaSetDeviceFlag(cudaDeviceMapHost) */
  169. starpu_cuda_set_device(devid);
  170. #ifdef HAVE_CUDA_MEMCPY_PEER
  171. if (starpu_get_env_number("STARPU_ENABLE_CUDA_GPU_GPU_DIRECT") > 0)
  172. {
  173. int nworkers = starpu_worker_get_count();
  174. for (workerid = 0; workerid < nworkers; workerid++)
  175. {
  176. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  177. if (worker->arch == STARPU_CUDA_WORKER && worker->devid != devid)
  178. {
  179. int can;
  180. cures = cudaDeviceCanAccessPeer(&can, devid, worker->devid);
  181. if (!cures && can)
  182. {
  183. cures = cudaDeviceEnablePeerAccess(worker->devid, 0);
  184. if (!cures)
  185. _STARPU_DEBUG("Enabled GPU-Direct %d -> %d\n", worker->devid, devid);
  186. }
  187. }
  188. }
  189. }
  190. #endif
  191. /* force CUDA to initialize the context for real */
  192. cures = cudaFree(0);
  193. if (STARPU_UNLIKELY(cures))
  194. {
  195. if (cures == cudaErrorDevicesUnavailable)
  196. {
  197. fprintf(stderr,"All CUDA-capable devices are busy or unavailable\n");
  198. exit(77);
  199. }
  200. STARPU_CUDA_REPORT_ERROR(cures);
  201. }
  202. cures = cudaGetDeviceProperties(&props[devid], devid);
  203. if (STARPU_UNLIKELY(cures))
  204. STARPU_CUDA_REPORT_ERROR(cures);
  205. #ifdef HAVE_CUDA_MEMCPY_PEER
  206. if (props[devid].computeMode == cudaComputeModeExclusive)
  207. {
  208. 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");
  209. STARPU_ABORT();
  210. }
  211. #endif
  212. workerid = starpu_worker_get_id();
  213. cures = cudaStreamCreate(&streams[workerid]);
  214. if (STARPU_UNLIKELY(cures))
  215. STARPU_CUDA_REPORT_ERROR(cures);
  216. cures = cudaStreamCreate(&in_transfer_streams[workerid]);
  217. if (STARPU_UNLIKELY(cures))
  218. STARPU_CUDA_REPORT_ERROR(cures);
  219. cures = cudaStreamCreate(&out_transfer_streams[workerid]);
  220. if (STARPU_UNLIKELY(cures))
  221. STARPU_CUDA_REPORT_ERROR(cures);
  222. cures = cudaStreamCreate(&peer_transfer_streams[workerid]);
  223. if (STARPU_UNLIKELY(cures))
  224. STARPU_CUDA_REPORT_ERROR(cures);
  225. }
  226. static void deinit_context(int workerid)
  227. {
  228. cudaError_t cures;
  229. cudaStreamDestroy(streams[workerid]);
  230. cudaStreamDestroy(in_transfer_streams[workerid]);
  231. cudaStreamDestroy(out_transfer_streams[workerid]);
  232. cudaStreamDestroy(peer_transfer_streams[workerid]);
  233. /* cleanup the runtime API internal stuffs (which CUBLAS is using) */
  234. cures = cudaThreadExit();
  235. if (cures)
  236. STARPU_CUDA_REPORT_ERROR(cures);
  237. }
  238. #endif /* !SIMGRID */
  239. static size_t _starpu_cuda_get_global_mem_size(unsigned devid)
  240. {
  241. return global_mem[devid];
  242. }
  243. /* Return the number of devices usable in the system.
  244. * The value returned cannot be greater than MAXCUDADEVS */
  245. unsigned _starpu_get_cuda_device_count(void)
  246. {
  247. #ifdef STARPU_SIMGRID
  248. return _starpu_simgrid_get_nbhosts("CUDA");
  249. #else
  250. int cnt;
  251. cudaError_t cures;
  252. cures = cudaGetDeviceCount(&cnt);
  253. if (STARPU_UNLIKELY(cures))
  254. return 0;
  255. if (cnt > STARPU_MAXCUDADEVS)
  256. {
  257. 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);
  258. cnt = STARPU_MAXCUDADEVS;
  259. }
  260. return (unsigned)cnt;
  261. #endif
  262. }
  263. void _starpu_init_cuda(void)
  264. {
  265. ncudagpus = _starpu_get_cuda_device_count();
  266. STARPU_ASSERT(ncudagpus <= STARPU_MAXCUDADEVS);
  267. }
  268. static int execute_job_on_cuda(struct _starpu_job *j, struct _starpu_worker *args)
  269. {
  270. int ret;
  271. uint32_t mask = 0;
  272. STARPU_ASSERT(j);
  273. struct starpu_task *task = j->task;
  274. struct timespec codelet_start, codelet_end;
  275. int profiling = starpu_profiling_status_get();
  276. STARPU_ASSERT(task);
  277. struct starpu_codelet *cl = task->cl;
  278. STARPU_ASSERT(cl);
  279. ret = _starpu_fetch_task_input(j, mask);
  280. if (ret != 0)
  281. {
  282. /* there was not enough memory, so the input of
  283. * the codelet cannot be fetched ... put the
  284. * codelet back, and try it later */
  285. return -EAGAIN;
  286. }
  287. _starpu_driver_start_job(args, j, &codelet_start, 0, profiling);
  288. #if defined(HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID)
  289. /* We make sure we do manipulate the proper device */
  290. starpu_cuda_set_device(args->devid);
  291. #endif
  292. starpu_cuda_func_t func = _starpu_task_get_cuda_nth_implementation(cl, j->nimpl);
  293. STARPU_ASSERT(func);
  294. #ifdef STARPU_SIMGRID
  295. _starpu_simgrid_execute_job(j, args->perf_arch, NAN);
  296. #else
  297. func(_STARPU_TASK_GET_INTERFACES(task), task->cl_arg);
  298. #endif
  299. _starpu_driver_end_job(args, j, &args->perf_arch, &codelet_end, 0, profiling);
  300. _starpu_driver_update_job_feedback(j, args, &args->perf_arch, &codelet_start, &codelet_end, profiling);
  301. _starpu_push_task_output(j, mask);
  302. return 0;
  303. }
  304. static struct _starpu_worker*
  305. _starpu_get_worker_from_driver(struct starpu_driver *d)
  306. {
  307. unsigned nworkers = starpu_worker_get_count();
  308. unsigned workerid;
  309. for (workerid = 0; workerid < nworkers; workerid++)
  310. {
  311. if (starpu_worker_get_type(workerid) == d->type)
  312. {
  313. struct _starpu_worker *worker;
  314. worker = _starpu_get_worker_struct(workerid);
  315. if (worker->devid == d->id.cuda_id)
  316. return worker;
  317. }
  318. }
  319. return NULL;
  320. }
  321. /* XXX Should this be merged with _starpu_init_cuda ? */
  322. int _starpu_cuda_driver_init(struct starpu_driver *d)
  323. {
  324. struct _starpu_worker* args = _starpu_get_worker_from_driver(d);
  325. STARPU_ASSERT(args);
  326. unsigned devid = args->devid;
  327. _starpu_worker_init(args, _STARPU_FUT_CUDA_KEY);
  328. #ifndef STARPU_SIMGRID
  329. init_context(devid);
  330. #endif
  331. _starpu_cuda_limit_gpu_mem_if_needed(devid);
  332. _starpu_memory_manager_set_global_memory_size(args->memory_node, _starpu_cuda_get_global_mem_size(devid));
  333. /* one more time to avoid hacks from third party lib :) */
  334. _starpu_bind_thread_on_cpu(args->config, args->bindid);
  335. args->status = STATUS_UNKNOWN;
  336. float size = (float) global_mem[devid] / (1<<30);
  337. #ifdef STARPU_SIMGRID
  338. const char *devname = "Simgrid";
  339. #else
  340. /* get the device's name */
  341. char devname[128];
  342. strncpy(devname, props[devid].name, 128);
  343. #endif
  344. #if defined(STARPU_HAVE_BUSID) && !defined(STARPU_SIMGRID)
  345. #if defined(STARPU_HAVE_DOMAINID) && !defined(STARPU_SIMGRID)
  346. if (props[devid].pciDomainID)
  347. snprintf(args->name, sizeof(args->name), "CUDA %u (%s %.1f GiB %04x:%02x:%02x.0)", devid, devname, size, props[devid].pciDomainID, props[devid].pciBusID, props[devid].pciDeviceID);
  348. else
  349. #endif
  350. snprintf(args->name, sizeof(args->name), "CUDA %u (%s %.1f GiB %02x:%02x.0)", devid, devname, size, props[devid].pciBusID, props[devid].pciDeviceID);
  351. #else
  352. snprintf(args->name, sizeof(args->name), "CUDA %u (%s %.1f GiB)", devid, devname, size);
  353. #endif
  354. snprintf(args->short_name, sizeof(args->short_name), "CUDA %u", devid);
  355. _STARPU_DEBUG("cuda (%s) dev id %u thread is ready to run on CPU %d !\n", devname, devid, args->bindid);
  356. _STARPU_TRACE_WORKER_INIT_END;
  357. /* tell the main thread that this one is ready */
  358. STARPU_PTHREAD_MUTEX_LOCK(&args->mutex);
  359. args->worker_is_initialized = 1;
  360. STARPU_PTHREAD_COND_SIGNAL(&args->ready_cond);
  361. STARPU_PTHREAD_MUTEX_UNLOCK(&args->mutex);
  362. return 0;
  363. }
  364. int _starpu_cuda_driver_run_once(struct starpu_driver *d)
  365. {
  366. struct _starpu_worker* args = _starpu_get_worker_from_driver(d);
  367. STARPU_ASSERT(args);
  368. unsigned memnode = args->memory_node;
  369. int workerid = args->workerid;
  370. _STARPU_TRACE_START_PROGRESS(memnode);
  371. _starpu_datawizard_progress(memnode, 1);
  372. _STARPU_TRACE_END_PROGRESS(memnode);
  373. struct starpu_task *task;
  374. struct _starpu_job *j = NULL;
  375. task = _starpu_get_worker_task(args, workerid, memnode);
  376. if (!task)
  377. return 0;
  378. j = _starpu_get_job_associated_to_task(task);
  379. /* can CUDA do that task ? */
  380. if (!_STARPU_CUDA_MAY_PERFORM(j))
  381. {
  382. /* this is neither a cuda or a cublas task */
  383. _starpu_push_task_to_workers(task);
  384. return 0;
  385. }
  386. _starpu_set_current_task(task);
  387. args->current_task = j->task;
  388. int res = execute_job_on_cuda(j, args);
  389. _starpu_set_current_task(NULL);
  390. args->current_task = NULL;
  391. if (res)
  392. {
  393. switch (res)
  394. {
  395. case -EAGAIN:
  396. _STARPU_DISP("ouch, CUDA could not actually run task %p, putting it back...\n", task);
  397. _starpu_push_task_to_workers(task);
  398. STARPU_ABORT();
  399. default:
  400. STARPU_ABORT();
  401. }
  402. }
  403. _starpu_handle_job_termination(j);
  404. return 0;
  405. }
  406. int _starpu_cuda_driver_deinit(struct starpu_driver *d)
  407. {
  408. struct _starpu_worker* args = _starpu_get_worker_from_driver(d);
  409. STARPU_ASSERT(args);
  410. unsigned memnode = args->memory_node;
  411. _STARPU_TRACE_WORKER_DEINIT_START;
  412. _starpu_handle_all_pending_node_data_requests(memnode);
  413. /* In case there remains some memory that was automatically
  414. * allocated by StarPU, we release it now. Note that data
  415. * coherency is not maintained anymore at that point ! */
  416. _starpu_free_all_automatically_allocated_buffers(memnode);
  417. #ifndef STARPU_SIMGRID
  418. deinit_context(args->workerid);
  419. #endif
  420. _STARPU_TRACE_WORKER_DEINIT_END(_STARPU_FUT_CUDA_KEY);
  421. return 0;
  422. }
  423. void *_starpu_cuda_worker(void *arg)
  424. {
  425. struct _starpu_worker* args = arg;
  426. struct starpu_driver d =
  427. {
  428. .type = STARPU_CUDA_WORKER,
  429. .id.cuda_id = args->devid
  430. };
  431. _starpu_cuda_driver_init(&d);
  432. while (_starpu_machine_is_running())
  433. _starpu_cuda_driver_run_once(&d);
  434. _starpu_cuda_driver_deinit(&d);
  435. return NULL;
  436. }
  437. #ifdef STARPU_USE_CUDA
  438. void starpu_cublas_report_error(const char *func, const char *file, int line, cublasStatus status)
  439. {
  440. char *errormsg;
  441. switch (status)
  442. {
  443. case CUBLAS_STATUS_SUCCESS:
  444. errormsg = "success";
  445. break;
  446. case CUBLAS_STATUS_NOT_INITIALIZED:
  447. errormsg = "not initialized";
  448. break;
  449. case CUBLAS_STATUS_ALLOC_FAILED:
  450. errormsg = "alloc failed";
  451. break;
  452. case CUBLAS_STATUS_INVALID_VALUE:
  453. errormsg = "invalid value";
  454. break;
  455. case CUBLAS_STATUS_ARCH_MISMATCH:
  456. errormsg = "arch mismatch";
  457. break;
  458. case CUBLAS_STATUS_EXECUTION_FAILED:
  459. errormsg = "execution failed";
  460. break;
  461. case CUBLAS_STATUS_INTERNAL_ERROR:
  462. errormsg = "internal error";
  463. break;
  464. default:
  465. errormsg = "unknown error";
  466. break;
  467. }
  468. fprintf(stderr, "oops in %s (%s:%d)... %d: %s \n", func, file, line, status, errormsg);
  469. STARPU_ABORT();
  470. }
  471. void starpu_cuda_report_error(const char *func, const char *file, int line, cudaError_t status)
  472. {
  473. const char *errormsg = cudaGetErrorString(status);
  474. printf("oops in %s (%s:%d)... %d: %s \n", func, file, line, status, errormsg);
  475. STARPU_ABORT();
  476. }
  477. #endif /* STARPU_USE_CUDA */
  478. #ifdef STARPU_USE_CUDA
  479. int
  480. starpu_cuda_copy_async_sync(void *src_ptr, unsigned src_node,
  481. void *dst_ptr, unsigned dst_node,
  482. size_t ssize, cudaStream_t stream,
  483. enum cudaMemcpyKind kind)
  484. {
  485. #ifdef HAVE_CUDA_MEMCPY_PEER
  486. int peer_copy = 0;
  487. int src_dev = -1, dst_dev = -1;
  488. #endif
  489. cudaError_t cures = 0;
  490. if (kind == cudaMemcpyDeviceToDevice && src_node != dst_node)
  491. {
  492. #ifdef HAVE_CUDA_MEMCPY_PEER
  493. peer_copy = 1;
  494. src_dev = _starpu_memory_node_get_devid(src_node);
  495. dst_dev = _starpu_memory_node_get_devid(dst_node);
  496. #else
  497. STARPU_ABORT();
  498. #endif
  499. }
  500. if (stream)
  501. {
  502. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  503. #ifdef HAVE_CUDA_MEMCPY_PEER
  504. if (peer_copy)
  505. {
  506. cures = cudaMemcpyPeerAsync((char *) dst_ptr, dst_dev,
  507. (char *) src_ptr, src_dev,
  508. ssize, stream);
  509. }
  510. else
  511. #endif
  512. {
  513. cures = cudaMemcpyAsync((char *)dst_ptr, (char *)src_ptr, ssize, kind, stream);
  514. }
  515. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  516. }
  517. /* Test if the asynchronous copy has failed or if the caller only asked for a synchronous copy */
  518. if (stream == NULL || cures)
  519. {
  520. /* do it in a synchronous fashion */
  521. #ifdef HAVE_CUDA_MEMCPY_PEER
  522. if (peer_copy)
  523. {
  524. cures = cudaMemcpyPeer((char *) dst_ptr, dst_dev,
  525. (char *) src_ptr, src_dev,
  526. ssize);
  527. }
  528. else
  529. #endif
  530. {
  531. cures = cudaMemcpy((char *)dst_ptr, (char *)src_ptr, ssize, kind);
  532. }
  533. if (STARPU_UNLIKELY(cures))
  534. STARPU_CUDA_REPORT_ERROR(cures);
  535. return 0;
  536. }
  537. return -EAGAIN;
  538. }
  539. #endif /* STARPU_USE_CUDA */
  540. int _starpu_run_cuda(struct starpu_driver *d)
  541. {
  542. STARPU_ASSERT(d && d->type == STARPU_CUDA_WORKER);
  543. int workerid = starpu_worker_get_by_devid(STARPU_CUDA_WORKER, d->id.cuda_id);
  544. _STARPU_DEBUG("Running cuda %u from the application\n", d->id.cuda_id);
  545. struct _starpu_worker *workerarg = _starpu_get_worker_struct(workerid);
  546. workerarg->set = NULL;
  547. workerarg->worker_is_initialized = 0;
  548. /* Let's go ! */
  549. _starpu_cuda_worker(workerarg);
  550. /* XXX: Should we wait for the driver to be ready, as it is done when
  551. * launching it the usual way ? Cf. the end of _starpu_launch_drivers()
  552. */
  553. return 0;
  554. }