copy_driver.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (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 <pthread.h>
  17. #include <common/config.h>
  18. #include <common/utils.h>
  19. #include <core/policies/sched_policy.h>
  20. #include <datawizard/datastats.h>
  21. #include <common/fxt.h>
  22. #include "copy_driver.h"
  23. #include "memalloc.h"
  24. void _starpu_wake_all_blocked_workers_on_node(unsigned nodeid)
  25. {
  26. /* wake up all queues on that node */
  27. unsigned q_id;
  28. starpu_mem_node_descr * const descr = _starpu_get_memory_node_description();
  29. pthread_rwlock_rdlock(&descr->attached_queues_rwlock);
  30. unsigned nqueues = descr->queues_count[nodeid];
  31. for (q_id = 0; q_id < nqueues; q_id++)
  32. {
  33. struct starpu_jobq_s *q;
  34. q = descr->attached_queues_per_node[nodeid][q_id];
  35. /* wake anybody waiting on that queue */
  36. PTHREAD_MUTEX_LOCK(&q->activity_mutex);
  37. PTHREAD_COND_BROADCAST(&q->activity_cond);
  38. PTHREAD_MUTEX_UNLOCK(&q->activity_mutex);
  39. }
  40. pthread_rwlock_unlock(&descr->attached_queues_rwlock);
  41. }
  42. void starpu_wake_all_blocked_workers(void)
  43. {
  44. /* workers may be blocked on the policy's global condition */
  45. struct starpu_sched_policy_s *sched = _starpu_get_sched_policy();
  46. pthread_cond_t *sched_cond = &sched->sched_activity_cond;
  47. pthread_mutex_t *sched_mutex = &sched->sched_activity_mutex;
  48. PTHREAD_MUTEX_LOCK(sched_mutex);
  49. PTHREAD_COND_BROADCAST(sched_cond);
  50. PTHREAD_MUTEX_UNLOCK(sched_mutex);
  51. /* workers may be blocked on the various queues' conditions */
  52. unsigned node;
  53. unsigned nnodes = _starpu_get_memory_nodes_count();
  54. for (node = 0; node < nnodes; node++)
  55. {
  56. _starpu_wake_all_blocked_workers_on_node(node);
  57. }
  58. }
  59. #ifdef STARPU_USE_FXT
  60. /* we need to identify each communication so that we can match the beginning
  61. * and the end of a communication in the trace, so we use a unique identifier
  62. * per communication */
  63. static unsigned communication_cnt = 0;
  64. #endif
  65. 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)))
  66. {
  67. int ret = 0;
  68. //ret = handle->ops->copy_data_1_to_1(handle, src_node, dst_node);
  69. const struct starpu_copy_data_methods_s *copy_methods = handle->ops->copy_methods;
  70. starpu_node_kind src_kind = _starpu_get_node_kind(src_node);
  71. starpu_node_kind dst_kind = _starpu_get_node_kind(dst_node);
  72. STARPU_ASSERT(handle->per_node[src_node].refcnt);
  73. STARPU_ASSERT(handle->per_node[dst_node].refcnt);
  74. STARPU_ASSERT(handle->per_node[src_node].allocated);
  75. STARPU_ASSERT(handle->per_node[dst_node].allocated);
  76. #ifdef STARPU_USE_CUDA
  77. cudaError_t cures;
  78. cudaStream_t *stream;
  79. #endif
  80. switch (dst_kind) {
  81. case STARPU_RAM:
  82. switch (src_kind) {
  83. case STARPU_RAM:
  84. /* STARPU_RAM -> STARPU_RAM */
  85. STARPU_ASSERT(copy_methods->ram_to_ram);
  86. copy_methods->ram_to_ram(handle, src_node, dst_node);
  87. break;
  88. #ifdef STARPU_USE_CUDA
  89. case STARPU_CUDA_RAM:
  90. /* CUBLAS_RAM -> STARPU_RAM */
  91. /* only the proper CUBLAS thread can initiate this ! */
  92. if (_starpu_get_local_memory_node() == src_node)
  93. {
  94. /* only the proper CUBLAS thread can initiate this directly ! */
  95. STARPU_ASSERT(copy_methods->cuda_to_ram);
  96. if (!req || !copy_methods->cuda_to_ram_async)
  97. {
  98. /* this is not associated to a request so it's synchronous */
  99. copy_methods->cuda_to_ram(handle, src_node, dst_node);
  100. }
  101. else {
  102. cures = cudaEventCreate(&req->async_channel.cuda_event);
  103. if (STARPU_UNLIKELY(cures != cudaSuccess)) STARPU_CUDA_REPORT_ERROR(cures);
  104. stream = starpu_cuda_get_local_stream();
  105. ret = copy_methods->cuda_to_ram_async(handle, src_node, dst_node, stream);
  106. cures = cudaEventRecord(req->async_channel.cuda_event, *stream);
  107. if (STARPU_UNLIKELY(cures != cudaSuccess)) STARPU_CUDA_REPORT_ERROR(cures);
  108. }
  109. }
  110. else
  111. {
  112. /* we should not have a blocking call ! */
  113. STARPU_ABORT();
  114. }
  115. break;
  116. #endif
  117. #ifdef STARPU_USE_OPENCL
  118. case STARPU_OPENCL_RAM:
  119. /* OpenCL -> RAM */
  120. if (_starpu_get_local_memory_node() == src_node)
  121. {
  122. STARPU_ASSERT(copy_methods->opencl_to_ram);
  123. if (!req || !copy_methods->opencl_to_ram_async)
  124. {
  125. /* this is not associated to a request so it's synchronous */
  126. copy_methods->opencl_to_ram(handle, src_node, dst_node);
  127. }
  128. else {
  129. ret = copy_methods->opencl_to_ram_async(handle, src_node, dst_node, &(req->async_channel.opencl_event));
  130. }
  131. }
  132. else
  133. {
  134. /* we should not have a blocking call ! */
  135. STARPU_ABORT();
  136. }
  137. break;
  138. #endif
  139. case STARPU_SPU_LS:
  140. STARPU_ABORT(); // TODO
  141. break;
  142. case STARPU_UNUSED:
  143. printf("error node %u STARPU_UNUSED\n", src_node);
  144. default:
  145. assert(0);
  146. break;
  147. }
  148. break;
  149. #ifdef STARPU_USE_CUDA
  150. case STARPU_CUDA_RAM:
  151. switch (src_kind) {
  152. case STARPU_RAM:
  153. /* STARPU_RAM -> CUBLAS_RAM */
  154. /* only the proper CUBLAS thread can initiate this ! */
  155. STARPU_ASSERT(_starpu_get_local_memory_node() == dst_node);
  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(handle, src_node, dst_node);
  161. }
  162. else {
  163. cures = cudaEventCreate(&req->async_channel.cuda_event);
  164. if (STARPU_UNLIKELY(cures != cudaSuccess)) STARPU_CUDA_REPORT_ERROR(cures);
  165. stream = starpu_cuda_get_local_stream();
  166. ret = copy_methods->ram_to_cuda_async(handle, src_node, dst_node, stream);
  167. cures = cudaEventRecord(req->async_channel.cuda_event, *stream);
  168. if (STARPU_UNLIKELY(cures != cudaSuccess)) STARPU_CUDA_REPORT_ERROR(cures);
  169. }
  170. break;
  171. case STARPU_CUDA_RAM:
  172. case STARPU_SPU_LS:
  173. STARPU_ABORT(); // TODO
  174. break;
  175. case STARPU_UNUSED:
  176. default:
  177. STARPU_ABORT();
  178. break;
  179. }
  180. break;
  181. #endif
  182. #ifdef STARPU_USE_OPENCL
  183. case STARPU_OPENCL_RAM:
  184. switch (src_kind) {
  185. case STARPU_RAM:
  186. /* STARPU_RAM -> STARPU_OPENCL_RAM */
  187. STARPU_ASSERT(_starpu_get_local_memory_node() == dst_node);
  188. STARPU_ASSERT(copy_methods->ram_to_opencl);
  189. if (!req || !copy_methods->ram_to_opencl_async)
  190. {
  191. /* this is not associated to a request so it's synchronous */
  192. copy_methods->ram_to_opencl(handle, src_node, dst_node);
  193. }
  194. else {
  195. ret = copy_methods->ram_to_opencl_async(handle, src_node, dst_node, &(req->async_channel.opencl_event));
  196. }
  197. break;
  198. case STARPU_CUDA_RAM:
  199. case STARPU_OPENCL_RAM:
  200. case STARPU_SPU_LS:
  201. STARPU_ABORT(); // TODO
  202. break;
  203. case STARPU_UNUSED:
  204. default:
  205. STARPU_ABORT();
  206. break;
  207. }
  208. break;
  209. #endif
  210. case STARPU_SPU_LS:
  211. STARPU_ABORT(); // TODO
  212. break;
  213. case STARPU_UNUSED:
  214. default:
  215. assert(0);
  216. break;
  217. }
  218. return ret;
  219. }
  220. int __attribute__((warn_unused_result)) _starpu_driver_copy_data_1_to_1(starpu_data_handle handle, uint32_t src_node,
  221. uint32_t dst_node, unsigned donotread, struct starpu_data_request_s *req, unsigned may_alloc)
  222. {
  223. if (!donotread)
  224. {
  225. STARPU_ASSERT(handle->per_node[src_node].allocated);
  226. STARPU_ASSERT(handle->per_node[src_node].refcnt);
  227. }
  228. int ret_alloc, ret_copy;
  229. unsigned __attribute__((unused)) com_id = 0;
  230. /* first make sure the destination has an allocated buffer */
  231. ret_alloc = _starpu_allocate_memory_on_node(handle, dst_node, may_alloc);
  232. if (ret_alloc)
  233. goto nomem;
  234. STARPU_ASSERT(handle->per_node[dst_node].allocated);
  235. STARPU_ASSERT(handle->per_node[dst_node].refcnt);
  236. /* if there is no need to actually read the data,
  237. * we do not perform any transfer */
  238. if (!donotread) {
  239. STARPU_ASSERT(handle->ops);
  240. //STARPU_ASSERT(handle->ops->copy_data_1_to_1);
  241. #ifdef STARPU_DATA_STATS
  242. size_t size = handle->ops->get_size(handle);
  243. _starpu_update_comm_amount(src_node, dst_node, size);
  244. #endif
  245. #ifdef STARPU_USE_FXT
  246. com_id = STARPU_ATOMIC_ADD(&communication_cnt, 1);
  247. if (req)
  248. req->com_id = com_id;
  249. #endif
  250. /* for now we set the size to 0 in the FxT trace XXX */
  251. STARPU_TRACE_START_DRIVER_COPY(src_node, dst_node, 0, com_id);
  252. ret_copy = copy_data_1_to_1_generic(handle, src_node, dst_node, req);
  253. #ifdef STARPU_USE_FXT
  254. if (ret_copy != EAGAIN)
  255. {
  256. size_t size = handle->ops->get_size(handle);
  257. STARPU_TRACE_END_DRIVER_COPY(src_node, dst_node, size, com_id);
  258. }
  259. #endif
  260. return ret_copy;
  261. }
  262. return 0;
  263. nomem:
  264. return ENOMEM;
  265. }
  266. void _starpu_driver_wait_request_completion(starpu_async_channel *async_channel __attribute__ ((unused)),
  267. unsigned handling_node)
  268. {
  269. starpu_node_kind kind = _starpu_get_node_kind(handling_node);
  270. #ifdef STARPU_USE_CUDA
  271. cudaEvent_t event;
  272. cudaError_t cures;
  273. #endif
  274. switch (kind) {
  275. #ifdef STARPU_USE_CUDA
  276. case STARPU_CUDA_RAM:
  277. event = (*async_channel).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. fprintf(stderr, "not implemented yet\n");
  289. STARPU_ABORT();
  290. break;
  291. #endif
  292. case STARPU_RAM:
  293. default:
  294. STARPU_ABORT();
  295. }
  296. }
  297. unsigned _starpu_driver_test_request_completion(starpu_async_channel *async_channel __attribute__ ((unused)),
  298. unsigned handling_node)
  299. {
  300. starpu_node_kind kind = _starpu_get_node_kind(handling_node);
  301. unsigned success;
  302. #ifdef STARPU_USE_CUDA
  303. cudaEvent_t event;
  304. #endif
  305. switch (kind) {
  306. #ifdef STARPU_USE_CUDA
  307. case STARPU_CUDA_RAM:
  308. event = (*async_channel).cuda_event;
  309. success = (cudaEventQuery(event) == cudaSuccess);
  310. if (success)
  311. cudaEventDestroy(event);
  312. break;
  313. #endif
  314. #ifdef STARPU_USE_OPENCL
  315. case STARPU_OPENCL_RAM:
  316. {
  317. cl_int event_status;
  318. cl_event opencl_event = (*async_channel).opencl_event;
  319. if (opencl_event == NULL) STARPU_ABORT();
  320. clGetEventInfo(opencl_event, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof(event_status), &event_status, NULL);
  321. success = (event_status == CL_COMPLETE);
  322. break;
  323. }
  324. #endif
  325. case STARPU_RAM:
  326. default:
  327. STARPU_ABORT();
  328. success = 0;
  329. }
  330. return success;
  331. }