data_request.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010 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 <datawizard/datawizard.h>
  20. /* requests that have not been treated at all */
  21. static starpu_data_request_list_t data_requests[STARPU_MAXNODES];
  22. static pthread_cond_t data_requests_list_cond[STARPU_MAXNODES];
  23. static pthread_mutex_t data_requests_list_mutex[STARPU_MAXNODES];
  24. /* requests that are not terminated (eg. async transfers) */
  25. static starpu_data_request_list_t data_requests_pending[STARPU_MAXNODES];
  26. static pthread_cond_t data_requests_pending_list_cond[STARPU_MAXNODES];
  27. static pthread_mutex_t data_requests_pending_list_mutex[STARPU_MAXNODES];
  28. void _starpu_init_data_request_lists(void)
  29. {
  30. unsigned i;
  31. for (i = 0; i < STARPU_MAXNODES; i++)
  32. {
  33. data_requests[i] = starpu_data_request_list_new();
  34. PTHREAD_MUTEX_INIT(&data_requests_list_mutex[i], NULL);
  35. PTHREAD_COND_INIT(&data_requests_list_cond[i], NULL);
  36. data_requests_pending[i] = starpu_data_request_list_new();
  37. PTHREAD_MUTEX_INIT(&data_requests_pending_list_mutex[i], NULL);
  38. PTHREAD_COND_INIT(&data_requests_pending_list_cond[i], NULL);
  39. }
  40. }
  41. void _starpu_deinit_data_request_lists(void)
  42. {
  43. unsigned i;
  44. for (i = 0; i < STARPU_MAXNODES; i++)
  45. {
  46. PTHREAD_COND_DESTROY(&data_requests_pending_list_cond[i]);
  47. PTHREAD_MUTEX_DESTROY(&data_requests_pending_list_mutex[i]);
  48. starpu_data_request_list_delete(data_requests_pending[i]);
  49. PTHREAD_COND_DESTROY(&data_requests_list_cond[i]);
  50. PTHREAD_MUTEX_DESTROY(&data_requests_list_mutex[i]);
  51. starpu_data_request_list_delete(data_requests[i]);
  52. }
  53. }
  54. /* this should be called with the lock r->handle->header_lock taken */
  55. static void starpu_data_request_destroy(starpu_data_request_t r)
  56. {
  57. unsigned node;
  58. /* If this is a write only request, then there is no source and we use
  59. * the destination node to cache the request. Otherwise we store the
  60. * pending requests between src and dst. */
  61. if (r->mode & STARPU_R)
  62. {
  63. node = r->src_replicate->memory_node;
  64. }
  65. else {
  66. node = r->dst_replicate->memory_node;
  67. }
  68. STARPU_ASSERT(r->dst_replicate->request[node] == r);
  69. r->dst_replicate->request[node] = NULL;
  70. //fprintf(stderr, "DESTROY REQ %p (%d) refcnt %d\n", r, node, r->refcnt);
  71. starpu_data_request_delete(r);
  72. }
  73. /* handle->lock should already be taken ! */
  74. starpu_data_request_t _starpu_create_data_request(starpu_data_handle handle,
  75. struct starpu_data_replicate_s *src_replicate,
  76. struct starpu_data_replicate_s *dst_replicate,
  77. uint32_t handling_node,
  78. starpu_access_mode mode,
  79. unsigned ndeps)
  80. {
  81. starpu_data_request_t r = starpu_data_request_new();
  82. _starpu_spin_init(&r->lock);
  83. r->handle = handle;
  84. r->src_replicate = src_replicate;
  85. r->dst_replicate = dst_replicate;
  86. r->mode = mode;
  87. r->handling_node = handling_node;
  88. r->completed = 0;
  89. r->retval = -1;
  90. r->ndeps = ndeps;
  91. r->next_req_count = 0;
  92. r->callbacks = NULL;
  93. _starpu_spin_lock(&r->lock);
  94. dst_replicate->refcnt++;
  95. if (mode & STARPU_R)
  96. {
  97. unsigned src_node = src_replicate->memory_node;
  98. dst_replicate->request[src_node] = r;
  99. src_replicate->refcnt++;
  100. }
  101. else {
  102. unsigned dst_node = dst_replicate->memory_node;
  103. dst_replicate->request[dst_node] = r;
  104. }
  105. r->refcnt = 1;
  106. _starpu_spin_unlock(&r->lock);
  107. return r;
  108. }
  109. int _starpu_wait_data_request_completion(starpu_data_request_t r, unsigned may_alloc)
  110. {
  111. int retval;
  112. int do_delete = 0;
  113. uint32_t local_node = _starpu_get_local_memory_node();
  114. do {
  115. _starpu_spin_lock(&r->lock);
  116. if (r->completed)
  117. break;
  118. _starpu_spin_unlock(&r->lock);
  119. #ifndef STARPU_NON_BLOCKING_DRIVERS
  120. _starpu_wake_all_blocked_workers_on_node(r->handling_node);
  121. #endif
  122. _starpu_datawizard_progress(local_node, may_alloc);
  123. } while (1);
  124. retval = r->retval;
  125. if (retval)
  126. _STARPU_DISP("REQUEST %p COMPLETED (retval %d) !\n", r, r->retval);
  127. r->refcnt--;
  128. /* if nobody is waiting on that request, we can get rid of it */
  129. if (r->refcnt == 0)
  130. do_delete = 1;
  131. _starpu_spin_unlock(&r->lock);
  132. if (do_delete)
  133. starpu_data_request_destroy(r);
  134. return retval;
  135. }
  136. /* this is non blocking */
  137. void _starpu_post_data_request(starpu_data_request_t r, uint32_t handling_node)
  138. {
  139. // _STARPU_DEBUG("POST REQUEST\n");
  140. /* If some dependencies are not fulfilled yet, we don't actually post the request */
  141. if (r->ndeps > 0)
  142. return;
  143. if (r->mode & STARPU_R)
  144. {
  145. STARPU_ASSERT(r->src_replicate->allocated);
  146. STARPU_ASSERT(r->src_replicate->refcnt);
  147. }
  148. /* insert the request in the proper list */
  149. PTHREAD_MUTEX_LOCK(&data_requests_list_mutex[handling_node]);
  150. starpu_data_request_list_push_front(data_requests[handling_node], r);
  151. PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[handling_node]);
  152. #ifndef STARPU_NON_BLOCKING_DRIVERS
  153. _starpu_wake_all_blocked_workers_on_node(handling_node);
  154. #endif
  155. }
  156. /* We assume that r->lock is taken by the caller */
  157. void _starpu_data_request_append_callback(starpu_data_request_t r, void (*callback_func)(void *), void *callback_arg)
  158. {
  159. STARPU_ASSERT(r);
  160. if (callback_func)
  161. {
  162. struct callback_list *link = (struct callback_list *) malloc(sizeof(struct callback_list));
  163. STARPU_ASSERT(link);
  164. link->callback_func = callback_func;
  165. link->callback_arg = callback_arg;
  166. link->next = r->callbacks;
  167. r->callbacks = link;
  168. }
  169. }
  170. /* This method is called with handle's header_lock taken */
  171. static void starpu_handle_data_request_completion(starpu_data_request_t r)
  172. {
  173. unsigned do_delete = 0;
  174. starpu_data_handle handle = r->handle;
  175. starpu_access_mode mode = r->mode;
  176. struct starpu_data_replicate_s *src_replicate = r->src_replicate;
  177. struct starpu_data_replicate_s *dst_replicate = r->dst_replicate;
  178. _starpu_update_data_state(handle, r->dst_replicate, mode);
  179. #ifdef STARPU_USE_FXT
  180. uint32_t src_node = src_replicate->memory_node;
  181. uint32_t dst_node = dst_replicate->memory_node;
  182. size_t size = _starpu_data_get_size(handle);
  183. STARPU_TRACE_END_DRIVER_COPY(src_node, dst_node, size, r->com_id);
  184. #endif
  185. /* Once the request has been fulfilled, we may submit the requests that
  186. * were chained to that request. */
  187. unsigned chained_req;
  188. for (chained_req = 0; chained_req < r->next_req_count; chained_req++)
  189. {
  190. struct starpu_data_request_s *next_req = r->next_req[chained_req];
  191. STARPU_ASSERT(next_req->ndeps > 0);
  192. next_req->ndeps--;
  193. _starpu_post_data_request(next_req, next_req->handling_node);
  194. }
  195. r->completed = 1;
  196. /* Remove a reference on the destination replicate */
  197. STARPU_ASSERT(dst_replicate->refcnt > 0);
  198. dst_replicate->refcnt--;
  199. /* In case the source was "locked" by the request too */
  200. if (mode & STARPU_R)
  201. {
  202. STARPU_ASSERT(src_replicate->refcnt > 0);
  203. src_replicate->refcnt--;
  204. }
  205. r->refcnt--;
  206. /* if nobody is waiting on that request, we can get rid of it */
  207. if (r->refcnt == 0)
  208. do_delete = 1;
  209. r->retval = 0;
  210. /* In case there are one or multiple callbacks, we execute them now. */
  211. struct callback_list *callbacks = r->callbacks;
  212. _starpu_spin_unlock(&r->lock);
  213. if (do_delete)
  214. starpu_data_request_destroy(r);
  215. _starpu_spin_unlock(&handle->header_lock);
  216. /* We do the callback once the lock is released so that they can do
  217. * blocking operations with the handle (eg. release it) */
  218. while (callbacks)
  219. {
  220. callbacks->callback_func(callbacks->callback_arg);
  221. struct callback_list *next = callbacks->next;
  222. free(callbacks);
  223. callbacks = next;
  224. }
  225. }
  226. /* TODO : accounting to see how much time was spent working for other people ... */
  227. static int starpu_handle_data_request(starpu_data_request_t r, unsigned may_alloc)
  228. {
  229. starpu_data_handle handle = r->handle;
  230. _starpu_spin_lock(&handle->header_lock);
  231. _starpu_spin_lock(&r->lock);
  232. struct starpu_data_replicate_s *src_replicate = r->src_replicate;
  233. struct starpu_data_replicate_s *dst_replicate = r->dst_replicate;
  234. starpu_access_mode r_mode = r->mode;
  235. STARPU_ASSERT(!(r_mode & STARPU_R) || src_replicate);
  236. STARPU_ASSERT(!(r_mode & STARPU_R) || src_replicate->allocated);
  237. STARPU_ASSERT(!(r_mode & STARPU_R) || src_replicate->refcnt);
  238. _starpu_spin_unlock(&r->lock);
  239. /* perform the transfer */
  240. /* the header of the data must be locked by the worker that submitted the request */
  241. r->retval = _starpu_driver_copy_data_1_to_1(handle, src_replicate,
  242. dst_replicate, !(r_mode & STARPU_R), r, may_alloc);
  243. if (r->retval == -ENOMEM)
  244. {
  245. /* If there was not enough memory, we will try to redo the
  246. * request later. */
  247. _starpu_spin_unlock(&handle->header_lock);
  248. return -ENOMEM;
  249. }
  250. if (r->retval == -EAGAIN)
  251. {
  252. /* The request was successful, but could not be terminted
  253. * immediatly. We will handle the completion of the request
  254. * asynchronously. The request is put in the list of "pending"
  255. * requests in the meantime. */
  256. _starpu_spin_unlock(&handle->header_lock);
  257. PTHREAD_MUTEX_LOCK(&data_requests_pending_list_mutex[r->handling_node]);
  258. starpu_data_request_list_push_front(data_requests_pending[r->handling_node], r);
  259. PTHREAD_MUTEX_UNLOCK(&data_requests_pending_list_mutex[r->handling_node]);
  260. return -EAGAIN;
  261. }
  262. /* the request has been handled */
  263. _starpu_spin_lock(&r->lock);
  264. starpu_handle_data_request_completion(r);
  265. return 0;
  266. }
  267. void _starpu_handle_node_data_requests(uint32_t src_node, unsigned may_alloc)
  268. {
  269. starpu_data_request_t r;
  270. /* take all the entries from the request list */
  271. PTHREAD_MUTEX_LOCK(&data_requests_list_mutex[src_node]);
  272. starpu_data_request_list_t local_list = data_requests[src_node];
  273. if (starpu_data_request_list_empty(local_list))
  274. {
  275. /* there is no request */
  276. PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[src_node]);
  277. return;
  278. }
  279. /* There is an entry: we create a new empty list to replace the list of
  280. * requests, and we handle the request(s) one by one in the former
  281. * list, without concurrency issues.*/
  282. data_requests[src_node] = starpu_data_request_list_new();
  283. PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[src_node]);
  284. /* for all entries of the list */
  285. while (!starpu_data_request_list_empty(local_list))
  286. {
  287. int res;
  288. r = starpu_data_request_list_pop_back(local_list);
  289. res = starpu_handle_data_request(r, may_alloc);
  290. if (res == -ENOMEM)
  291. {
  292. PTHREAD_MUTEX_LOCK(&data_requests_list_mutex[src_node]);
  293. starpu_data_request_list_push_front(data_requests[src_node], r);
  294. PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[src_node]);
  295. }
  296. /* wake the requesting worker up */
  297. // if we do not progress ..
  298. // pthread_cond_broadcast(&data_requests_list_cond[src_node]);
  299. }
  300. starpu_data_request_list_delete(local_list);
  301. }
  302. static void _handle_pending_node_data_requests(uint32_t src_node, unsigned force)
  303. {
  304. // _STARPU_DEBUG("_starpu_handle_pending_node_data_requests ...\n");
  305. PTHREAD_MUTEX_LOCK(&data_requests_pending_list_mutex[src_node]);
  306. /* for all entries of the list */
  307. starpu_data_request_list_t local_list = data_requests_pending[src_node];
  308. data_requests_pending[src_node] = starpu_data_request_list_new();
  309. PTHREAD_MUTEX_UNLOCK(&data_requests_pending_list_mutex[src_node]);
  310. while (!starpu_data_request_list_empty(local_list))
  311. {
  312. starpu_data_request_t r;
  313. r = starpu_data_request_list_pop_back(local_list);
  314. starpu_data_handle handle = r->handle;
  315. _starpu_spin_lock(&handle->header_lock);
  316. _starpu_spin_lock(&r->lock);
  317. /* wait until the transfer is terminated */
  318. if (force)
  319. {
  320. _starpu_driver_wait_request_completion(&r->async_channel);
  321. starpu_handle_data_request_completion(r);
  322. }
  323. else {
  324. if (_starpu_driver_test_request_completion(&r->async_channel))
  325. {
  326. /* The request was completed */
  327. starpu_handle_data_request_completion(r);
  328. }
  329. else {
  330. /* The request was not completed, so we put it
  331. * back again on the list of pending requests
  332. * so that it can be handled later on. */
  333. _starpu_spin_unlock(&r->lock);
  334. _starpu_spin_unlock(&handle->header_lock);
  335. PTHREAD_MUTEX_LOCK(&data_requests_pending_list_mutex[src_node]);
  336. starpu_data_request_list_push_front(data_requests_pending[src_node], r);
  337. PTHREAD_MUTEX_UNLOCK(&data_requests_pending_list_mutex[src_node]);
  338. }
  339. }
  340. }
  341. starpu_data_request_list_delete(local_list);
  342. }
  343. void _starpu_handle_pending_node_data_requests(uint32_t src_node)
  344. {
  345. _handle_pending_node_data_requests(src_node, 0);
  346. }
  347. void _starpu_handle_all_pending_node_data_requests(uint32_t src_node)
  348. {
  349. _handle_pending_node_data_requests(src_node, 1);
  350. }
  351. int _starpu_check_that_no_data_request_exists(uint32_t node)
  352. {
  353. /* XXX lock that !!! that's a quick'n'dirty test */
  354. int no_request = starpu_data_request_list_empty(data_requests[node]);
  355. int no_pending = starpu_data_request_list_empty(data_requests_pending[node]);
  356. return (no_request && no_pending);
  357. }