coherency.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2011 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 <common/config.h>
  18. #include <datawizard/coherency.h>
  19. #include <datawizard/copy_driver.h>
  20. #include <datawizard/write_back.h>
  21. #include <core/dependencies/data_concurrency.h>
  22. #include <profiling/profiling.h>
  23. #include <math.h>
  24. static int link_supports_direct_transfers(starpu_data_handle_t handle, unsigned src_node, unsigned dst_node, unsigned *handling_node);
  25. uint32_t _starpu_select_src_node(starpu_data_handle_t handle, unsigned destination)
  26. {
  27. int src_node = -1;
  28. unsigned i;
  29. unsigned nnodes = starpu_memory_nodes_get_count();
  30. /* first find a valid copy, either a STARPU_OWNER or a STARPU_SHARED */
  31. uint32_t node;
  32. uint32_t src_node_mask = 0;
  33. size_t size = _starpu_data_get_size(handle);
  34. double cost = INFINITY;
  35. for (node = 0; node < nnodes; node++)
  36. {
  37. if (handle->per_node[node].state != STARPU_INVALID)
  38. {
  39. /* we found a copy ! */
  40. src_node_mask |= (1<<node);
  41. }
  42. }
  43. /* we should have found at least one copy ! */
  44. STARPU_ASSERT(src_node_mask != 0);
  45. /* Without knowing the size, we won't know the cost */
  46. if (!size)
  47. cost = 0;
  48. /* Check whether we have transfer cost for all nodes, if so, take the minimum */
  49. if (cost)
  50. for (i = 0; i < nnodes; i++)
  51. {
  52. if (src_node_mask & (1<<i))
  53. {
  54. double time = _starpu_predict_transfer_time(i, destination, size);
  55. unsigned handling_node;
  56. /* Avoid indirect transfers */
  57. if (!link_supports_direct_transfers(handle, i, destination, &handling_node))
  58. continue;
  59. if (time == 0.0)
  60. {
  61. /* No estimation, will have to revert to dumb strategy */
  62. cost = 0.0;
  63. break;
  64. }
  65. else if (time < cost)
  66. {
  67. cost = time;
  68. src_node = i;
  69. }
  70. }
  71. }
  72. if (cost && src_node != -1)
  73. /* Could estimate through cost, return that */
  74. return src_node;
  75. /* Revert to dumb strategy: take RAM unless only a GPU has it */
  76. for (i = 0; i < nnodes; i++)
  77. {
  78. if (src_node_mask & (1<<i))
  79. {
  80. /* this is a potential candidate */
  81. src_node = i;
  82. /* however GPU are expensive sources, really !
  83. * Unless peer transfer is supported.
  84. * Other should be ok */
  85. if (
  86. #ifndef HAVE_CUDA_MEMCPY_PEER
  87. _starpu_get_node_kind(i) != STARPU_CUDA_RAM &&
  88. #endif
  89. _starpu_get_node_kind(i) != STARPU_OPENCL_RAM)
  90. break ;
  91. }
  92. }
  93. STARPU_ASSERT(src_node != -1);
  94. return src_node;
  95. }
  96. /* this may be called once the data is fetched with header and STARPU_RW-lock hold */
  97. void _starpu_update_data_state(starpu_data_handle_t handle,
  98. struct _starpu_data_replicate *requesting_replicate,
  99. enum starpu_access_mode mode)
  100. {
  101. /* There is nothing to do for relaxed coherency modes (scratch or
  102. * reductions) */
  103. if (!(mode & STARPU_RW))
  104. return;
  105. unsigned nnodes = starpu_memory_nodes_get_count();
  106. /* the data is present now */
  107. unsigned requesting_node = requesting_replicate->memory_node;
  108. requesting_replicate->requested[requesting_node] = 0;
  109. if (mode & STARPU_W)
  110. {
  111. /* the requesting node now has the only valid copy */
  112. uint32_t node;
  113. for (node = 0; node < nnodes; node++)
  114. handle->per_node[node].state = STARPU_INVALID;
  115. requesting_replicate->state = STARPU_OWNER;
  116. }
  117. else
  118. { /* read only */
  119. if (requesting_replicate->state != STARPU_OWNER)
  120. {
  121. /* there was at least another copy of the data */
  122. uint32_t node;
  123. for (node = 0; node < nnodes; node++)
  124. {
  125. struct _starpu_data_replicate *replicate = &handle->per_node[node];
  126. if (replicate->state != STARPU_INVALID)
  127. replicate->state = STARPU_SHARED;
  128. }
  129. requesting_replicate->state = STARPU_SHARED;
  130. }
  131. }
  132. }
  133. static int worker_supports_direct_access(unsigned node, unsigned handling_node)
  134. {
  135. if (node == handling_node)
  136. return 1;
  137. if (!_starpu_memory_node_workers(handling_node))
  138. /* No worker to process the request from that node */
  139. return 0;
  140. int type = _starpu_get_node_kind(node);
  141. switch (type)
  142. {
  143. case STARPU_CUDA_RAM:
  144. #ifdef HAVE_CUDA_MEMCPY_PEER
  145. /* GPUs not always allow direct remote access: if CUDA4
  146. * is enabled, we allow two CUDA devices to communicate. */
  147. return (_starpu_get_node_kind(handling_node) != STARPU_OPENCL_RAM);
  148. #else
  149. /* Direct GPU-GPU transfers are not allowed in general */
  150. return 0;
  151. #endif
  152. case STARPU_OPENCL_RAM:
  153. return 0;
  154. default:
  155. return 1;
  156. }
  157. }
  158. static int link_supports_direct_transfers(starpu_data_handle_t handle, unsigned src_node, unsigned dst_node, unsigned *handling_node)
  159. {
  160. /* XXX That's a hack until we get cudaMemcpy3DPeerAsync to work !
  161. * Perhaps not all data interface provide a direct GPU-GPU transfer
  162. * method ! */
  163. #ifdef STARPU_USE_CUDA
  164. if (src_node != dst_node && _starpu_get_node_kind(src_node) == STARPU_CUDA_RAM && _starpu_get_node_kind(dst_node) == STARPU_CUDA_RAM)
  165. {
  166. const struct starpu_data_copy_methods *copy_methods = handle->ops->copy_methods;
  167. if (!copy_methods->cuda_to_cuda_async)
  168. return 0;
  169. }
  170. #endif
  171. if (worker_supports_direct_access(src_node, dst_node))
  172. {
  173. *handling_node = dst_node;
  174. return 1;
  175. }
  176. if (worker_supports_direct_access(dst_node, src_node))
  177. {
  178. *handling_node = src_node;
  179. return 1;
  180. }
  181. return 0;
  182. }
  183. /* Determines the path of a request : each hop is defined by (src,dst) and the
  184. * node that handles the hop. The returned value indicates the number of hops,
  185. * and the max_len is the maximum number of hops (ie. the size of the
  186. * src_nodes, dst_nodes and handling_nodes arrays. */
  187. static int determine_request_path(starpu_data_handle_t handle,
  188. unsigned src_node, unsigned dst_node,
  189. enum starpu_access_mode mode, int max_len,
  190. unsigned *src_nodes, unsigned *dst_nodes,
  191. unsigned *handling_nodes)
  192. {
  193. if (!(mode & STARPU_R))
  194. {
  195. /* The destination node should only allocate the data, no transfer is required */
  196. STARPU_ASSERT(max_len >= 1);
  197. src_nodes[0] = 0; // ignored
  198. dst_nodes[0] = dst_node;
  199. handling_nodes[0] = dst_node;
  200. return 1;
  201. }
  202. unsigned handling_node;
  203. int link_is_valid = link_supports_direct_transfers(handle, src_node, dst_node, &handling_node);
  204. if (!link_is_valid)
  205. {
  206. /* We need an intermediate hop to implement data staging
  207. * through main memory. */
  208. STARPU_ASSERT(max_len >= 2);
  209. /* XXX we hardcode 0 as the RAM node ... */
  210. /* GPU -> RAM */
  211. src_nodes[0] = src_node;
  212. dst_nodes[0] = 0;
  213. handling_nodes[0] = src_node;
  214. /* RAM -> GPU */
  215. src_nodes[1] = 0;
  216. dst_nodes[1] = dst_node;
  217. handling_nodes[1] = dst_node;
  218. return 2;
  219. }
  220. else
  221. {
  222. STARPU_ASSERT(max_len >= 1);
  223. src_nodes[0] = src_node;
  224. dst_nodes[0] = dst_node;
  225. handling_nodes[0] = handling_node;
  226. #ifndef HAVE_CUDA_MEMCPY_PEER
  227. STARPU_ASSERT(!(mode & STARPU_R) || _starpu_get_node_kind(src_node) != STARPU_CUDA_RAM || _starpu_get_node_kind(dst_node) != STARPU_CUDA_RAM);
  228. #endif
  229. return 1;
  230. }
  231. }
  232. /* handle->lock should be taken. r is returned locked. The node parameter
  233. * indicate either the source of the request, or the destination for a
  234. * write-only request. */
  235. static struct _starpu_data_request *_starpu_search_existing_data_request(struct _starpu_data_replicate *replicate, unsigned node, enum starpu_access_mode mode, unsigned is_prefetch)
  236. {
  237. struct _starpu_data_request *r;
  238. r = replicate->request[node];
  239. if (r)
  240. {
  241. _starpu_spin_lock(&r->lock);
  242. /* perhaps we need to "upgrade" the request */
  243. if (is_prefetch < r->prefetch)
  244. _starpu_update_prefetch_status(r);
  245. if (mode & STARPU_R)
  246. {
  247. /* in case the exisiting request did not imply a memory
  248. * transfer yet, we have to increment the refcnt now
  249. * (so that the source remains valid) */
  250. if (!(r->mode & STARPU_R))
  251. {
  252. replicate->refcnt++;
  253. replicate->handle->busy_count++;
  254. }
  255. r->mode = (enum starpu_access_mode) ((int) r->mode | (int) STARPU_R);
  256. }
  257. if (mode & STARPU_W)
  258. r->mode = (enum starpu_access_mode) ((int) r->mode | (int) STARPU_W);
  259. }
  260. return r;
  261. }
  262. /*
  263. * This function is called when the data is needed on the local node, this
  264. * returns a pointer to the local copy
  265. *
  266. * R STARPU_W STARPU_RW
  267. * Owner OK OK OK
  268. * Shared OK 1 1
  269. * Invalid 2 3 4
  270. *
  271. * case 1 : shared + (read)write :
  272. * no data copy but shared->Invalid/Owner
  273. * case 2 : invalid + read :
  274. * data copy + invalid->shared + owner->shared (STARPU_ASSERT(there is a valid))
  275. * case 3 : invalid + write :
  276. * no data copy + invalid->owner + (owner,shared)->invalid
  277. * case 4 : invalid + R/STARPU_W :
  278. * data copy + if (STARPU_W) (invalid->owner + owner->invalid)
  279. * else (invalid,owner->shared)
  280. */
  281. /* This function is called with handle's header lock taken */
  282. struct _starpu_data_request *_starpu_create_request_to_fetch_data(starpu_data_handle_t handle,
  283. struct _starpu_data_replicate *dst_replicate,
  284. enum starpu_access_mode mode, unsigned is_prefetch,
  285. void (*callback_func)(void *), void *callback_arg)
  286. {
  287. unsigned requesting_node = dst_replicate->memory_node;
  288. if (dst_replicate->state != STARPU_INVALID)
  289. {
  290. #ifdef STARPU_MEMORY_STATUS
  291. enum _starpu_cache_state old_state = dst_replicate->state;
  292. #endif
  293. /* the data is already available so we can stop */
  294. _starpu_update_data_state(handle, dst_replicate, mode);
  295. _starpu_msi_cache_hit(requesting_node);
  296. #ifdef STARPU_MEMORY_STATUS
  297. _starpu_handle_stats_cache_hit(handle, requesting_node);
  298. /* XXX Broken ? */
  299. if (old_state == STARPU_SHARED
  300. && dst_replicate->state == STARPU_OWNER)
  301. _starpu_handle_stats_shared_to_owner(handle, requesting_node);
  302. #endif
  303. _starpu_memchunk_recently_used(dst_replicate->mc, requesting_node);
  304. _starpu_spin_unlock(&handle->header_lock);
  305. if (callback_func)
  306. callback_func(callback_arg);
  307. _STARPU_LOG_OUT_TAG("data available");
  308. return NULL;
  309. }
  310. _starpu_msi_cache_miss(requesting_node);
  311. /* the only remaining situation is that the local copy was invalid */
  312. STARPU_ASSERT(dst_replicate->state == STARPU_INVALID);
  313. /* find someone who already has the data */
  314. uint32_t src_node = 0;
  315. /* if the data is in write only mode, there is no need for a source */
  316. if (mode & STARPU_R)
  317. {
  318. src_node = _starpu_select_src_node(handle, requesting_node);
  319. STARPU_ASSERT(src_node != requesting_node);
  320. }
  321. /* We can safely assume that there won't be more than 2 hops in the
  322. * current implementation */
  323. unsigned src_nodes[4], dst_nodes[4], handling_nodes[4];
  324. int nhops = determine_request_path(handle, src_node, requesting_node, mode, 4,
  325. src_nodes, dst_nodes, handling_nodes);
  326. STARPU_ASSERT(nhops >= 1 && nhops <= 4);
  327. struct _starpu_data_request *requests[nhops];
  328. /* Did we reuse a request for that hop ? */
  329. int reused_requests[nhops];
  330. /* Construct an array with a list of requests, possibly reusing existing requests */
  331. int hop;
  332. for (hop = 0; hop < nhops; hop++)
  333. {
  334. struct _starpu_data_request *r;
  335. unsigned hop_src_node = src_nodes[hop];
  336. unsigned hop_dst_node = dst_nodes[hop];
  337. unsigned hop_handling_node = handling_nodes[hop];
  338. struct _starpu_data_replicate *hop_src_replicate;
  339. struct _starpu_data_replicate *hop_dst_replicate;
  340. /* Only the first request is independant */
  341. unsigned ndeps = (hop == 0)?0:1;
  342. hop_src_replicate = &handle->per_node[hop_src_node];
  343. hop_dst_replicate = (hop != nhops - 1)?&handle->per_node[hop_dst_node]:dst_replicate;
  344. /* Try to reuse a request if possible */
  345. r = _starpu_search_existing_data_request(hop_dst_replicate,
  346. (mode & STARPU_R)?hop_src_node:hop_dst_node,
  347. mode, is_prefetch);
  348. reused_requests[hop] = !!r;
  349. if (!r)
  350. {
  351. /* Create a new request if there was no request to reuse */
  352. r = _starpu_create_data_request(handle, hop_src_replicate,
  353. hop_dst_replicate, hop_handling_node,
  354. mode, ndeps, is_prefetch);
  355. }
  356. requests[hop] = r;
  357. }
  358. /* Chain these requests */
  359. for (hop = 0; hop < nhops; hop++)
  360. {
  361. struct _starpu_data_request *r;
  362. r = requests[hop];
  363. if (hop != nhops - 1)
  364. {
  365. if (!reused_requests[hop + 1])
  366. r->next_req[r->next_req_count++] = requests[hop + 1];
  367. }
  368. else
  369. _starpu_data_request_append_callback(r, callback_func, callback_arg);
  370. if (reused_requests[hop])
  371. _starpu_spin_unlock(&r->lock);
  372. }
  373. if (!is_prefetch)
  374. requests[nhops - 1]->refcnt++;
  375. /* we only submit the first request, the remaining will be
  376. * automatically submitted afterward */
  377. if (!reused_requests[0])
  378. _starpu_post_data_request(requests[0], handling_nodes[0]);
  379. return requests[nhops - 1];
  380. }
  381. int _starpu_fetch_data_on_node(starpu_data_handle_t handle, struct _starpu_data_replicate *dst_replicate,
  382. enum starpu_access_mode mode, unsigned is_prefetch,
  383. void (*callback_func)(void *), void *callback_arg)
  384. {
  385. uint32_t local_node = _starpu_get_local_memory_node();
  386. _STARPU_LOG_IN();
  387. while (_starpu_spin_trylock(&handle->header_lock))
  388. _starpu_datawizard_progress(local_node, 1);
  389. if (!is_prefetch)
  390. {
  391. dst_replicate->refcnt++;
  392. dst_replicate->handle->busy_count++;
  393. }
  394. struct _starpu_data_request *r;
  395. r = _starpu_create_request_to_fetch_data(handle, dst_replicate, mode,
  396. is_prefetch, callback_func, callback_arg);
  397. /* If no request was created, the handle was already up-to-date on the
  398. * node. In this case, _starpu_create_request_to_fetch_data has already
  399. * unlocked the header. */
  400. if (!r)
  401. return 0;
  402. _starpu_spin_unlock(&handle->header_lock);
  403. int ret = is_prefetch?0:_starpu_wait_data_request_completion(r, 1);
  404. _STARPU_LOG_OUT();
  405. return ret;
  406. }
  407. static int prefetch_data_on_node(starpu_data_handle_t handle, struct _starpu_data_replicate *replicate, enum starpu_access_mode mode)
  408. {
  409. return _starpu_fetch_data_on_node(handle, replicate, mode, 1, NULL, NULL);
  410. }
  411. static int fetch_data(starpu_data_handle_t handle, struct _starpu_data_replicate *replicate, enum starpu_access_mode mode)
  412. {
  413. return _starpu_fetch_data_on_node(handle, replicate, mode, 0, NULL, NULL);
  414. }
  415. uint32_t _starpu_get_data_refcnt(starpu_data_handle_t handle, uint32_t node)
  416. {
  417. return handle->per_node[node].refcnt;
  418. }
  419. size_t _starpu_data_get_size(starpu_data_handle_t handle)
  420. {
  421. return handle->data_size;
  422. }
  423. uint32_t _starpu_data_get_footprint(starpu_data_handle_t handle)
  424. {
  425. return handle->footprint;
  426. }
  427. /* in case the data was accessed on a write mode, do not forget to
  428. * make it accessible again once it is possible ! */
  429. void _starpu_release_data_on_node(starpu_data_handle_t handle, uint32_t default_wt_mask, struct _starpu_data_replicate *replicate)
  430. {
  431. uint32_t wt_mask;
  432. wt_mask = default_wt_mask | handle->wt_mask;
  433. wt_mask &= (1<<starpu_memory_nodes_get_count())-1;
  434. /* Note that it is possible that there is no valid copy of the data (if
  435. * starpu_data_invalidate was called for instance). In that case, we do
  436. * not enforce any write-through mechanism. */
  437. unsigned memory_node = replicate->memory_node;
  438. if (replicate->state != STARPU_INVALID && handle->current_mode & STARPU_W)
  439. if ((wt_mask & ~(1<<memory_node)))
  440. _starpu_write_through_data(handle, memory_node, wt_mask);
  441. uint32_t local_node = _starpu_get_local_memory_node();
  442. while (_starpu_spin_trylock(&handle->header_lock))
  443. _starpu_datawizard_progress(local_node, 1);
  444. replicate->refcnt--;
  445. STARPU_ASSERT(replicate->refcnt >= 0);
  446. STARPU_ASSERT(handle->busy_count > 0);
  447. handle->busy_count--;
  448. _starpu_data_check_not_busy(handle);
  449. /* In case there was a temporary handle (eg. used for reduction), this
  450. * handle may have requested to be destroyed when the data is released
  451. * */
  452. unsigned handle_was_destroyed = handle->lazy_unregister;
  453. _starpu_notify_data_dependencies(handle);
  454. if (!handle_was_destroyed)
  455. _starpu_spin_unlock(&handle->header_lock);
  456. }
  457. static void _starpu_set_data_requested_flag_if_needed(struct _starpu_data_replicate *replicate)
  458. {
  459. // XXX : this is just a hint, so we don't take the lock ...
  460. // _STARPU_PTHREAD_SPIN_LOCK(&handle->header_lock);
  461. if (replicate->state == STARPU_INVALID)
  462. {
  463. unsigned dst_node = replicate->memory_node;
  464. replicate->requested[dst_node] = 1;
  465. }
  466. // _STARPU_PTHREAD_SPIN_UNLOCK(&handle->header_lock);
  467. }
  468. int starpu_prefetch_task_input_on_node(struct starpu_task *task, uint32_t node)
  469. {
  470. unsigned nbuffers = task->cl->nbuffers;
  471. unsigned index;
  472. for (index = 0; index < nbuffers; index++)
  473. {
  474. starpu_data_handle_t handle = task->handles[index];
  475. enum starpu_access_mode mode = task->cl->modes[index];
  476. if (mode & (STARPU_SCRATCH|STARPU_REDUX))
  477. continue;
  478. struct _starpu_data_replicate *replicate = &handle->per_node[node];
  479. prefetch_data_on_node(handle, replicate, mode);
  480. _starpu_set_data_requested_flag_if_needed(replicate);
  481. }
  482. return 0;
  483. }
  484. static struct _starpu_data_replicate *get_replicate(starpu_data_handle_t handle, enum starpu_access_mode mode, int workerid, unsigned local_memory_node)
  485. {
  486. if (mode & (STARPU_SCRATCH|STARPU_REDUX))
  487. return &handle->per_worker[workerid];
  488. else
  489. /* That's a "normal" buffer (R/W) */
  490. return &handle->per_node[local_memory_node];
  491. }
  492. int _starpu_fetch_task_input(struct _starpu_job *j, uint32_t mask)
  493. {
  494. _STARPU_TRACE_START_FETCH_INPUT(NULL);
  495. int profiling = starpu_profiling_status_get();
  496. struct starpu_task *task = j->task;
  497. if (profiling && task->profiling_info)
  498. _starpu_clock_gettime(&task->profiling_info->acquire_data_start_time);
  499. struct starpu_buffer_descr *descrs = j->ordered_buffers;
  500. unsigned nbuffers = task->cl->nbuffers;
  501. unsigned local_memory_node = _starpu_get_local_memory_node();
  502. int workerid = starpu_worker_get_id();
  503. unsigned index;
  504. for (index = 0; index < nbuffers; index++)
  505. {
  506. int ret;
  507. starpu_data_handle_t handle = descrs[index].handle;
  508. enum starpu_access_mode mode = descrs[index].mode;
  509. struct _starpu_data_replicate *local_replicate;
  510. if (index && descrs[index-1].handle == descrs[index].handle)
  511. /* We have already released this data, skip it. This
  512. * depends on ordering putting writes before reads, see
  513. * _starpu_compar_handles */
  514. continue;
  515. local_replicate = get_replicate(handle, mode, workerid, local_memory_node);
  516. ret = fetch_data(handle, local_replicate, mode);
  517. if (STARPU_UNLIKELY(ret))
  518. goto enomem;
  519. }
  520. /* Now that we have taken the data locks in locking order, fill the codelet interfaces in function order. */
  521. for (index = 0; index < nbuffers; index++)
  522. {
  523. starpu_data_handle_t handle = task->handles[index];
  524. enum starpu_access_mode mode = task->cl->modes[index];
  525. struct _starpu_data_replicate *local_replicate;
  526. local_replicate = get_replicate(handle, mode, workerid, local_memory_node);
  527. task->interfaces[index] = local_replicate->data_interface;
  528. if (mode & STARPU_REDUX)
  529. {
  530. /* If the replicate was not initialized yet, we have to do it now */
  531. if (!local_replicate->initialized)
  532. _starpu_redux_init_data_replicate(handle, local_replicate, workerid);
  533. }
  534. }
  535. if (profiling && task->profiling_info)
  536. _starpu_clock_gettime(&task->profiling_info->acquire_data_end_time);
  537. _STARPU_TRACE_END_FETCH_INPUT(NULL);
  538. return 0;
  539. enomem:
  540. /* try to unreference all the input that were successfully taken */
  541. /* XXX broken ... */
  542. _STARPU_DISP("something went wrong with buffer %u\n", index);
  543. //push_codelet_output(task, index, mask);
  544. _starpu_push_task_output(j, mask);
  545. return -1;
  546. }
  547. void _starpu_push_task_output(struct _starpu_job *j, uint32_t mask)
  548. {
  549. _STARPU_TRACE_START_PUSH_OUTPUT(NULL);
  550. int profiling = starpu_profiling_status_get();
  551. struct starpu_task *task = j->task;
  552. if (profiling && task->profiling_info)
  553. _starpu_clock_gettime(&task->profiling_info->release_data_start_time);
  554. struct starpu_buffer_descr *descrs = j->ordered_buffers;
  555. unsigned nbuffers = task->cl->nbuffers;
  556. int workerid = starpu_worker_get_id();
  557. unsigned local_memory_node = _starpu_get_local_memory_node();
  558. unsigned index;
  559. for (index = 0; index < nbuffers; index++)
  560. {
  561. starpu_data_handle_t handle = descrs[index].handle;
  562. enum starpu_access_mode mode = descrs[index].mode;
  563. struct _starpu_data_replicate *local_replicate;
  564. if (index && descrs[index-1].handle == descrs[index].handle)
  565. /* We have already released this data, skip it. This
  566. * depends on ordering putting writes before reads, see
  567. * _starpu_compar_handles */
  568. continue;
  569. local_replicate = get_replicate(handle, mode, workerid, local_memory_node);
  570. /* In case there was a temporary handle (eg. used for
  571. * reduction), this handle may have requested to be destroyed
  572. * when the data is released
  573. * */
  574. unsigned handle_was_destroyed = handle->lazy_unregister;
  575. _starpu_release_data_on_node(handle, mask, local_replicate);
  576. if (!handle_was_destroyed)
  577. _starpu_release_data_enforce_sequential_consistency(task, handle);
  578. }
  579. if (profiling && task->profiling_info)
  580. _starpu_clock_gettime(&task->profiling_info->release_data_end_time);
  581. _STARPU_TRACE_END_PUSH_OUTPUT(NULL);
  582. }
  583. /* NB : this value can only be an indication of the status of a data
  584. at some point, but there is no strong garantee ! */
  585. unsigned _starpu_is_data_present_or_requested(starpu_data_handle_t handle, uint32_t node)
  586. {
  587. unsigned ret = 0;
  588. // XXX : this is just a hint, so we don't take the lock ...
  589. // _STARPU_PTHREAD_SPIN_LOCK(&handle->header_lock);
  590. if (handle->per_node[node].state != STARPU_INVALID)
  591. {
  592. ret = 1;
  593. }
  594. else
  595. {
  596. unsigned i;
  597. unsigned nnodes = starpu_memory_nodes_get_count();
  598. for (i = 0; i < nnodes; i++)
  599. {
  600. if (handle->per_node[node].requested[i] || handle->per_node[node].request[i])
  601. ret = 1;
  602. }
  603. }
  604. // _STARPU_PTHREAD_SPIN_UNLOCK(&handle->header_lock);
  605. return ret;
  606. }