copy_driver.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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 = cudaEventCreate(&req->async_channel.event.cuda_event);
  137. if (STARPU_UNLIKELY(cures != cudaSuccess)) STARPU_CUDA_REPORT_ERROR(cures);
  138. stream = starpu_cuda_get_out_transfer_stream(src_node);
  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 = cudaEventCreate(&req->async_channel.event.cuda_event);
  170. if (STARPU_UNLIKELY(cures != cudaSuccess))
  171. STARPU_CUDA_REPORT_ERROR(cures);
  172. stream = starpu_cuda_get_in_transfer_stream(dst_node);
  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 = cudaEventCreate(&req->async_channel.event.cuda_event);
  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. _STARPU_TRACE_START_DRIVER_COPY(src_node, dst_node, size, com_id);
  448. ret_copy = copy_data_1_to_1_generic(handle, src_replicate, dst_replicate, req);
  449. if (!req)
  450. /* Synchronous, this is already finished */
  451. _STARPU_TRACE_END_DRIVER_COPY(src_node, dst_node, size, com_id);
  452. return ret_copy;
  453. }
  454. return 0;
  455. }
  456. /* This can be used by interfaces to easily transfer a piece of data without
  457. * caring about the particular CUDA/OpenCL methods. */
  458. /* This should either return 0 if the transfer is complete, or -EAGAIN if the
  459. * transfer is still pending, and will have to be waited for by
  460. * _starpu_driver_test_request_completion/_starpu_driver_wait_request_completion
  461. */
  462. 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)
  463. {
  464. struct _starpu_async_channel *async_channel = async_data;
  465. enum starpu_node_kind src_kind = starpu_node_get_kind(src_node);
  466. enum starpu_node_kind dst_kind = starpu_node_get_kind(dst_node);
  467. switch (_STARPU_MEMORY_NODE_TUPLE(src_kind,dst_kind))
  468. {
  469. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM,STARPU_CPU_RAM):
  470. memcpy((void *) dst + dst_offset, (void *) src + src_offset, size);
  471. return 0;
  472. #ifdef STARPU_USE_CUDA
  473. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CUDA_RAM,STARPU_CPU_RAM):
  474. return starpu_cuda_copy_async_sync(
  475. (void*) src + src_offset, src_node,
  476. (void*) dst + dst_offset, dst_node,
  477. size,
  478. async_channel?starpu_cuda_get_out_transfer_stream(src_node):NULL,
  479. cudaMemcpyDeviceToHost);
  480. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM,STARPU_CUDA_RAM):
  481. return starpu_cuda_copy_async_sync(
  482. (void*) src + src_offset, src_node,
  483. (void*) dst + dst_offset, dst_node,
  484. size,
  485. async_channel?starpu_cuda_get_in_transfer_stream(dst_node):NULL,
  486. cudaMemcpyHostToDevice);
  487. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CUDA_RAM,STARPU_CUDA_RAM):
  488. return starpu_cuda_copy_async_sync(
  489. (void*) src + src_offset, src_node,
  490. (void*) dst + dst_offset, dst_node,
  491. size,
  492. async_channel?starpu_cuda_get_peer_transfer_stream(src_node, dst_node):NULL,
  493. cudaMemcpyDeviceToDevice);
  494. #endif
  495. #ifdef STARPU_USE_OPENCL
  496. case _STARPU_MEMORY_NODE_TUPLE(STARPU_OPENCL_RAM,STARPU_CPU_RAM):
  497. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM,STARPU_OPENCL_RAM):
  498. case _STARPU_MEMORY_NODE_TUPLE(STARPU_OPENCL_RAM,STARPU_OPENCL_RAM):
  499. return starpu_opencl_copy_async_sync(
  500. src, src_offset, src_node,
  501. dst, dst_offset, dst_node,
  502. size,
  503. &async_channel->event.opencl_event);
  504. #endif
  505. #ifdef STARPU_USE_MIC
  506. case _STARPU_MEMORY_NODE_TUPLE(STARPU_MIC_RAM,STARPU_CPU_RAM):
  507. if (async_data)
  508. return _starpu_mic_copy_mic_to_ram_async(
  509. (void*) src + src_offset, src_node,
  510. (void*) dst + dst_offset, dst_node,
  511. size);
  512. else
  513. return _starpu_mic_copy_mic_to_ram(
  514. (void*) src + src_offset, src_node,
  515. (void*) dst + dst_offset, dst_node,
  516. size);
  517. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM,STARPU_MIC_RAM):
  518. if (async_data)
  519. return _starpu_mic_copy_ram_to_mic_async(
  520. (void*) src + src_offset, src_node,
  521. (void*) dst + dst_offset, dst_node,
  522. size);
  523. else
  524. return _starpu_mic_copy_ram_to_mic(
  525. (void*) src + src_offset, src_node,
  526. (void*) dst + dst_offset, dst_node,
  527. size);
  528. #endif
  529. #ifdef STARPU_USE_SCC
  530. case _STARPU_MEMORY_NODE_TUPLE(STARPU_SCC_RAM,STARPU_CPU_RAM):
  531. return _starpu_scc_copy_sink_to_src(
  532. (void*) src + src_offset, src_node,
  533. (void*) dst + dst_offset, dst_node,
  534. size);
  535. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM,STARPU_SCC_RAM):
  536. return _starpu_scc_copy_src_to_sink(
  537. (void*) src + src_offset, src_node,
  538. (void*) dst + dst_offset, dst_node,
  539. size);
  540. case _STARPU_MEMORY_NODE_TUPLE(STARPU_SCC_RAM,STARPU_SCC_RAM):
  541. return _starpu_scc_copy_sink_to_sink(
  542. (void*) src + src_offset, src_node,
  543. (void*) dst + dst_offset, dst_node,
  544. size);
  545. #endif
  546. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM, STARPU_DISK_RAM):
  547. {
  548. return _starpu_disk_copy_src_to_disk(
  549. (void*) src + src_offset, src_node,
  550. (void*) dst, dst_offset, dst_node,
  551. size, async_channel);
  552. }
  553. case _STARPU_MEMORY_NODE_TUPLE(STARPU_DISK_RAM, STARPU_CPU_RAM):
  554. return _starpu_disk_copy_disk_to_src(
  555. (void*) src, src_offset, src_node,
  556. (void*) dst + dst_offset, dst_node,
  557. size, async_channel);
  558. case _STARPU_MEMORY_NODE_TUPLE(STARPU_DISK_RAM, STARPU_DISK_RAM):
  559. return _starpu_disk_copy_disk_to_disk(
  560. (void*) src, src_offset, src_node,
  561. (void*) dst, dst_offset, dst_node,
  562. size, async_channel);
  563. default:
  564. STARPU_ABORT();
  565. return -1;
  566. }
  567. return 0;
  568. }
  569. void _starpu_driver_wait_request_completion(struct _starpu_async_channel *async_channel)
  570. {
  571. #ifdef STARPU_SIMGRID
  572. STARPU_PTHREAD_MUTEX_LOCK(&async_channel->event.mutex);
  573. while (!async_channel->event.finished)
  574. STARPU_PTHREAD_COND_WAIT(&async_channel->event.cond, &async_channel->event.mutex);
  575. STARPU_PTHREAD_MUTEX_UNLOCK(&async_channel->event.mutex);
  576. #else /* !SIMGRID */
  577. enum starpu_node_kind kind = async_channel->type;
  578. #ifdef STARPU_USE_CUDA
  579. cudaEvent_t event;
  580. cudaError_t cures;
  581. #endif
  582. switch (kind)
  583. {
  584. #ifdef STARPU_USE_CUDA
  585. case STARPU_CUDA_RAM:
  586. event = (*async_channel).event.cuda_event;
  587. cures = cudaEventSynchronize(event);
  588. if (STARPU_UNLIKELY(cures))
  589. STARPU_CUDA_REPORT_ERROR(cures);
  590. cures = cudaEventDestroy(event);
  591. if (STARPU_UNLIKELY(cures))
  592. STARPU_CUDA_REPORT_ERROR(cures);
  593. break;
  594. #endif
  595. #ifdef STARPU_USE_OPENCL
  596. case STARPU_OPENCL_RAM:
  597. {
  598. cl_int err;
  599. if ((*async_channel).event.opencl_event == NULL)
  600. STARPU_ABORT();
  601. err = clWaitForEvents(1, &((*async_channel).event.opencl_event));
  602. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  603. STARPU_OPENCL_REPORT_ERROR(err);
  604. err = clReleaseEvent((*async_channel).event.opencl_event);
  605. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  606. STARPU_OPENCL_REPORT_ERROR(err);
  607. break;
  608. }
  609. #endif
  610. #ifdef STARPU_USE_MIC
  611. case STARPU_MIC_RAM:
  612. _starpu_mic_wait_request_completion(&(async_channel->event.mic_event));
  613. break;
  614. #endif
  615. case STARPU_MAIN_RAM:
  616. starpu_disk_wait_request(async_channel);
  617. case STARPU_CPU_RAM:
  618. default:
  619. STARPU_ABORT();
  620. }
  621. #endif /* !SIMGRID */
  622. }
  623. unsigned _starpu_driver_test_request_completion(struct _starpu_async_channel *async_channel)
  624. {
  625. #ifdef STARPU_SIMGRID
  626. unsigned ret;
  627. STARPU_PTHREAD_MUTEX_LOCK(&async_channel->event.mutex);
  628. ret = async_channel->event.finished;
  629. STARPU_PTHREAD_MUTEX_UNLOCK(&async_channel->event.mutex);
  630. return ret;
  631. #else /* !SIMGRID */
  632. enum starpu_node_kind kind = async_channel->type;
  633. unsigned success = 0;
  634. #ifdef STARPU_USE_CUDA
  635. cudaEvent_t event;
  636. #endif
  637. switch (kind)
  638. {
  639. #ifdef STARPU_USE_CUDA
  640. case STARPU_CUDA_RAM:
  641. event = (*async_channel).event.cuda_event;
  642. cudaError_t cures = cudaEventQuery(event);
  643. success = (cures == cudaSuccess);
  644. if (success)
  645. cudaEventDestroy(event);
  646. else if (cures != cudaErrorNotReady)
  647. STARPU_CUDA_REPORT_ERROR(cures);
  648. break;
  649. #endif
  650. #ifdef STARPU_USE_OPENCL
  651. case STARPU_OPENCL_RAM:
  652. {
  653. cl_int event_status;
  654. cl_event opencl_event = (*async_channel).event.opencl_event;
  655. if (opencl_event == NULL) STARPU_ABORT();
  656. cl_int err = clGetEventInfo(opencl_event, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof(event_status), &event_status, NULL);
  657. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  658. STARPU_OPENCL_REPORT_ERROR(err);
  659. if (event_status < 0)
  660. STARPU_OPENCL_REPORT_ERROR(event_status);
  661. success = (event_status == CL_COMPLETE);
  662. break;
  663. }
  664. #endif
  665. #ifdef STARPU_USE_MIC
  666. case STARPU_MIC_RAM:
  667. success = _starpu_mic_request_is_complete(&(async_channel->event.mic_event));
  668. break;
  669. #endif
  670. case STARPU_DISK_RAM:
  671. success = starpu_disk_test_request(async_channel);
  672. break;
  673. case STARPU_CPU_RAM:
  674. default:
  675. STARPU_ABORT();
  676. }
  677. return success;
  678. #endif /* !SIMGRID */
  679. }