copy_driver.c 25 KB

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