data_request.c 13 KB

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