data_request.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2011 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 <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 starpu_data_request_list_t prefetch_requests[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_mutex_t data_requests_pending_list_mutex[STARPU_MAXNODES];
  27. int starpu_memstrategy_drop_prefetch[STARPU_MAXNODES];
  28. void _starpu_init_data_request_lists(void)
  29. {
  30. unsigned i;
  31. for (i = 0; i < STARPU_MAXNODES; i++)
  32. {
  33. prefetch_requests[i] = starpu_data_request_list_new();
  34. data_requests[i] = starpu_data_request_list_new();
  35. _STARPU_PTHREAD_MUTEX_INIT(&data_requests_list_mutex[i], NULL);
  36. data_requests_pending[i] = starpu_data_request_list_new();
  37. _STARPU_PTHREAD_MUTEX_INIT(&data_requests_pending_list_mutex[i], NULL);
  38. starpu_memstrategy_drop_prefetch[i]=0;
  39. }
  40. }
  41. void _starpu_deinit_data_request_lists(void)
  42. {
  43. unsigned i;
  44. for (i = 0; i < STARPU_MAXNODES; i++)
  45. {
  46. _STARPU_PTHREAD_MUTEX_DESTROY(&data_requests_pending_list_mutex[i]);
  47. starpu_data_request_list_delete(data_requests_pending[i]);
  48. _STARPU_PTHREAD_MUTEX_DESTROY(&data_requests_list_mutex[i]);
  49. starpu_data_request_list_delete(data_requests[i]);
  50. starpu_data_request_list_delete(prefetch_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. unsigned node;
  57. /* If this is a write only request, then there is no source and we use
  58. * the destination node to cache the request. Otherwise we store the
  59. * pending requests between src and dst. */
  60. if (r->mode & STARPU_R)
  61. {
  62. node = r->src_replicate->memory_node;
  63. }
  64. else {
  65. node = r->dst_replicate->memory_node;
  66. }
  67. STARPU_ASSERT(r->dst_replicate->request[node] == r);
  68. r->dst_replicate->request[node] = NULL;
  69. //fprintf(stderr, "DESTROY REQ %p (%d) refcnt %d\n", r, node, r->refcnt);
  70. starpu_data_request_delete(r);
  71. }
  72. /* handle->lock should already be taken ! */
  73. starpu_data_request_t _starpu_create_data_request(starpu_data_handle_t handle,
  74. struct starpu_data_replicate_s *src_replicate,
  75. struct starpu_data_replicate_s *dst_replicate,
  76. uint32_t handling_node,
  77. enum starpu_access_mode mode,
  78. unsigned ndeps,
  79. unsigned is_prefetch)
  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->prefetch = is_prefetch;
  90. r->retval = -1;
  91. r->ndeps = ndeps;
  92. r->next_req_count = 0;
  93. r->callbacks = NULL;
  94. _starpu_spin_lock(&r->lock);
  95. dst_replicate->refcnt++;
  96. handle->busy_count++;
  97. if (mode & STARPU_R)
  98. {
  99. unsigned src_node = src_replicate->memory_node;
  100. dst_replicate->request[src_node] = r;
  101. src_replicate->refcnt++;
  102. handle->busy_count++;
  103. }
  104. else {
  105. unsigned dst_node = dst_replicate->memory_node;
  106. dst_replicate->request[dst_node] = r;
  107. }
  108. r->refcnt = 1;
  109. _starpu_spin_unlock(&r->lock);
  110. return r;
  111. }
  112. int _starpu_wait_data_request_completion(starpu_data_request_t r, unsigned may_alloc)
  113. {
  114. int retval;
  115. int do_delete = 0;
  116. uint32_t local_node = _starpu_get_local_memory_node();
  117. do {
  118. _starpu_spin_lock(&r->lock);
  119. if (r->completed)
  120. break;
  121. _starpu_spin_unlock(&r->lock);
  122. #ifndef STARPU_NON_BLOCKING_DRIVERS
  123. _starpu_wake_all_blocked_workers_on_node(r->handling_node);
  124. #endif
  125. _starpu_datawizard_progress(local_node, may_alloc);
  126. } while (1);
  127. retval = r->retval;
  128. if (retval)
  129. _STARPU_DISP("REQUEST %p COMPLETED (retval %d) !\n", r, r->retval);
  130. r->refcnt--;
  131. /* if nobody is waiting on that request, we can get rid of it */
  132. if (r->refcnt == 0)
  133. do_delete = 1;
  134. _starpu_spin_unlock(&r->lock);
  135. if (do_delete)
  136. starpu_data_request_destroy(r);
  137. return retval;
  138. }
  139. /* this is non blocking */
  140. void _starpu_post_data_request(starpu_data_request_t r, uint32_t handling_node)
  141. {
  142. // _STARPU_DEBUG("POST REQUEST\n");
  143. /* If some dependencies are not fulfilled yet, we don't actually post the request */
  144. if (r->ndeps > 0)
  145. return;
  146. if (r->mode & STARPU_R)
  147. {
  148. STARPU_ASSERT(r->src_replicate->allocated);
  149. STARPU_ASSERT(r->src_replicate->refcnt);
  150. }
  151. /* insert the request in the proper list */
  152. _STARPU_PTHREAD_MUTEX_LOCK(&data_requests_list_mutex[handling_node]);
  153. if (r->prefetch) {
  154. starpu_data_request_list_push_back(prefetch_requests[handling_node], r);
  155. } else
  156. starpu_data_request_list_push_back(data_requests[handling_node], r);
  157. _STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[handling_node]);
  158. #ifndef STARPU_NON_BLOCKING_DRIVERS
  159. _starpu_wake_all_blocked_workers_on_node(handling_node);
  160. #endif
  161. }
  162. /* We assume that r->lock is taken by the caller */
  163. void _starpu_data_request_append_callback(starpu_data_request_t r, void (*callback_func)(void *), void *callback_arg)
  164. {
  165. STARPU_ASSERT(r);
  166. if (callback_func)
  167. {
  168. struct _starpu_callback_list *link = (struct _starpu_callback_list *) malloc(sizeof(struct _starpu_callback_list));
  169. STARPU_ASSERT(link);
  170. link->callback_func = callback_func;
  171. link->callback_arg = callback_arg;
  172. link->next = r->callbacks;
  173. r->callbacks = link;
  174. }
  175. }
  176. /* This method is called with handle's header_lock taken */
  177. static void starpu_handle_data_request_completion(starpu_data_request_t r)
  178. {
  179. unsigned do_delete = 0;
  180. starpu_data_handle_t handle = r->handle;
  181. enum starpu_access_mode mode = r->mode;
  182. struct starpu_data_replicate_s *src_replicate = r->src_replicate;
  183. struct starpu_data_replicate_s *dst_replicate = r->dst_replicate;
  184. #ifdef STARPU_MEMORY_STATUS
  185. enum _starpu_cache_state old_src_replicate_state = src_replicate->state;
  186. #endif
  187. _starpu_update_data_state(handle, r->dst_replicate, mode);
  188. #ifdef STARPU_MEMORY_STATUS
  189. if (src_replicate->state == STARPU_INVALID)
  190. {
  191. if (old_src_replicate_state == STARPU_OWNER)
  192. _starpu_handle_stats_invalidated(handle, src_replicate->memory_node);
  193. else
  194. {
  195. /* XXX Currently only ex-OWNER are tagged as invalidated */
  196. /* XXX Have to check all old state of every node in case a SHARED data become OWNED by the dst_replicate */
  197. }
  198. }
  199. if (dst_replicate->state == STARPU_SHARED)
  200. _starpu_handle_stats_loaded_shared(handle, dst_replicate->memory_node);
  201. else if (dst_replicate->state == STARPU_OWNER)
  202. {
  203. _starpu_handle_stats_loaded_owner(handle, dst_replicate->memory_node);
  204. }
  205. #endif
  206. #ifdef STARPU_USE_FXT
  207. uint32_t src_node = src_replicate->memory_node;
  208. uint32_t dst_node = dst_replicate->memory_node;
  209. size_t size = _starpu_data_get_size(handle);
  210. _STARPU_TRACE_END_DRIVER_COPY(src_node, dst_node, size, r->com_id);
  211. #endif
  212. /* Once the request has been fulfilled, we may submit the requests that
  213. * were chained to that request. */
  214. unsigned chained_req;
  215. for (chained_req = 0; chained_req < r->next_req_count; chained_req++)
  216. {
  217. struct starpu_data_request_s *next_req = r->next_req[chained_req];
  218. STARPU_ASSERT(next_req->ndeps > 0);
  219. next_req->ndeps--;
  220. _starpu_post_data_request(next_req, next_req->handling_node);
  221. }
  222. r->completed = 1;
  223. /* Remove a reference on the destination replicate */
  224. STARPU_ASSERT(dst_replicate->refcnt > 0);
  225. dst_replicate->refcnt--;
  226. STARPU_ASSERT(handle->busy_count > 0);
  227. handle->busy_count--;
  228. /* In case the source was "locked" by the request too */
  229. if (mode & STARPU_R)
  230. {
  231. STARPU_ASSERT(src_replicate->refcnt > 0);
  232. src_replicate->refcnt--;
  233. STARPU_ASSERT(handle->busy_count > 0);
  234. handle->busy_count--;
  235. }
  236. _starpu_data_check_not_busy(handle);
  237. r->refcnt--;
  238. /* if nobody is waiting on that request, we can get rid of it */
  239. if (r->refcnt == 0)
  240. do_delete = 1;
  241. r->retval = 0;
  242. /* In case there are one or multiple callbacks, we execute them now. */
  243. struct _starpu_callback_list *callbacks = r->callbacks;
  244. _starpu_spin_unlock(&r->lock);
  245. if (do_delete)
  246. starpu_data_request_destroy(r);
  247. _starpu_spin_unlock(&handle->header_lock);
  248. /* We do the callback once the lock is released so that they can do
  249. * blocking operations with the handle (eg. release it) */
  250. while (callbacks)
  251. {
  252. callbacks->callback_func(callbacks->callback_arg);
  253. struct _starpu_callback_list *next = callbacks->next;
  254. free(callbacks);
  255. callbacks = next;
  256. }
  257. }
  258. /* TODO : accounting to see how much time was spent working for other people ... */
  259. static int starpu_handle_data_request(starpu_data_request_t r, unsigned may_alloc)
  260. {
  261. starpu_data_handle_t handle = r->handle;
  262. _starpu_spin_lock(&handle->header_lock);
  263. _starpu_spin_lock(&r->lock);
  264. struct starpu_data_replicate_s *src_replicate = r->src_replicate;
  265. struct starpu_data_replicate_s *dst_replicate = r->dst_replicate;
  266. enum starpu_access_mode r_mode = r->mode;
  267. STARPU_ASSERT(!(r_mode & STARPU_R) || src_replicate);
  268. STARPU_ASSERT(!(r_mode & STARPU_R) || src_replicate->allocated);
  269. STARPU_ASSERT(!(r_mode & STARPU_R) || src_replicate->refcnt);
  270. _starpu_spin_unlock(&r->lock);
  271. /* perform the transfer */
  272. /* the header of the data must be locked by the worker that submitted the request */
  273. r->retval = _starpu_driver_copy_data_1_to_1(handle, src_replicate,
  274. dst_replicate, !(r_mode & STARPU_R), r, may_alloc);
  275. if (r->retval == -ENOMEM)
  276. {
  277. /* If there was not enough memory, we will try to redo the
  278. * request later. */
  279. _starpu_spin_unlock(&handle->header_lock);
  280. return -ENOMEM;
  281. }
  282. if (r->retval == -EAGAIN)
  283. {
  284. /* The request was successful, but could not be terminted
  285. * immediatly. We will handle the completion of the request
  286. * asynchronously. The request is put in the list of "pending"
  287. * requests in the meantime. */
  288. _starpu_spin_unlock(&handle->header_lock);
  289. _STARPU_PTHREAD_MUTEX_LOCK(&data_requests_pending_list_mutex[r->handling_node]);
  290. starpu_data_request_list_push_front(data_requests_pending[r->handling_node], r);
  291. _STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_pending_list_mutex[r->handling_node]);
  292. return -EAGAIN;
  293. }
  294. /* the request has been handled */
  295. _starpu_spin_lock(&r->lock);
  296. starpu_handle_data_request_completion(r);
  297. return 0;
  298. }
  299. void _starpu_handle_node_data_requests(uint32_t src_node, unsigned may_alloc)
  300. {
  301. starpu_data_request_t r;
  302. starpu_data_request_list_t new_data_requests;
  303. /* take all the entries from the request list */
  304. _STARPU_PTHREAD_MUTEX_LOCK(&data_requests_list_mutex[src_node]);
  305. starpu_data_request_list_t local_list = data_requests[src_node];
  306. if (starpu_data_request_list_empty(local_list))
  307. {
  308. /* there is no request */
  309. _STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[src_node]);
  310. return;
  311. }
  312. /* There is an entry: we create a new empty list to replace the list of
  313. * requests, and we handle the request(s) one by one in the former
  314. * list, without concurrency issues.*/
  315. data_requests[src_node] = starpu_data_request_list_new();
  316. _STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[src_node]);
  317. new_data_requests = starpu_data_request_list_new();
  318. /* for all entries of the list */
  319. while (!starpu_data_request_list_empty(local_list))
  320. {
  321. int res;
  322. r = starpu_data_request_list_pop_front(local_list);
  323. res = starpu_handle_data_request(r, may_alloc);
  324. if (res == -ENOMEM)
  325. {
  326. starpu_data_request_list_push_back(new_data_requests, r);
  327. }
  328. }
  329. _STARPU_PTHREAD_MUTEX_LOCK(&data_requests_list_mutex[src_node]);
  330. starpu_data_request_list_push_list_front(new_data_requests, data_requests[src_node]);
  331. _STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[src_node]);
  332. starpu_data_request_list_delete(new_data_requests);
  333. starpu_data_request_list_delete(local_list);
  334. }
  335. void _starpu_handle_node_prefetch_requests(uint32_t src_node, unsigned may_alloc){
  336. starpu_memstrategy_drop_prefetch[src_node]=0;
  337. starpu_data_request_t r;
  338. starpu_data_request_list_t new_data_requests;
  339. starpu_data_request_list_t new_prefetch_requests;
  340. /* take all the entries from the request list */
  341. _STARPU_PTHREAD_MUTEX_LOCK(&data_requests_list_mutex[src_node]);
  342. starpu_data_request_list_t local_list = prefetch_requests[src_node];
  343. if (starpu_data_request_list_empty(local_list))
  344. {
  345. /* there is no request */
  346. _STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[src_node]);
  347. return;
  348. }
  349. /* There is an entry: we create a new empty list to replace the list of
  350. * requests, and we handle the request(s) one by one in the former
  351. * list, without concurrency issues.*/
  352. prefetch_requests[src_node] = starpu_data_request_list_new();
  353. _STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[src_node]);
  354. new_data_requests = starpu_data_request_list_new();
  355. new_prefetch_requests = starpu_data_request_list_new();
  356. /* for all entries of the list */
  357. while (!starpu_data_request_list_empty(local_list))
  358. {
  359. int res;
  360. r = starpu_data_request_list_pop_front(local_list);
  361. res = starpu_handle_data_request(r, may_alloc);
  362. if (res == -ENOMEM )
  363. {
  364. starpu_memstrategy_drop_prefetch[src_node]=1;
  365. if (r->prefetch)
  366. starpu_data_request_list_push_back(new_prefetch_requests, r);
  367. else
  368. {
  369. /* Prefetch request promoted while in tmp list*/
  370. starpu_data_request_list_push_back(new_data_requests, r);
  371. }
  372. break;
  373. }
  374. }
  375. while(!starpu_data_request_list_empty(local_list) && starpu_memstrategy_drop_prefetch[src_node])
  376. {
  377. r = starpu_data_request_list_pop_front(local_list);
  378. if (r->prefetch)
  379. starpu_data_request_list_push_back(new_prefetch_requests, r);
  380. else
  381. starpu_data_request_list_push_back(new_data_requests, r);
  382. }
  383. _STARPU_PTHREAD_MUTEX_LOCK(&data_requests_list_mutex[src_node]);
  384. starpu_data_request_list_push_list_front(new_data_requests, data_requests[src_node]);
  385. starpu_data_request_list_push_list_front(new_prefetch_requests, prefetch_requests[src_node]);
  386. _STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[src_node]);
  387. starpu_data_request_list_delete(new_data_requests);
  388. starpu_data_request_list_delete(new_prefetch_requests);
  389. starpu_data_request_list_delete(local_list);
  390. }
  391. static void _handle_pending_node_data_requests(uint32_t src_node, unsigned force)
  392. {
  393. // _STARPU_DEBUG("_starpu_handle_pending_node_data_requests ...\n");
  394. //
  395. starpu_data_request_list_t new_data_requests_pending = starpu_data_request_list_new();
  396. _STARPU_PTHREAD_MUTEX_LOCK(&data_requests_pending_list_mutex[src_node]);
  397. /* for all entries of the list */
  398. starpu_data_request_list_t local_list = data_requests_pending[src_node];
  399. data_requests_pending[src_node] = starpu_data_request_list_new();
  400. _STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_pending_list_mutex[src_node]);
  401. while (!starpu_data_request_list_empty(local_list))
  402. {
  403. starpu_data_request_t r;
  404. r = starpu_data_request_list_pop_front(local_list);
  405. starpu_data_handle_t handle = r->handle;
  406. _starpu_spin_lock(&handle->header_lock);
  407. _starpu_spin_lock(&r->lock);
  408. /* wait until the transfer is terminated */
  409. if (force)
  410. {
  411. _starpu_driver_wait_request_completion(&r->async_channel);
  412. starpu_handle_data_request_completion(r);
  413. }
  414. else {
  415. if (_starpu_driver_test_request_completion(&r->async_channel))
  416. {
  417. /* The request was completed */
  418. starpu_handle_data_request_completion(r);
  419. }
  420. else {
  421. /* The request was not completed, so we put it
  422. * back again on the list of pending requests
  423. * so that it can be handled later on. */
  424. _starpu_spin_unlock(&r->lock);
  425. _starpu_spin_unlock(&handle->header_lock);
  426. starpu_data_request_list_push_back(new_data_requests_pending, r);
  427. }
  428. }
  429. }
  430. _STARPU_PTHREAD_MUTEX_LOCK(&data_requests_pending_list_mutex[src_node]);
  431. starpu_data_request_list_push_list_back(data_requests_pending[src_node], new_data_requests_pending);
  432. _STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_pending_list_mutex[src_node]);
  433. starpu_data_request_list_delete(local_list);
  434. starpu_data_request_list_delete(new_data_requests_pending);
  435. }
  436. void _starpu_handle_pending_node_data_requests(uint32_t src_node)
  437. {
  438. _handle_pending_node_data_requests(src_node, 0);
  439. }
  440. void _starpu_handle_all_pending_node_data_requests(uint32_t src_node)
  441. {
  442. _handle_pending_node_data_requests(src_node, 1);
  443. }
  444. int _starpu_check_that_no_data_request_exists(uint32_t node)
  445. {
  446. /* XXX lock that !!! that's a quick'n'dirty test */
  447. int no_request = starpu_data_request_list_empty(data_requests[node]);
  448. int no_pending = starpu_data_request_list_empty(data_requests_pending[node]);
  449. return (no_request && no_pending);
  450. }
  451. void _starpu_update_prefetch_status(starpu_data_request_t r){
  452. STARPU_ASSERT(r->prefetch > 0);
  453. r->prefetch=0;
  454. /* We have to promote chained_request too! */
  455. unsigned chained_req;
  456. for (chained_req = 0; chained_req < r->next_req_count; chained_req++)
  457. {
  458. struct starpu_data_request_s *next_req = r->next_req[chained_req];
  459. if (next_req->prefetch)
  460. _starpu_update_prefetch_status(next_req);
  461. }
  462. _STARPU_PTHREAD_MUTEX_LOCK(&data_requests_list_mutex[r->handling_node]);
  463. /* The request can be in a different list (handling request or the temp list)
  464. * we have to check that it is really in the prefetch list. */
  465. starpu_data_request_t r_iter;
  466. for (r_iter = starpu_data_request_list_begin(prefetch_requests[r->handling_node]);
  467. r_iter != starpu_data_request_list_end(prefetch_requests[r->handling_node]);
  468. r_iter = starpu_data_request_list_next(r_iter))
  469. {
  470. if (r==r_iter)
  471. {
  472. starpu_data_request_list_erase(prefetch_requests[r->handling_node],r);
  473. starpu_data_request_list_push_front(data_requests[r->handling_node],r);
  474. break;
  475. }
  476. }
  477. _STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[r->handling_node]);
  478. }