data_request.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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. enum _starpu_worker_status old_status = STATUS_UNKNOWN;
  188. if (worker)
  189. {
  190. old_status = worker->status ;
  191. _starpu_set_worker_status(worker, STATUS_WAITING);
  192. }
  193. do
  194. {
  195. #ifdef STARPU_SIMGRID
  196. starpu_pthread_wait_reset(&wait);
  197. #endif
  198. STARPU_SYNCHRONIZE();
  199. if (STARPU_RUNNING_ON_VALGRIND)
  200. completed = 1;
  201. else
  202. completed = r->completed;
  203. if (completed)
  204. {
  205. _starpu_spin_lock(&r->lock);
  206. if (r->completed)
  207. break;
  208. _starpu_spin_unlock(&r->lock);
  209. }
  210. #ifndef STARPU_SIMGRID
  211. #ifndef STARPU_NON_BLOCKING_DRIVERS
  212. /* XXX: shouldn't be needed, and doesn't work with chained requests anyway */
  213. _starpu_wake_all_blocked_workers_on_node(r->handling_node);
  214. #endif
  215. #endif
  216. _starpu_datawizard_progress(may_alloc);
  217. #ifdef STARPU_SIMGRID
  218. starpu_pthread_wait_wait(&wait);
  219. #endif
  220. }
  221. while (1);
  222. if (worker)
  223. {
  224. _starpu_set_worker_status(worker, old_status);
  225. }
  226. #ifdef STARPU_SIMGRID
  227. starpu_pthread_queue_unregister(&wait, &_starpu_simgrid_transfer_queue[local_node]);
  228. starpu_pthread_queue_unregister(&wait, &_starpu_simgrid_transfer_queue[(unsigned) r->dst_replicate->memory_node]);
  229. starpu_pthread_wait_destroy(&wait);
  230. #endif
  231. retval = r->retval;
  232. if (retval)
  233. _STARPU_DISP("REQUEST %p completed with retval %d!\n", r, r->retval);
  234. r->refcnt--;
  235. /* if nobody is waiting on that request, we can get rid of it */
  236. if (r->refcnt == 0)
  237. do_delete = 1;
  238. _starpu_spin_unlock(&r->lock);
  239. if (do_delete)
  240. _starpu_data_request_destroy(r);
  241. return retval;
  242. }
  243. /* this is non blocking */
  244. void _starpu_post_data_request(struct _starpu_data_request *r)
  245. {
  246. unsigned handling_node = r->handling_node;
  247. STARPU_ASSERT(starpu_node_get_kind(handling_node) == STARPU_CPU_RAM || _starpu_memory_node_get_nworkers(handling_node));
  248. // _STARPU_DEBUG("POST REQUEST\n");
  249. /* If some dependencies are not fulfilled yet, we don't actually post the request */
  250. if (r->ndeps > 0)
  251. return;
  252. if (r->mode & STARPU_R)
  253. {
  254. STARPU_ASSERT(r->src_replicate->allocated);
  255. STARPU_ASSERT(r->src_replicate->refcnt);
  256. }
  257. /* insert the request in the proper list */
  258. STARPU_PTHREAD_MUTEX_LOCK(&data_requests_list_mutex[handling_node]);
  259. if (r->prefetch == 2)
  260. _starpu_data_request_prio_list_push_back(&idle_requests[handling_node], r);
  261. else if (r->prefetch)
  262. _starpu_data_request_prio_list_push_back(&prefetch_requests[handling_node], r);
  263. else
  264. _starpu_data_request_prio_list_push_back(&data_requests[handling_node], r);
  265. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[handling_node]);
  266. #ifndef STARPU_NON_BLOCKING_DRIVERS
  267. _starpu_wake_all_blocked_workers_on_node(handling_node);
  268. #endif
  269. }
  270. /* We assume that r->lock is taken by the caller */
  271. void _starpu_data_request_append_callback(struct _starpu_data_request *r, void (*callback_func)(void *), void *callback_arg)
  272. {
  273. STARPU_ASSERT(r);
  274. if (callback_func)
  275. {
  276. struct _starpu_callback_list *link;
  277. _STARPU_MALLOC(link, sizeof(struct _starpu_callback_list));
  278. link->callback_func = callback_func;
  279. link->callback_arg = callback_arg;
  280. link->next = r->callbacks;
  281. r->callbacks = link;
  282. }
  283. }
  284. /* This method is called with handle's header_lock taken, and unlocks it */
  285. static void starpu_handle_data_request_completion(struct _starpu_data_request *r)
  286. {
  287. unsigned do_delete = 0;
  288. starpu_data_handle_t handle = r->handle;
  289. enum starpu_data_access_mode mode = r->mode;
  290. struct _starpu_data_replicate *src_replicate = r->src_replicate;
  291. struct _starpu_data_replicate *dst_replicate = r->dst_replicate;
  292. if (dst_replicate)
  293. {
  294. #ifdef STARPU_MEMORY_STATS
  295. enum _starpu_cache_state old_src_replicate_state = src_replicate->state;
  296. #endif
  297. _starpu_spin_checklocked(&handle->header_lock);
  298. _starpu_update_data_state(handle, r->dst_replicate, mode);
  299. #ifdef STARPU_MEMORY_STATS
  300. if (src_replicate->state == STARPU_INVALID)
  301. {
  302. if (old_src_replicate_state == STARPU_OWNER)
  303. _starpu_memory_handle_stats_invalidated(handle, src_replicate->memory_node);
  304. else
  305. {
  306. /* XXX Currently only ex-OWNER are tagged as invalidated */
  307. /* XXX Have to check all old state of every node in case a SHARED data become OWNED by the dst_replicate */
  308. }
  309. }
  310. if (dst_replicate->state == STARPU_SHARED)
  311. _starpu_memory_handle_stats_loaded_shared(handle, dst_replicate->memory_node);
  312. else if (dst_replicate->state == STARPU_OWNER)
  313. {
  314. _starpu_memory_handle_stats_loaded_owner(handle, dst_replicate->memory_node);
  315. }
  316. #endif
  317. }
  318. if (r->com_id > 0)
  319. {
  320. #ifdef STARPU_USE_FXT
  321. unsigned src_node = src_replicate->memory_node;
  322. unsigned dst_node = dst_replicate->memory_node;
  323. size_t size = _starpu_data_get_size(handle);
  324. _STARPU_TRACE_END_DRIVER_COPY(src_node, dst_node, size, r->com_id, r->prefetch);
  325. #endif
  326. }
  327. /* Once the request has been fulfilled, we may submit the requests that
  328. * were chained to that request. */
  329. unsigned chained_req;
  330. for (chained_req = 0; chained_req < r->next_req_count; chained_req++)
  331. {
  332. struct _starpu_data_request *next_req = r->next_req[chained_req];
  333. STARPU_ASSERT(next_req->ndeps > 0);
  334. next_req->ndeps--;
  335. _starpu_post_data_request(next_req);
  336. }
  337. r->completed = 1;
  338. #ifdef STARPU_SIMGRID
  339. /* Wake potential worker which was waiting for it */
  340. if (dst_replicate)
  341. _starpu_wake_all_blocked_workers_on_node(dst_replicate->memory_node);
  342. #endif
  343. /* Remove a reference on the destination replicate for the request */
  344. if (dst_replicate)
  345. {
  346. STARPU_ASSERT(dst_replicate->refcnt > 0);
  347. dst_replicate->refcnt--;
  348. }
  349. STARPU_ASSERT(handle->busy_count > 0);
  350. handle->busy_count--;
  351. /* In case the source was "locked" by the request too */
  352. if (mode & STARPU_R)
  353. {
  354. STARPU_ASSERT(src_replicate->refcnt > 0);
  355. src_replicate->refcnt--;
  356. STARPU_ASSERT(handle->busy_count > 0);
  357. handle->busy_count--;
  358. }
  359. _starpu_data_request_unlink(r);
  360. unsigned destroyed = _starpu_data_check_not_busy(handle);
  361. r->refcnt--;
  362. /* if nobody is waiting on that request, we can get rid of it */
  363. if (r->refcnt == 0)
  364. do_delete = 1;
  365. r->retval = 0;
  366. /* In case there are one or multiple callbacks, we execute them now. */
  367. struct _starpu_callback_list *callbacks = r->callbacks;
  368. _starpu_spin_unlock(&r->lock);
  369. if (do_delete)
  370. _starpu_data_request_destroy(r);
  371. if (!destroyed)
  372. _starpu_spin_unlock(&handle->header_lock);
  373. /* We do the callback once the lock is released so that they can do
  374. * blocking operations with the handle (eg. release it) */
  375. while (callbacks)
  376. {
  377. callbacks->callback_func(callbacks->callback_arg);
  378. struct _starpu_callback_list *next = callbacks->next;
  379. free(callbacks);
  380. callbacks = next;
  381. }
  382. }
  383. /* TODO : accounting to see how much time was spent working for other people ... */
  384. static int starpu_handle_data_request(struct _starpu_data_request *r, unsigned may_alloc, int prefetch)
  385. {
  386. starpu_data_handle_t handle = r->handle;
  387. #ifndef STARPU_SIMGRID
  388. if (_starpu_spin_trylock(&handle->header_lock))
  389. return -EBUSY;
  390. if (_starpu_spin_trylock(&r->lock))
  391. {
  392. _starpu_spin_unlock(&handle->header_lock);
  393. return -EBUSY;
  394. }
  395. #else
  396. /* Have to wait for the handle, whatever it takes, in simgrid,
  397. * since we can not afford going to sleep, since nobody would wake us
  398. * up. */
  399. _starpu_spin_lock(&handle->header_lock);
  400. _starpu_spin_lock(&r->lock);
  401. #endif
  402. struct _starpu_data_replicate *src_replicate = r->src_replicate;
  403. struct _starpu_data_replicate *dst_replicate = r->dst_replicate;
  404. enum starpu_data_access_mode r_mode = r->mode;
  405. STARPU_ASSERT(!(r_mode & STARPU_R) || src_replicate);
  406. STARPU_ASSERT(!(r_mode & STARPU_R) || src_replicate->allocated);
  407. STARPU_ASSERT(!(r_mode & STARPU_R) || src_replicate->refcnt);
  408. _starpu_spin_unlock(&r->lock);
  409. /* FIXME: the request may get upgraded from here to freeing it... */
  410. /* perform the transfer */
  411. /* the header of the data must be locked by the worker that submitted the request */
  412. if (dst_replicate && dst_replicate->state == STARPU_INVALID)
  413. r->retval = _starpu_driver_copy_data_1_to_1(handle, src_replicate,
  414. dst_replicate, !(r_mode & STARPU_R), r, may_alloc, prefetch);
  415. else
  416. /* Already valid actually, no need to transfer anything */
  417. r->retval = 0;
  418. if (r->retval == -ENOMEM)
  419. {
  420. /* If there was not enough memory, we will try to redo the
  421. * request later. */
  422. _starpu_spin_unlock(&handle->header_lock);
  423. return -ENOMEM;
  424. }
  425. if (r->retval == -EAGAIN)
  426. {
  427. /* The request was successful, but could not be terminated
  428. * immediately. We will handle the completion of the request
  429. * asynchronously. The request is put in the list of "pending"
  430. * requests in the meantime. */
  431. _starpu_spin_unlock(&handle->header_lock);
  432. STARPU_PTHREAD_MUTEX_LOCK(&data_requests_pending_list_mutex[r->handling_node]);
  433. _starpu_data_request_prio_list_push_back(&data_requests_pending[r->handling_node], r);
  434. data_requests_npending[r->handling_node]++;
  435. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_pending_list_mutex[r->handling_node]);
  436. return -EAGAIN;
  437. }
  438. /* the request has been handled */
  439. _starpu_spin_lock(&r->lock);
  440. starpu_handle_data_request_completion(r);
  441. return 0;
  442. }
  443. 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)
  444. {
  445. struct _starpu_data_request *r;
  446. struct _starpu_data_request_prio_list new_data_requests[prefetch + 1]; /* Indexed by prefetch level */
  447. unsigned i;
  448. int ret = 0;
  449. *pushed = 0;
  450. #ifdef STARPU_NON_BLOCKING_DRIVERS
  451. /* This is racy, but not posing problems actually, since we know we
  452. * will come back here to probe again regularly anyway.
  453. * Thus, do not expose this optimization to helgrind */
  454. if (!STARPU_RUNNING_ON_VALGRIND && _starpu_data_request_prio_list_empty(&reqlist[src_node]))
  455. return 0;
  456. #endif
  457. /* TODO optimize */
  458. #ifdef STARPU_NON_BLOCKING_DRIVERS
  459. /* take all the entries from the request list */
  460. if (STARPU_PTHREAD_MUTEX_TRYLOCK(&data_requests_list_mutex[src_node]))
  461. {
  462. /* List is busy, do not bother with it */
  463. return -EBUSY;
  464. }
  465. #else
  466. STARPU_PTHREAD_MUTEX_LOCK(&data_requests_list_mutex[src_node]);
  467. #endif
  468. if (_starpu_data_request_prio_list_empty(&reqlist[src_node]))
  469. {
  470. /* there is no request */
  471. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[src_node]);
  472. return 0;
  473. }
  474. /* There is an entry: we create a new empty list to replace the list of
  475. * requests, and we handle the request(s) one by one in the former
  476. * list, without concurrency issues.*/
  477. struct _starpu_data_request_prio_list local_list = reqlist[src_node];
  478. _starpu_data_request_prio_list_init(&reqlist[src_node]);
  479. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[src_node]);
  480. for (i = 0; i <= prefetch; i++)
  481. _starpu_data_request_prio_list_init(&new_data_requests[i]);
  482. double start = starpu_timing_now();
  483. /* for all entries of the list */
  484. while (!_starpu_data_request_prio_list_empty(&local_list))
  485. {
  486. int res;
  487. if (data_requests_npending[src_node] >= n)
  488. {
  489. /* Too many requests at the same time, skip pushing
  490. * more for now */
  491. ret = -EBUSY;
  492. break;
  493. }
  494. r = _starpu_data_request_prio_list_pop_front(&local_list);
  495. res = starpu_handle_data_request(r, may_alloc, prefetch);
  496. if (res != 0 && res != -EAGAIN)
  497. {
  498. /* handle is busy, or not enough memory, postpone for now */
  499. ret = res;
  500. /* Prefetch requests might have gotten promoted while in tmp list */
  501. _starpu_data_request_prio_list_push_back(&new_data_requests[r->prefetch], r);
  502. if (prefetch)
  503. /* Prefetching more there would make the situation even worse */
  504. break;
  505. }
  506. (*pushed)++;
  507. if (starpu_timing_now() - start >= MAX_PUSH_TIME)
  508. {
  509. /* We have spent a lot of time doing requests, skip pushing more for now */
  510. ret = -EBUSY;
  511. break;
  512. }
  513. }
  514. /* Push back requests we didn't handle on the proper list */
  515. while (!_starpu_data_request_prio_list_empty(&local_list))
  516. {
  517. r = _starpu_data_request_prio_list_pop_front(&local_list);
  518. /* Prefetch requests might have gotten promoted while in tmp list */
  519. _starpu_data_request_prio_list_push_back(&new_data_requests[r->prefetch], r);
  520. }
  521. for (i = 0; i <= prefetch; i++)
  522. if (!_starpu_data_request_prio_list_empty(&new_data_requests[i]))
  523. break;
  524. if (i <= prefetch)
  525. {
  526. STARPU_PTHREAD_MUTEX_LOCK(&data_requests_list_mutex[src_node]);
  527. if (!(_starpu_data_request_prio_list_empty(&new_data_requests[0])))
  528. {
  529. _starpu_data_request_prio_list_push_prio_list_back(&new_data_requests[0], &data_requests[src_node]);
  530. data_requests[src_node] = new_data_requests[0];
  531. }
  532. if (prefetch >= 1 && !(_starpu_data_request_prio_list_empty(&new_data_requests[1])))
  533. {
  534. _starpu_data_request_prio_list_push_prio_list_back(&new_data_requests[1], &prefetch_requests[src_node]);
  535. prefetch_requests[src_node] = new_data_requests[1];
  536. }
  537. if (prefetch >= 2 && !(_starpu_data_request_prio_list_empty(&new_data_requests[2])))
  538. {
  539. _starpu_data_request_prio_list_push_prio_list_back(&new_data_requests[2], &idle_requests[src_node]);
  540. idle_requests[src_node] = new_data_requests[2];
  541. }
  542. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[src_node]);
  543. #ifdef STARPU_SIMGRID
  544. if (*pushed)
  545. {
  546. /* We couldn't process the request due to missing
  547. * space. Advance the clock a bit to let eviction have
  548. * the time to make some room for us. Ideally we should
  549. * rather have the caller block, and explicitly wait
  550. * for eviction to happen.
  551. */
  552. MSG_process_sleep(0.000001);
  553. _starpu_wake_all_blocked_workers_on_node(src_node);
  554. }
  555. #elif !defined(STARPU_NON_BLOCKING_DRIVERS)
  556. _starpu_wake_all_blocked_workers_on_node(src_node);
  557. #endif
  558. }
  559. return ret;
  560. }
  561. int _starpu_handle_node_data_requests(unsigned src_node, unsigned may_alloc, unsigned *pushed)
  562. {
  563. return __starpu_handle_node_data_requests(data_requests, src_node, may_alloc, MAX_PENDING_REQUESTS_PER_NODE, pushed, 0);
  564. }
  565. int _starpu_handle_node_prefetch_requests(unsigned src_node, unsigned may_alloc, unsigned *pushed)
  566. {
  567. return __starpu_handle_node_data_requests(prefetch_requests, src_node, may_alloc, MAX_PENDING_PREFETCH_REQUESTS_PER_NODE, pushed, 1);
  568. }
  569. int _starpu_handle_node_idle_requests(unsigned src_node, unsigned may_alloc, unsigned *pushed)
  570. {
  571. return __starpu_handle_node_data_requests(idle_requests, src_node, may_alloc, MAX_PENDING_IDLE_REQUESTS_PER_NODE, pushed, 2);
  572. }
  573. static int _handle_pending_node_data_requests(unsigned src_node, unsigned force)
  574. {
  575. // _STARPU_DEBUG("_starpu_handle_pending_node_data_requests ...\n");
  576. //
  577. struct _starpu_data_request_prio_list new_data_requests_pending;
  578. struct _starpu_data_request_prio_list empty_list;
  579. unsigned taken, kept;
  580. #ifdef STARPU_NON_BLOCKING_DRIVERS
  581. /* Here helgrind would should that this is an un protected access.
  582. * We however don't care about missing an entry, we will get called
  583. * again sooner or later. */
  584. if (!STARPU_RUNNING_ON_VALGRIND && _starpu_data_request_prio_list_empty(&data_requests_pending[src_node]))
  585. return 0;
  586. #endif
  587. _starpu_data_request_prio_list_init(&empty_list);
  588. #ifdef STARPU_NON_BLOCKING_DRIVERS
  589. if (!force)
  590. {
  591. if (STARPU_PTHREAD_MUTEX_TRYLOCK(&data_requests_pending_list_mutex[src_node]))
  592. {
  593. /* List is busy, do not bother with it */
  594. return 0;
  595. }
  596. }
  597. else
  598. #endif
  599. /* We really want to handle requests */
  600. STARPU_PTHREAD_MUTEX_LOCK(&data_requests_pending_list_mutex[src_node]);
  601. if (_starpu_data_request_prio_list_empty(&data_requests_pending[src_node]))
  602. {
  603. /* there is no request */
  604. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_pending_list_mutex[src_node]);
  605. return 0;
  606. }
  607. /* for all entries of the list */
  608. struct _starpu_data_request_prio_list local_list = data_requests_pending[src_node];
  609. _starpu_data_request_prio_list_init(&data_requests_pending[src_node]);
  610. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_pending_list_mutex[src_node]);
  611. _starpu_data_request_prio_list_init(&new_data_requests_pending);
  612. taken = 0;
  613. kept = 0;
  614. while (!_starpu_data_request_prio_list_empty(&local_list))
  615. {
  616. struct _starpu_data_request *r;
  617. r = _starpu_data_request_prio_list_pop_front(&local_list);
  618. taken++;
  619. starpu_data_handle_t handle = r->handle;
  620. #ifndef STARPU_SIMGRID
  621. if (force)
  622. /* Have to wait for the handle, whatever it takes */
  623. #endif
  624. /* Or when running in simgrid, in which case we can not
  625. * afford going to sleep, since nobody would wake us
  626. * up. */
  627. _starpu_spin_lock(&handle->header_lock);
  628. #ifndef STARPU_SIMGRID
  629. else
  630. if (_starpu_spin_trylock(&handle->header_lock))
  631. {
  632. /* Handle is busy, retry this later */
  633. _starpu_data_request_prio_list_push_back(&new_data_requests_pending, r);
  634. kept++;
  635. continue;
  636. }
  637. #endif
  638. /* This shouldn't be too hard to acquire */
  639. _starpu_spin_lock(&r->lock);
  640. /* wait until the transfer is terminated */
  641. if (force)
  642. {
  643. _starpu_driver_wait_request_completion(&r->async_channel);
  644. starpu_handle_data_request_completion(r);
  645. }
  646. else
  647. {
  648. if (_starpu_driver_test_request_completion(&r->async_channel))
  649. {
  650. /* The request was completed */
  651. starpu_handle_data_request_completion(r);
  652. }
  653. else
  654. {
  655. /* The request was not completed, so we put it
  656. * back again on the list of pending requests
  657. * so that it can be handled later on. */
  658. _starpu_spin_unlock(&r->lock);
  659. _starpu_spin_unlock(&handle->header_lock);
  660. _starpu_data_request_prio_list_push_back(&new_data_requests_pending, r);
  661. kept++;
  662. }
  663. }
  664. }
  665. STARPU_PTHREAD_MUTEX_LOCK(&data_requests_pending_list_mutex[src_node]);
  666. data_requests_npending[src_node] -= taken - kept;
  667. if (kept)
  668. _starpu_data_request_prio_list_push_prio_list_back(&data_requests_pending[src_node], &new_data_requests_pending);
  669. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_pending_list_mutex[src_node]);
  670. return taken - kept;
  671. }
  672. int _starpu_handle_pending_node_data_requests(unsigned src_node)
  673. {
  674. return _handle_pending_node_data_requests(src_node, 0);
  675. }
  676. int _starpu_handle_all_pending_node_data_requests(unsigned src_node)
  677. {
  678. return _handle_pending_node_data_requests(src_node, 1);
  679. }
  680. /* Note: the returned value will be outdated since the locks are not taken at
  681. * entry/exit */
  682. int _starpu_check_that_no_data_request_exists(unsigned node)
  683. {
  684. int no_request;
  685. int no_pending;
  686. STARPU_PTHREAD_MUTEX_LOCK(&data_requests_list_mutex[node]);
  687. no_request = _starpu_data_request_prio_list_empty(&data_requests[node])
  688. && _starpu_data_request_prio_list_empty(&prefetch_requests[node])
  689. && _starpu_data_request_prio_list_empty(&idle_requests[node]);
  690. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[node]);
  691. STARPU_PTHREAD_MUTEX_LOCK(&data_requests_pending_list_mutex[node]);
  692. no_pending = !data_requests_npending[node];
  693. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_pending_list_mutex[node]);
  694. return (no_request && no_pending);
  695. }
  696. /* Note: the returned value will be outdated since the locks are not taken at
  697. * entry/exit */
  698. int _starpu_check_that_no_data_request_is_pending(unsigned node)
  699. {
  700. return !data_requests_npending[node];
  701. }
  702. void _starpu_update_prefetch_status(struct _starpu_data_request *r, unsigned prefetch)
  703. {
  704. STARPU_ASSERT(r->prefetch > prefetch);
  705. r->prefetch=prefetch;
  706. /* We have to promote chained_request too! */
  707. unsigned chained_req;
  708. for (chained_req = 0; chained_req < r->next_req_count; chained_req++)
  709. {
  710. struct _starpu_data_request *next_req = r->next_req[chained_req];
  711. if (next_req->prefetch > prefetch)
  712. _starpu_update_prefetch_status(next_req, prefetch);
  713. }
  714. STARPU_PTHREAD_MUTEX_LOCK(&data_requests_list_mutex[r->handling_node]);
  715. /* The request can be in a different list (handling request or the temp list)
  716. * we have to check that it is really in the prefetch list. */
  717. if (_starpu_data_request_prio_list_ismember(&prefetch_requests[r->handling_node], r))
  718. {
  719. _starpu_data_request_prio_list_erase(&prefetch_requests[r->handling_node],r);
  720. _starpu_data_request_prio_list_push_back(&data_requests[r->handling_node],r);
  721. }
  722. /* The request can be in a different list (handling request or the temp list)
  723. * we have to check that it is really in the idle list. */
  724. else if (_starpu_data_request_prio_list_ismember(&idle_requests[r->handling_node], r))
  725. {
  726. _starpu_data_request_prio_list_erase(&idle_requests[r->handling_node],r);
  727. if (prefetch == 1)
  728. _starpu_data_request_prio_list_push_back(&prefetch_requests[r->handling_node],r);
  729. else
  730. _starpu_data_request_prio_list_push_back(&data_requests[r->handling_node],r);
  731. }
  732. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[r->handling_node]);
  733. #ifndef STARPU_NON_BLOCKING_DRIVERS
  734. _starpu_wake_all_blocked_workers_on_node(r->handling_node);
  735. #endif
  736. }