data_request.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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. /* requests that have not been treated at all */
  26. static struct _starpu_data_request_prio_list data_requests[STARPU_MAXNODES];
  27. static struct _starpu_data_request_prio_list prefetch_requests[STARPU_MAXNODES];
  28. static struct _starpu_data_request_prio_list idle_requests[STARPU_MAXNODES];
  29. static starpu_pthread_mutex_t data_requests_list_mutex[STARPU_MAXNODES];
  30. /* requests that are not terminated (eg. async transfers) */
  31. static struct _starpu_data_request_prio_list data_requests_pending[STARPU_MAXNODES];
  32. static unsigned data_requests_npending[STARPU_MAXNODES];
  33. static starpu_pthread_mutex_t data_requests_pending_list_mutex[STARPU_MAXNODES];
  34. void _starpu_init_data_request_lists(void)
  35. {
  36. unsigned i;
  37. for (i = 0; i < STARPU_MAXNODES; i++)
  38. {
  39. _starpu_data_request_prio_list_init(&data_requests[i]);
  40. _starpu_data_request_prio_list_init(&prefetch_requests[i]);
  41. _starpu_data_request_prio_list_init(&idle_requests[i]);
  42. #ifndef STARPU_DEBUG
  43. /* Tell helgrind that we are fine with checking for list_empty
  44. * in _starpu_handle_node_data_requests, we will call it
  45. * periodically anyway */
  46. STARPU_HG_DISABLE_CHECKING(data_requests[i].tree.root);
  47. STARPU_HG_DISABLE_CHECKING(prefetch_requests[i].tree.root);
  48. STARPU_HG_DISABLE_CHECKING(idle_requests[i].tree.root);
  49. #endif
  50. STARPU_PTHREAD_MUTEX_INIT(&data_requests_list_mutex[i], NULL);
  51. _starpu_data_request_prio_list_init(&data_requests_pending[i]);
  52. data_requests_npending[i] = 0;
  53. STARPU_PTHREAD_MUTEX_INIT(&data_requests_pending_list_mutex[i], NULL);
  54. }
  55. STARPU_HG_DISABLE_CHECKING(data_requests_npending);
  56. }
  57. void _starpu_deinit_data_request_lists(void)
  58. {
  59. unsigned i;
  60. for (i = 0; i < STARPU_MAXNODES; i++)
  61. {
  62. _starpu_data_request_prio_list_deinit(&data_requests[i]);
  63. _starpu_data_request_prio_list_deinit(&prefetch_requests[i]);
  64. _starpu_data_request_prio_list_deinit(&idle_requests[i]);
  65. STARPU_PTHREAD_MUTEX_DESTROY(&data_requests_pending_list_mutex[i]);
  66. STARPU_PTHREAD_MUTEX_DESTROY(&data_requests_list_mutex[i]);
  67. }
  68. }
  69. /* Unlink the request from the handle. New requests can then be made. */
  70. /* this should be called with the lock r->handle->header_lock taken */
  71. static void _starpu_data_request_unlink(struct _starpu_data_request *r)
  72. {
  73. _starpu_spin_checklocked(&r->handle->header_lock);
  74. /* If this is a write invalidation request, we store it in the handle
  75. */
  76. if (r->handle->write_invalidation_req == r)
  77. {
  78. STARPU_ASSERT(r->mode == STARPU_W);
  79. r->handle->write_invalidation_req = NULL;
  80. }
  81. else if (r->mode & STARPU_R)
  82. {
  83. /* If this is a read request, we store the pending requests
  84. * between src and dst. */
  85. unsigned node = r->src_replicate->memory_node;
  86. STARPU_ASSERT(r->dst_replicate->request[node] == r);
  87. r->dst_replicate->request[node] = NULL;
  88. }
  89. else
  90. {
  91. /* If this is a write only request, then there is no source and
  92. * we use the destination node to cache the request. */
  93. unsigned node = r->dst_replicate->memory_node;
  94. STARPU_ASSERT(r->dst_replicate->request[node] == r);
  95. r->dst_replicate->request[node] = NULL;
  96. }
  97. }
  98. static void _starpu_data_request_destroy(struct _starpu_data_request *r)
  99. {
  100. //fprintf(stderr, "DESTROY REQ %p (%d) refcnt %d\n", r, node, r->refcnt);
  101. _starpu_data_request_delete(r);
  102. }
  103. /* handle->lock should already be taken ! */
  104. struct _starpu_data_request *_starpu_create_data_request(starpu_data_handle_t handle,
  105. struct _starpu_data_replicate *src_replicate,
  106. struct _starpu_data_replicate *dst_replicate,
  107. int handling_node,
  108. enum starpu_data_access_mode mode,
  109. unsigned ndeps,
  110. unsigned is_prefetch,
  111. int prio,
  112. unsigned is_write_invalidation,
  113. const char *origin)
  114. {
  115. struct _starpu_data_request *r = _starpu_data_request_new();
  116. _starpu_spin_checklocked(&handle->header_lock);
  117. _starpu_spin_init(&r->lock);
  118. r->origin = origin;
  119. r->handle = handle;
  120. r->src_replicate = src_replicate;
  121. r->dst_replicate = dst_replicate;
  122. r->mode = mode;
  123. r->async_channel.type = STARPU_UNUSED;
  124. r->async_channel.starpu_mp_common_finished_sender = 0;
  125. r->async_channel.starpu_mp_common_finished_receiver = 0;
  126. r->async_channel.polling_node_sender = NULL;
  127. r->async_channel.polling_node_receiver = NULL;
  128. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  129. r->async_channel.event.mpi_ms_event.requests = NULL;
  130. #endif
  131. if (handling_node == -1)
  132. handling_node = STARPU_MAIN_RAM;
  133. r->handling_node = handling_node;
  134. STARPU_ASSERT(starpu_node_get_kind(handling_node) == STARPU_CPU_RAM || _starpu_memory_node_get_nworkers(handling_node));
  135. r->completed = 0;
  136. r->prefetch = is_prefetch;
  137. r->prio = prio;
  138. r->retval = -1;
  139. r->ndeps = ndeps;
  140. r->next_req_count = 0;
  141. r->callbacks = NULL;
  142. r->com_id = 0;
  143. _starpu_spin_lock(&r->lock);
  144. /* Take a reference on the target for the request to be able to write it */
  145. if (dst_replicate)
  146. dst_replicate->refcnt++;
  147. handle->busy_count++;
  148. if (is_write_invalidation)
  149. {
  150. STARPU_ASSERT(!handle->write_invalidation_req);
  151. handle->write_invalidation_req = r;
  152. }
  153. else if (mode & STARPU_R)
  154. {
  155. unsigned src_node = src_replicate->memory_node;
  156. STARPU_ASSERT(!dst_replicate->request[src_node]);
  157. dst_replicate->request[src_node] = r;
  158. /* Take a reference on the source for the request to be able to read it */
  159. src_replicate->refcnt++;
  160. handle->busy_count++;
  161. }
  162. else
  163. {
  164. unsigned dst_node = dst_replicate->memory_node;
  165. STARPU_ASSERT(!dst_replicate->request[dst_node]);
  166. dst_replicate->request[dst_node] = r;
  167. }
  168. r->refcnt = 1;
  169. _starpu_spin_unlock(&r->lock);
  170. return r;
  171. }
  172. int _starpu_wait_data_request_completion(struct _starpu_data_request *r, unsigned may_alloc)
  173. {
  174. int retval;
  175. int do_delete = 0;
  176. int completed;
  177. #ifdef STARPU_SIMGRID
  178. unsigned local_node = _starpu_memory_node_get_local_key();
  179. starpu_pthread_wait_t wait;
  180. starpu_pthread_wait_init(&wait);
  181. /* We need to get woken both when requests finish on our node, and on
  182. * the target node of the request we are waiting for */
  183. starpu_pthread_queue_register(&wait, &_starpu_simgrid_transfer_queue[local_node]);
  184. starpu_pthread_queue_register(&wait, &_starpu_simgrid_transfer_queue[(unsigned) r->dst_replicate->memory_node]);
  185. #endif
  186. struct _starpu_worker *worker = _starpu_get_local_worker_key();
  187. if (worker)
  188. {
  189. STARPU_ASSERT(worker->status == STATUS_UNKNOWN);
  190. _starpu_set_worker_status(worker, STATUS_WAITING);
  191. }
  192. do
  193. {
  194. #ifdef STARPU_SIMGRID
  195. starpu_pthread_wait_reset(&wait);
  196. #endif
  197. STARPU_SYNCHRONIZE();
  198. if (STARPU_RUNNING_ON_VALGRIND)
  199. completed = 1;
  200. else
  201. completed = r->completed;
  202. if (completed)
  203. {
  204. _starpu_spin_lock(&r->lock);
  205. if (r->completed)
  206. break;
  207. _starpu_spin_unlock(&r->lock);
  208. }
  209. #ifndef STARPU_SIMGRID
  210. #ifndef STARPU_NON_BLOCKING_DRIVERS
  211. /* XXX: shouldn't be needed, and doesn't work with chained requests anyway */
  212. _starpu_wake_all_blocked_workers_on_node(r->handling_node);
  213. #endif
  214. #endif
  215. _starpu_datawizard_progress(may_alloc);
  216. #ifdef STARPU_SIMGRID
  217. starpu_pthread_wait_wait(&wait);
  218. #endif
  219. }
  220. while (1);
  221. if (worker)
  222. {
  223. _starpu_set_worker_status(worker, STATUS_UNKNOWN);
  224. }
  225. #ifdef STARPU_SIMGRID
  226. starpu_pthread_queue_unregister(&wait, &_starpu_simgrid_transfer_queue[local_node]);
  227. starpu_pthread_queue_unregister(&wait, &_starpu_simgrid_transfer_queue[(unsigned) r->dst_replicate->memory_node]);
  228. starpu_pthread_wait_destroy(&wait);
  229. #endif
  230. retval = r->retval;
  231. if (retval)
  232. _STARPU_DISP("REQUEST %p completed with retval %d!\n", r, r->retval);
  233. r->refcnt--;
  234. /* if nobody is waiting on that request, we can get rid of it */
  235. if (r->refcnt == 0)
  236. do_delete = 1;
  237. _starpu_spin_unlock(&r->lock);
  238. if (do_delete)
  239. _starpu_data_request_destroy(r);
  240. return retval;
  241. }
  242. /* this is non blocking */
  243. void _starpu_post_data_request(struct _starpu_data_request *r)
  244. {
  245. unsigned handling_node = r->handling_node;
  246. STARPU_ASSERT(starpu_node_get_kind(handling_node) == STARPU_CPU_RAM || _starpu_memory_node_get_nworkers(handling_node));
  247. // _STARPU_DEBUG("POST REQUEST\n");
  248. /* If some dependencies are not fulfilled yet, we don't actually post the request */
  249. if (r->ndeps > 0)
  250. return;
  251. if (r->mode & STARPU_R)
  252. {
  253. STARPU_ASSERT(r->src_replicate->allocated);
  254. STARPU_ASSERT(r->src_replicate->refcnt);
  255. }
  256. /* insert the request in the proper list */
  257. STARPU_PTHREAD_MUTEX_LOCK(&data_requests_list_mutex[handling_node]);
  258. if (r->prefetch == 2)
  259. _starpu_data_request_prio_list_push_back(&idle_requests[handling_node], r);
  260. else if (r->prefetch)
  261. _starpu_data_request_prio_list_push_back(&prefetch_requests[handling_node], r);
  262. else
  263. _starpu_data_request_prio_list_push_back(&data_requests[handling_node], r);
  264. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[handling_node]);
  265. #ifndef STARPU_NON_BLOCKING_DRIVERS
  266. _starpu_wake_all_blocked_workers_on_node(handling_node);
  267. #endif
  268. }
  269. /* We assume that r->lock is taken by the caller */
  270. void _starpu_data_request_append_callback(struct _starpu_data_request *r, void (*callback_func)(void *), void *callback_arg)
  271. {
  272. STARPU_ASSERT(r);
  273. if (callback_func)
  274. {
  275. struct _starpu_callback_list *link;
  276. _STARPU_MALLOC(link, sizeof(struct _starpu_callback_list));
  277. link->callback_func = callback_func;
  278. link->callback_arg = callback_arg;
  279. link->next = r->callbacks;
  280. r->callbacks = link;
  281. }
  282. }
  283. /* This method is called with handle's header_lock taken, and unlocks it */
  284. static void starpu_handle_data_request_completion(struct _starpu_data_request *r)
  285. {
  286. unsigned do_delete = 0;
  287. starpu_data_handle_t handle = r->handle;
  288. enum starpu_data_access_mode mode = r->mode;
  289. struct _starpu_data_replicate *src_replicate = r->src_replicate;
  290. struct _starpu_data_replicate *dst_replicate = r->dst_replicate;
  291. if (dst_replicate)
  292. {
  293. #ifdef STARPU_MEMORY_STATS
  294. enum _starpu_cache_state old_src_replicate_state = src_replicate->state;
  295. #endif
  296. _starpu_spin_checklocked(&handle->header_lock);
  297. _starpu_update_data_state(handle, r->dst_replicate, mode);
  298. #ifdef STARPU_MEMORY_STATS
  299. if (src_replicate->state == STARPU_INVALID)
  300. {
  301. if (old_src_replicate_state == STARPU_OWNER)
  302. _starpu_memory_handle_stats_invalidated(handle, src_replicate->memory_node);
  303. else
  304. {
  305. /* XXX Currently only ex-OWNER are tagged as invalidated */
  306. /* XXX Have to check all old state of every node in case a SHARED data become OWNED by the dst_replicate */
  307. }
  308. }
  309. if (dst_replicate->state == STARPU_SHARED)
  310. _starpu_memory_handle_stats_loaded_shared(handle, dst_replicate->memory_node);
  311. else if (dst_replicate->state == STARPU_OWNER)
  312. {
  313. _starpu_memory_handle_stats_loaded_owner(handle, dst_replicate->memory_node);
  314. }
  315. #endif
  316. }
  317. if (r->com_id > 0)
  318. {
  319. #ifdef STARPU_USE_FXT
  320. unsigned src_node = src_replicate->memory_node;
  321. unsigned dst_node = dst_replicate->memory_node;
  322. size_t size = _starpu_data_get_size(handle);
  323. _STARPU_TRACE_END_DRIVER_COPY(src_node, dst_node, size, r->com_id, r->prefetch);
  324. #endif
  325. }
  326. /* Once the request has been fulfilled, we may submit the requests that
  327. * were chained to that request. */
  328. unsigned chained_req;
  329. for (chained_req = 0; chained_req < r->next_req_count; chained_req++)
  330. {
  331. struct _starpu_data_request *next_req = r->next_req[chained_req];
  332. STARPU_ASSERT(next_req->ndeps > 0);
  333. next_req->ndeps--;
  334. _starpu_post_data_request(next_req);
  335. }
  336. r->completed = 1;
  337. #ifdef STARPU_SIMGRID
  338. /* Wake potential worker which was waiting for it */
  339. if (dst_replicate)
  340. _starpu_wake_all_blocked_workers_on_node(dst_replicate->memory_node);
  341. #endif
  342. /* Remove a reference on the destination replicate for the request */
  343. if (dst_replicate)
  344. {
  345. STARPU_ASSERT(dst_replicate->refcnt > 0);
  346. dst_replicate->refcnt--;
  347. }
  348. STARPU_ASSERT(handle->busy_count > 0);
  349. handle->busy_count--;
  350. /* In case the source was "locked" by the request too */
  351. if (mode & STARPU_R)
  352. {
  353. STARPU_ASSERT(src_replicate->refcnt > 0);
  354. src_replicate->refcnt--;
  355. STARPU_ASSERT(handle->busy_count > 0);
  356. handle->busy_count--;
  357. }
  358. _starpu_data_request_unlink(r);
  359. unsigned destroyed = _starpu_data_check_not_busy(handle);
  360. r->refcnt--;
  361. /* if nobody is waiting on that request, we can get rid of it */
  362. if (r->refcnt == 0)
  363. do_delete = 1;
  364. r->retval = 0;
  365. /* In case there are one or multiple callbacks, we execute them now. */
  366. struct _starpu_callback_list *callbacks = r->callbacks;
  367. _starpu_spin_unlock(&r->lock);
  368. if (do_delete)
  369. _starpu_data_request_destroy(r);
  370. if (!destroyed)
  371. _starpu_spin_unlock(&handle->header_lock);
  372. /* We do the callback once the lock is released so that they can do
  373. * blocking operations with the handle (eg. release it) */
  374. while (callbacks)
  375. {
  376. callbacks->callback_func(callbacks->callback_arg);
  377. struct _starpu_callback_list *next = callbacks->next;
  378. free(callbacks);
  379. callbacks = next;
  380. }
  381. }
  382. /* TODO : accounting to see how much time was spent working for other people ... */
  383. static int starpu_handle_data_request(struct _starpu_data_request *r, unsigned may_alloc, int prefetch)
  384. {
  385. starpu_data_handle_t handle = r->handle;
  386. #ifndef STARPU_SIMGRID
  387. if (_starpu_spin_trylock(&handle->header_lock))
  388. return -EBUSY;
  389. if (_starpu_spin_trylock(&r->lock))
  390. {
  391. _starpu_spin_unlock(&handle->header_lock);
  392. return -EBUSY;
  393. }
  394. #else
  395. /* Have to wait for the handle, whatever it takes, in simgrid,
  396. * since we can not afford going to sleep, since nobody would wake us
  397. * up. */
  398. _starpu_spin_lock(&handle->header_lock);
  399. _starpu_spin_lock(&r->lock);
  400. #endif
  401. struct _starpu_data_replicate *src_replicate = r->src_replicate;
  402. struct _starpu_data_replicate *dst_replicate = r->dst_replicate;
  403. enum starpu_data_access_mode r_mode = r->mode;
  404. STARPU_ASSERT(!(r_mode & STARPU_R) || src_replicate);
  405. STARPU_ASSERT(!(r_mode & STARPU_R) || src_replicate->allocated);
  406. STARPU_ASSERT(!(r_mode & STARPU_R) || src_replicate->refcnt);
  407. _starpu_spin_unlock(&r->lock);
  408. /* FIXME: the request may get upgraded from here to freeing it... */
  409. /* perform the transfer */
  410. /* the header of the data must be locked by the worker that submitted the request */
  411. if (dst_replicate && dst_replicate->state == STARPU_INVALID)
  412. r->retval = _starpu_driver_copy_data_1_to_1(handle, src_replicate,
  413. dst_replicate, !(r_mode & STARPU_R), r, may_alloc, prefetch);
  414. else
  415. /* Already valid actually, no need to transfer anything */
  416. r->retval = 0;
  417. if (r->retval == -ENOMEM)
  418. {
  419. /* If there was not enough memory, we will try to redo the
  420. * request later. */
  421. _starpu_spin_unlock(&handle->header_lock);
  422. return -ENOMEM;
  423. }
  424. if (r->retval == -EAGAIN)
  425. {
  426. /* The request was successful, but could not be terminated
  427. * immediately. We will handle the completion of the request
  428. * asynchronously. The request is put in the list of "pending"
  429. * requests in the meantime. */
  430. _starpu_spin_unlock(&handle->header_lock);
  431. STARPU_PTHREAD_MUTEX_LOCK(&data_requests_pending_list_mutex[r->handling_node]);
  432. _starpu_data_request_prio_list_push_back(&data_requests_pending[r->handling_node], r);
  433. data_requests_npending[r->handling_node]++;
  434. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_pending_list_mutex[r->handling_node]);
  435. return -EAGAIN;
  436. }
  437. /* the request has been handled */
  438. _starpu_spin_lock(&r->lock);
  439. starpu_handle_data_request_completion(r);
  440. return 0;
  441. }
  442. 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)
  443. {
  444. struct _starpu_data_request *r;
  445. struct _starpu_data_request_prio_list new_data_requests[prefetch + 1]; /* Indexed by prefetch level */
  446. unsigned i;
  447. int ret = 0;
  448. *pushed = 0;
  449. #ifdef STARPU_NON_BLOCKING_DRIVERS
  450. /* This is racy, but not posing problems actually, since we know we
  451. * will come back here to probe again regularly anyway.
  452. * Thus, do not expose this optimization to helgrind */
  453. if (!STARPU_RUNNING_ON_VALGRIND && _starpu_data_request_prio_list_empty(&reqlist[src_node]))
  454. return 0;
  455. #endif
  456. /* TODO optimize */
  457. #ifdef STARPU_NON_BLOCKING_DRIVERS
  458. /* take all the entries from the request list */
  459. if (STARPU_PTHREAD_MUTEX_TRYLOCK(&data_requests_list_mutex[src_node]))
  460. {
  461. /* List is busy, do not bother with it */
  462. return -EBUSY;
  463. }
  464. #else
  465. STARPU_PTHREAD_MUTEX_LOCK(&data_requests_list_mutex[src_node]);
  466. #endif
  467. if (_starpu_data_request_prio_list_empty(&reqlist[src_node]))
  468. {
  469. /* there is no request */
  470. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[src_node]);
  471. return 0;
  472. }
  473. /* There is an entry: we create a new empty list to replace the list of
  474. * requests, and we handle the request(s) one by one in the former
  475. * list, without concurrency issues.*/
  476. struct _starpu_data_request_prio_list local_list = reqlist[src_node];
  477. _starpu_data_request_prio_list_init(&reqlist[src_node]);
  478. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[src_node]);
  479. for (i = 0; i <= prefetch; i++)
  480. _starpu_data_request_prio_list_init(&new_data_requests[i]);
  481. double start = starpu_timing_now();
  482. /* for all entries of the list */
  483. while (!_starpu_data_request_prio_list_empty(&local_list))
  484. {
  485. int res;
  486. if (data_requests_npending[src_node] >= n)
  487. {
  488. /* Too many requests at the same time, skip pushing
  489. * more for now */
  490. ret = -EBUSY;
  491. break;
  492. }
  493. r = _starpu_data_request_prio_list_pop_front(&local_list);
  494. res = starpu_handle_data_request(r, may_alloc, prefetch);
  495. if (res != 0 && res != -EAGAIN)
  496. {
  497. /* handle is busy, or not enough memory, postpone for now */
  498. ret = res;
  499. /* Prefetch requests might have gotten promoted while in tmp list */
  500. _starpu_data_request_prio_list_push_back(&new_data_requests[r->prefetch], r);
  501. if (prefetch)
  502. /* Prefetching more there would make the situation even worse */
  503. break;
  504. }
  505. (*pushed)++;
  506. if (starpu_timing_now() - start >= MAX_PUSH_TIME)
  507. {
  508. /* We have spent a lot of time doing requests, skip pushing more for now */
  509. ret = -EBUSY;
  510. break;
  511. }
  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. }