coherency.c 24 KB

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