copy_driver.c 23 KB

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