copy_driver.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu.h>
  17. #include <common/config.h>
  18. #include <common/utils.h>
  19. #include <core/sched_policy.h>
  20. #include <datawizard/datastats.h>
  21. #include <common/fxt.h>
  22. #include "copy_driver.h"
  23. #include "memalloc.h"
  24. #include <starpu_opencl.h>
  25. #include <starpu_cuda.h>
  26. #include <profiling/profiling.h>
  27. void _starpu_wake_all_blocked_workers_on_node(unsigned nodeid)
  28. {
  29. /* wake up all workers on that memory node */
  30. unsigned cond_id;
  31. starpu_mem_node_descr * const descr = _starpu_get_memory_node_description();
  32. PTHREAD_RWLOCK_RDLOCK(&descr->conditions_rwlock);
  33. unsigned nconds = descr->condition_count[nodeid];
  34. for (cond_id = 0; cond_id < nconds; cond_id++)
  35. {
  36. struct _cond_and_mutex *condition;
  37. condition = &descr->conditions_attached_to_node[nodeid][cond_id];
  38. /* wake anybody waiting on that condition */
  39. PTHREAD_MUTEX_LOCK(condition->mutex);
  40. PTHREAD_COND_BROADCAST(condition->cond);
  41. PTHREAD_MUTEX_UNLOCK(condition->mutex);
  42. }
  43. PTHREAD_RWLOCK_UNLOCK(&descr->conditions_rwlock);
  44. }
  45. void starpu_wake_all_blocked_workers(void)
  46. {
  47. /* workers may be blocked on the various queues' conditions */
  48. unsigned cond_id;
  49. starpu_mem_node_descr * const descr = _starpu_get_memory_node_description();
  50. PTHREAD_RWLOCK_RDLOCK(&descr->conditions_rwlock);
  51. unsigned nconds = descr->total_condition_count;
  52. for (cond_id = 0; cond_id < nconds; cond_id++)
  53. {
  54. struct _cond_and_mutex *condition;
  55. condition = &descr->conditions_all[cond_id];
  56. /* wake anybody waiting on that condition */
  57. PTHREAD_MUTEX_LOCK(condition->mutex);
  58. PTHREAD_COND_BROADCAST(condition->cond);
  59. PTHREAD_MUTEX_UNLOCK(condition->mutex);
  60. }
  61. PTHREAD_RWLOCK_UNLOCK(&descr->conditions_rwlock);
  62. }
  63. #ifdef STARPU_USE_FXT
  64. /* we need to identify each communication so that we can match the beginning
  65. * and the end of a communication in the trace, so we use a unique identifier
  66. * per communication */
  67. static unsigned communication_cnt = 0;
  68. #endif
  69. static int copy_data_1_to_1_generic(starpu_data_handle handle, uint32_t src_node, uint32_t dst_node, struct starpu_data_request_s *req __attribute__((unused)))
  70. {
  71. int ret = 0;
  72. const struct starpu_data_copy_methods *copy_methods = handle->ops->copy_methods;
  73. starpu_node_kind src_kind = _starpu_get_node_kind(src_node);
  74. starpu_node_kind dst_kind = _starpu_get_node_kind(dst_node);
  75. STARPU_ASSERT(handle->per_node[src_node].refcnt);
  76. STARPU_ASSERT(handle->per_node[dst_node].refcnt);
  77. STARPU_ASSERT(handle->per_node[src_node].allocated);
  78. STARPU_ASSERT(handle->per_node[dst_node].allocated);
  79. #ifdef STARPU_USE_CUDA
  80. cudaError_t cures;
  81. cudaStream_t *stream;
  82. #endif
  83. void *src_interface = starpu_data_get_interface_on_node(handle, src_node);
  84. void *dst_interface = starpu_data_get_interface_on_node(handle, dst_node);
  85. switch (_STARPU_MEMORY_NODE_TUPLE(src_kind,dst_kind)) {
  86. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM,STARPU_CPU_RAM):
  87. /* STARPU_CPU_RAM -> STARPU_CPU_RAM */
  88. STARPU_ASSERT(copy_methods->ram_to_ram);
  89. copy_methods->ram_to_ram(src_interface, src_node, dst_interface, dst_node);
  90. break;
  91. #ifdef STARPU_USE_CUDA
  92. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CUDA_RAM,STARPU_CPU_RAM):
  93. /* CUBLAS_RAM -> STARPU_CPU_RAM */
  94. /* only the proper CUBLAS thread can initiate this ! */
  95. if (_starpu_get_local_memory_node() == src_node) {
  96. /* only the proper CUBLAS thread can initiate this directly ! */
  97. STARPU_ASSERT(copy_methods->cuda_to_ram);
  98. if (!req || !copy_methods->cuda_to_ram_async) {
  99. /* this is not associated to a request so it's synchronous */
  100. copy_methods->cuda_to_ram(src_interface, src_node, dst_interface, dst_node);
  101. }
  102. else {
  103. cures = cudaEventCreate(&req->async_channel.cuda_event);
  104. if (STARPU_UNLIKELY(cures != cudaSuccess)) STARPU_CUDA_REPORT_ERROR(cures);
  105. stream = starpu_cuda_get_local_stream();
  106. ret = copy_methods->cuda_to_ram_async(src_interface, src_node, dst_interface, dst_node, stream);
  107. cures = cudaEventRecord(req->async_channel.cuda_event, *stream);
  108. if (STARPU_UNLIKELY(cures != cudaSuccess)) STARPU_CUDA_REPORT_ERROR(cures);
  109. }
  110. }
  111. else {
  112. /* we should not have a blocking call ! */
  113. STARPU_ABORT();
  114. }
  115. break;
  116. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM,STARPU_CUDA_RAM):
  117. /* STARPU_CPU_RAM -> CUBLAS_RAM */
  118. /* only the proper CUBLAS thread can initiate this ! */
  119. STARPU_ASSERT(_starpu_get_local_memory_node() == dst_node);
  120. STARPU_ASSERT(copy_methods->ram_to_cuda);
  121. if (!req || !copy_methods->ram_to_cuda_async) {
  122. /* this is not associated to a request so it's synchronous */
  123. copy_methods->ram_to_cuda(src_interface, src_node, dst_interface, dst_node);
  124. }
  125. else {
  126. cures = cudaEventCreate(&req->async_channel.cuda_event);
  127. if (STARPU_UNLIKELY(cures != cudaSuccess)) STARPU_CUDA_REPORT_ERROR(cures);
  128. stream = starpu_cuda_get_local_stream();
  129. ret = copy_methods->ram_to_cuda_async(src_interface, src_node, dst_interface, dst_node, stream);
  130. cures = cudaEventRecord(req->async_channel.cuda_event, *stream);
  131. if (STARPU_UNLIKELY(cures != cudaSuccess)) STARPU_CUDA_REPORT_ERROR(cures);
  132. }
  133. break;
  134. #endif
  135. #ifdef STARPU_USE_OPENCL
  136. case _STARPU_MEMORY_NODE_TUPLE(STARPU_OPENCL_RAM,STARPU_CPU_RAM):
  137. /* OpenCL -> RAM */
  138. if (_starpu_get_local_memory_node() == src_node) {
  139. STARPU_ASSERT(copy_methods->opencl_to_ram);
  140. if (!req || !copy_methods->opencl_to_ram_async) {
  141. /* this is not associated to a request so it's synchronous */
  142. copy_methods->opencl_to_ram(src_interface, src_node, dst_interface, dst_node);
  143. }
  144. else {
  145. ret = copy_methods->opencl_to_ram_async(src_interface, src_node, dst_interface, dst_node, &(req->async_channel.opencl_event));
  146. }
  147. }
  148. else {
  149. /* we should not have a blocking call ! */
  150. STARPU_ABORT();
  151. }
  152. break;
  153. case _STARPU_MEMORY_NODE_TUPLE(STARPU_CPU_RAM,STARPU_OPENCL_RAM):
  154. /* STARPU_CPU_RAM -> STARPU_OPENCL_RAM */
  155. STARPU_ASSERT(_starpu_get_local_memory_node() == dst_node);
  156. STARPU_ASSERT(copy_methods->ram_to_opencl);
  157. if (!req || !copy_methods->ram_to_opencl_async) {
  158. /* this is not associated to a request so it's synchronous */
  159. copy_methods->ram_to_opencl(src_interface, src_node, dst_interface, dst_node);
  160. }
  161. else {
  162. ret = copy_methods->ram_to_opencl_async(src_interface, src_node, dst_interface, dst_node, &(req->async_channel.opencl_event));
  163. }
  164. break;
  165. #endif
  166. default:
  167. STARPU_ABORT();
  168. break;
  169. }
  170. return ret;
  171. }
  172. int __attribute__((warn_unused_result)) _starpu_driver_copy_data_1_to_1(starpu_data_handle handle, uint32_t src_node,
  173. uint32_t dst_node, unsigned donotread, struct starpu_data_request_s *req, unsigned may_alloc)
  174. {
  175. if (!donotread)
  176. {
  177. STARPU_ASSERT(handle->per_node[src_node].allocated);
  178. STARPU_ASSERT(handle->per_node[src_node].refcnt);
  179. }
  180. int ret_alloc, ret_copy;
  181. unsigned __attribute__((unused)) com_id = 0;
  182. /* first make sure the destination has an allocated buffer */
  183. ret_alloc = _starpu_allocate_memory_on_node(handle, dst_node, may_alloc);
  184. if (ret_alloc)
  185. goto nomem;
  186. STARPU_ASSERT(handle->per_node[dst_node].allocated);
  187. STARPU_ASSERT(handle->per_node[dst_node].refcnt);
  188. /* if there is no need to actually read the data,
  189. * we do not perform any transfer */
  190. if (!donotread) {
  191. STARPU_ASSERT(handle->ops);
  192. //STARPU_ASSERT(handle->ops->copy_data_1_to_1);
  193. size_t size = _starpu_data_get_size(handle);
  194. _starpu_bus_update_profiling_info((int)src_node, (int)dst_node, size);
  195. #ifdef STARPU_USE_FXT
  196. com_id = STARPU_ATOMIC_ADD(&communication_cnt, 1);
  197. if (req)
  198. req->com_id = com_id;
  199. #endif
  200. /* for now we set the size to 0 in the FxT trace XXX */
  201. STARPU_TRACE_START_DRIVER_COPY(src_node, dst_node, 0, com_id);
  202. ret_copy = copy_data_1_to_1_generic(handle, src_node, dst_node, req);
  203. #ifdef STARPU_USE_FXT
  204. if (ret_copy != EAGAIN)
  205. {
  206. size_t size = _starpu_data_get_size(handle);
  207. STARPU_TRACE_END_DRIVER_COPY(src_node, dst_node, size, com_id);
  208. }
  209. #endif
  210. return ret_copy;
  211. }
  212. return 0;
  213. nomem:
  214. return ENOMEM;
  215. }
  216. void _starpu_driver_wait_request_completion(starpu_async_channel *async_channel __attribute__ ((unused)),
  217. unsigned handling_node)
  218. {
  219. starpu_node_kind kind = _starpu_get_node_kind(handling_node);
  220. #ifdef STARPU_USE_CUDA
  221. cudaEvent_t event;
  222. cudaError_t cures;
  223. #endif
  224. switch (kind) {
  225. #ifdef STARPU_USE_CUDA
  226. case STARPU_CUDA_RAM:
  227. event = (*async_channel).cuda_event;
  228. cures = cudaEventSynchronize(event);
  229. if (STARPU_UNLIKELY(cures))
  230. STARPU_CUDA_REPORT_ERROR(cures);
  231. cures = cudaEventDestroy(event);
  232. if (STARPU_UNLIKELY(cures))
  233. STARPU_CUDA_REPORT_ERROR(cures);
  234. break;
  235. #endif
  236. #ifdef STARPU_USE_OPENCL
  237. case STARPU_OPENCL_RAM:
  238. {
  239. cl_event opencl_event = (*async_channel).opencl_event;
  240. if (opencl_event == NULL) STARPU_ABORT();
  241. cl_int err = clWaitForEvents(1, &opencl_event);
  242. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  243. clReleaseEvent(opencl_event);
  244. }
  245. break;
  246. #endif
  247. case STARPU_CPU_RAM:
  248. default:
  249. STARPU_ABORT();
  250. }
  251. }
  252. unsigned _starpu_driver_test_request_completion(starpu_async_channel *async_channel __attribute__ ((unused)),
  253. unsigned handling_node)
  254. {
  255. starpu_node_kind kind = _starpu_get_node_kind(handling_node);
  256. unsigned success;
  257. #ifdef STARPU_USE_CUDA
  258. cudaEvent_t event;
  259. #endif
  260. switch (kind) {
  261. #ifdef STARPU_USE_CUDA
  262. case STARPU_CUDA_RAM:
  263. event = (*async_channel).cuda_event;
  264. success = (cudaEventQuery(event) == cudaSuccess);
  265. if (success)
  266. cudaEventDestroy(event);
  267. break;
  268. #endif
  269. #ifdef STARPU_USE_OPENCL
  270. case STARPU_OPENCL_RAM:
  271. {
  272. cl_int event_status;
  273. cl_event opencl_event = (*async_channel).opencl_event;
  274. if (opencl_event == NULL) STARPU_ABORT();
  275. cl_int err = clGetEventInfo(opencl_event, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof(event_status), &event_status, NULL);
  276. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  277. success = (event_status == CL_COMPLETE);
  278. break;
  279. }
  280. #endif
  281. case STARPU_CPU_RAM:
  282. default:
  283. STARPU_ABORT();
  284. success = 0;
  285. }
  286. return success;
  287. }