copy_driver.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 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. starpu_mem_node_descr * const descr = _starpu_get_memory_node_description();
  33. 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 _cond_and_mutex *condition;
  38. condition = &descr->conditions_attached_to_node[nodeid][cond_id];
  39. /* wake anybody waiting on that condition */
  40. PTHREAD_MUTEX_LOCK(condition->mutex);
  41. PTHREAD_COND_BROADCAST(condition->cond);
  42. PTHREAD_MUTEX_UNLOCK(condition->mutex);
  43. }
  44. 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. starpu_mem_node_descr * const descr = _starpu_get_memory_node_description();
  51. 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 _cond_and_mutex *condition;
  56. condition = &descr->conditions_all[cond_id];
  57. /* wake anybody waiting on that condition */
  58. PTHREAD_MUTEX_LOCK(condition->mutex);
  59. PTHREAD_COND_BROADCAST(condition->cond);
  60. PTHREAD_MUTEX_UNLOCK(condition->mutex);
  61. }
  62. 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 handle, struct starpu_data_replicate_s *src_replicate, struct starpu_data_replicate_s *dst_replicate, struct starpu_data_request_s *req STARPU_ATTRIBUTE_UNUSED)
  71. {
  72. int ret = 0;
  73. const struct starpu_data_copy_methods *copy_methods = handle->ops->copy_methods;
  74. unsigned src_node = src_replicate->memory_node;
  75. unsigned dst_node = dst_replicate->memory_node;
  76. starpu_node_kind src_kind = _starpu_get_node_kind(src_node);
  77. starpu_node_kind dst_kind = _starpu_get_node_kind(dst_node);
  78. STARPU_ASSERT(src_replicate->refcnt);
  79. STARPU_ASSERT(dst_replicate->refcnt);
  80. STARPU_ASSERT(src_replicate->allocated);
  81. STARPU_ASSERT(dst_replicate->allocated);
  82. #ifdef STARPU_USE_CUDA
  83. cudaError_t cures;
  84. cudaStream_t stream;
  85. #endif
  86. void *src_interface = src_replicate->data_interface;
  87. void *dst_interface = dst_replicate->data_interface;
  88. #if defined(STARPU_USE_CUDA) && defined(HAVE_CUDA_MEMCPY_PEER)
  89. if ((src_kind == STARPU_CUDA_RAM) || (dst_kind == STARPU_CUDA_RAM))
  90. {
  91. int node = (dst_kind == STARPU_CUDA_RAM)?dst_node:src_node;
  92. cures = cudaSetDevice(starpu_memory_node_to_devid(node));
  93. STARPU_ASSERT(cures == cudaSuccess);
  94. }
  95. #endif
  96. switch (_STARPU_MEMORY_NODE_TUPLE(src_kind,dst_kind)) {
  97. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM,STARPU_CPU_RAM):
  98. /* STARPU_CPU_RAM -> STARPU_CPU_RAM */
  99. STARPU_ASSERT(copy_methods->ram_to_ram);
  100. copy_methods->ram_to_ram(src_interface, src_node, dst_interface, dst_node);
  101. break;
  102. #ifdef STARPU_USE_CUDA
  103. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CUDA_RAM,STARPU_CPU_RAM):
  104. /* only the proper CUBLAS thread can initiate this directly ! */
  105. STARPU_ASSERT(copy_methods->cuda_to_ram);
  106. if (!req || !copy_methods->cuda_to_ram_async) {
  107. /* this is not associated to a request so it's synchronous */
  108. copy_methods->cuda_to_ram(src_interface, src_node, dst_interface, dst_node);
  109. }
  110. else {
  111. req->async_channel.type = STARPU_CUDA_RAM;
  112. cures = cudaEventCreate(&req->async_channel.event.cuda_event);
  113. if (STARPU_UNLIKELY(cures != cudaSuccess)) STARPU_CUDA_REPORT_ERROR(cures);
  114. stream = starpu_cuda_get_local_transfer_stream();
  115. ret = copy_methods->cuda_to_ram_async(src_interface, src_node, dst_interface, dst_node, stream);
  116. cures = cudaEventRecord(req->async_channel.event.cuda_event, stream);
  117. if (STARPU_UNLIKELY(cures != cudaSuccess)) STARPU_CUDA_REPORT_ERROR(cures);
  118. }
  119. break;
  120. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM,STARPU_CUDA_RAM):
  121. /* STARPU_CPU_RAM -> CUBLAS_RAM */
  122. /* only the proper CUBLAS thread can initiate this ! */
  123. STARPU_ASSERT(_starpu_get_local_memory_node() == dst_node);
  124. STARPU_ASSERT(copy_methods->ram_to_cuda);
  125. if (!req || !copy_methods->ram_to_cuda_async) {
  126. /* this is not associated to a request so it's synchronous */
  127. copy_methods->ram_to_cuda(src_interface, src_node, dst_interface, dst_node);
  128. }
  129. else {
  130. req->async_channel.type = STARPU_CUDA_RAM;
  131. cures = cudaEventCreate(&req->async_channel.event.cuda_event);
  132. if (STARPU_UNLIKELY(cures != cudaSuccess))
  133. STARPU_CUDA_REPORT_ERROR(cures);
  134. stream = starpu_cuda_get_local_stream();
  135. ret = copy_methods->ram_to_cuda_async(src_interface, src_node, dst_interface, dst_node, stream);
  136. cures = cudaEventRecord(req->async_channel.event.cuda_event, stream);
  137. if (STARPU_UNLIKELY(cures != cudaSuccess))
  138. STARPU_CUDA_REPORT_ERROR(cures);
  139. }
  140. break;
  141. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CUDA_RAM,STARPU_CUDA_RAM):
  142. /* CUDA - CUDA transfer */
  143. STARPU_ASSERT(copy_methods->cuda_to_cuda || copy_methods->cuda_to_cuda_async);
  144. if (!req || !copy_methods->cuda_to_cuda_async) {
  145. /* this is not associated to a request so it's synchronous */
  146. copy_methods->cuda_to_cuda(src_interface, src_node, dst_interface, dst_node);
  147. }
  148. else {
  149. req->async_channel.type = STARPU_CUDA_RAM;
  150. cures = cudaEventCreate(&req->async_channel.event.cuda_event);
  151. if (STARPU_UNLIKELY(cures != cudaSuccess)) STARPU_CUDA_REPORT_ERROR(cures);
  152. stream = starpu_cuda_get_local_stream();
  153. ret = copy_methods->cuda_to_cuda_async(src_interface, src_node, dst_interface, dst_node, stream);
  154. cures = cudaEventRecord(req->async_channel.event.cuda_event, stream);
  155. if (STARPU_UNLIKELY(cures != cudaSuccess)) STARPU_CUDA_REPORT_ERROR(cures);
  156. }
  157. break;
  158. #endif
  159. #ifdef STARPU_USE_OPENCL
  160. case _STARPU_MEMORY_NODE_TUPLE(STARPU_OPENCL_RAM,STARPU_CPU_RAM):
  161. /* OpenCL -> RAM */
  162. if (_starpu_get_local_memory_node() == src_node) {
  163. STARPU_ASSERT(copy_methods->opencl_to_ram);
  164. if (!req || !copy_methods->opencl_to_ram_async) {
  165. /* this is not associated to a request so it's synchronous */
  166. copy_methods->opencl_to_ram(src_interface, src_node, dst_interface, dst_node);
  167. }
  168. else {
  169. req->async_channel.type = STARPU_OPENCL_RAM;
  170. ret = copy_methods->opencl_to_ram_async(src_interface, src_node, dst_interface, dst_node, &(req->async_channel.event.opencl_event));
  171. }
  172. }
  173. else {
  174. /* we should not have a blocking call ! */
  175. STARPU_ABORT();
  176. }
  177. break;
  178. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM,STARPU_OPENCL_RAM):
  179. /* STARPU_CPU_RAM -> STARPU_OPENCL_RAM */
  180. STARPU_ASSERT(_starpu_get_local_memory_node() == dst_node);
  181. STARPU_ASSERT(copy_methods->ram_to_opencl);
  182. if (!req || !copy_methods->ram_to_opencl_async) {
  183. /* this is not associated to a request so it's synchronous */
  184. copy_methods->ram_to_opencl(src_interface, src_node, dst_interface, dst_node);
  185. }
  186. else {
  187. req->async_channel.type = STARPU_OPENCL_RAM;
  188. ret = copy_methods->ram_to_opencl_async(src_interface, src_node, dst_interface, dst_node, &(req->async_channel.event.opencl_event));
  189. }
  190. break;
  191. #endif
  192. default:
  193. STARPU_ABORT();
  194. break;
  195. }
  196. return ret;
  197. }
  198. int __attribute__((warn_unused_result)) _starpu_driver_copy_data_1_to_1(starpu_data_handle handle,
  199. struct starpu_data_replicate_s *src_replicate,
  200. struct starpu_data_replicate_s *dst_replicate,
  201. unsigned donotread,
  202. struct starpu_data_request_s *req,
  203. unsigned may_alloc)
  204. {
  205. if (!donotread)
  206. {
  207. STARPU_ASSERT(src_replicate->allocated);
  208. STARPU_ASSERT(src_replicate->refcnt);
  209. }
  210. int ret_alloc, ret_copy;
  211. unsigned STARPU_ATTRIBUTE_UNUSED com_id = 0;
  212. unsigned src_node = src_replicate->memory_node;
  213. unsigned dst_node = dst_replicate->memory_node;
  214. /* first make sure the destination has an allocated buffer */
  215. if (!dst_replicate->allocated)
  216. {
  217. if (!may_alloc)
  218. return -ENOMEM;
  219. ret_alloc = _starpu_allocate_memory_on_node(handle, dst_replicate);
  220. if (ret_alloc)
  221. return -ENOMEM;
  222. }
  223. STARPU_ASSERT(dst_replicate->allocated);
  224. STARPU_ASSERT(dst_replicate->refcnt);
  225. /* if there is no need to actually read the data,
  226. * we do not perform any transfer */
  227. if (!donotread) {
  228. size_t size = _starpu_data_get_size(handle);
  229. _starpu_bus_update_profiling_info((int)src_node, (int)dst_node, size);
  230. #ifdef STARPU_USE_FXT
  231. com_id = STARPU_ATOMIC_ADD(&communication_cnt, 1);
  232. if (req)
  233. req->com_id = com_id;
  234. #endif
  235. STARPU_TRACE_START_DRIVER_COPY(src_node, dst_node, size, com_id);
  236. ret_copy = copy_data_1_to_1_generic(handle, src_replicate, dst_replicate, req);
  237. #ifdef STARPU_USE_FXT
  238. if (ret_copy != -EAGAIN)
  239. {
  240. STARPU_TRACE_END_DRIVER_COPY(src_node, dst_node, size, com_id);
  241. }
  242. #endif
  243. return ret_copy;
  244. }
  245. return 0;
  246. }
  247. void _starpu_driver_wait_request_completion(struct starpu_async_channel *async_channel)
  248. {
  249. starpu_node_kind kind = async_channel->type;
  250. #ifdef STARPU_USE_CUDA
  251. cudaEvent_t event;
  252. cudaError_t cures;
  253. #endif
  254. switch (kind) {
  255. #ifdef STARPU_USE_CUDA
  256. case STARPU_CUDA_RAM:
  257. event = (*async_channel).event.cuda_event;
  258. cures = cudaEventSynchronize(event);
  259. if (STARPU_UNLIKELY(cures))
  260. STARPU_CUDA_REPORT_ERROR(cures);
  261. cures = cudaEventDestroy(event);
  262. if (STARPU_UNLIKELY(cures))
  263. STARPU_CUDA_REPORT_ERROR(cures);
  264. break;
  265. #endif
  266. #ifdef STARPU_USE_OPENCL
  267. case STARPU_OPENCL_RAM:
  268. {
  269. if ((*async_channel).event.opencl_event == NULL) STARPU_ABORT();
  270. cl_int err = clWaitForEvents(1, &((*async_channel).event.opencl_event));
  271. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  272. clReleaseEvent((*async_channel).event.opencl_event);
  273. }
  274. break;
  275. #endif
  276. case STARPU_CPU_RAM:
  277. default:
  278. STARPU_ABORT();
  279. }
  280. }
  281. unsigned _starpu_driver_test_request_completion(struct starpu_async_channel *async_channel)
  282. {
  283. starpu_node_kind kind = async_channel->type;
  284. unsigned success;
  285. #ifdef STARPU_USE_CUDA
  286. cudaEvent_t event;
  287. #endif
  288. switch (kind) {
  289. #ifdef STARPU_USE_CUDA
  290. case STARPU_CUDA_RAM:
  291. event = (*async_channel).event.cuda_event;
  292. success = (cudaEventQuery(event) == cudaSuccess);
  293. if (success)
  294. cudaEventDestroy(event);
  295. break;
  296. #endif
  297. #ifdef STARPU_USE_OPENCL
  298. case STARPU_OPENCL_RAM:
  299. {
  300. cl_int event_status;
  301. cl_event opencl_event = (*async_channel).event.opencl_event;
  302. if (opencl_event == NULL) STARPU_ABORT();
  303. cl_int err = clGetEventInfo(opencl_event, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof(event_status), &event_status, NULL);
  304. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  305. success = (event_status == CL_COMPLETE);
  306. break;
  307. }
  308. #endif
  309. case STARPU_CPU_RAM:
  310. default:
  311. STARPU_ABORT();
  312. success = 0;
  313. }
  314. return success;
  315. }