data_request.c 27 KB

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