data_request.c 33 KB

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