copy_driver.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2016 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2013, 2016 CNRS
  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 <common/config.h>
  19. #include <common/utils.h>
  20. #include <core/sched_policy.h>
  21. #include <datawizard/datastats.h>
  22. #include <datawizard/memory_nodes.h>
  23. #include <drivers/disk/driver_disk.h>
  24. #include <common/fxt.h>
  25. #include "copy_driver.h"
  26. #include "memalloc.h"
  27. #include <starpu_opencl.h>
  28. #include <starpu_cuda.h>
  29. #include <profiling/profiling.h>
  30. #include <core/disk.h>
  31. #ifdef STARPU_SIMGRID
  32. #include <core/simgrid.h>
  33. #endif
  34. void _starpu_wake_all_blocked_workers_on_node(unsigned nodeid)
  35. {
  36. /* wake up all workers on that memory node */
  37. unsigned cond_id;
  38. struct _starpu_memory_node_descr * const descr = _starpu_memory_node_get_description();
  39. starpu_pthread_mutex_t *mymutex = NULL;
  40. starpu_pthread_cond_t *mycond = NULL;
  41. const int myworkerid = starpu_worker_get_id();
  42. if (myworkerid >= 0)
  43. starpu_worker_get_sched_condition(myworkerid, &mymutex, &mycond);
  44. STARPU_PTHREAD_RWLOCK_RDLOCK(&descr->conditions_rwlock);
  45. unsigned nconds = descr->condition_count[nodeid];
  46. for (cond_id = 0; cond_id < nconds; cond_id++)
  47. {
  48. struct _starpu_cond_and_mutex *condition;
  49. condition = &descr->conditions_attached_to_node[nodeid][cond_id];
  50. if (condition->mutex == mymutex)
  51. /* No need to wake myself, and I might be called from
  52. * the scheduler with mutex locked, through
  53. * starpu_prefetch_task_input_on_node */
  54. continue;
  55. /* wake anybody waiting on that condition */
  56. STARPU_PTHREAD_MUTEX_LOCK_SCHED(condition->mutex);
  57. STARPU_PTHREAD_COND_BROADCAST(condition->cond);
  58. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(condition->mutex);
  59. }
  60. STARPU_PTHREAD_RWLOCK_UNLOCK(&descr->conditions_rwlock);
  61. #ifdef STARPU_SIMGRID
  62. starpu_pthread_queue_broadcast(&_starpu_simgrid_transfer_queue[nodeid]);
  63. #endif
  64. }
  65. void starpu_wake_all_blocked_workers(void)
  66. {
  67. /* workers may be blocked on the various queues' conditions */
  68. unsigned cond_id;
  69. struct _starpu_memory_node_descr * const descr = _starpu_memory_node_get_description();
  70. starpu_pthread_mutex_t *mymutex = NULL;
  71. starpu_pthread_cond_t *mycond = NULL;
  72. const int myworkerid = starpu_worker_get_id();
  73. if (myworkerid >= 0)
  74. starpu_worker_get_sched_condition(myworkerid, &mymutex, &mycond);
  75. STARPU_PTHREAD_RWLOCK_RDLOCK(&descr->conditions_rwlock);
  76. unsigned nconds = descr->total_condition_count;
  77. for (cond_id = 0; cond_id < nconds; cond_id++)
  78. {
  79. struct _starpu_cond_and_mutex *condition;
  80. condition = &descr->conditions_all[cond_id];
  81. if (condition->mutex == mymutex)
  82. /* No need to wake myself, and I might be called from
  83. * the scheduler with mutex locked, through
  84. * starpu_prefetch_task_input_on_node */
  85. continue;
  86. /* wake anybody waiting on that condition */
  87. STARPU_PTHREAD_MUTEX_LOCK_SCHED(condition->mutex);
  88. STARPU_PTHREAD_COND_BROADCAST(condition->cond);
  89. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(condition->mutex);
  90. }
  91. STARPU_PTHREAD_RWLOCK_UNLOCK(&descr->conditions_rwlock);
  92. #ifdef STARPU_SIMGRID
  93. unsigned workerid, nodeid;
  94. for (workerid = 0; workerid < starpu_worker_get_count(); workerid++)
  95. starpu_pthread_queue_broadcast(&_starpu_simgrid_task_queue[workerid]);
  96. for (nodeid = 0; nodeid < starpu_memory_nodes_get_count(); nodeid++)
  97. starpu_pthread_queue_broadcast(&_starpu_simgrid_transfer_queue[nodeid]);
  98. #endif
  99. }
  100. #ifdef STARPU_USE_FXT
  101. /* we need to identify each communication so that we can match the beginning
  102. * and the end of a communication in the trace, so we use a unique identifier
  103. * per communication */
  104. static unsigned long communication_cnt = 0;
  105. #endif
  106. static int copy_data_1_to_1_generic(starpu_data_handle_t handle,
  107. struct _starpu_data_replicate *src_replicate,
  108. struct _starpu_data_replicate *dst_replicate,
  109. struct _starpu_data_request *req)
  110. {
  111. unsigned src_node = src_replicate->memory_node;
  112. unsigned dst_node = dst_replicate->memory_node;
  113. STARPU_ASSERT(src_replicate->refcnt);
  114. STARPU_ASSERT(dst_replicate->refcnt);
  115. STARPU_ASSERT(src_replicate->allocated);
  116. STARPU_ASSERT(dst_replicate->allocated);
  117. #ifdef STARPU_SIMGRID
  118. return _starpu_simgrid_transfer(handle->ops->get_size(handle), src_node, dst_node, req);
  119. #else /* !SIMGRID */
  120. int ret = 0;
  121. const struct starpu_data_copy_methods *copy_methods = handle->ops->copy_methods;
  122. enum starpu_node_kind src_kind = starpu_node_get_kind(src_node);
  123. enum starpu_node_kind dst_kind = starpu_node_get_kind(dst_node);
  124. #ifdef STARPU_USE_CUDA
  125. cudaError_t cures;
  126. cudaStream_t stream;
  127. #endif
  128. void *src_interface = src_replicate->data_interface;
  129. void *dst_interface = dst_replicate->data_interface;
  130. #if defined(STARPU_USE_CUDA) && defined(HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID)
  131. if ((src_kind == STARPU_CUDA_RAM) || (dst_kind == STARPU_CUDA_RAM))
  132. {
  133. unsigned devid;
  134. if ((src_kind == STARPU_CUDA_RAM) && (dst_kind == STARPU_CUDA_RAM))
  135. {
  136. /* GPU-GPU transfer, issue it from the device we are supposed to drive */
  137. int worker = starpu_worker_get_id_check();
  138. devid = starpu_worker_get_devid(worker);
  139. }
  140. else
  141. {
  142. unsigned node = (dst_kind == STARPU_CUDA_RAM)?dst_node:src_node;
  143. devid = _starpu_memory_node_get_devid(node);
  144. }
  145. starpu_cuda_set_device(devid);
  146. }
  147. #endif
  148. switch (_STARPU_MEMORY_NODE_TUPLE(src_kind,dst_kind))
  149. {
  150. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM,STARPU_CPU_RAM):
  151. /* STARPU_CPU_RAM -> STARPU_CPU_RAM */
  152. if (copy_methods->ram_to_ram)
  153. copy_methods->ram_to_ram(src_interface, src_node, dst_interface, dst_node);
  154. else
  155. copy_methods->any_to_any(src_interface, src_node, dst_interface, dst_node, req ? &req->async_channel : NULL);
  156. break;
  157. #ifdef STARPU_USE_CUDA
  158. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CUDA_RAM,STARPU_CPU_RAM):
  159. /* only the proper CUBLAS thread can initiate this directly ! */
  160. #if !defined(HAVE_CUDA_MEMCPY_PEER)
  161. STARPU_ASSERT(_starpu_memory_node_get_local_key() == src_node);
  162. #endif
  163. if (!req || starpu_asynchronous_copy_disabled() || starpu_asynchronous_cuda_copy_disabled() ||
  164. !(copy_methods->cuda_to_ram_async || copy_methods->any_to_any))
  165. {
  166. /* this is not associated to a request so it's synchronous */
  167. STARPU_ASSERT(copy_methods->cuda_to_ram || copy_methods->any_to_any);
  168. if (copy_methods->cuda_to_ram)
  169. copy_methods->cuda_to_ram(src_interface, src_node, dst_interface, dst_node);
  170. else
  171. copy_methods->any_to_any(src_interface, src_node, dst_interface, dst_node, NULL);
  172. }
  173. else
  174. {
  175. req->async_channel.type = STARPU_CUDA_RAM;
  176. cures = cudaEventCreateWithFlags(&req->async_channel.event.cuda_event, cudaEventDisableTiming);
  177. if (STARPU_UNLIKELY(cures != cudaSuccess)) STARPU_CUDA_REPORT_ERROR(cures);
  178. stream = starpu_cuda_get_local_out_transfer_stream();
  179. if (copy_methods->cuda_to_ram_async)
  180. ret = copy_methods->cuda_to_ram_async(src_interface, src_node, dst_interface, dst_node, stream);
  181. else
  182. {
  183. STARPU_ASSERT(copy_methods->any_to_any);
  184. ret = copy_methods->any_to_any(src_interface, src_node, dst_interface, dst_node, &req->async_channel);
  185. }
  186. cures = cudaEventRecord(req->async_channel.event.cuda_event, stream);
  187. if (STARPU_UNLIKELY(cures != cudaSuccess)) STARPU_CUDA_REPORT_ERROR(cures);
  188. }
  189. break;
  190. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM,STARPU_CUDA_RAM):
  191. /* STARPU_CPU_RAM -> CUBLAS_RAM */
  192. /* only the proper CUBLAS thread can initiate this ! */
  193. #if !defined(HAVE_CUDA_MEMCPY_PEER)
  194. STARPU_ASSERT(_starpu_memory_node_get_local_key() == dst_node);
  195. #endif
  196. if (!req || starpu_asynchronous_copy_disabled() || starpu_asynchronous_cuda_copy_disabled() ||
  197. !(copy_methods->ram_to_cuda_async || copy_methods->any_to_any))
  198. {
  199. /* this is not associated to a request so it's synchronous */
  200. STARPU_ASSERT(copy_methods->ram_to_cuda || copy_methods->any_to_any);
  201. if (copy_methods->ram_to_cuda)
  202. copy_methods->ram_to_cuda(src_interface, src_node, dst_interface, dst_node);
  203. else
  204. copy_methods->any_to_any(src_interface, src_node, dst_interface, dst_node, NULL);
  205. }
  206. else
  207. {
  208. req->async_channel.type = STARPU_CUDA_RAM;
  209. cures = cudaEventCreateWithFlags(&req->async_channel.event.cuda_event, cudaEventDisableTiming);
  210. if (STARPU_UNLIKELY(cures != cudaSuccess))
  211. STARPU_CUDA_REPORT_ERROR(cures);
  212. stream = starpu_cuda_get_local_in_transfer_stream();
  213. if (copy_methods->ram_to_cuda_async)
  214. ret = copy_methods->ram_to_cuda_async(src_interface, src_node, dst_interface, dst_node, stream);
  215. else
  216. {
  217. STARPU_ASSERT(copy_methods->any_to_any);
  218. ret = copy_methods->any_to_any(src_interface, src_node, dst_interface, dst_node, &req->async_channel);
  219. }
  220. cures = cudaEventRecord(req->async_channel.event.cuda_event, stream);
  221. if (STARPU_UNLIKELY(cures != cudaSuccess))
  222. STARPU_CUDA_REPORT_ERROR(cures);
  223. }
  224. break;
  225. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CUDA_RAM,STARPU_CUDA_RAM):
  226. /* CUDA - CUDA transfer */
  227. if (!req || starpu_asynchronous_copy_disabled() || starpu_asynchronous_cuda_copy_disabled() ||
  228. !(copy_methods->cuda_to_cuda_async || copy_methods->any_to_any))
  229. {
  230. STARPU_ASSERT(copy_methods->cuda_to_cuda || copy_methods->any_to_any);
  231. /* this is not associated to a request so it's synchronous */
  232. if (copy_methods->cuda_to_cuda)
  233. copy_methods->cuda_to_cuda(src_interface, src_node, dst_interface, dst_node);
  234. else
  235. copy_methods->any_to_any(src_interface, src_node, dst_interface, dst_node, NULL);
  236. }
  237. else
  238. {
  239. req->async_channel.type = STARPU_CUDA_RAM;
  240. cures = cudaEventCreateWithFlags(&req->async_channel.event.cuda_event, cudaEventDisableTiming);
  241. if (STARPU_UNLIKELY(cures != cudaSuccess)) STARPU_CUDA_REPORT_ERROR(cures);
  242. stream = starpu_cuda_get_peer_transfer_stream(src_node, dst_node);
  243. if (copy_methods->cuda_to_cuda_async)
  244. ret = copy_methods->cuda_to_cuda_async(src_interface, src_node, dst_interface, dst_node, stream);
  245. else
  246. {
  247. STARPU_ASSERT(copy_methods->any_to_any);
  248. ret = copy_methods->any_to_any(src_interface, src_node, dst_interface, dst_node, &req->async_channel);
  249. }
  250. cures = cudaEventRecord(req->async_channel.event.cuda_event, stream);
  251. if (STARPU_UNLIKELY(cures != cudaSuccess)) STARPU_CUDA_REPORT_ERROR(cures);
  252. }
  253. break;
  254. #endif
  255. #ifdef STARPU_USE_OPENCL
  256. case _STARPU_MEMORY_NODE_TUPLE(STARPU_OPENCL_RAM,STARPU_CPU_RAM):
  257. /* OpenCL -> RAM */
  258. STARPU_ASSERT(_starpu_memory_node_get_local_key() == src_node);
  259. if (!req || starpu_asynchronous_copy_disabled() || starpu_asynchronous_opencl_copy_disabled() ||
  260. !(copy_methods->opencl_to_ram_async || copy_methods->any_to_any))
  261. {
  262. STARPU_ASSERT(copy_methods->opencl_to_ram || copy_methods->any_to_any);
  263. /* this is not associated to a request so it's synchronous */
  264. if (copy_methods->opencl_to_ram)
  265. copy_methods->opencl_to_ram(src_interface, src_node, dst_interface, dst_node);
  266. else
  267. copy_methods->any_to_any(src_interface, src_node, dst_interface, dst_node, NULL);
  268. }
  269. else
  270. {
  271. req->async_channel.type = STARPU_OPENCL_RAM;
  272. if (copy_methods->opencl_to_ram_async)
  273. ret = copy_methods->opencl_to_ram_async(src_interface, src_node, dst_interface, dst_node, &(req->async_channel.event.opencl_event));
  274. else
  275. {
  276. STARPU_ASSERT(copy_methods->any_to_any);
  277. ret = copy_methods->any_to_any(src_interface, src_node, dst_interface, dst_node, &req->async_channel);
  278. }
  279. }
  280. break;
  281. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM,STARPU_OPENCL_RAM):
  282. /* STARPU_CPU_RAM -> STARPU_OPENCL_RAM */
  283. STARPU_ASSERT(_starpu_memory_node_get_local_key() == dst_node);
  284. if (!req || starpu_asynchronous_copy_disabled() || starpu_asynchronous_opencl_copy_disabled() ||
  285. !(copy_methods->ram_to_opencl_async || copy_methods->any_to_any))
  286. {
  287. STARPU_ASSERT(copy_methods->ram_to_opencl || copy_methods->any_to_any);
  288. /* this is not associated to a request so it's synchronous */
  289. if (copy_methods->ram_to_opencl)
  290. copy_methods->ram_to_opencl(src_interface, src_node, dst_interface, dst_node);
  291. else
  292. copy_methods->any_to_any(src_interface, src_node, dst_interface, dst_node, NULL);
  293. }
  294. else
  295. {
  296. req->async_channel.type = STARPU_OPENCL_RAM;
  297. if (copy_methods->ram_to_opencl_async)
  298. ret = copy_methods->ram_to_opencl_async(src_interface, src_node, dst_interface, dst_node, &(req->async_channel.event.opencl_event));
  299. else
  300. {
  301. STARPU_ASSERT(copy_methods->any_to_any);
  302. ret = copy_methods->any_to_any(src_interface, src_node, dst_interface, dst_node, &req->async_channel);
  303. }
  304. }
  305. break;
  306. case _STARPU_MEMORY_NODE_TUPLE(STARPU_OPENCL_RAM,STARPU_OPENCL_RAM):
  307. /* STARPU_OPENCL_RAM -> STARPU_OPENCL_RAM */
  308. STARPU_ASSERT(_starpu_memory_node_get_local_key() == dst_node || _starpu_memory_node_get_local_key() == src_node);
  309. if (!req || starpu_asynchronous_copy_disabled() || starpu_asynchronous_opencl_copy_disabled() ||
  310. !(copy_methods->opencl_to_opencl_async || copy_methods->any_to_any))
  311. {
  312. STARPU_ASSERT(copy_methods->opencl_to_opencl || copy_methods->any_to_any);
  313. /* this is not associated to a request so it's synchronous */
  314. if (copy_methods->opencl_to_opencl)
  315. copy_methods->opencl_to_opencl(src_interface, src_node, dst_interface, dst_node);
  316. else
  317. copy_methods->any_to_any(src_interface, src_node, dst_interface, dst_node, NULL);
  318. }
  319. else
  320. {
  321. req->async_channel.type = STARPU_OPENCL_RAM;
  322. if (copy_methods->opencl_to_opencl_async)
  323. ret = copy_methods->opencl_to_opencl_async(src_interface, src_node, dst_interface, dst_node, &(req->async_channel.event.opencl_event));
  324. else
  325. {
  326. STARPU_ASSERT(copy_methods->any_to_any);
  327. ret = copy_methods->any_to_any(src_interface, src_node, dst_interface, dst_node, &req->async_channel);
  328. }
  329. }
  330. break;
  331. #endif
  332. #ifdef STARPU_USE_MIC
  333. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM,STARPU_MIC_RAM):
  334. /* RAM -> MIC */
  335. if (!req || starpu_asynchronous_copy_disabled() || starpu_asynchronous_mic_copy_disabled() ||
  336. !(copy_methods->ram_to_mic_async || copy_methods->any_to_any))
  337. {
  338. /* this is not associated to a request so it's synchronous */
  339. STARPU_ASSERT(copy_methods->ram_to_mic || copy_methods->any_to_any);
  340. if (copy_methods->ram_to_mic)
  341. copy_methods->ram_to_mic(src_interface, src_node, dst_interface, dst_node);
  342. else
  343. copy_methods->any_to_any(src_interface, src_node, dst_interface, dst_node, NULL);
  344. }
  345. else
  346. {
  347. req->async_channel.type = STARPU_MIC_RAM;
  348. if (copy_methods->ram_to_mic_async)
  349. ret = copy_methods->ram_to_mic_async(src_interface, src_node, dst_interface, dst_node);
  350. else
  351. {
  352. STARPU_ASSERT(copy_methods->any_to_any);
  353. ret = copy_methods->any_to_any(src_interface, src_node, dst_interface, dst_node, &req->async_channel);
  354. }
  355. _starpu_mic_init_event(&(req->async_channel.event.mic_event), dst_node);
  356. }
  357. break;
  358. case _STARPU_MEMORY_NODE_TUPLE(STARPU_MIC_RAM,STARPU_CPU_RAM):
  359. /* MIC -> RAM */
  360. if (!req || starpu_asynchronous_copy_disabled() || starpu_asynchronous_mic_copy_disabled() ||
  361. !(copy_methods->mic_to_ram_async || copy_methods->any_to_any))
  362. {
  363. /* this is not associated to a request so it's synchronous */
  364. STARPU_ASSERT(copy_methods->mic_to_ram || copy_methods->any_to_any);
  365. if (copy_methods->mic_to_ram)
  366. copy_methods->mic_to_ram(src_interface, src_node, dst_interface, dst_node);
  367. else
  368. copy_methods->any_to_any(src_interface, src_node, dst_interface, dst_node, NULL);
  369. }
  370. else
  371. {
  372. req->async_channel.type = STARPU_MIC_RAM;
  373. if (copy_methods->mic_to_ram_async)
  374. ret = copy_methods->mic_to_ram_async(src_interface, src_node, dst_interface, dst_node);
  375. else
  376. {
  377. STARPU_ASSERT(copy_methods->any_to_any);
  378. ret = copy_methods->any_to_any(src_interface, src_node, dst_interface, dst_node, &req->async_channel);
  379. }
  380. _starpu_mic_init_event(&(req->async_channel.event.mic_event), src_node);
  381. }
  382. break;
  383. /* TODO: MIC -> MIC */
  384. #endif
  385. #ifdef STARPU_USE_SCC
  386. /* SCC RAM associated to the master process is considered as
  387. * the main memory node. */
  388. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM,STARPU_SCC_RAM):
  389. /* master private SCC RAM -> slave private SCC RAM */
  390. if (copy_methods->scc_src_to_sink)
  391. copy_methods->scc_src_to_sink(src_interface, src_node, dst_interface, dst_node);
  392. else
  393. copy_methods->any_to_any(src_interface, src_node, dst_interface, dst_node, NULL);
  394. break;
  395. case _STARPU_MEMORY_NODE_TUPLE(STARPU_SCC_RAM,STARPU_CPU_RAM):
  396. /* slave private SCC RAM -> master private SCC RAM */
  397. if (copy_methods->scc_sink_to_src)
  398. copy_methods->scc_sink_to_src(src_interface, src_node, dst_interface, dst_node);
  399. else
  400. copy_methods->any_to_any(src_interface, src_node, dst_interface, dst_node, NULL);
  401. break;
  402. case _STARPU_MEMORY_NODE_TUPLE(STARPU_SCC_RAM,STARPU_SCC_RAM):
  403. /* slave private SCC RAM -> slave private SCC RAM */
  404. if (copy_methods->scc_sink_to_sink)
  405. copy_methods->scc_sink_to_sink(src_interface, src_node, dst_interface, dst_node);
  406. else
  407. copy_methods->any_to_any(src_interface, src_node, dst_interface, dst_node, NULL);
  408. break;
  409. #endif
  410. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM,STARPU_DISK_RAM):
  411. if(copy_methods->any_to_any)
  412. ret = copy_methods->any_to_any(src_interface, src_node, dst_interface, dst_node, req && !starpu_asynchronous_copy_disabled() ? &req->async_channel : NULL);
  413. else
  414. {
  415. void *obj = starpu_data_handle_to_pointer(handle, dst_node);
  416. void * ptr = NULL;
  417. starpu_ssize_t size = 0;
  418. handle->ops->pack_data(handle, src_node, &ptr, &size);
  419. ret = _starpu_disk_full_write(src_node, dst_node, obj, ptr, size, req && !starpu_asynchronous_copy_disabled() ? &req->async_channel : NULL);
  420. if (ret == 0)
  421. /* write is already finished, ptr was allocated in pack_data */
  422. free(ptr);
  423. /* For now, asynchronous is not supported */
  424. STARPU_ASSERT(ret == 0);
  425. }
  426. break;
  427. case _STARPU_MEMORY_NODE_TUPLE(STARPU_DISK_RAM,STARPU_CPU_RAM):
  428. if(copy_methods->any_to_any)
  429. ret = copy_methods->any_to_any(src_interface, src_node, dst_interface, dst_node, req && !starpu_asynchronous_copy_disabled() ? &req->async_channel : NULL);
  430. else
  431. {
  432. void *obj = starpu_data_handle_to_pointer(handle, src_node);
  433. void * ptr = NULL;
  434. size_t size = 0;
  435. ret = _starpu_disk_full_read(src_node, dst_node, obj, &ptr, &size, req && !starpu_asynchronous_copy_disabled() ? &req->async_channel : NULL);
  436. if (ret == 0)
  437. {
  438. /* read is already finished, we can already unpack */
  439. handle->ops->unpack_data(handle, dst_node, ptr, size);
  440. /* ptr is allocated in full_read */
  441. free(ptr);
  442. }
  443. /* For now, asynchronous is not supported */
  444. STARPU_ASSERT(ret == 0);
  445. }
  446. break;
  447. case _STARPU_MEMORY_NODE_TUPLE(STARPU_DISK_RAM,STARPU_DISK_RAM):
  448. ret = copy_methods->any_to_any(src_interface, src_node, dst_interface, dst_node, req ? &req->async_channel : NULL);
  449. break;
  450. default:
  451. STARPU_ABORT();
  452. break;
  453. }
  454. return ret;
  455. #endif /* !SIMGRID */
  456. }
  457. int STARPU_ATTRIBUTE_WARN_UNUSED_RESULT _starpu_driver_copy_data_1_to_1(starpu_data_handle_t handle,
  458. struct _starpu_data_replicate *src_replicate,
  459. struct _starpu_data_replicate *dst_replicate,
  460. unsigned donotread,
  461. struct _starpu_data_request *req,
  462. unsigned may_alloc,
  463. unsigned prefetch STARPU_ATTRIBUTE_UNUSED)
  464. {
  465. if (!donotread)
  466. {
  467. STARPU_ASSERT(src_replicate->allocated);
  468. STARPU_ASSERT(src_replicate->refcnt);
  469. }
  470. unsigned src_node = src_replicate->memory_node;
  471. unsigned dst_node = dst_replicate->memory_node;
  472. /* first make sure the destination has an allocated buffer */
  473. if (!dst_replicate->allocated)
  474. {
  475. if (!may_alloc || _starpu_is_reclaiming(dst_node))
  476. /* We're not supposed to allocate there at the moment */
  477. return -ENOMEM;
  478. int ret_alloc = _starpu_allocate_memory_on_node(handle, dst_replicate, req ? req->prefetch : 0);
  479. if (ret_alloc)
  480. return -ENOMEM;
  481. }
  482. STARPU_ASSERT(dst_replicate->allocated);
  483. STARPU_ASSERT(dst_replicate->refcnt);
  484. /* if there is no need to actually read the data,
  485. * we do not perform any transfer */
  486. if (!donotread)
  487. {
  488. unsigned long STARPU_ATTRIBUTE_UNUSED com_id = 0;
  489. size_t size = _starpu_data_get_size(handle);
  490. _starpu_bus_update_profiling_info((int)src_node, (int)dst_node, size);
  491. #ifdef STARPU_USE_FXT
  492. com_id = STARPU_ATOMIC_ADDL(&communication_cnt, 1);
  493. if (req)
  494. req->com_id = com_id;
  495. #endif
  496. dst_replicate->initialized = 1;
  497. _STARPU_TRACE_START_DRIVER_COPY(src_node, dst_node, size, com_id, prefetch, handle);
  498. int ret_copy = copy_data_1_to_1_generic(handle, src_replicate, dst_replicate, req);
  499. if (!req)
  500. /* Synchronous, this is already finished */
  501. _STARPU_TRACE_END_DRIVER_COPY(src_node, dst_node, size, com_id, prefetch);
  502. return ret_copy;
  503. }
  504. return 0;
  505. }
  506. /* This can be used by interfaces to easily transfer a piece of data without
  507. * caring about the particular transfer methods. */
  508. /* This should either return 0 if the transfer is complete, or -EAGAIN if the
  509. * transfer is still pending, and will have to be waited for by
  510. * _starpu_driver_test_request_completion/_starpu_driver_wait_request_completion
  511. */
  512. int starpu_interface_copy(uintptr_t src, size_t src_offset, unsigned src_node, uintptr_t dst, size_t dst_offset, unsigned dst_node, size_t size, void *async_data)
  513. {
  514. struct _starpu_async_channel *async_channel = async_data;
  515. enum starpu_node_kind src_kind = starpu_node_get_kind(src_node);
  516. enum starpu_node_kind dst_kind = starpu_node_get_kind(dst_node);
  517. switch (_STARPU_MEMORY_NODE_TUPLE(src_kind,dst_kind))
  518. {
  519. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM,STARPU_CPU_RAM):
  520. memcpy((void *) (dst + dst_offset), (void *) (src + src_offset), size);
  521. return 0;
  522. #ifdef STARPU_USE_CUDA
  523. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CUDA_RAM,STARPU_CPU_RAM):
  524. return starpu_cuda_copy_async_sync(
  525. (void*) (src + src_offset), src_node,
  526. (void*) (dst + dst_offset), dst_node,
  527. size,
  528. async_channel?starpu_cuda_get_local_out_transfer_stream():NULL,
  529. cudaMemcpyDeviceToHost);
  530. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM,STARPU_CUDA_RAM):
  531. return starpu_cuda_copy_async_sync(
  532. (void*) (src + src_offset), src_node,
  533. (void*) (dst + dst_offset), dst_node,
  534. size,
  535. async_channel?starpu_cuda_get_local_in_transfer_stream():NULL,
  536. cudaMemcpyHostToDevice);
  537. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CUDA_RAM,STARPU_CUDA_RAM):
  538. return starpu_cuda_copy_async_sync(
  539. (void*) (src + src_offset), src_node,
  540. (void*) (dst + dst_offset), dst_node,
  541. size,
  542. async_channel?starpu_cuda_get_peer_transfer_stream(src_node, dst_node):NULL,
  543. cudaMemcpyDeviceToDevice);
  544. #endif
  545. #ifdef STARPU_USE_OPENCL
  546. case _STARPU_MEMORY_NODE_TUPLE(STARPU_OPENCL_RAM,STARPU_CPU_RAM):
  547. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM,STARPU_OPENCL_RAM):
  548. case _STARPU_MEMORY_NODE_TUPLE(STARPU_OPENCL_RAM,STARPU_OPENCL_RAM):
  549. return starpu_opencl_copy_async_sync(
  550. src, src_offset, src_node,
  551. dst, dst_offset, dst_node,
  552. size,
  553. &async_channel->event.opencl_event);
  554. #endif
  555. #ifdef STARPU_USE_MIC
  556. case _STARPU_MEMORY_NODE_TUPLE(STARPU_MIC_RAM,STARPU_CPU_RAM):
  557. if (async_data)
  558. return _starpu_mic_copy_mic_to_ram_async(
  559. (void*) (src + src_offset), src_node,
  560. (void*) (dst + dst_offset), dst_node,
  561. size);
  562. else
  563. return _starpu_mic_copy_mic_to_ram(
  564. (void*) (src + src_offset), src_node,
  565. (void*) (dst + dst_offset), dst_node,
  566. size);
  567. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM,STARPU_MIC_RAM):
  568. if (async_data)
  569. return _starpu_mic_copy_ram_to_mic_async(
  570. (void*) (src + src_offset), src_node,
  571. (void*) (dst + dst_offset), dst_node,
  572. size);
  573. else
  574. return _starpu_mic_copy_ram_to_mic(
  575. (void*) (src + src_offset), src_node,
  576. (void*) (dst + dst_offset), dst_node,
  577. size);
  578. /* TODO: MIC->MIC */
  579. #endif
  580. #ifdef STARPU_USE_SCC
  581. case _STARPU_MEMORY_NODE_TUPLE(STARPU_SCC_RAM,STARPU_CPU_RAM):
  582. return _starpu_scc_copy_sink_to_src(
  583. (void*) (src + src_offset), src_node,
  584. (void*) (dst + dst_offset), dst_node,
  585. size);
  586. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM,STARPU_SCC_RAM):
  587. return _starpu_scc_copy_src_to_sink(
  588. (void*) (src + src_offset), src_node,
  589. (void*) (dst + dst_offset), dst_node,
  590. size);
  591. case _STARPU_MEMORY_NODE_TUPLE(STARPU_SCC_RAM,STARPU_SCC_RAM):
  592. return _starpu_scc_copy_sink_to_sink(
  593. (void*) (src + src_offset), src_node,
  594. (void*) (dst + dst_offset), dst_node,
  595. size);
  596. #endif
  597. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM, STARPU_DISK_RAM):
  598. {
  599. return _starpu_disk_copy_src_to_disk(
  600. (void*) (src + src_offset), src_node,
  601. (void*) dst, dst_offset, dst_node,
  602. size, async_channel);
  603. }
  604. case _STARPU_MEMORY_NODE_TUPLE(STARPU_DISK_RAM, STARPU_CPU_RAM):
  605. return _starpu_disk_copy_disk_to_src(
  606. (void*) src, src_offset, src_node,
  607. (void*) (dst + dst_offset), dst_node,
  608. size, async_channel);
  609. case _STARPU_MEMORY_NODE_TUPLE(STARPU_DISK_RAM, STARPU_DISK_RAM):
  610. return _starpu_disk_copy_disk_to_disk(
  611. (void*) src, src_offset, src_node,
  612. (void*) dst, dst_offset, dst_node,
  613. size, async_channel);
  614. default:
  615. STARPU_ABORT();
  616. return -1;
  617. }
  618. return 0;
  619. }
  620. void _starpu_driver_wait_request_completion(struct _starpu_async_channel *async_channel)
  621. {
  622. #ifdef STARPU_SIMGRID
  623. STARPU_PTHREAD_MUTEX_LOCK(&async_channel->event.mutex);
  624. while (!async_channel->event.finished)
  625. STARPU_PTHREAD_COND_WAIT(&async_channel->event.cond, &async_channel->event.mutex);
  626. STARPU_PTHREAD_MUTEX_UNLOCK(&async_channel->event.mutex);
  627. #else /* !SIMGRID */
  628. enum starpu_node_kind kind = async_channel->type;
  629. #ifdef STARPU_USE_CUDA
  630. cudaEvent_t event;
  631. cudaError_t cures;
  632. #endif
  633. switch (kind)
  634. {
  635. #ifdef STARPU_USE_CUDA
  636. case STARPU_CUDA_RAM:
  637. event = (*async_channel).event.cuda_event;
  638. cures = cudaEventSynchronize(event);
  639. if (STARPU_UNLIKELY(cures))
  640. STARPU_CUDA_REPORT_ERROR(cures);
  641. cures = cudaEventDestroy(event);
  642. if (STARPU_UNLIKELY(cures))
  643. STARPU_CUDA_REPORT_ERROR(cures);
  644. break;
  645. #endif
  646. #ifdef STARPU_USE_OPENCL
  647. case STARPU_OPENCL_RAM:
  648. {
  649. cl_int err;
  650. if ((*async_channel).event.opencl_event == NULL)
  651. STARPU_ABORT();
  652. err = clWaitForEvents(1, &((*async_channel).event.opencl_event));
  653. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  654. STARPU_OPENCL_REPORT_ERROR(err);
  655. err = clReleaseEvent((*async_channel).event.opencl_event);
  656. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  657. STARPU_OPENCL_REPORT_ERROR(err);
  658. break;
  659. }
  660. #endif
  661. #ifdef STARPU_USE_MIC
  662. case STARPU_MIC_RAM:
  663. _starpu_mic_wait_request_completion(&(async_channel->event.mic_event));
  664. break;
  665. #endif
  666. case STARPU_MAIN_RAM:
  667. #ifdef STARPU_USE_NUMA
  668. case STARPU_CPU_RAM:
  669. #endif /* STARPU_USE_NUMA */
  670. starpu_disk_wait_request(async_channel);
  671. break;
  672. #ifndef STARPU_USE_NUMA
  673. case STARPU_CPU_RAM:
  674. #endif /* !STARPU_USE_NUMA */
  675. default:
  676. STARPU_ABORT();
  677. }
  678. #endif /* !SIMGRID */
  679. }
  680. unsigned _starpu_driver_test_request_completion(struct _starpu_async_channel *async_channel)
  681. {
  682. #ifdef STARPU_SIMGRID
  683. unsigned ret;
  684. STARPU_PTHREAD_MUTEX_LOCK(&async_channel->event.mutex);
  685. ret = async_channel->event.finished;
  686. STARPU_PTHREAD_MUTEX_UNLOCK(&async_channel->event.mutex);
  687. return ret;
  688. #else /* !SIMGRID */
  689. enum starpu_node_kind kind = async_channel->type;
  690. unsigned success = 0;
  691. #ifdef STARPU_USE_CUDA
  692. cudaEvent_t event;
  693. #endif
  694. switch (kind)
  695. {
  696. #ifdef STARPU_USE_CUDA
  697. case STARPU_CUDA_RAM:
  698. event = (*async_channel).event.cuda_event;
  699. cudaError_t cures = cudaEventQuery(event);
  700. success = (cures == cudaSuccess);
  701. if (success)
  702. cudaEventDestroy(event);
  703. else if (cures != cudaErrorNotReady)
  704. STARPU_CUDA_REPORT_ERROR(cures);
  705. break;
  706. #endif
  707. #ifdef STARPU_USE_OPENCL
  708. case STARPU_OPENCL_RAM:
  709. {
  710. cl_int event_status;
  711. cl_event opencl_event = (*async_channel).event.opencl_event;
  712. if (opencl_event == NULL) STARPU_ABORT();
  713. cl_int err = clGetEventInfo(opencl_event, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof(event_status), &event_status, NULL);
  714. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  715. STARPU_OPENCL_REPORT_ERROR(err);
  716. if (event_status < 0)
  717. STARPU_OPENCL_REPORT_ERROR(event_status);
  718. if (event_status == CL_COMPLETE)
  719. {
  720. err = clReleaseEvent(opencl_event);
  721. if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
  722. }
  723. success = (event_status == CL_COMPLETE);
  724. break;
  725. }
  726. #endif
  727. #ifdef STARPU_USE_MIC
  728. case STARPU_MIC_RAM:
  729. success = _starpu_mic_request_is_complete(&(async_channel->event.mic_event));
  730. break;
  731. #endif
  732. case STARPU_DISK_RAM:
  733. success = starpu_disk_test_request(async_channel);
  734. break;
  735. case STARPU_CPU_RAM:
  736. default:
  737. STARPU_ABORT();
  738. }
  739. return success;
  740. #endif /* !SIMGRID */
  741. }