data_request.c 28 KB

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