copy_driver.c 26 KB

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