data_request.c 31 KB

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