data_request.c 27 KB

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