copy_driver.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. 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_node_get_kind(src_node);
  80. enum starpu_node_kind dst_kind = starpu_node_get_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. starpu_cuda_set_device(_starpu_memory_node_to_devid(node));
  97. }
  98. #endif
  99. switch (_STARPU_MEMORY_NODE_TUPLE(src_kind,dst_kind))
  100. {
  101. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM,STARPU_CPU_RAM):
  102. /* STARPU_CPU_RAM -> STARPU_CPU_RAM */
  103. STARPU_ASSERT(copy_methods->ram_to_ram);
  104. copy_methods->ram_to_ram(src_interface, src_node, dst_interface, dst_node);
  105. break;
  106. #ifdef STARPU_USE_CUDA
  107. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CUDA_RAM,STARPU_CPU_RAM):
  108. /* only the proper CUBLAS thread can initiate this directly ! */
  109. #if !defined(HAVE_CUDA_MEMCPY_PEER)
  110. STARPU_ASSERT(_starpu_get_local_memory_node() == src_node);
  111. #endif
  112. STARPU_ASSERT(copy_methods->cuda_to_ram);
  113. if (!req || !copy_methods->cuda_to_ram_async)
  114. {
  115. /* this is not associated to a request so it's synchronous */
  116. copy_methods->cuda_to_ram(src_interface, src_node, dst_interface, dst_node);
  117. }
  118. else
  119. {
  120. req->async_channel.type = STARPU_CUDA_RAM;
  121. cures = cudaEventCreate(&req->async_channel.event.cuda_event);
  122. if (STARPU_UNLIKELY(cures != cudaSuccess)) STARPU_CUDA_REPORT_ERROR(cures);
  123. stream = starpu_cuda_get_local_transfer_stream();
  124. ret = copy_methods->cuda_to_ram_async(src_interface, src_node, dst_interface, dst_node, stream);
  125. cures = cudaEventRecord(req->async_channel.event.cuda_event, stream);
  126. if (STARPU_UNLIKELY(cures != cudaSuccess)) STARPU_CUDA_REPORT_ERROR(cures);
  127. }
  128. break;
  129. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM,STARPU_CUDA_RAM):
  130. /* STARPU_CPU_RAM -> CUBLAS_RAM */
  131. /* only the proper CUBLAS thread can initiate this ! */
  132. #if !defined(HAVE_CUDA_MEMCPY_PEER)
  133. STARPU_ASSERT(_starpu_get_local_memory_node() == dst_node);
  134. #endif
  135. STARPU_ASSERT(copy_methods->ram_to_cuda);
  136. if (!req || !copy_methods->ram_to_cuda_async)
  137. {
  138. /* this is not associated to a request so it's synchronous */
  139. copy_methods->ram_to_cuda(src_interface, src_node, dst_interface, dst_node);
  140. }
  141. else
  142. {
  143. req->async_channel.type = STARPU_CUDA_RAM;
  144. cures = cudaEventCreate(&req->async_channel.event.cuda_event);
  145. if (STARPU_UNLIKELY(cures != cudaSuccess))
  146. STARPU_CUDA_REPORT_ERROR(cures);
  147. stream = starpu_cuda_get_local_transfer_stream();
  148. ret = copy_methods->ram_to_cuda_async(src_interface, src_node, dst_interface, dst_node, stream);
  149. cures = cudaEventRecord(req->async_channel.event.cuda_event, stream);
  150. if (STARPU_UNLIKELY(cures != cudaSuccess))
  151. STARPU_CUDA_REPORT_ERROR(cures);
  152. }
  153. break;
  154. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CUDA_RAM,STARPU_CUDA_RAM):
  155. /* CUDA - CUDA transfer */
  156. STARPU_ASSERT(copy_methods->cuda_to_cuda || copy_methods->cuda_to_cuda_async);
  157. if (!req || !copy_methods->cuda_to_cuda_async)
  158. {
  159. STARPU_ASSERT(copy_methods->cuda_to_cuda);
  160. /* this is not associated to a request so it's synchronous */
  161. copy_methods->cuda_to_cuda(src_interface, src_node, dst_interface, dst_node);
  162. }
  163. else
  164. {
  165. req->async_channel.type = STARPU_CUDA_RAM;
  166. cures = cudaEventCreate(&req->async_channel.event.cuda_event);
  167. if (STARPU_UNLIKELY(cures != cudaSuccess)) STARPU_CUDA_REPORT_ERROR(cures);
  168. stream = starpu_cuda_get_local_transfer_stream();
  169. ret = copy_methods->cuda_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)) STARPU_CUDA_REPORT_ERROR(cures);
  172. }
  173. break;
  174. #endif
  175. #ifdef STARPU_USE_OPENCL
  176. case _STARPU_MEMORY_NODE_TUPLE(STARPU_OPENCL_RAM,STARPU_CPU_RAM):
  177. /* OpenCL -> RAM */
  178. if (_starpu_get_local_memory_node() == src_node)
  179. {
  180. STARPU_ASSERT(copy_methods->opencl_to_ram);
  181. if (!req || !copy_methods->opencl_to_ram_async)
  182. {
  183. /* this is not associated to a request so it's synchronous */
  184. copy_methods->opencl_to_ram(src_interface, src_node, dst_interface, dst_node);
  185. }
  186. else
  187. {
  188. req->async_channel.type = STARPU_OPENCL_RAM;
  189. ret = copy_methods->opencl_to_ram_async(src_interface, src_node, dst_interface, dst_node, &(req->async_channel.event.opencl_event));
  190. }
  191. }
  192. else
  193. {
  194. /* we should not have a blocking call ! */
  195. STARPU_ABORT();
  196. }
  197. break;
  198. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM,STARPU_OPENCL_RAM):
  199. /* STARPU_CPU_RAM -> STARPU_OPENCL_RAM */
  200. STARPU_ASSERT(_starpu_get_local_memory_node() == dst_node);
  201. STARPU_ASSERT(copy_methods->ram_to_opencl);
  202. if (!req || !copy_methods->ram_to_opencl_async)
  203. {
  204. /* this is not associated to a request so it's synchronous */
  205. copy_methods->ram_to_opencl(src_interface, src_node, dst_interface, dst_node);
  206. }
  207. else
  208. {
  209. req->async_channel.type = STARPU_OPENCL_RAM;
  210. ret = copy_methods->ram_to_opencl_async(src_interface, src_node, dst_interface, dst_node, &(req->async_channel.event.opencl_event));
  211. }
  212. break;
  213. #endif
  214. default:
  215. STARPU_ABORT();
  216. break;
  217. }
  218. return ret;
  219. }
  220. int __attribute__((warn_unused_result)) _starpu_driver_copy_data_1_to_1(starpu_data_handle_t handle,
  221. struct _starpu_data_replicate *src_replicate,
  222. struct _starpu_data_replicate *dst_replicate,
  223. unsigned donotread,
  224. struct _starpu_data_request *req,
  225. unsigned may_alloc)
  226. {
  227. if (!donotread)
  228. {
  229. STARPU_ASSERT(src_replicate->allocated);
  230. STARPU_ASSERT(src_replicate->refcnt);
  231. }
  232. int ret_alloc, ret_copy;
  233. unsigned STARPU_ATTRIBUTE_UNUSED com_id = 0;
  234. unsigned src_node = src_replicate->memory_node;
  235. unsigned dst_node = dst_replicate->memory_node;
  236. /* first make sure the destination has an allocated buffer */
  237. if (!dst_replicate->allocated)
  238. {
  239. if (!may_alloc)
  240. return -ENOMEM;
  241. ret_alloc = _starpu_allocate_memory_on_node(handle, dst_replicate,req->prefetch);
  242. if (ret_alloc)
  243. return -ENOMEM;
  244. }
  245. STARPU_ASSERT(dst_replicate->allocated);
  246. STARPU_ASSERT(dst_replicate->refcnt);
  247. /* if there is no need to actually read the data,
  248. * we do not perform any transfer */
  249. if (!donotread)
  250. {
  251. size_t size = _starpu_data_get_size(handle);
  252. _starpu_bus_update_profiling_info((int)src_node, (int)dst_node, size);
  253. #ifdef STARPU_USE_FXT
  254. com_id = STARPU_ATOMIC_ADD(&communication_cnt, 1);
  255. if (req)
  256. req->com_id = com_id;
  257. #endif
  258. _STARPU_TRACE_START_DRIVER_COPY(src_node, dst_node, size, com_id);
  259. ret_copy = copy_data_1_to_1_generic(handle, src_replicate, dst_replicate, req);
  260. #ifdef STARPU_USE_FXT
  261. if (ret_copy != -EAGAIN)
  262. {
  263. _STARPU_TRACE_END_DRIVER_COPY(src_node, dst_node, size, com_id);
  264. }
  265. #endif
  266. return ret_copy;
  267. }
  268. return 0;
  269. }
  270. void _starpu_driver_wait_request_completion(struct _starpu_async_channel *async_channel)
  271. {
  272. enum starpu_node_kind kind = async_channel->type;
  273. #ifdef STARPU_USE_CUDA
  274. cudaEvent_t event;
  275. cudaError_t cures;
  276. #endif
  277. switch (kind)
  278. {
  279. #ifdef STARPU_USE_CUDA
  280. case STARPU_CUDA_RAM:
  281. event = (*async_channel).event.cuda_event;
  282. cures = cudaEventSynchronize(event);
  283. if (STARPU_UNLIKELY(cures))
  284. STARPU_CUDA_REPORT_ERROR(cures);
  285. cures = cudaEventDestroy(event);
  286. if (STARPU_UNLIKELY(cures))
  287. STARPU_CUDA_REPORT_ERROR(cures);
  288. break;
  289. #endif
  290. #ifdef STARPU_USE_OPENCL
  291. case STARPU_OPENCL_RAM:
  292. {
  293. if ((*async_channel).event.opencl_event == NULL) STARPU_ABORT();
  294. cl_int err = clWaitForEvents(1, &((*async_channel).event.opencl_event));
  295. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  296. clReleaseEvent((*async_channel).event.opencl_event);
  297. break;
  298. }
  299. #endif
  300. case STARPU_CPU_RAM:
  301. default:
  302. STARPU_ABORT();
  303. }
  304. }
  305. unsigned _starpu_driver_test_request_completion(struct _starpu_async_channel *async_channel)
  306. {
  307. enum starpu_node_kind kind = async_channel->type;
  308. unsigned success = 0;
  309. #ifdef STARPU_USE_CUDA
  310. cudaEvent_t event;
  311. #endif
  312. switch (kind)
  313. {
  314. #ifdef STARPU_USE_CUDA
  315. case STARPU_CUDA_RAM:
  316. event = (*async_channel).event.cuda_event;
  317. cudaError_t cures = cudaEventQuery(event);
  318. success = (cures == cudaSuccess);
  319. if (success)
  320. cudaEventDestroy(event);
  321. else if (cures != cudaErrorNotReady)
  322. STARPU_CUDA_REPORT_ERROR(cures);
  323. break;
  324. #endif
  325. #ifdef STARPU_USE_OPENCL
  326. case STARPU_OPENCL_RAM:
  327. {
  328. cl_int event_status;
  329. cl_event opencl_event = (*async_channel).event.opencl_event;
  330. if (opencl_event == NULL) STARPU_ABORT();
  331. cl_int err = clGetEventInfo(opencl_event, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof(event_status), &event_status, NULL);
  332. if (err != CL_SUCCESS)
  333. STARPU_OPENCL_REPORT_ERROR(err);
  334. if (event_status < 0)
  335. STARPU_OPENCL_REPORT_ERROR(event_status);
  336. success = (event_status == CL_COMPLETE);
  337. break;
  338. }
  339. #endif
  340. case STARPU_CPU_RAM:
  341. default:
  342. STARPU_ABORT();
  343. }
  344. return success;
  345. }