data_request.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2008-2021 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->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 handling_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[handling_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[handling_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[handling_node]);
  476. #endif
  477. if (_starpu_data_request_prio_list_empty(&reqlist[handling_node]))
  478. {
  479. /* there is no request */
  480. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[handling_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[handling_node];
  487. _starpu_data_request_prio_list_init(&reqlist[handling_node]);
  488. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[handling_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[handling_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. else
  516. (*pushed)++;
  517. if (starpu_timing_now() - start >= MAX_PUSH_TIME)
  518. {
  519. /* We have spent a lot of time doing requests, skip pushing more for now */
  520. ret = -EBUSY;
  521. break;
  522. }
  523. }
  524. /* Push back requests we didn't handle on the proper list */
  525. while (!_starpu_data_request_prio_list_empty(&local_list))
  526. {
  527. r = _starpu_data_request_prio_list_pop_front_highest(&local_list);
  528. /* Prefetch requests might have gotten promoted while in tmp list */
  529. _starpu_data_request_prio_list_push_back(&new_data_requests[r->prefetch], r);
  530. }
  531. _starpu_data_request_prio_list_deinit(&local_list);
  532. for (i = 0; i <= prefetch; i++)
  533. if (!_starpu_data_request_prio_list_empty(&new_data_requests[i]))
  534. break;
  535. if (i <= prefetch)
  536. {
  537. STARPU_PTHREAD_MUTEX_LOCK(&data_requests_list_mutex[handling_node]);
  538. if (!(_starpu_data_request_prio_list_empty(&new_data_requests[STARPU_FETCH])))
  539. {
  540. _starpu_data_request_prio_list_push_prio_list_back(&new_data_requests[STARPU_FETCH], &data_requests[handling_node]);
  541. data_requests[handling_node] = new_data_requests[STARPU_FETCH];
  542. }
  543. if (prefetch >= STARPU_TASK_PREFETCH && !(_starpu_data_request_prio_list_empty(&new_data_requests[STARPU_TASK_PREFETCH])))
  544. {
  545. _starpu_data_request_prio_list_push_prio_list_back(&new_data_requests[STARPU_TASK_PREFETCH], &prefetch_requests[handling_node]);
  546. prefetch_requests[handling_node] = new_data_requests[STARPU_TASK_PREFETCH];
  547. }
  548. if (prefetch >= STARPU_PREFETCH && !(_starpu_data_request_prio_list_empty(&new_data_requests[STARPU_PREFETCH])))
  549. {
  550. _starpu_data_request_prio_list_push_prio_list_back(&new_data_requests[STARPU_PREFETCH], &prefetch_requests[handling_node]);
  551. prefetch_requests[handling_node] = new_data_requests[STARPU_PREFETCH];
  552. }
  553. if (prefetch >= STARPU_IDLEFETCH && !(_starpu_data_request_prio_list_empty(&new_data_requests[STARPU_IDLEFETCH])))
  554. {
  555. _starpu_data_request_prio_list_push_prio_list_back(&new_data_requests[STARPU_IDLEFETCH], &idle_requests[handling_node]);
  556. idle_requests[handling_node] = new_data_requests[STARPU_IDLEFETCH];
  557. }
  558. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[handling_node]);
  559. #ifdef STARPU_SIMGRID
  560. if (*pushed)
  561. {
  562. /* We couldn't process the request due to missing
  563. * space. Advance the clock a bit to let eviction have
  564. * the time to make some room for us. Ideally we should
  565. * rather have the caller block, and explicitly wait
  566. * for eviction to happen.
  567. */
  568. starpu_sleep(0.000001);
  569. _starpu_wake_all_blocked_workers_on_node(handling_node);
  570. }
  571. #elif !defined(STARPU_NON_BLOCKING_DRIVERS)
  572. _starpu_wake_all_blocked_workers_on_node(handling_node);
  573. #endif
  574. }
  575. return ret;
  576. }
  577. int _starpu_handle_node_data_requests(unsigned handling_node, unsigned may_alloc, unsigned *pushed)
  578. {
  579. return __starpu_handle_node_data_requests(data_requests, handling_node, may_alloc, MAX_PENDING_REQUESTS_PER_NODE, pushed, STARPU_FETCH);
  580. }
  581. int _starpu_handle_node_prefetch_requests(unsigned handling_node, unsigned may_alloc, unsigned *pushed)
  582. {
  583. return __starpu_handle_node_data_requests(prefetch_requests, handling_node, may_alloc, MAX_PENDING_PREFETCH_REQUESTS_PER_NODE, pushed, STARPU_PREFETCH);
  584. }
  585. int _starpu_handle_node_idle_requests(unsigned handling_node, unsigned may_alloc, unsigned *pushed)
  586. {
  587. return __starpu_handle_node_data_requests(idle_requests, handling_node, may_alloc, MAX_PENDING_IDLE_REQUESTS_PER_NODE, pushed, STARPU_IDLEFETCH);
  588. }
  589. static int _handle_pending_node_data_requests(unsigned handling_node, unsigned force)
  590. {
  591. // _STARPU_DEBUG("_starpu_handle_pending_node_data_requests ...\n");
  592. //
  593. struct _starpu_data_request_prio_list new_data_requests_pending;
  594. unsigned taken, kept;
  595. #ifdef STARPU_NON_BLOCKING_DRIVERS
  596. /* Here helgrind would should that this is an un protected access.
  597. * We however don't care about missing an entry, we will get called
  598. * again sooner or later. */
  599. if (!STARPU_RUNNING_ON_VALGRIND && _starpu_data_request_prio_list_empty(&data_requests_pending[handling_node]))
  600. return 0;
  601. #endif
  602. #ifdef STARPU_NON_BLOCKING_DRIVERS
  603. if (!force)
  604. {
  605. if (STARPU_PTHREAD_MUTEX_TRYLOCK(&data_requests_pending_list_mutex[handling_node]))
  606. {
  607. /* List is busy, do not bother with it */
  608. return 0;
  609. }
  610. }
  611. else
  612. #endif
  613. /* We really want to handle requests */
  614. STARPU_PTHREAD_MUTEX_LOCK(&data_requests_pending_list_mutex[handling_node]);
  615. if (_starpu_data_request_prio_list_empty(&data_requests_pending[handling_node]))
  616. {
  617. /* there is no request */
  618. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_pending_list_mutex[handling_node]);
  619. return 0;
  620. }
  621. /* for all entries of the list */
  622. struct _starpu_data_request_prio_list local_list = data_requests_pending[handling_node];
  623. _starpu_data_request_prio_list_init(&data_requests_pending[handling_node]);
  624. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_pending_list_mutex[handling_node]);
  625. _starpu_data_request_prio_list_init(&new_data_requests_pending);
  626. taken = 0;
  627. kept = 0;
  628. while (!_starpu_data_request_prio_list_empty(&local_list))
  629. {
  630. struct _starpu_data_request *r;
  631. r = _starpu_data_request_prio_list_pop_front_highest(&local_list);
  632. taken++;
  633. starpu_data_handle_t handle = r->handle;
  634. #ifndef STARPU_SIMGRID
  635. if (force)
  636. /* Have to wait for the handle, whatever it takes */
  637. #endif
  638. /* Or when running in simgrid, in which case we can not
  639. * afford going to sleep, since nobody would wake us
  640. * up. */
  641. _starpu_spin_lock(&handle->header_lock);
  642. #ifndef STARPU_SIMGRID
  643. else
  644. if (_starpu_spin_trylock(&handle->header_lock))
  645. {
  646. /* Handle is busy, retry this later */
  647. _starpu_data_request_prio_list_push_back(&new_data_requests_pending, r);
  648. kept++;
  649. continue;
  650. }
  651. #endif
  652. /* This shouldn't be too hard to acquire */
  653. _starpu_spin_lock(&r->lock);
  654. /* wait until the transfer is terminated */
  655. if (force)
  656. {
  657. _starpu_driver_wait_request_completion(&r->async_channel);
  658. starpu_handle_data_request_completion(r);
  659. }
  660. else
  661. {
  662. if (_starpu_driver_test_request_completion(&r->async_channel))
  663. {
  664. /* The request was completed */
  665. starpu_handle_data_request_completion(r);
  666. }
  667. else
  668. {
  669. /* The request was not completed, so we put it
  670. * back again on the list of pending requests
  671. * so that it can be handled later on. */
  672. _starpu_spin_unlock(&r->lock);
  673. _starpu_spin_unlock(&handle->header_lock);
  674. _starpu_data_request_prio_list_push_back(&new_data_requests_pending, r);
  675. kept++;
  676. }
  677. }
  678. }
  679. _starpu_data_request_prio_list_deinit(&local_list);
  680. STARPU_PTHREAD_MUTEX_LOCK(&data_requests_pending_list_mutex[handling_node]);
  681. data_requests_npending[handling_node] -= taken - kept;
  682. if (kept)
  683. _starpu_data_request_prio_list_push_prio_list_back(&data_requests_pending[handling_node], &new_data_requests_pending);
  684. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_pending_list_mutex[handling_node]);
  685. return taken - kept;
  686. }
  687. int _starpu_handle_pending_node_data_requests(unsigned handling_node)
  688. {
  689. return _handle_pending_node_data_requests(handling_node, 0);
  690. }
  691. int _starpu_handle_all_pending_node_data_requests(unsigned handling_node)
  692. {
  693. return _handle_pending_node_data_requests(handling_node, 1);
  694. }
  695. /* Note: the returned value will be outdated since the locks are not taken at
  696. * entry/exit */
  697. int _starpu_check_that_no_data_request_exists(unsigned node)
  698. {
  699. int no_request;
  700. int no_pending;
  701. STARPU_PTHREAD_MUTEX_LOCK(&data_requests_list_mutex[node]);
  702. no_request = _starpu_data_request_prio_list_empty(&data_requests[node])
  703. && _starpu_data_request_prio_list_empty(&prefetch_requests[node])
  704. && _starpu_data_request_prio_list_empty(&idle_requests[node]);
  705. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[node]);
  706. STARPU_PTHREAD_MUTEX_LOCK(&data_requests_pending_list_mutex[node]);
  707. no_pending = !data_requests_npending[node];
  708. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_pending_list_mutex[node]);
  709. return no_request && no_pending;
  710. }
  711. /* Note: the returned value will be outdated since the locks are not taken at
  712. * entry/exit */
  713. int _starpu_check_that_no_data_request_is_pending(unsigned node)
  714. {
  715. return !data_requests_npending[node];
  716. }
  717. void _starpu_update_prefetch_status(struct _starpu_data_request *r, enum starpu_is_prefetch prefetch)
  718. {
  719. STARPU_ASSERT(r->prefetch > prefetch);
  720. r->prefetch=prefetch;
  721. if (prefetch >= STARPU_IDLEFETCH)
  722. /* No possible actual change */
  723. return;
  724. /* We have to promote chained_request too! */
  725. unsigned chained_req;
  726. for (chained_req = 0; chained_req < r->next_req_count; chained_req++)
  727. {
  728. struct _starpu_data_request *next_req = r->next_req[chained_req];
  729. if (next_req->prefetch > prefetch)
  730. _starpu_update_prefetch_status(next_req, prefetch);
  731. }
  732. STARPU_PTHREAD_MUTEX_LOCK(&data_requests_list_mutex[r->handling_node]);
  733. int found = 1;
  734. /* The request can be in a different list (handling request or the temp list)
  735. * we have to check that it is really in the prefetch or idle list. */
  736. if (_starpu_data_request_prio_list_ismember(&prefetch_requests[r->handling_node], r))
  737. _starpu_data_request_prio_list_erase(&prefetch_requests[r->handling_node], r);
  738. else if (_starpu_data_request_prio_list_ismember(&idle_requests[r->handling_node], r))
  739. _starpu_data_request_prio_list_erase(&idle_requests[r->handling_node], r);
  740. else
  741. found = 0;
  742. if (found)
  743. {
  744. if (prefetch > STARPU_FETCH)
  745. _starpu_data_request_prio_list_push_back(&prefetch_requests[r->handling_node],r);
  746. else
  747. _starpu_data_request_prio_list_push_back(&data_requests[r->handling_node],r);
  748. }
  749. STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[r->handling_node]);
  750. #ifndef STARPU_NON_BLOCKING_DRIVERS
  751. _starpu_wake_all_blocked_workers_on_node(r->handling_node);
  752. #endif
  753. }