data_request.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2016 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015 CNRS
  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 <common/utils.h>
  20. #include <datawizard/datawizard.h>
  21. #include <datawizard/memory_nodes.h>
  22. #include <core/disk.h>
  23. #include <core/simgrid.h>
  24. /* TODO: This should be tuned according to driver capabilities
  25. * Data interfaces should also have to declare how many asynchronous requests
  26. * they have actually started (think of e.g. csr).
  27. */
  28. #define MAX_PENDING_REQUESTS_PER_NODE 20
  29. #define MAX_PENDING_PREFETCH_REQUESTS_PER_NODE 10
  30. #define MAX_PENDING_IDLE_REQUESTS_PER_NODE 1
  31. /* requests that have not been treated at all */
  32. static struct _starpu_data_request_prio_list data_requests[STARPU_MAXNODES];
  33. static struct _starpu_data_request_prio_list prefetch_requests[STARPU_MAXNODES];
  34. static struct _starpu_data_request_prio_list idle_requests[STARPU_MAXNODES];
  35. static starpu_pthread_mutex_t data_requests_list_mutex[STARPU_MAXNODES];
  36. /* requests that are not terminated (eg. async transfers) */
  37. static struct _starpu_data_request_prio_list data_requests_pending[STARPU_MAXNODES];
  38. static unsigned data_requests_npending[STARPU_MAXNODES];
  39. static starpu_pthread_mutex_t data_requests_pending_list_mutex[STARPU_MAXNODES];
  40. void _starpu_init_data_request_lists(void)
  41. {
  42. unsigned i;
  43. for (i = 0; i < STARPU_MAXNODES; i++)
  44. {
  45. _starpu_data_request_prio_list_init(&data_requests[i]);
  46. _starpu_data_request_prio_list_init(&prefetch_requests[i]);
  47. _starpu_data_request_prio_list_init(&idle_requests[i]);
  48. #ifndef STARPU_DEBUG
  49. /* Tell helgrind that we are fine with checking for list_empty
  50. * in _starpu_handle_node_data_requests, we will call it
  51. * periodically anyway */
  52. STARPU_HG_DISABLE_CHECKING(data_requests[i].tree.root);
  53. STARPU_HG_DISABLE_CHECKING(prefetch_requests[i].tree.root);
  54. STARPU_HG_DISABLE_CHECKING(idle_requests[i].tree.root);
  55. #endif
  56. STARPU_PTHREAD_MUTEX_INIT(&data_requests_list_mutex[i], NULL);
  57. _starpu_data_request_prio_list_init(&data_requests_pending[i]);
  58. data_requests_npending[i] = 0;
  59. STARPU_PTHREAD_MUTEX_INIT(&data_requests_pending_list_mutex[i], NULL);
  60. }
  61. STARPU_HG_DISABLE_CHECKING(data_requests_npending);
  62. }
  63. void _starpu_deinit_data_request_lists(void)
  64. {
  65. unsigned i;
  66. for (i = 0; i < STARPU_MAXNODES; i++)
  67. {
  68. _starpu_data_request_prio_list_deinit(&data_requests[i]);
  69. _starpu_data_request_prio_list_deinit(&prefetch_requests[i]);
  70. _starpu_data_request_prio_list_deinit(&idle_requests[i]);
  71. STARPU_PTHREAD_MUTEX_DESTROY(&data_requests_pending_list_mutex[i]);
  72. STARPU_PTHREAD_MUTEX_DESTROY(&data_requests_list_mutex[i]);
  73. }
  74. }
  75. /* Unlink the request from the handle. New requests can then be made. */
  76. /* this should be called with the lock r->handle->header_lock taken */
  77. static void _starpu_data_request_unlink(struct _starpu_data_request *r)
  78. {
  79. _starpu_spin_checklocked(&r->handle->header_lock);
  80. /* If this is a write invalidation request, we store it in the handle
  81. */
  82. if (r->handle->write_invalidation_req == r)
  83. {
  84. STARPU_ASSERT(r->mode == STARPU_W);
  85. r->handle->write_invalidation_req = NULL;
  86. }
  87. else if (r->mode & STARPU_R)
  88. {
  89. /* If this is a read request, we store the pending requests
  90. * between src and dst. */
  91. unsigned node = r->src_replicate->memory_node;
  92. STARPU_ASSERT(r->dst_replicate->request[node] == r);
  93. r->dst_replicate->request[node] = NULL;
  94. }
  95. else
  96. {
  97. /* If this is a write only request, then there is no source and
  98. * we use the destination node to cache the request. */
  99. unsigned node = r->dst_replicate->memory_node;
  100. STARPU_ASSERT(r->dst_replicate->request[node] == r);
  101. r->dst_replicate->request[node] = NULL;
  102. }
  103. }
  104. static void _starpu_data_request_destroy(struct _starpu_data_request *r)
  105. {
  106. switch (r->async_channel.type)
  107. {
  108. case STARPU_DISK_RAM:
  109. starpu_disk_free_request(&r->async_channel);
  110. break;
  111. default:
  112. break;
  113. }
  114. //fprintf(stderr, "DESTROY REQ %p (%d) refcnt %d\n", r, node, r->refcnt);
  115. _starpu_data_request_delete(r);
  116. }
  117. /* handle->lock should already be taken ! */
  118. struct _starpu_data_request *_starpu_create_data_request(starpu_data_handle_t handle,
  119. struct _starpu_data_replicate *src_replicate,
  120. struct _starpu_data_replicate *dst_replicate,
  121. unsigned handling_node,
  122. enum starpu_data_access_mode mode,
  123. unsigned ndeps,
  124. unsigned is_prefetch,
  125. int prio,
  126. unsigned is_write_invalidation)
  127. {
  128. struct _starpu_data_request *r = _starpu_data_request_new();
  129. _starpu_spin_checklocked(&handle->header_lock);
  130. _starpu_spin_init(&r->lock);
  131. r->handle = handle;
  132. r->src_replicate = src_replicate;
  133. r->dst_replicate = dst_replicate;
  134. r->mode = mode;
  135. r->async_channel.type = STARPU_UNUSED;
  136. r->handling_node = handling_node;
  137. STARPU_ASSERT(handling_node == STARPU_MAIN_RAM || _starpu_memory_node_get_nworkers(handling_node));
  138. r->completed = 0;
  139. r->prefetch = is_prefetch;
  140. r->prio = prio;
  141. r->retval = -1;
  142. r->ndeps = ndeps;
  143. r->next_req_count = 0;
  144. r->callbacks = NULL;
  145. r->com_id = 0;
  146. _starpu_spin_lock(&r->lock);
  147. /* Take a reference on the target for the request to be able to write it */
  148. dst_replicate->refcnt++;
  149. handle->busy_count++;
  150. if (is_write_invalidation)
  151. {
  152. STARPU_ASSERT(!handle->write_invalidation_req);
  153. handle->write_invalidation_req = r;
  154. }
  155. else if (mode & STARPU_R)
  156. {
  157. unsigned src_node = src_replicate->memory_node;
  158. STARPU_ASSERT(!dst_replicate->request[src_node]);
  159. dst_replicate->request[src_node] = r;
  160. /* Take a reference on the source for the request to be able to read it */
  161. src_replicate->refcnt++;
  162. handle->busy_count++;
  163. }
  164. else
  165. {
  166. unsigned dst_node = dst_replicate->memory_node;
  167. STARPU_ASSERT(!dst_replicate->request[dst_node]);
  168. dst_replicate->request[dst_node] = r;
  169. }
  170. r->refcnt = 1;
  171. _starpu_spin_unlock(&r->lock);
  172. return r;
  173. }
  174. int _starpu_wait_data_request_completion(struct _starpu_data_request *r, unsigned may_alloc)
  175. {
  176. int retval;
  177. int do_delete = 0;
  178. int completed;
  179. unsigned local_node = _starpu_memory_node_get_local_key();
  180. #ifdef STARPU_SIMGRID
  181. starpu_pthread_wait_t wait;
  182. starpu_pthread_wait_init(&wait);
  183. /* We need to get woken both when requests finish on our node, and on
  184. * the target node of the request we are waiting for */
  185. starpu_pthread_queue_register(&wait, &_starpu_simgrid_transfer_queue[local_node]);
  186. starpu_pthread_queue_register(&wait, &_starpu_simgrid_transfer_queue[r->dst_replicate->memory_node]);
  187. #endif
  188. do
  189. {
  190. #ifdef STARPU_SIMGRID
  191. starpu_pthread_wait_reset(&wait);
  192. #endif
  193. STARPU_SYNCHRONIZE();
  194. if (STARPU_RUNNING_ON_VALGRIND)
  195. completed = 1;
  196. else
  197. completed = r->completed;
  198. if (completed)
  199. {
  200. _starpu_spin_lock(&r->lock);
  201. if (r->completed)
  202. break;
  203. _starpu_spin_unlock(&r->lock);
  204. }
  205. #ifndef STARPU_SIMGRID
  206. #ifndef STARPU_NON_BLOCKING_DRIVERS
  207. /* XXX: shouldn't be needed, and doesn't work with chained requests anyway */
  208. _starpu_wake_all_blocked_workers_on_node(r->handling_node);
  209. #endif
  210. #endif
  211. _starpu_datawizard_progress(local_node, may_alloc);
  212. #ifdef STARPU_SIMGRID
  213. starpu_pthread_wait_wait(&wait);
  214. #endif
  215. }
  216. while (1);
  217. #ifdef STARPU_SIMGRID
  218. starpu_pthread_queue_unregister(&wait, &_starpu_simgrid_transfer_queue[local_node]);
  219. starpu_pthread_queue_unregister(&wait, &_starpu_simgrid_transfer_queue[r->dst_replicate->memory_node]);
  220. starpu_pthread_wait_destroy(&wait);
  221. #endif
  222. retval = r->retval;
  223. if (retval)
  224. _STARPU_DISP("REQUEST %p completed with retval %d!\n", r, r->retval);
  225. r->refcnt--;
  226. /* if nobody is waiting on that request, we can get rid of it */
  227. if (r->refcnt == 0)
  228. do_delete = 1;
  229. _starpu_spin_unlock(&r->lock);
  230. if (do_delete)
  231. _starpu_data_request_destroy(r);
  232. return retval;
  233. }
  234. /* this is non blocking */
  235. void _starpu_post_data_request(struct _starpu_data_request *r, unsigned handling_node)
  236. {
  237. /* We don't have a worker for disk nodes, these should have been posted to a main RAM node */
  238. STARPU_ASSERT(starpu_node_get_kind(handling_node) != STARPU_DISK_RAM);
  239. STARPU_ASSERT(handling_node == STARPU_MAIN_RAM || _starpu_memory_node_get_nworkers(handling_node));
  240. // _STARPU_DEBUG("POST REQUEST\n");
  241. /* If some dependencies are not fulfilled yet, we don't actually post the request */
  242. if (r->ndeps > 0)
  243. return;
  244. if (r->mode & STARPU_R)
  245. {
  246. STARPU_ASSERT(r->src_replicate->allocated);
  247. STARPU_ASSERT(r->src_replicate->refcnt);
  248. }
  249. /* insert the request in the proper list */
  250. STARPU_PTHREAD_MUTEX_LOCK(&data_requests_list_mutex[handling_node]);
  251. if (r->prefetch == 2)
  252. _starpu_data_request_prio_list_push_back(&idle_requests[handling_node], r);
  253. else if (r->prefetch)
  254. _starpu_data_request_prio_list_push_back(&prefetch_requests[handling_node], r);
  255. else
  256. _starpu_data_request_prio_list_push_back(&data_requests[handling_node], r);
  257. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[handling_node]);
  258. #ifndef STARPU_NON_BLOCKING_DRIVERS
  259. _starpu_wake_all_blocked_workers_on_node(handling_node);
  260. #endif
  261. }
  262. /* We assume that r->lock is taken by the caller */
  263. void _starpu_data_request_append_callback(struct _starpu_data_request *r, void (*callback_func)(void *), void *callback_arg)
  264. {
  265. STARPU_ASSERT(r);
  266. if (callback_func)
  267. {
  268. struct _starpu_callback_list *link = (struct _starpu_callback_list *) malloc(sizeof(struct _starpu_callback_list));
  269. STARPU_ASSERT(link);
  270. link->callback_func = callback_func;
  271. link->callback_arg = callback_arg;
  272. link->next = r->callbacks;
  273. r->callbacks = link;
  274. }
  275. }
  276. /* This method is called with handle's header_lock taken, and unlocks it */
  277. static void starpu_handle_data_request_completion(struct _starpu_data_request *r)
  278. {
  279. unsigned do_delete = 0;
  280. starpu_data_handle_t handle = r->handle;
  281. enum starpu_data_access_mode mode = r->mode;
  282. struct _starpu_data_replicate *src_replicate = r->src_replicate;
  283. struct _starpu_data_replicate *dst_replicate = r->dst_replicate;
  284. #ifdef STARPU_MEMORY_STATS
  285. enum _starpu_cache_state old_src_replicate_state = src_replicate->state;
  286. #endif
  287. _starpu_spin_checklocked(&handle->header_lock);
  288. _starpu_update_data_state(handle, r->dst_replicate, mode);
  289. #ifdef STARPU_MEMORY_STATS
  290. if (src_replicate->state == STARPU_INVALID)
  291. {
  292. if (old_src_replicate_state == STARPU_OWNER)
  293. _starpu_memory_handle_stats_invalidated(handle, src_replicate->memory_node);
  294. else
  295. {
  296. /* XXX Currently only ex-OWNER are tagged as invalidated */
  297. /* XXX Have to check all old state of every node in case a SHARED data become OWNED by the dst_replicate */
  298. }
  299. }
  300. if (dst_replicate->state == STARPU_SHARED)
  301. _starpu_memory_handle_stats_loaded_shared(handle, dst_replicate->memory_node);
  302. else if (dst_replicate->state == STARPU_OWNER)
  303. {
  304. _starpu_memory_handle_stats_loaded_owner(handle, dst_replicate->memory_node);
  305. }
  306. #endif
  307. if (r->com_id > 0)
  308. {
  309. #ifdef STARPU_USE_FXT
  310. unsigned src_node = src_replicate->memory_node;
  311. unsigned dst_node = dst_replicate->memory_node;
  312. size_t size = _starpu_data_get_size(handle);
  313. _STARPU_TRACE_END_DRIVER_COPY(src_node, dst_node, size, r->com_id, r->prefetch);
  314. #endif
  315. }
  316. /* Once the request has been fulfilled, we may submit the requests that
  317. * were chained to that request. */
  318. unsigned chained_req;
  319. for (chained_req = 0; chained_req < r->next_req_count; chained_req++)
  320. {
  321. struct _starpu_data_request *next_req = r->next_req[chained_req];
  322. STARPU_ASSERT(next_req->ndeps > 0);
  323. next_req->ndeps--;
  324. _starpu_post_data_request(next_req, next_req->handling_node);
  325. }
  326. r->completed = 1;
  327. #ifdef STARPU_SIMGRID
  328. /* Wake potential worker which was waiting for it */
  329. _starpu_wake_all_blocked_workers_on_node(dst_replicate->memory_node);
  330. #endif
  331. /* Remove a reference on the destination replicate for the request */
  332. STARPU_ASSERT(dst_replicate->refcnt > 0);
  333. dst_replicate->refcnt--;
  334. STARPU_ASSERT(handle->busy_count > 0);
  335. handle->busy_count--;
  336. /* In case the source was "locked" by the request too */
  337. if (mode & STARPU_R)
  338. {
  339. STARPU_ASSERT(src_replicate->refcnt > 0);
  340. src_replicate->refcnt--;
  341. STARPU_ASSERT(handle->busy_count > 0);
  342. handle->busy_count--;
  343. }
  344. _starpu_data_request_unlink(r);
  345. unsigned destroyed = _starpu_data_check_not_busy(handle);
  346. r->refcnt--;
  347. /* if nobody is waiting on that request, we can get rid of it */
  348. if (r->refcnt == 0)
  349. do_delete = 1;
  350. r->retval = 0;
  351. /* In case there are one or multiple callbacks, we execute them now. */
  352. struct _starpu_callback_list *callbacks = r->callbacks;
  353. _starpu_spin_unlock(&r->lock);
  354. if (do_delete)
  355. _starpu_data_request_destroy(r);
  356. if (!destroyed)
  357. _starpu_spin_unlock(&handle->header_lock);
  358. /* We do the callback once the lock is released so that they can do
  359. * blocking operations with the handle (eg. release it) */
  360. while (callbacks)
  361. {
  362. callbacks->callback_func(callbacks->callback_arg);
  363. struct _starpu_callback_list *next = callbacks->next;
  364. free(callbacks);
  365. callbacks = next;
  366. }
  367. }
  368. /* TODO : accounting to see how much time was spent working for other people ... */
  369. static int starpu_handle_data_request(struct _starpu_data_request *r, unsigned may_alloc, int prefetch)
  370. {
  371. starpu_data_handle_t handle = r->handle;
  372. #ifndef STARPU_SIMGRID
  373. if (_starpu_spin_trylock(&handle->header_lock))
  374. return -EBUSY;
  375. if (_starpu_spin_trylock(&r->lock))
  376. {
  377. _starpu_spin_unlock(&handle->header_lock);
  378. return -EBUSY;
  379. }
  380. #else
  381. /* Have to wait for the handle, whatever it takes, in simgrid,
  382. * since we can not afford going to sleep, since nobody would wake us
  383. * up. */
  384. _starpu_spin_lock(&handle->header_lock);
  385. _starpu_spin_lock(&r->lock);
  386. #endif
  387. struct _starpu_data_replicate *src_replicate = r->src_replicate;
  388. struct _starpu_data_replicate *dst_replicate = r->dst_replicate;
  389. enum starpu_data_access_mode r_mode = r->mode;
  390. STARPU_ASSERT(!(r_mode & STARPU_R) || src_replicate);
  391. STARPU_ASSERT(!(r_mode & STARPU_R) || src_replicate->allocated);
  392. STARPU_ASSERT(!(r_mode & STARPU_R) || src_replicate->refcnt);
  393. _starpu_spin_unlock(&r->lock);
  394. /* FIXME: the request may get upgraded from here to freeing it... */
  395. /* perform the transfer */
  396. /* the header of the data must be locked by the worker that submitted the request */
  397. if (dst_replicate->state == STARPU_INVALID)
  398. r->retval = _starpu_driver_copy_data_1_to_1(handle, src_replicate,
  399. dst_replicate, !(r_mode & STARPU_R), r, may_alloc, prefetch);
  400. else
  401. /* Already valid actually, no need to transfer anything */
  402. r->retval = 0;
  403. if (r->retval == -ENOMEM)
  404. {
  405. /* If there was not enough memory, we will try to redo the
  406. * request later. */
  407. _starpu_spin_unlock(&handle->header_lock);
  408. return -ENOMEM;
  409. }
  410. if (r->retval == -EAGAIN)
  411. {
  412. /* The request was successful, but could not be terminated
  413. * immediately. We will handle the completion of the request
  414. * asynchronously. The request is put in the list of "pending"
  415. * requests in the meantime. */
  416. _starpu_spin_unlock(&handle->header_lock);
  417. STARPU_PTHREAD_MUTEX_LOCK(&data_requests_pending_list_mutex[r->handling_node]);
  418. _starpu_data_request_prio_list_push_back(&data_requests_pending[r->handling_node], r);
  419. data_requests_npending[r->handling_node]++;
  420. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_pending_list_mutex[r->handling_node]);
  421. return -EAGAIN;
  422. }
  423. /* the request has been handled */
  424. _starpu_spin_lock(&r->lock);
  425. starpu_handle_data_request_completion(r);
  426. return 0;
  427. }
  428. static int __starpu_handle_node_data_requests(struct _starpu_data_request_prio_list *reqlist, unsigned src_node, unsigned may_alloc, unsigned n, unsigned *pushed, unsigned prefetch)
  429. {
  430. struct _starpu_data_request *r;
  431. struct _starpu_data_request_prio_list new_data_requests[prefetch + 1]; /* Indexed by prefetch level */
  432. unsigned i;
  433. int ret = 0;
  434. *pushed = 0;
  435. #ifdef STARPU_NON_BLOCKING_DRIVERS
  436. /* This is racy, but not posing problems actually, since we know we
  437. * will come back here to probe again regularly anyway.
  438. * Thus, do not expose this optimization to helgrind */
  439. if (!STARPU_RUNNING_ON_VALGRIND && _starpu_data_request_prio_list_empty(&reqlist[src_node]))
  440. return 0;
  441. #endif
  442. /* TODO optimize */
  443. #ifdef STARPU_NON_BLOCKING_DRIVERS
  444. /* take all the entries from the request list */
  445. if (STARPU_PTHREAD_MUTEX_TRYLOCK(&data_requests_list_mutex[src_node]))
  446. {
  447. /* List is busy, do not bother with it */
  448. return -EBUSY;
  449. }
  450. #else
  451. STARPU_PTHREAD_MUTEX_LOCK(&data_requests_list_mutex[src_node]);
  452. #endif
  453. if (_starpu_data_request_prio_list_empty(&reqlist[src_node]))
  454. {
  455. /* there is no request */
  456. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[src_node]);
  457. return 0;
  458. }
  459. /* There is an entry: we create a new empty list to replace the list of
  460. * requests, and we handle the request(s) one by one in the former
  461. * list, without concurrency issues.*/
  462. struct _starpu_data_request_prio_list local_list = reqlist[src_node];
  463. _starpu_data_request_prio_list_init(&reqlist[src_node]);
  464. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[src_node]);
  465. for (i = 0; i <= prefetch; i++)
  466. _starpu_data_request_prio_list_init(&new_data_requests[i]);
  467. /* for all entries of the list */
  468. while (!_starpu_data_request_prio_list_empty(&local_list))
  469. {
  470. int res;
  471. if (data_requests_npending[src_node] >= n)
  472. {
  473. /* Too many requests at the same time, skip pushing
  474. * more for now */
  475. ret = -EBUSY;
  476. break;
  477. }
  478. r = _starpu_data_request_prio_list_pop_front(&local_list);
  479. res = starpu_handle_data_request(r, may_alloc, prefetch);
  480. if (res != 0 && res != -EAGAIN)
  481. {
  482. /* handle is busy, or not enough memory, postpone for now */
  483. ret = res;
  484. /* Prefetch requests might have gotten promoted while in tmp list */
  485. _starpu_data_request_prio_list_push_back(&new_data_requests[r->prefetch], r);
  486. if (prefetch)
  487. /* Prefetching more there would make the situation even worse */
  488. break;
  489. }
  490. (*pushed)++;
  491. }
  492. /* Push back requests we didn't handle on the proper list */
  493. while (!_starpu_data_request_prio_list_empty(&local_list))
  494. {
  495. r = _starpu_data_request_prio_list_pop_front(&local_list);
  496. /* Prefetch requests might have gotten promoted while in tmp list */
  497. _starpu_data_request_prio_list_push_back(&new_data_requests[r->prefetch], r);
  498. }
  499. for (i = 0; i <= prefetch; i++)
  500. if (!_starpu_data_request_prio_list_empty(&new_data_requests[i]))
  501. break;
  502. if (i <= prefetch)
  503. {
  504. STARPU_PTHREAD_MUTEX_LOCK(&data_requests_list_mutex[src_node]);
  505. if (!(_starpu_data_request_prio_list_empty(&new_data_requests[0])))
  506. {
  507. _starpu_data_request_prio_list_push_prio_list_back(&new_data_requests[0], &data_requests[src_node]);
  508. data_requests[src_node] = new_data_requests[0];
  509. }
  510. if (prefetch >= 1 && !(_starpu_data_request_prio_list_empty(&new_data_requests[1])))
  511. {
  512. _starpu_data_request_prio_list_push_prio_list_back(&new_data_requests[1], &prefetch_requests[src_node]);
  513. prefetch_requests[src_node] = new_data_requests[1];
  514. }
  515. if (prefetch >= 2 && !(_starpu_data_request_prio_list_empty(&new_data_requests[2])))
  516. {
  517. _starpu_data_request_prio_list_push_prio_list_back(&new_data_requests[2], &idle_requests[src_node]);
  518. idle_requests[src_node] = new_data_requests[2];
  519. }
  520. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[src_node]);
  521. #ifdef STARPU_SIMGRID
  522. if (*pushed)
  523. {
  524. /* We couldn't process the request due to missing
  525. * space. Advance the clock a bit to let eviction have
  526. * the time to make some room for us. Ideally we should
  527. * rather have the caller block, and explicitly wait
  528. * for eviction to happen.
  529. */
  530. MSG_process_sleep(0.000010);
  531. _starpu_wake_all_blocked_workers_on_node(src_node);
  532. }
  533. #elif !defined(STARPU_NON_BLOCKING_DRIVERS)
  534. _starpu_wake_all_blocked_workers_on_node(src_node);
  535. #endif
  536. }
  537. return ret;
  538. }
  539. int _starpu_handle_node_data_requests(unsigned src_node, unsigned may_alloc, unsigned *pushed)
  540. {
  541. return __starpu_handle_node_data_requests(data_requests, src_node, may_alloc, MAX_PENDING_REQUESTS_PER_NODE, pushed, 0);
  542. }
  543. int _starpu_handle_node_prefetch_requests(unsigned src_node, unsigned may_alloc, unsigned *pushed)
  544. {
  545. return __starpu_handle_node_data_requests(prefetch_requests, src_node, may_alloc, MAX_PENDING_PREFETCH_REQUESTS_PER_NODE, pushed, 1);
  546. }
  547. int _starpu_handle_node_idle_requests(unsigned src_node, unsigned may_alloc, unsigned *pushed)
  548. {
  549. return __starpu_handle_node_data_requests(idle_requests, src_node, may_alloc, MAX_PENDING_IDLE_REQUESTS_PER_NODE, pushed, 2);
  550. }
  551. static int _handle_pending_node_data_requests(unsigned src_node, unsigned force)
  552. {
  553. // _STARPU_DEBUG("_starpu_handle_pending_node_data_requests ...\n");
  554. //
  555. struct _starpu_data_request_prio_list new_data_requests_pending;
  556. struct _starpu_data_request_prio_list empty_list;
  557. unsigned taken, kept;
  558. #ifdef STARPU_NON_BLOCKING_DRIVERS
  559. /* Here helgrind would should that this is an un protected access.
  560. * We however don't care about missing an entry, we will get called
  561. * again sooner or later. */
  562. if (!STARPU_RUNNING_ON_VALGRIND && _starpu_data_request_prio_list_empty(&data_requests_pending[src_node]))
  563. return 0;
  564. #endif
  565. _starpu_data_request_prio_list_init(&empty_list);
  566. #ifdef STARPU_NON_BLOCKING_DRIVERS
  567. if (!force)
  568. {
  569. if (STARPU_PTHREAD_MUTEX_TRYLOCK(&data_requests_pending_list_mutex[src_node]))
  570. {
  571. /* List is busy, do not bother with it */
  572. return 0;
  573. }
  574. }
  575. else
  576. #endif
  577. /* We really want to handle requests */
  578. STARPU_PTHREAD_MUTEX_LOCK(&data_requests_pending_list_mutex[src_node]);
  579. if (_starpu_data_request_prio_list_empty(&data_requests_pending[src_node]))
  580. {
  581. /* there is no request */
  582. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_pending_list_mutex[src_node]);
  583. return 0;
  584. }
  585. /* for all entries of the list */
  586. struct _starpu_data_request_prio_list local_list = data_requests_pending[src_node];
  587. _starpu_data_request_prio_list_init(&data_requests_pending[src_node]);
  588. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_pending_list_mutex[src_node]);
  589. _starpu_data_request_prio_list_init(&new_data_requests_pending);
  590. taken = 0;
  591. kept = 0;
  592. while (!_starpu_data_request_prio_list_empty(&local_list))
  593. {
  594. struct _starpu_data_request *r;
  595. r = _starpu_data_request_prio_list_pop_front(&local_list);
  596. taken++;
  597. starpu_data_handle_t handle = r->handle;
  598. #ifndef STARPU_SIMGRID
  599. if (force)
  600. /* Have to wait for the handle, whatever it takes */
  601. #endif
  602. /* Or when running in simgrid, in which case we can not
  603. * afford going to sleep, since nobody would wake us
  604. * up. */
  605. _starpu_spin_lock(&handle->header_lock);
  606. #ifndef STARPU_SIMGRID
  607. else
  608. if (_starpu_spin_trylock(&handle->header_lock))
  609. {
  610. /* Handle is busy, retry this later */
  611. _starpu_data_request_prio_list_push_back(&new_data_requests_pending, r);
  612. kept++;
  613. continue;
  614. }
  615. #endif
  616. /* This shouldn't be too hard to acquire */
  617. _starpu_spin_lock(&r->lock);
  618. /* wait until the transfer is terminated */
  619. if (force)
  620. {
  621. _starpu_driver_wait_request_completion(&r->async_channel);
  622. starpu_handle_data_request_completion(r);
  623. }
  624. else
  625. {
  626. if (_starpu_driver_test_request_completion(&r->async_channel))
  627. {
  628. /* The request was completed */
  629. starpu_handle_data_request_completion(r);
  630. }
  631. else
  632. {
  633. /* The request was not completed, so we put it
  634. * back again on the list of pending requests
  635. * so that it can be handled later on. */
  636. _starpu_spin_unlock(&r->lock);
  637. _starpu_spin_unlock(&handle->header_lock);
  638. _starpu_data_request_prio_list_push_back(&new_data_requests_pending, r);
  639. kept++;
  640. }
  641. }
  642. }
  643. STARPU_PTHREAD_MUTEX_LOCK(&data_requests_pending_list_mutex[src_node]);
  644. data_requests_npending[src_node] -= taken - kept;
  645. if (kept)
  646. _starpu_data_request_prio_list_push_prio_list_back(&data_requests_pending[src_node], &new_data_requests_pending);
  647. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_pending_list_mutex[src_node]);
  648. return taken - kept;
  649. }
  650. int _starpu_handle_pending_node_data_requests(unsigned src_node)
  651. {
  652. return _handle_pending_node_data_requests(src_node, 0);
  653. }
  654. int _starpu_handle_all_pending_node_data_requests(unsigned src_node)
  655. {
  656. return _handle_pending_node_data_requests(src_node, 1);
  657. }
  658. /* Note: the returned value will be outdated since the locks are not taken at
  659. * entry/exit */
  660. int _starpu_check_that_no_data_request_exists(unsigned node)
  661. {
  662. int no_request;
  663. int no_pending;
  664. STARPU_PTHREAD_MUTEX_LOCK(&data_requests_list_mutex[node]);
  665. no_request = _starpu_data_request_prio_list_empty(&data_requests[node])
  666. && _starpu_data_request_prio_list_empty(&prefetch_requests[node])
  667. && _starpu_data_request_prio_list_empty(&idle_requests[node]);
  668. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[node]);
  669. STARPU_PTHREAD_MUTEX_LOCK(&data_requests_pending_list_mutex[node]);
  670. no_pending = !data_requests_npending[node];
  671. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_pending_list_mutex[node]);
  672. return (no_request && no_pending);
  673. }
  674. /* Note: the returned value will be outdated since the locks are not taken at
  675. * entry/exit */
  676. int _starpu_check_that_no_data_request_is_pending(unsigned node)
  677. {
  678. return !data_requests_npending[node];
  679. }
  680. void _starpu_update_prefetch_status(struct _starpu_data_request *r, unsigned prefetch)
  681. {
  682. STARPU_ASSERT(r->prefetch > prefetch);
  683. r->prefetch=prefetch;
  684. /* We have to promote chained_request too! */
  685. unsigned chained_req;
  686. for (chained_req = 0; chained_req < r->next_req_count; chained_req++)
  687. {
  688. struct _starpu_data_request *next_req = r->next_req[chained_req];
  689. if (next_req->prefetch > prefetch)
  690. _starpu_update_prefetch_status(next_req, prefetch);
  691. }
  692. STARPU_PTHREAD_MUTEX_LOCK(&data_requests_list_mutex[r->handling_node]);
  693. /* The request can be in a different list (handling request or the temp list)
  694. * we have to check that it is really in the prefetch list. */
  695. if (_starpu_data_request_prio_list_ismember(&prefetch_requests[r->handling_node], r))
  696. {
  697. _starpu_data_request_prio_list_erase(&prefetch_requests[r->handling_node],r);
  698. _starpu_data_request_prio_list_push_back(&data_requests[r->handling_node],r);
  699. }
  700. /* The request can be in a different list (handling request or the temp list)
  701. * we have to check that it is really in the idle list. */
  702. else if (_starpu_data_request_prio_list_ismember(&idle_requests[r->handling_node], r))
  703. {
  704. _starpu_data_request_prio_list_erase(&idle_requests[r->handling_node],r);
  705. if (prefetch == 1)
  706. _starpu_data_request_prio_list_push_back(&prefetch_requests[r->handling_node],r);
  707. else
  708. _starpu_data_request_prio_list_push_back(&data_requests[r->handling_node],r);
  709. }
  710. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[r->handling_node]);
  711. #ifndef STARPU_NON_BLOCKING_DRIVERS
  712. _starpu_wake_all_blocked_workers_on_node(r->handling_node);
  713. #endif
  714. }