data_request.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2012 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <starpu.h>
  18. #include <common/config.h>
  19. #include <datawizard/datawizard.h>
  20. /* requests that have not been treated at all */
  21. static struct _starpu_data_request_list *data_requests[STARPU_MAXNODES];
  22. static struct _starpu_data_request_list *prefetch_requests[STARPU_MAXNODES];
  23. static pthread_mutex_t data_requests_list_mutex[STARPU_MAXNODES];
  24. /* requests that are not terminated (eg. async transfers) */
  25. static struct _starpu_data_request_list *data_requests_pending[STARPU_MAXNODES];
  26. static pthread_mutex_t data_requests_pending_list_mutex[STARPU_MAXNODES];
  27. void _starpu_init_data_request_lists(void)
  28. {
  29. unsigned i;
  30. for (i = 0; i < STARPU_MAXNODES; i++)
  31. {
  32. prefetch_requests[i] = _starpu_data_request_list_new();
  33. data_requests[i] = _starpu_data_request_list_new();
  34. _STARPU_PTHREAD_MUTEX_INIT(&data_requests_list_mutex[i], NULL);
  35. data_requests_pending[i] = _starpu_data_request_list_new();
  36. _STARPU_PTHREAD_MUTEX_INIT(&data_requests_pending_list_mutex[i], NULL);
  37. }
  38. }
  39. void _starpu_deinit_data_request_lists(void)
  40. {
  41. unsigned i;
  42. for (i = 0; i < STARPU_MAXNODES; i++)
  43. {
  44. _STARPU_PTHREAD_MUTEX_DESTROY(&data_requests_pending_list_mutex[i]);
  45. _starpu_data_request_list_delete(data_requests_pending[i]);
  46. _STARPU_PTHREAD_MUTEX_DESTROY(&data_requests_list_mutex[i]);
  47. _starpu_data_request_list_delete(data_requests[i]);
  48. _starpu_data_request_list_delete(prefetch_requests[i]);
  49. }
  50. }
  51. /* this should be called with the lock r->handle->header_lock taken */
  52. static void starpu_data_request_destroy(struct _starpu_data_request *r)
  53. {
  54. unsigned node;
  55. /* If this is a write only request, then there is no source and we use
  56. * the destination node to cache the request. Otherwise we store the
  57. * pending requests between src and dst. */
  58. if (r->mode & STARPU_R)
  59. {
  60. node = r->src_replicate->memory_node;
  61. }
  62. else
  63. {
  64. node = r->dst_replicate->memory_node;
  65. }
  66. STARPU_ASSERT(r->dst_replicate->request[node] == r);
  67. r->dst_replicate->request[node] = NULL;
  68. //fprintf(stderr, "DESTROY REQ %p (%d) refcnt %d\n", r, node, r->refcnt);
  69. _starpu_data_request_delete(r);
  70. }
  71. /* handle->lock should already be taken ! */
  72. struct _starpu_data_request *_starpu_create_data_request(starpu_data_handle_t handle,
  73. struct _starpu_data_replicate *src_replicate,
  74. struct _starpu_data_replicate *dst_replicate,
  75. uint32_t handling_node,
  76. enum starpu_access_mode mode,
  77. unsigned ndeps,
  78. unsigned is_prefetch)
  79. {
  80. struct _starpu_data_request *r = _starpu_data_request_new();
  81. _starpu_spin_init(&r->lock);
  82. r->handle = handle;
  83. r->src_replicate = src_replicate;
  84. r->dst_replicate = dst_replicate;
  85. r->mode = mode;
  86. r->handling_node = handling_node;
  87. r->completed = 0;
  88. r->prefetch = is_prefetch;
  89. r->retval = -1;
  90. r->ndeps = ndeps;
  91. r->next_req_count = 0;
  92. r->callbacks = NULL;
  93. _starpu_spin_lock(&r->lock);
  94. /* Take a reference on the target for the request to be able to write it */
  95. dst_replicate->refcnt++;
  96. handle->busy_count++;
  97. if (mode & STARPU_R)
  98. {
  99. unsigned src_node = src_replicate->memory_node;
  100. dst_replicate->request[src_node] = r;
  101. /* Take a reference on the source for the request to be able to read it */
  102. src_replicate->refcnt++;
  103. handle->busy_count++;
  104. }
  105. else
  106. {
  107. unsigned dst_node = dst_replicate->memory_node;
  108. dst_replicate->request[dst_node] = r;
  109. }
  110. r->refcnt = 1;
  111. _starpu_spin_unlock(&r->lock);
  112. return r;
  113. }
  114. int _starpu_wait_data_request_completion(struct _starpu_data_request *r, unsigned may_alloc)
  115. {
  116. int retval;
  117. int do_delete = 0;
  118. uint32_t local_node = _starpu_get_local_memory_node();
  119. do
  120. {
  121. _starpu_spin_lock(&r->lock);
  122. if (r->completed)
  123. break;
  124. _starpu_spin_unlock(&r->lock);
  125. #ifndef STARPU_NON_BLOCKING_DRIVERS
  126. _starpu_wake_all_blocked_workers_on_node(r->handling_node);
  127. #endif
  128. _starpu_datawizard_progress(local_node, may_alloc);
  129. }
  130. while (1);
  131. retval = r->retval;
  132. if (retval)
  133. _STARPU_DISP("REQUEST %p COMPLETED (retval %d) !\n", r, r->retval);
  134. r->refcnt--;
  135. /* if nobody is waiting on that request, we can get rid of it */
  136. if (r->refcnt == 0)
  137. do_delete = 1;
  138. _starpu_spin_unlock(&r->lock);
  139. if (do_delete)
  140. starpu_data_request_destroy(r);
  141. return retval;
  142. }
  143. /* this is non blocking */
  144. void _starpu_post_data_request(struct _starpu_data_request *r, uint32_t handling_node)
  145. {
  146. // _STARPU_DEBUG("POST REQUEST\n");
  147. /* If some dependencies are not fulfilled yet, we don't actually post the request */
  148. if (r->ndeps > 0)
  149. return;
  150. if (r->mode & STARPU_R)
  151. {
  152. STARPU_ASSERT(r->src_replicate->allocated);
  153. STARPU_ASSERT(r->src_replicate->refcnt);
  154. }
  155. /* insert the request in the proper list */
  156. _STARPU_PTHREAD_MUTEX_LOCK(&data_requests_list_mutex[handling_node]);
  157. if (r->prefetch)
  158. _starpu_data_request_list_push_back(prefetch_requests[handling_node], r);
  159. else
  160. _starpu_data_request_list_push_back(data_requests[handling_node], r);
  161. _STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[handling_node]);
  162. #ifndef STARPU_NON_BLOCKING_DRIVERS
  163. _starpu_wake_all_blocked_workers_on_node(handling_node);
  164. #endif
  165. }
  166. /* We assume that r->lock is taken by the caller */
  167. void _starpu_data_request_append_callback(struct _starpu_data_request *r, void (*callback_func)(void *), void *callback_arg)
  168. {
  169. STARPU_ASSERT(r);
  170. if (callback_func)
  171. {
  172. struct _starpu_callback_list *link = (struct _starpu_callback_list *) malloc(sizeof(struct _starpu_callback_list));
  173. STARPU_ASSERT(link);
  174. link->callback_func = callback_func;
  175. link->callback_arg = callback_arg;
  176. link->next = r->callbacks;
  177. r->callbacks = link;
  178. }
  179. }
  180. /* This method is called with handle's header_lock taken */
  181. static void starpu_handle_data_request_completion(struct _starpu_data_request *r)
  182. {
  183. unsigned do_delete = 0;
  184. starpu_data_handle_t handle = r->handle;
  185. enum starpu_access_mode mode = r->mode;
  186. struct _starpu_data_replicate *src_replicate = r->src_replicate;
  187. struct _starpu_data_replicate *dst_replicate = r->dst_replicate;
  188. #ifdef STARPU_MEMORY_STATUS
  189. enum _starpu_cache_state old_src_replicate_state = src_replicate->state;
  190. #endif
  191. _starpu_update_data_state(handle, r->dst_replicate, mode);
  192. #ifdef STARPU_MEMORY_STATUS
  193. if (src_replicate->state == STARPU_INVALID)
  194. {
  195. if (old_src_replicate_state == STARPU_OWNER)
  196. _starpu_handle_stats_invalidated(handle, src_replicate->memory_node);
  197. else
  198. {
  199. /* XXX Currently only ex-OWNER are tagged as invalidated */
  200. /* XXX Have to check all old state of every node in case a SHARED data become OWNED by the dst_replicate */
  201. }
  202. }
  203. if (dst_replicate->state == STARPU_SHARED)
  204. _starpu_handle_stats_loaded_shared(handle, dst_replicate->memory_node);
  205. else if (dst_replicate->state == STARPU_OWNER)
  206. {
  207. _starpu_handle_stats_loaded_owner(handle, dst_replicate->memory_node);
  208. }
  209. #endif
  210. #ifdef STARPU_USE_FXT
  211. uint32_t src_node = src_replicate->memory_node;
  212. uint32_t dst_node = dst_replicate->memory_node;
  213. size_t size = _starpu_data_get_size(handle);
  214. _STARPU_TRACE_END_DRIVER_COPY(src_node, dst_node, size, r->com_id);
  215. #endif
  216. /* Once the request has been fulfilled, we may submit the requests that
  217. * were chained to that request. */
  218. unsigned chained_req;
  219. for (chained_req = 0; chained_req < r->next_req_count; chained_req++)
  220. {
  221. struct _starpu_data_request *next_req = r->next_req[chained_req];
  222. STARPU_ASSERT(next_req->ndeps > 0);
  223. next_req->ndeps--;
  224. _starpu_post_data_request(next_req, next_req->handling_node);
  225. }
  226. r->completed = 1;
  227. /* Remove a reference on the destination replicate for the request */
  228. STARPU_ASSERT(dst_replicate->refcnt > 0);
  229. dst_replicate->refcnt--;
  230. STARPU_ASSERT(handle->busy_count > 0);
  231. handle->busy_count--;
  232. /* In case the source was "locked" by the request too */
  233. if (mode & STARPU_R)
  234. {
  235. STARPU_ASSERT(src_replicate->refcnt > 0);
  236. src_replicate->refcnt--;
  237. STARPU_ASSERT(handle->busy_count > 0);
  238. handle->busy_count--;
  239. }
  240. _starpu_data_check_not_busy(handle);
  241. r->refcnt--;
  242. /* if nobody is waiting on that request, we can get rid of it */
  243. if (r->refcnt == 0)
  244. do_delete = 1;
  245. r->retval = 0;
  246. /* In case there are one or multiple callbacks, we execute them now. */
  247. struct _starpu_callback_list *callbacks = r->callbacks;
  248. _starpu_spin_unlock(&r->lock);
  249. if (do_delete)
  250. starpu_data_request_destroy(r);
  251. _starpu_spin_unlock(&handle->header_lock);
  252. /* We do the callback once the lock is released so that they can do
  253. * blocking operations with the handle (eg. release it) */
  254. while (callbacks)
  255. {
  256. callbacks->callback_func(callbacks->callback_arg);
  257. struct _starpu_callback_list *next = callbacks->next;
  258. free(callbacks);
  259. callbacks = next;
  260. }
  261. }
  262. /* TODO : accounting to see how much time was spent working for other people ... */
  263. static int starpu_handle_data_request(struct _starpu_data_request *r, unsigned may_alloc)
  264. {
  265. starpu_data_handle_t handle = r->handle;
  266. _starpu_spin_lock(&handle->header_lock);
  267. _starpu_spin_lock(&r->lock);
  268. struct _starpu_data_replicate *src_replicate = r->src_replicate;
  269. struct _starpu_data_replicate *dst_replicate = r->dst_replicate;
  270. enum starpu_access_mode r_mode = r->mode;
  271. STARPU_ASSERT(!(r_mode & STARPU_R) || src_replicate);
  272. STARPU_ASSERT(!(r_mode & STARPU_R) || src_replicate->allocated);
  273. STARPU_ASSERT(!(r_mode & STARPU_R) || src_replicate->refcnt);
  274. _starpu_spin_unlock(&r->lock);
  275. /* perform the transfer */
  276. /* the header of the data must be locked by the worker that submitted the request */
  277. r->retval = _starpu_driver_copy_data_1_to_1(handle, src_replicate,
  278. dst_replicate, !(r_mode & STARPU_R), r, may_alloc);
  279. if (r->retval == -ENOMEM)
  280. {
  281. /* If there was not enough memory, we will try to redo the
  282. * request later. */
  283. _starpu_spin_unlock(&handle->header_lock);
  284. return -ENOMEM;
  285. }
  286. if (r->retval == -EAGAIN)
  287. {
  288. /* The request was successful, but could not be terminted
  289. * immediatly. We will handle the completion of the request
  290. * asynchronously. The request is put in the list of "pending"
  291. * requests in the meantime. */
  292. _starpu_spin_unlock(&handle->header_lock);
  293. _STARPU_PTHREAD_MUTEX_LOCK(&data_requests_pending_list_mutex[r->handling_node]);
  294. _starpu_data_request_list_push_front(data_requests_pending[r->handling_node], r);
  295. _STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_pending_list_mutex[r->handling_node]);
  296. return -EAGAIN;
  297. }
  298. /* the request has been handled */
  299. _starpu_spin_lock(&r->lock);
  300. starpu_handle_data_request_completion(r);
  301. return 0;
  302. }
  303. void _starpu_handle_node_data_requests(uint32_t src_node, unsigned may_alloc)
  304. {
  305. struct _starpu_data_request *r;
  306. struct _starpu_data_request_list *new_data_requests;
  307. /* take all the entries from the request list */
  308. _STARPU_PTHREAD_MUTEX_LOCK(&data_requests_list_mutex[src_node]);
  309. struct _starpu_data_request_list *local_list = data_requests[src_node];
  310. if (_starpu_data_request_list_empty(local_list))
  311. {
  312. /* there is no request */
  313. _STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[src_node]);
  314. return;
  315. }
  316. /* There is an entry: we create a new empty list to replace the list of
  317. * requests, and we handle the request(s) one by one in the former
  318. * list, without concurrency issues.*/
  319. data_requests[src_node] = _starpu_data_request_list_new();
  320. _STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[src_node]);
  321. new_data_requests = _starpu_data_request_list_new();
  322. /* for all entries of the list */
  323. while (!_starpu_data_request_list_empty(local_list))
  324. {
  325. int res;
  326. r = _starpu_data_request_list_pop_front(local_list);
  327. res = starpu_handle_data_request(r, may_alloc);
  328. if (res == -ENOMEM)
  329. {
  330. _starpu_data_request_list_push_back(new_data_requests, r);
  331. }
  332. }
  333. _STARPU_PTHREAD_MUTEX_LOCK(&data_requests_list_mutex[src_node]);
  334. _starpu_data_request_list_push_list_front(new_data_requests, data_requests[src_node]);
  335. _STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[src_node]);
  336. _starpu_data_request_list_delete(new_data_requests);
  337. _starpu_data_request_list_delete(local_list);
  338. }
  339. void _starpu_handle_node_prefetch_requests(uint32_t src_node, unsigned may_alloc)
  340. {
  341. struct _starpu_data_request *r;
  342. struct _starpu_data_request_list *new_data_requests;
  343. struct _starpu_data_request_list *new_prefetch_requests;
  344. /* take all the entries from the request list */
  345. _STARPU_PTHREAD_MUTEX_LOCK(&data_requests_list_mutex[src_node]);
  346. struct _starpu_data_request_list *local_list = prefetch_requests[src_node];
  347. if (_starpu_data_request_list_empty(local_list))
  348. {
  349. /* there is no request */
  350. _STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[src_node]);
  351. return;
  352. }
  353. /* There is an entry: we create a new empty list to replace the list of
  354. * requests, and we handle the request(s) one by one in the former
  355. * list, without concurrency issues.*/
  356. prefetch_requests[src_node] = _starpu_data_request_list_new();
  357. _STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[src_node]);
  358. new_data_requests = _starpu_data_request_list_new();
  359. new_prefetch_requests = _starpu_data_request_list_new();
  360. /* for all entries of the list */
  361. while (!_starpu_data_request_list_empty(local_list))
  362. {
  363. int res;
  364. r = _starpu_data_request_list_pop_front(local_list);
  365. res = starpu_handle_data_request(r, may_alloc);
  366. if (res == -ENOMEM )
  367. {
  368. if (r->prefetch)
  369. _starpu_data_request_list_push_back(new_prefetch_requests, r);
  370. else
  371. {
  372. /* Prefetch request promoted while in tmp list*/
  373. _starpu_data_request_list_push_back(new_data_requests, r);
  374. }
  375. break;
  376. }
  377. }
  378. while(!_starpu_data_request_list_empty(local_list))
  379. {
  380. r = _starpu_data_request_list_pop_front(local_list);
  381. if (r->prefetch)
  382. _starpu_data_request_list_push_back(new_prefetch_requests, r);
  383. else
  384. /* Prefetch request promoted while in tmp list*/
  385. _starpu_data_request_list_push_back(new_data_requests, r);
  386. }
  387. _STARPU_PTHREAD_MUTEX_LOCK(&data_requests_list_mutex[src_node]);
  388. _starpu_data_request_list_push_list_front(new_data_requests, data_requests[src_node]);
  389. _starpu_data_request_list_push_list_front(new_prefetch_requests, prefetch_requests[src_node]);
  390. _STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[src_node]);
  391. _starpu_data_request_list_delete(new_data_requests);
  392. _starpu_data_request_list_delete(new_prefetch_requests);
  393. _starpu_data_request_list_delete(local_list);
  394. }
  395. static void _handle_pending_node_data_requests(uint32_t src_node, unsigned force)
  396. {
  397. // _STARPU_DEBUG("_starpu_handle_pending_node_data_requests ...\n");
  398. //
  399. struct _starpu_data_request_list *new_data_requests_pending = _starpu_data_request_list_new();
  400. _STARPU_PTHREAD_MUTEX_LOCK(&data_requests_pending_list_mutex[src_node]);
  401. /* for all entries of the list */
  402. struct _starpu_data_request_list *local_list = data_requests_pending[src_node];
  403. data_requests_pending[src_node] = _starpu_data_request_list_new();
  404. _STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_pending_list_mutex[src_node]);
  405. while (!_starpu_data_request_list_empty(local_list))
  406. {
  407. struct _starpu_data_request *r;
  408. r = _starpu_data_request_list_pop_front(local_list);
  409. starpu_data_handle_t handle = r->handle;
  410. _starpu_spin_lock(&handle->header_lock);
  411. _starpu_spin_lock(&r->lock);
  412. /* wait until the transfer is terminated */
  413. if (force)
  414. {
  415. _starpu_driver_wait_request_completion(&r->async_channel);
  416. starpu_handle_data_request_completion(r);
  417. }
  418. else
  419. {
  420. if (_starpu_driver_test_request_completion(&r->async_channel))
  421. {
  422. /* The request was completed */
  423. starpu_handle_data_request_completion(r);
  424. }
  425. else
  426. {
  427. /* The request was not completed, so we put it
  428. * back again on the list of pending requests
  429. * so that it can be handled later on. */
  430. _starpu_spin_unlock(&r->lock);
  431. _starpu_spin_unlock(&handle->header_lock);
  432. _starpu_data_request_list_push_back(new_data_requests_pending, r);
  433. }
  434. }
  435. }
  436. _STARPU_PTHREAD_MUTEX_LOCK(&data_requests_pending_list_mutex[src_node]);
  437. _starpu_data_request_list_push_list_back(data_requests_pending[src_node], new_data_requests_pending);
  438. _STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_pending_list_mutex[src_node]);
  439. _starpu_data_request_list_delete(local_list);
  440. _starpu_data_request_list_delete(new_data_requests_pending);
  441. }
  442. void _starpu_handle_pending_node_data_requests(uint32_t src_node)
  443. {
  444. _handle_pending_node_data_requests(src_node, 0);
  445. }
  446. void _starpu_handle_all_pending_node_data_requests(uint32_t src_node)
  447. {
  448. _handle_pending_node_data_requests(src_node, 1);
  449. }
  450. int _starpu_check_that_no_data_request_exists(uint32_t node)
  451. {
  452. /* XXX lock that !!! that's a quick'n'dirty test */
  453. int no_request = _starpu_data_request_list_empty(data_requests[node]);
  454. int no_pending = _starpu_data_request_list_empty(data_requests_pending[node]);
  455. return (no_request && no_pending);
  456. }
  457. void _starpu_update_prefetch_status(struct _starpu_data_request *r)
  458. {
  459. STARPU_ASSERT(r->prefetch > 0);
  460. r->prefetch=0;
  461. /* We have to promote chained_request too! */
  462. unsigned chained_req;
  463. for (chained_req = 0; chained_req < r->next_req_count; chained_req++)
  464. {
  465. struct _starpu_data_request *next_req = r->next_req[chained_req];
  466. if (next_req->prefetch)
  467. _starpu_update_prefetch_status(next_req);
  468. }
  469. _STARPU_PTHREAD_MUTEX_LOCK(&data_requests_list_mutex[r->handling_node]);
  470. /* The request can be in a different list (handling request or the temp list)
  471. * we have to check that it is really in the prefetch list. */
  472. struct _starpu_data_request *r_iter;
  473. for (r_iter = _starpu_data_request_list_begin(prefetch_requests[r->handling_node]);
  474. r_iter != _starpu_data_request_list_end(prefetch_requests[r->handling_node]);
  475. r_iter = _starpu_data_request_list_next(r_iter))
  476. {
  477. if (r==r_iter)
  478. {
  479. _starpu_data_request_list_erase(prefetch_requests[r->handling_node],r);
  480. _starpu_data_request_list_push_front(data_requests[r->handling_node],r);
  481. break;
  482. }
  483. }
  484. _STARPU_PTHREAD_MUTEX_UNLOCK(&data_requests_list_mutex[r->handling_node]);
  485. }