data_request.c 27 KB

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