coherency.c 24 KB

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