copy_driver.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2012 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 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_mem_node_descr * const descr = _starpu_get_memory_node_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_mem_node_descr * const descr = _starpu_get_memory_node_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. msg_task_t task = _starpu_simgrid_transfer_task_create(src_node, dst_node, handle->ops->get_size(handle));
  88. if (!req) {
  89. /* this is not associated to a request so it's synchronous */
  90. MSG_task_execute(task);
  91. MSG_task_destroy(task);
  92. return 0;
  93. }
  94. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  95. req->async_channel.event.finished = 0;
  96. _STARPU_PTHREAD_MUTEX_INIT(&req->async_channel.event.mutex, NULL);
  97. _STARPU_PTHREAD_COND_INIT(&req->async_channel.event.cond, NULL);
  98. _starpu_simgrid_post_task(task, &req->async_channel.event.finished, &req->async_channel.event.mutex, &req->async_channel.event.cond);
  99. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  100. _STARPU_TRACE_DATA_COPY(src_node, dst_node, handle->ops->get_size(handle));
  101. return -EAGAIN;
  102. #else /* !SIMGRID */
  103. int ret = 0;
  104. const struct starpu_data_copy_methods *copy_methods = handle->ops->copy_methods;
  105. enum starpu_node_kind src_kind = starpu_node_get_kind(src_node);
  106. enum starpu_node_kind dst_kind = starpu_node_get_kind(dst_node);
  107. #ifdef STARPU_USE_CUDA
  108. cudaError_t cures;
  109. cudaStream_t stream;
  110. #endif
  111. void *src_interface = src_replicate->data_interface;
  112. void *dst_interface = dst_replicate->data_interface;
  113. #if defined(STARPU_USE_CUDA) && defined(HAVE_CUDA_MEMCPY_PEER)
  114. if ((src_kind == STARPU_CUDA_RAM) || (dst_kind == STARPU_CUDA_RAM))
  115. {
  116. int node = (dst_kind == STARPU_CUDA_RAM)?dst_node:src_node;
  117. starpu_cuda_set_device(_starpu_memory_node_to_devid(node));
  118. }
  119. #endif
  120. switch (_STARPU_MEMORY_NODE_TUPLE(src_kind,dst_kind))
  121. {
  122. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM,STARPU_CPU_RAM):
  123. /* STARPU_CPU_RAM -> STARPU_CPU_RAM */
  124. STARPU_ASSERT(copy_methods->ram_to_ram);
  125. copy_methods->ram_to_ram(src_interface, src_node, dst_interface, dst_node);
  126. break;
  127. #ifdef STARPU_USE_CUDA
  128. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CUDA_RAM,STARPU_CPU_RAM):
  129. /* only the proper CUBLAS thread can initiate this directly ! */
  130. #if !defined(HAVE_CUDA_MEMCPY_PEER)
  131. STARPU_ASSERT(_starpu_get_local_memory_node() == src_node);
  132. #endif
  133. STARPU_ASSERT(copy_methods->cuda_to_ram);
  134. if (!req || !copy_methods->cuda_to_ram_async)
  135. {
  136. /* this is not associated to a request so it's synchronous */
  137. copy_methods->cuda_to_ram(src_interface, src_node, dst_interface, dst_node);
  138. }
  139. else
  140. {
  141. req->async_channel.type = STARPU_CUDA_RAM;
  142. cures = cudaEventCreate(&req->async_channel.event.cuda_event);
  143. if (STARPU_UNLIKELY(cures != cudaSuccess)) STARPU_CUDA_REPORT_ERROR(cures);
  144. stream = starpu_cuda_get_local_transfer_stream();
  145. ret = copy_methods->cuda_to_ram_async(src_interface, src_node, dst_interface, dst_node, stream);
  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_get_local_memory_node() == dst_node);
  155. #endif
  156. STARPU_ASSERT(copy_methods->ram_to_cuda);
  157. if (!req || !copy_methods->ram_to_cuda_async)
  158. {
  159. /* this is not associated to a request so it's synchronous */
  160. copy_methods->ram_to_cuda(src_interface, src_node, dst_interface, dst_node);
  161. }
  162. else
  163. {
  164. req->async_channel.type = STARPU_CUDA_RAM;
  165. cures = cudaEventCreate(&req->async_channel.event.cuda_event);
  166. if (STARPU_UNLIKELY(cures != cudaSuccess))
  167. STARPU_CUDA_REPORT_ERROR(cures);
  168. stream = starpu_cuda_get_local_transfer_stream();
  169. ret = copy_methods->ram_to_cuda_async(src_interface, src_node, dst_interface, dst_node, stream);
  170. cures = cudaEventRecord(req->async_channel.event.cuda_event, stream);
  171. if (STARPU_UNLIKELY(cures != cudaSuccess))
  172. STARPU_CUDA_REPORT_ERROR(cures);
  173. }
  174. break;
  175. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CUDA_RAM,STARPU_CUDA_RAM):
  176. /* CUDA - CUDA transfer */
  177. STARPU_ASSERT(copy_methods->cuda_to_cuda || copy_methods->cuda_to_cuda_async);
  178. if (!req || !copy_methods->cuda_to_cuda_async)
  179. {
  180. STARPU_ASSERT(copy_methods->cuda_to_cuda);
  181. /* this is not associated to a request so it's synchronous */
  182. copy_methods->cuda_to_cuda(src_interface, src_node, dst_interface, dst_node);
  183. }
  184. else
  185. {
  186. req->async_channel.type = STARPU_CUDA_RAM;
  187. cures = cudaEventCreate(&req->async_channel.event.cuda_event);
  188. if (STARPU_UNLIKELY(cures != cudaSuccess)) STARPU_CUDA_REPORT_ERROR(cures);
  189. stream = starpu_cuda_get_local_transfer_stream();
  190. ret = copy_methods->cuda_to_cuda_async(src_interface, src_node, dst_interface, dst_node, stream);
  191. cures = cudaEventRecord(req->async_channel.event.cuda_event, stream);
  192. if (STARPU_UNLIKELY(cures != cudaSuccess)) STARPU_CUDA_REPORT_ERROR(cures);
  193. }
  194. break;
  195. #endif
  196. #ifdef STARPU_USE_OPENCL
  197. case _STARPU_MEMORY_NODE_TUPLE(STARPU_OPENCL_RAM,STARPU_CPU_RAM):
  198. /* OpenCL -> RAM */
  199. if (_starpu_get_local_memory_node() == src_node)
  200. {
  201. STARPU_ASSERT(copy_methods->opencl_to_ram);
  202. if (!req || !copy_methods->opencl_to_ram_async)
  203. {
  204. /* this is not associated to a request so it's synchronous */
  205. copy_methods->opencl_to_ram(src_interface, src_node, dst_interface, dst_node);
  206. }
  207. else
  208. {
  209. req->async_channel.type = STARPU_OPENCL_RAM;
  210. ret = copy_methods->opencl_to_ram_async(src_interface, src_node, dst_interface, dst_node, &(req->async_channel.event.opencl_event));
  211. }
  212. }
  213. else
  214. {
  215. /* we should not have a blocking call ! */
  216. STARPU_ABORT();
  217. }
  218. break;
  219. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM,STARPU_OPENCL_RAM):
  220. /* STARPU_CPU_RAM -> STARPU_OPENCL_RAM */
  221. STARPU_ASSERT(_starpu_get_local_memory_node() == dst_node);
  222. STARPU_ASSERT(copy_methods->ram_to_opencl);
  223. if (!req || !copy_methods->ram_to_opencl_async)
  224. {
  225. /* this is not associated to a request so it's synchronous */
  226. copy_methods->ram_to_opencl(src_interface, src_node, dst_interface, dst_node);
  227. }
  228. else
  229. {
  230. req->async_channel.type = STARPU_OPENCL_RAM;
  231. ret = copy_methods->ram_to_opencl_async(src_interface, src_node, dst_interface, dst_node, &(req->async_channel.event.opencl_event));
  232. }
  233. break;
  234. #endif
  235. default:
  236. STARPU_ABORT();
  237. break;
  238. }
  239. return ret;
  240. #endif /* !SIMGRID */
  241. }
  242. int __attribute__((warn_unused_result)) _starpu_driver_copy_data_1_to_1(starpu_data_handle_t handle,
  243. struct _starpu_data_replicate *src_replicate,
  244. struct _starpu_data_replicate *dst_replicate,
  245. unsigned donotread,
  246. struct _starpu_data_request *req,
  247. unsigned may_alloc)
  248. {
  249. if (!donotread)
  250. {
  251. STARPU_ASSERT(src_replicate->allocated);
  252. STARPU_ASSERT(src_replicate->refcnt);
  253. }
  254. int ret_alloc, ret_copy;
  255. unsigned STARPU_ATTRIBUTE_UNUSED com_id = 0;
  256. unsigned src_node = src_replicate->memory_node;
  257. unsigned dst_node = dst_replicate->memory_node;
  258. /* first make sure the destination has an allocated buffer */
  259. if (!dst_replicate->allocated)
  260. {
  261. if (!may_alloc)
  262. return -ENOMEM;
  263. ret_alloc = _starpu_allocate_memory_on_node(handle, dst_replicate,req->prefetch);
  264. if (ret_alloc)
  265. return -ENOMEM;
  266. }
  267. STARPU_ASSERT(dst_replicate->allocated);
  268. STARPU_ASSERT(dst_replicate->refcnt);
  269. /* if there is no need to actually read the data,
  270. * we do not perform any transfer */
  271. if (!donotread)
  272. {
  273. size_t size = _starpu_data_get_size(handle);
  274. _starpu_bus_update_profiling_info((int)src_node, (int)dst_node, size);
  275. #ifdef STARPU_USE_FXT
  276. com_id = STARPU_ATOMIC_ADD(&communication_cnt, 1);
  277. if (req)
  278. req->com_id = com_id;
  279. #endif
  280. _STARPU_TRACE_START_DRIVER_COPY(src_node, dst_node, size, com_id);
  281. ret_copy = copy_data_1_to_1_generic(handle, src_replicate, dst_replicate, req);
  282. #ifdef STARPU_USE_FXT
  283. if (ret_copy != -EAGAIN)
  284. {
  285. _STARPU_TRACE_END_DRIVER_COPY(src_node, dst_node, size, com_id);
  286. }
  287. #endif
  288. return ret_copy;
  289. }
  290. return 0;
  291. }
  292. void _starpu_driver_wait_request_completion(struct _starpu_async_channel *async_channel)
  293. {
  294. #ifdef STARPU_SIMGRID
  295. if (async_channel->event.finished)
  296. return;
  297. _STARPU_PTHREAD_MUTEX_LOCK(&async_channel->event.mutex);
  298. while (!async_channel->event.finished)
  299. _STARPU_PTHREAD_COND_WAIT(&async_channel->event.cond, &async_channel->event.mutex);
  300. _STARPU_PTHREAD_MUTEX_UNLOCK(&async_channel->event.mutex);
  301. #else /* !SIMGRID */
  302. enum starpu_node_kind kind = async_channel->type;
  303. #ifdef STARPU_USE_CUDA
  304. cudaEvent_t event;
  305. cudaError_t cures;
  306. #endif
  307. switch (kind)
  308. {
  309. #ifdef STARPU_USE_CUDA
  310. case STARPU_CUDA_RAM:
  311. event = (*async_channel).event.cuda_event;
  312. cures = cudaEventSynchronize(event);
  313. if (STARPU_UNLIKELY(cures))
  314. STARPU_CUDA_REPORT_ERROR(cures);
  315. cures = cudaEventDestroy(event);
  316. if (STARPU_UNLIKELY(cures))
  317. STARPU_CUDA_REPORT_ERROR(cures);
  318. break;
  319. #endif
  320. #ifdef STARPU_USE_OPENCL
  321. case STARPU_OPENCL_RAM:
  322. {
  323. if ((*async_channel).event.opencl_event == NULL) STARPU_ABORT();
  324. cl_int err = clWaitForEvents(1, &((*async_channel).event.opencl_event));
  325. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  326. clReleaseEvent((*async_channel).event.opencl_event);
  327. break;
  328. }
  329. #endif
  330. case STARPU_CPU_RAM:
  331. default:
  332. STARPU_ABORT();
  333. }
  334. #endif /* !SIMGRID */
  335. }
  336. unsigned _starpu_driver_test_request_completion(struct _starpu_async_channel *async_channel)
  337. {
  338. #ifdef STARPU_SIMGRID
  339. return async_channel->event.finished;
  340. #else /* !SIMGRID */
  341. enum starpu_node_kind kind = async_channel->type;
  342. unsigned success = 0;
  343. #ifdef STARPU_USE_CUDA
  344. cudaEvent_t event;
  345. #endif
  346. switch (kind)
  347. {
  348. #ifdef STARPU_USE_CUDA
  349. case STARPU_CUDA_RAM:
  350. event = (*async_channel).event.cuda_event;
  351. cudaError_t cures = cudaEventQuery(event);
  352. success = (cures == cudaSuccess);
  353. if (success)
  354. cudaEventDestroy(event);
  355. else if (cures != cudaErrorNotReady)
  356. STARPU_CUDA_REPORT_ERROR(cures);
  357. break;
  358. #endif
  359. #ifdef STARPU_USE_OPENCL
  360. case STARPU_OPENCL_RAM:
  361. {
  362. cl_int event_status;
  363. cl_event opencl_event = (*async_channel).event.opencl_event;
  364. if (opencl_event == NULL) STARPU_ABORT();
  365. cl_int err = clGetEventInfo(opencl_event, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof(event_status), &event_status, NULL);
  366. if (err != CL_SUCCESS)
  367. STARPU_OPENCL_REPORT_ERROR(err);
  368. if (event_status < 0)
  369. STARPU_OPENCL_REPORT_ERROR(event_status);
  370. success = (event_status == CL_COMPLETE);
  371. break;
  372. }
  373. #endif
  374. case STARPU_CPU_RAM:
  375. default:
  376. STARPU_ABORT();
  377. }
  378. return success;
  379. #endif /* !SIMGRID */
  380. }