data_request.c 26 KB

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