copy_driver.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2011 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. void _starpu_wake_all_blocked_workers_on_node(unsigned nodeid)
  29. {
  30. /* wake up all workers on that memory node */
  31. unsigned cond_id;
  32. struct _starpu_mem_node_descr * const descr = _starpu_get_memory_node_description();
  33. _STARPU_PTHREAD_RWLOCK_RDLOCK(&descr->conditions_rwlock);
  34. unsigned nconds = descr->condition_count[nodeid];
  35. for (cond_id = 0; cond_id < nconds; cond_id++)
  36. {
  37. struct _starpu_cond_and_mutex *condition;
  38. condition = &descr->conditions_attached_to_node[nodeid][cond_id];
  39. /* wake anybody waiting on that condition */
  40. _STARPU_PTHREAD_MUTEX_LOCK(condition->mutex);
  41. _STARPU_PTHREAD_COND_BROADCAST(condition->cond);
  42. _STARPU_PTHREAD_MUTEX_UNLOCK(condition->mutex);
  43. }
  44. _STARPU_PTHREAD_RWLOCK_UNLOCK(&descr->conditions_rwlock);
  45. }
  46. void starpu_wake_all_blocked_workers(void)
  47. {
  48. /* workers may be blocked on the various queues' conditions */
  49. unsigned cond_id;
  50. struct _starpu_mem_node_descr * const descr = _starpu_get_memory_node_description();
  51. _STARPU_PTHREAD_RWLOCK_RDLOCK(&descr->conditions_rwlock);
  52. unsigned nconds = descr->total_condition_count;
  53. for (cond_id = 0; cond_id < nconds; cond_id++)
  54. {
  55. struct _starpu_cond_and_mutex *condition;
  56. condition = &descr->conditions_all[cond_id];
  57. /* wake anybody waiting on that condition */
  58. _STARPU_PTHREAD_MUTEX_LOCK(condition->mutex);
  59. _STARPU_PTHREAD_COND_BROADCAST(condition->cond);
  60. _STARPU_PTHREAD_MUTEX_UNLOCK(condition->mutex);
  61. }
  62. _STARPU_PTHREAD_RWLOCK_UNLOCK(&descr->conditions_rwlock);
  63. }
  64. #ifdef STARPU_USE_FXT
  65. /* we need to identify each communication so that we can match the beginning
  66. * and the end of a communication in the trace, so we use a unique identifier
  67. * per communication */
  68. static unsigned communication_cnt = 0;
  69. #endif
  70. static int copy_data_1_to_1_generic(starpu_data_handle_t handle,
  71. struct _starpu_data_replicate *src_replicate,
  72. struct _starpu_data_replicate *dst_replicate,
  73. struct _starpu_data_request *req STARPU_ATTRIBUTE_UNUSED)
  74. {
  75. int ret = 0;
  76. const struct starpu_data_copy_methods *copy_methods = handle->ops->copy_methods;
  77. unsigned src_node = src_replicate->memory_node;
  78. unsigned dst_node = dst_replicate->memory_node;
  79. enum _starpu_node_kind src_kind = _starpu_get_node_kind(src_node);
  80. enum _starpu_node_kind dst_kind = _starpu_get_node_kind(dst_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. #ifdef STARPU_USE_CUDA
  86. cudaError_t cures;
  87. cudaStream_t stream;
  88. #endif
  89. _starpu_comm_amounts_inc(src_node, dst_node, handle->ops->get_size(handle));
  90. void *src_interface = src_replicate->data_interface;
  91. void *dst_interface = dst_replicate->data_interface;
  92. #if defined(STARPU_USE_CUDA) && defined(HAVE_CUDA_MEMCPY_PEER)
  93. if ((src_kind == STARPU_CUDA_RAM) || (dst_kind == STARPU_CUDA_RAM))
  94. {
  95. int node = (dst_kind == STARPU_CUDA_RAM)?dst_node:src_node;
  96. cures = cudaSetDevice(_starpu_memory_node_to_devid(node));
  97. STARPU_ASSERT(cures == cudaSuccess);
  98. }
  99. #endif
  100. switch (_STARPU_MEMORY_NODE_TUPLE(src_kind,dst_kind))
  101. {
  102. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM,STARPU_CPU_RAM):
  103. /* STARPU_CPU_RAM -> STARPU_CPU_RAM */
  104. STARPU_ASSERT(copy_methods->ram_to_ram);
  105. copy_methods->ram_to_ram(src_interface, src_node, dst_interface, dst_node);
  106. break;
  107. #ifdef STARPU_USE_CUDA
  108. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CUDA_RAM,STARPU_CPU_RAM):
  109. /* only the proper CUBLAS thread can initiate this directly ! */
  110. STARPU_ASSERT(copy_methods->cuda_to_ram);
  111. if (!req || !copy_methods->cuda_to_ram_async)
  112. {
  113. /* this is not associated to a request so it's synchronous */
  114. copy_methods->cuda_to_ram(src_interface, src_node, dst_interface, dst_node);
  115. }
  116. else
  117. {
  118. req->async_channel.type = STARPU_CUDA_RAM;
  119. cures = cudaEventCreate(&req->async_channel.event.cuda_event);
  120. if (STARPU_UNLIKELY(cures != cudaSuccess)) STARPU_CUDA_REPORT_ERROR(cures);
  121. stream = starpu_cuda_get_local_transfer_stream();
  122. ret = copy_methods->cuda_to_ram_async(src_interface, src_node, dst_interface, dst_node, stream);
  123. cures = cudaEventRecord(req->async_channel.event.cuda_event, stream);
  124. if (STARPU_UNLIKELY(cures != cudaSuccess)) STARPU_CUDA_REPORT_ERROR(cures);
  125. }
  126. break;
  127. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM,STARPU_CUDA_RAM):
  128. /* STARPU_CPU_RAM -> CUBLAS_RAM */
  129. /* only the proper CUBLAS thread can initiate this ! */
  130. STARPU_ASSERT(_starpu_get_local_memory_node() == dst_node);
  131. STARPU_ASSERT(copy_methods->ram_to_cuda);
  132. if (!req || !copy_methods->ram_to_cuda_async)
  133. {
  134. /* this is not associated to a request so it's synchronous */
  135. copy_methods->ram_to_cuda(src_interface, src_node, dst_interface, dst_node);
  136. }
  137. else
  138. {
  139. req->async_channel.type = STARPU_CUDA_RAM;
  140. cures = cudaEventCreate(&req->async_channel.event.cuda_event);
  141. if (STARPU_UNLIKELY(cures != cudaSuccess))
  142. STARPU_CUDA_REPORT_ERROR(cures);
  143. stream = starpu_cuda_get_local_transfer_stream();
  144. ret = copy_methods->ram_to_cuda_async(src_interface, src_node, dst_interface, dst_node, stream);
  145. cures = cudaEventRecord(req->async_channel.event.cuda_event, stream);
  146. if (STARPU_UNLIKELY(cures != cudaSuccess))
  147. STARPU_CUDA_REPORT_ERROR(cures);
  148. }
  149. break;
  150. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CUDA_RAM,STARPU_CUDA_RAM):
  151. /* CUDA - CUDA transfer */
  152. STARPU_ASSERT(copy_methods->cuda_to_cuda || copy_methods->cuda_to_cuda_async);
  153. if (!req || !copy_methods->cuda_to_cuda_async)
  154. {
  155. STARPU_ASSERT(copy_methods->cuda_to_cuda);
  156. /* this is not associated to a request so it's synchronous */
  157. copy_methods->cuda_to_cuda(src_interface, src_node, dst_interface, dst_node);
  158. }
  159. else
  160. {
  161. req->async_channel.type = STARPU_CUDA_RAM;
  162. cures = cudaEventCreate(&req->async_channel.event.cuda_event);
  163. if (STARPU_UNLIKELY(cures != cudaSuccess)) STARPU_CUDA_REPORT_ERROR(cures);
  164. stream = starpu_cuda_get_local_transfer_stream();
  165. ret = copy_methods->cuda_to_cuda_async(src_interface, src_node, dst_interface, dst_node, stream);
  166. cures = cudaEventRecord(req->async_channel.event.cuda_event, stream);
  167. if (STARPU_UNLIKELY(cures != cudaSuccess)) STARPU_CUDA_REPORT_ERROR(cures);
  168. }
  169. break;
  170. #endif
  171. #ifdef STARPU_USE_OPENCL
  172. case _STARPU_MEMORY_NODE_TUPLE(STARPU_OPENCL_RAM,STARPU_CPU_RAM):
  173. /* OpenCL -> RAM */
  174. if (_starpu_get_local_memory_node() == src_node)
  175. {
  176. STARPU_ASSERT(copy_methods->opencl_to_ram);
  177. if (!req || !copy_methods->opencl_to_ram_async)
  178. {
  179. /* this is not associated to a request so it's synchronous */
  180. copy_methods->opencl_to_ram(src_interface, src_node, dst_interface, dst_node);
  181. }
  182. else
  183. {
  184. req->async_channel.type = STARPU_OPENCL_RAM;
  185. ret = copy_methods->opencl_to_ram_async(src_interface, src_node, dst_interface, dst_node, &(req->async_channel.event.opencl_event));
  186. }
  187. }
  188. else
  189. {
  190. /* we should not have a blocking call ! */
  191. STARPU_ABORT();
  192. }
  193. break;
  194. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM,STARPU_OPENCL_RAM):
  195. /* STARPU_CPU_RAM -> STARPU_OPENCL_RAM */
  196. STARPU_ASSERT(_starpu_get_local_memory_node() == dst_node);
  197. STARPU_ASSERT(copy_methods->ram_to_opencl);
  198. if (!req || !copy_methods->ram_to_opencl_async)
  199. {
  200. /* this is not associated to a request so it's synchronous */
  201. copy_methods->ram_to_opencl(src_interface, src_node, dst_interface, dst_node);
  202. }
  203. else
  204. {
  205. req->async_channel.type = STARPU_OPENCL_RAM;
  206. ret = copy_methods->ram_to_opencl_async(src_interface, src_node, dst_interface, dst_node, &(req->async_channel.event.opencl_event));
  207. }
  208. break;
  209. #endif
  210. default:
  211. STARPU_ABORT();
  212. break;
  213. }
  214. return ret;
  215. }
  216. int __attribute__((warn_unused_result)) _starpu_driver_copy_data_1_to_1(starpu_data_handle_t handle,
  217. struct _starpu_data_replicate *src_replicate,
  218. struct _starpu_data_replicate *dst_replicate,
  219. unsigned donotread,
  220. struct _starpu_data_request *req,
  221. unsigned may_alloc)
  222. {
  223. if (!donotread)
  224. {
  225. STARPU_ASSERT(src_replicate->allocated);
  226. STARPU_ASSERT(src_replicate->refcnt);
  227. }
  228. int ret_alloc, ret_copy;
  229. unsigned STARPU_ATTRIBUTE_UNUSED com_id = 0;
  230. unsigned src_node = src_replicate->memory_node;
  231. unsigned dst_node = dst_replicate->memory_node;
  232. /* first make sure the destination has an allocated buffer */
  233. if (!dst_replicate->allocated)
  234. {
  235. if (!may_alloc)
  236. return -ENOMEM;
  237. ret_alloc = _starpu_allocate_memory_on_node(handle, dst_replicate,req->prefetch);
  238. if (ret_alloc)
  239. return -ENOMEM;
  240. }
  241. STARPU_ASSERT(dst_replicate->allocated);
  242. STARPU_ASSERT(dst_replicate->refcnt);
  243. /* if there is no need to actually read the data,
  244. * we do not perform any transfer */
  245. if (!donotread)
  246. {
  247. size_t size = _starpu_data_get_size(handle);
  248. _starpu_bus_update_profiling_info((int)src_node, (int)dst_node, size);
  249. #ifdef STARPU_USE_FXT
  250. com_id = STARPU_ATOMIC_ADD(&communication_cnt, 1);
  251. if (req)
  252. req->com_id = com_id;
  253. #endif
  254. _STARPU_TRACE_START_DRIVER_COPY(src_node, dst_node, size, com_id);
  255. ret_copy = copy_data_1_to_1_generic(handle, src_replicate, dst_replicate, req);
  256. #ifdef STARPU_USE_FXT
  257. if (ret_copy != -EAGAIN)
  258. {
  259. _STARPU_TRACE_END_DRIVER_COPY(src_node, dst_node, size, com_id);
  260. }
  261. #endif
  262. return ret_copy;
  263. }
  264. return 0;
  265. }
  266. void _starpu_driver_wait_request_completion(struct _starpu_async_channel *async_channel)
  267. {
  268. enum _starpu_node_kind kind = async_channel->type;
  269. #ifdef STARPU_USE_CUDA
  270. cudaEvent_t event;
  271. cudaError_t cures;
  272. #endif
  273. switch (kind)
  274. {
  275. #ifdef STARPU_USE_CUDA
  276. case STARPU_CUDA_RAM:
  277. event = (*async_channel).event.cuda_event;
  278. cures = cudaEventSynchronize(event);
  279. if (STARPU_UNLIKELY(cures))
  280. STARPU_CUDA_REPORT_ERROR(cures);
  281. cures = cudaEventDestroy(event);
  282. if (STARPU_UNLIKELY(cures))
  283. STARPU_CUDA_REPORT_ERROR(cures);
  284. break;
  285. #endif
  286. #ifdef STARPU_USE_OPENCL
  287. case STARPU_OPENCL_RAM:
  288. {
  289. if ((*async_channel).event.opencl_event == NULL) STARPU_ABORT();
  290. cl_int err = clWaitForEvents(1, &((*async_channel).event.opencl_event));
  291. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  292. clReleaseEvent((*async_channel).event.opencl_event);
  293. break;
  294. }
  295. #endif
  296. case STARPU_CPU_RAM:
  297. default:
  298. STARPU_ABORT();
  299. }
  300. }
  301. unsigned _starpu_driver_test_request_completion(struct _starpu_async_channel *async_channel)
  302. {
  303. enum _starpu_node_kind kind = async_channel->type;
  304. unsigned success;
  305. #ifdef STARPU_USE_CUDA
  306. cudaEvent_t event;
  307. #endif
  308. switch (kind)
  309. {
  310. #ifdef STARPU_USE_CUDA
  311. case STARPU_CUDA_RAM:
  312. event = (*async_channel).event.cuda_event;
  313. cudaError_t cures = cudaEventQuery(event);
  314. success = (cures == cudaSuccess);
  315. if (success)
  316. cudaEventDestroy(event);
  317. else if (cures != cudaErrorNotReady)
  318. STARPU_CUDA_REPORT_ERROR(cures);
  319. break;
  320. #endif
  321. #ifdef STARPU_USE_OPENCL
  322. case STARPU_OPENCL_RAM:
  323. {
  324. cl_int event_status;
  325. cl_event opencl_event = (*async_channel).event.opencl_event;
  326. if (opencl_event == NULL) STARPU_ABORT();
  327. cl_int err = clGetEventInfo(opencl_event, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof(event_status), &event_status, NULL);
  328. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  329. success = (event_status == CL_COMPLETE);
  330. break;
  331. }
  332. #endif
  333. case STARPU_CPU_RAM:
  334. default:
  335. STARPU_ABORT();
  336. success = 0;
  337. }
  338. return success;
  339. }