coherency.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures. *
  2. * Copyright (C) 2009-2017 Université de Bordeaux
  3. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016 CNRS
  4. * Copyright (C) 2014 INRIA
  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 <datawizard/memory_nodes.h>
  22. #include <core/dependencies/data_concurrency.h>
  23. #include <core/disk.h>
  24. #include <profiling/profiling.h>
  25. #include <math.h>
  26. #include <core/task.h>
  27. #include <starpu_scheduler.h>
  28. #include <core/workers.h>
  29. #include <limits.h>
  30. #ifdef STARPU_SIMGRID
  31. #include <core/simgrid.h>
  32. #endif
  33. static int link_supports_direct_transfers(starpu_data_handle_t handle, unsigned src_node, unsigned dst_node, unsigned *handling_node);
  34. int _starpu_select_src_node(starpu_data_handle_t handle, unsigned destination)
  35. {
  36. int src_node = -1;
  37. unsigned i;
  38. unsigned nnodes = starpu_memory_nodes_get_count();
  39. /* first find a valid copy, either a STARPU_OWNER or a STARPU_SHARED */
  40. unsigned node;
  41. size_t size = _starpu_data_get_size(handle);
  42. double cost = INFINITY;
  43. unsigned src_node_mask = 0;
  44. for (node = 0; node < nnodes; node++)
  45. {
  46. if (handle->per_node[node].state != STARPU_INVALID)
  47. {
  48. /* we found a copy ! */
  49. src_node_mask |= (1<<node);
  50. }
  51. }
  52. if (src_node_mask == 0 && handle->init_cl)
  53. {
  54. /* No copy yet, but applicationg told us how to build it. */
  55. return -1;
  56. }
  57. /* we should have found at least one copy ! */
  58. STARPU_ASSERT_MSG(src_node_mask != 0, "The data for the handle %p is requested, but the handle does not have a valid value. Perhaps some initialization task is missing?", handle);
  59. /* Without knowing the size, we won't know the cost */
  60. if (!size)
  61. cost = 0;
  62. /* Check whether we have transfer cost for all nodes, if so, take the minimum */
  63. if (cost)
  64. for (i = 0; i < nnodes; i++)
  65. {
  66. if (src_node_mask & (1<<i))
  67. {
  68. double time = starpu_transfer_predict(i, destination, size);
  69. unsigned handling_node;
  70. /* Avoid indirect transfers */
  71. if (!link_supports_direct_transfers(handle, i, destination, &handling_node))
  72. continue;
  73. if (_STARPU_IS_ZERO(time))
  74. {
  75. /* No estimation, will have to revert to dumb strategy */
  76. cost = 0.0;
  77. break;
  78. }
  79. else if (time < cost)
  80. {
  81. cost = time;
  82. src_node = i;
  83. }
  84. }
  85. }
  86. if (cost && src_node != -1)
  87. {
  88. /* Could estimate through cost, return that */
  89. STARPU_ASSERT(handle->per_node[src_node].allocated);
  90. STARPU_ASSERT(handle->per_node[src_node].initialized);
  91. return src_node;
  92. }
  93. int i_ram = -1;
  94. int i_gpu = -1;
  95. int i_disk = -1;
  96. /* Revert to dumb strategy: take RAM unless only a GPU has it */
  97. for (i = 0; i < nnodes; i++)
  98. {
  99. if (src_node_mask & (1<<i))
  100. {
  101. int (*can_copy)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, unsigned handling_node) = handle->ops->copy_methods->can_copy;
  102. /* Avoid transfers which the interface does not want */
  103. if (can_copy)
  104. {
  105. void *src_interface = handle->per_node[i].data_interface;
  106. void *dst_interface = handle->per_node[destination].data_interface;
  107. unsigned handling_node;
  108. if (!link_supports_direct_transfers(handle, i, destination, &handling_node))
  109. {
  110. /* Avoid through RAM if the interface does not want it */
  111. void *ram_interface = handle->per_node[STARPU_MAIN_RAM].data_interface;
  112. if ((!can_copy(src_interface, i, ram_interface, STARPU_MAIN_RAM, i)
  113. && !can_copy(src_interface, i, ram_interface, STARPU_MAIN_RAM, STARPU_MAIN_RAM))
  114. || (!can_copy(ram_interface, STARPU_MAIN_RAM, dst_interface, destination, STARPU_MAIN_RAM)
  115. && !can_copy(ram_interface, STARPU_MAIN_RAM, dst_interface, destination, destination)))
  116. continue;
  117. }
  118. }
  119. /* however GPU are expensive sources, really !
  120. * Unless peer transfer is supported (and it would then have been selected above).
  121. * Other should be ok */
  122. if (starpu_node_get_kind(i) == STARPU_CUDA_RAM ||
  123. starpu_node_get_kind(i) == STARPU_OPENCL_RAM ||
  124. starpu_node_get_kind(i) == STARPU_MIC_RAM)
  125. i_gpu = i;
  126. if (starpu_node_get_kind(i) == STARPU_CPU_RAM ||
  127. starpu_node_get_kind(i) == STARPU_SCC_RAM ||
  128. starpu_node_get_kind(i) == STARPU_SCC_SHM ||
  129. starpu_node_get_kind(i) == STARPU_MPI_MS_RAM)
  130. i_ram = i;
  131. if (starpu_node_get_kind(i) == STARPU_DISK_RAM)
  132. i_disk = i;
  133. }
  134. }
  135. /* we have to use cpu_ram in first */
  136. if (i_ram != -1)
  137. src_node = i_ram;
  138. /* no luck we have to use the disk memory */
  139. else if (i_gpu != -1)
  140. src_node = i_gpu;
  141. else
  142. src_node = i_disk;
  143. STARPU_ASSERT(src_node != -1);
  144. STARPU_ASSERT(handle->per_node[src_node].allocated);
  145. STARPU_ASSERT(handle->per_node[src_node].initialized);
  146. return src_node;
  147. }
  148. /* this may be called once the data is fetched with header and STARPU_RW-lock hold */
  149. void _starpu_update_data_state(starpu_data_handle_t handle,
  150. struct _starpu_data_replicate *requesting_replicate,
  151. enum starpu_data_access_mode mode)
  152. {
  153. /* There is nothing to do for relaxed coherency modes (scratch or
  154. * reductions) */
  155. if (!(mode & STARPU_RW))
  156. return;
  157. unsigned nnodes = starpu_memory_nodes_get_count();
  158. /* the data is present now */
  159. unsigned requesting_node = requesting_replicate->memory_node;
  160. requesting_replicate->requested &= ~(1UL << requesting_node);
  161. if (mode & STARPU_W)
  162. {
  163. /* the requesting node now has the only valid copy */
  164. unsigned node;
  165. for (node = 0; node < nnodes; node++)
  166. {
  167. _STARPU_TRACE_DATA_INVALIDATE(handle, node);
  168. handle->per_node[node].state = STARPU_INVALID;
  169. }
  170. requesting_replicate->state = STARPU_OWNER;
  171. if (handle->home_node != -1 && handle->per_node[handle->home_node].state == STARPU_INVALID)
  172. /* Notify that this MC is now dirty */
  173. _starpu_memchunk_dirty(requesting_replicate->mc, requesting_replicate->memory_node);
  174. }
  175. else
  176. { /* read only */
  177. if (requesting_replicate->state != STARPU_OWNER)
  178. {
  179. /* there was at least another copy of the data */
  180. unsigned node;
  181. for (node = 0; node < nnodes; node++)
  182. {
  183. struct _starpu_data_replicate *replicate = &handle->per_node[node];
  184. if (replicate->state != STARPU_INVALID)
  185. replicate->state = STARPU_SHARED;
  186. }
  187. requesting_replicate->state = STARPU_SHARED;
  188. }
  189. }
  190. }
  191. static int worker_supports_direct_access(unsigned node, unsigned handling_node)
  192. {
  193. /* only support disk <-> ram and disk <-> disk */
  194. if (starpu_node_get_kind(node) == STARPU_DISK_RAM || starpu_node_get_kind(handling_node) == STARPU_DISK_RAM)
  195. return 0;
  196. if (node == handling_node)
  197. return 1;
  198. if (!_starpu_memory_node_get_nworkers(handling_node))
  199. /* No worker to process the request from that node */
  200. return 0;
  201. int type = starpu_node_get_kind(node);
  202. switch (type)
  203. {
  204. case STARPU_CUDA_RAM:
  205. {
  206. /* GPUs not always allow direct remote access: if CUDA4
  207. * is enabled, we allow two CUDA devices to communicate. */
  208. #ifdef STARPU_SIMGRID
  209. if (starpu_node_get_kind(handling_node) == STARPU_CUDA_RAM)
  210. {
  211. msg_host_t host = _starpu_simgrid_get_memnode_host(handling_node);
  212. const char* cuda_memcpy_peer = MSG_host_get_property_value(host, "memcpy_peer");
  213. return cuda_memcpy_peer && atoll(cuda_memcpy_peer);
  214. }
  215. else
  216. return 0;
  217. #elif defined(HAVE_CUDA_MEMCPY_PEER)
  218. /* simgrid */
  219. enum starpu_node_kind kind = starpu_node_get_kind(handling_node);
  220. return kind == STARPU_CUDA_RAM;
  221. #else /* HAVE_CUDA_MEMCPY_PEER */
  222. /* Direct GPU-GPU transfers are not allowed in general */
  223. return 0;
  224. #endif /* HAVE_CUDA_MEMCPY_PEER */
  225. }
  226. case STARPU_OPENCL_RAM:
  227. return 0;
  228. case STARPU_MIC_RAM:
  229. /* TODO: We don't handle direct MIC-MIC transfers yet */
  230. return 0;
  231. case STARPU_MPI_MS_RAM:
  232. {
  233. enum starpu_node_kind kind = starpu_node_get_kind(handling_node);
  234. return kind == STARPU_MPI_MS_RAM;
  235. }
  236. case STARPU_SCC_RAM:
  237. return 1;
  238. default:
  239. return 1;
  240. }
  241. }
  242. static int link_supports_direct_transfers(starpu_data_handle_t handle, unsigned src_node, unsigned dst_node, unsigned *handling_node)
  243. {
  244. int (*can_copy)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, unsigned handling_node) = handle->ops->copy_methods->can_copy;
  245. void *src_interface = handle->per_node[src_node].data_interface;
  246. void *dst_interface = handle->per_node[dst_node].data_interface;
  247. /* XXX That's a hack until we fix cudaMemcpy3DPeerAsync in the block interface
  248. * Perhaps not all data interface provide a direct GPU-GPU transfer
  249. * method ! */
  250. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  251. if (src_node != dst_node && starpu_node_get_kind(src_node) == STARPU_CUDA_RAM && starpu_node_get_kind(dst_node) == STARPU_CUDA_RAM)
  252. {
  253. const struct starpu_data_copy_methods *copy_methods = handle->ops->copy_methods;
  254. if (!copy_methods->cuda_to_cuda_async && !copy_methods->any_to_any)
  255. return 0;
  256. }
  257. #endif
  258. /* Note: with CUDA, performance seems a bit better when issuing the transfer from the destination (tested without GPUDirect, but GPUDirect probably behave the same) */
  259. if (worker_supports_direct_access(src_node, dst_node) && (!can_copy || can_copy(src_interface, src_node, dst_interface, dst_node, dst_node)))
  260. {
  261. *handling_node = dst_node;
  262. return 1;
  263. }
  264. if (worker_supports_direct_access(dst_node, src_node) && (!can_copy || can_copy(src_interface, src_node, dst_interface, dst_node, src_node)))
  265. {
  266. *handling_node = src_node;
  267. return 1;
  268. }
  269. /* Link between disk and ram */
  270. if ((starpu_node_get_kind(src_node) == STARPU_DISK_RAM && starpu_node_get_kind(dst_node) == STARPU_CPU_RAM) ||
  271. (starpu_node_get_kind(src_node) == STARPU_CPU_RAM && starpu_node_get_kind(dst_node) == STARPU_DISK_RAM))
  272. {
  273. /* FIXME: not necessarily a worker :/ */
  274. *handling_node = STARPU_MAIN_RAM;
  275. return 1;
  276. }
  277. /* link between disk and disk, and they have the same kind */
  278. if (_starpu_is_same_kind_disk(src_node, dst_node))
  279. return 1;
  280. return 0;
  281. }
  282. /* Determines the path of a request : each hop is defined by (src,dst) and the
  283. * node that handles the hop. The returned value indicates the number of hops,
  284. * and the max_len is the maximum number of hops (ie. the size of the
  285. * src_nodes, dst_nodes and handling_nodes arrays. */
  286. static int determine_request_path(starpu_data_handle_t handle,
  287. int src_node, int dst_node,
  288. enum starpu_data_access_mode mode, int max_len,
  289. unsigned *src_nodes, unsigned *dst_nodes,
  290. unsigned *handling_nodes, unsigned write_invalidation)
  291. {
  292. if (src_node == dst_node || !(mode & STARPU_R))
  293. {
  294. if (write_invalidation)
  295. /* The invalidation request will be enough */
  296. return 0;
  297. /* The destination node should only allocate the data, no transfer is required */
  298. STARPU_ASSERT(max_len >= 1);
  299. src_nodes[0] = STARPU_MAIN_RAM; // ignored
  300. dst_nodes[0] = dst_node;
  301. handling_nodes[0] = dst_node;
  302. return 1;
  303. }
  304. unsigned handling_node;
  305. int link_is_valid = link_supports_direct_transfers(handle, src_node, dst_node, &handling_node);
  306. /* TODO: NUMA nodes */
  307. if (!link_is_valid)
  308. {
  309. int (*can_copy)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, unsigned handling_node) = handle->ops->copy_methods->can_copy;
  310. void *src_interface = handle->per_node[src_node].data_interface;
  311. void *dst_interface = handle->per_node[dst_node].data_interface;
  312. /* We need an intermediate hop to implement data staging
  313. * through main memory. */
  314. STARPU_ASSERT(max_len >= 2);
  315. STARPU_ASSERT(src_node >= 0);
  316. /* GPU -> RAM */
  317. src_nodes[0] = src_node;
  318. dst_nodes[0] = STARPU_MAIN_RAM;
  319. if (starpu_node_get_kind(src_node) == STARPU_DISK_RAM)
  320. /* Disks don't have their own driver thread */
  321. handling_nodes[0] = dst_node;
  322. else if (!can_copy || can_copy(src_interface, src_node, dst_interface, dst_node, src_node))
  323. {
  324. handling_nodes[0] = src_node;
  325. }
  326. else
  327. {
  328. STARPU_ASSERT_MSG(can_copy(src_interface, src_node, dst_interface, dst_node, dst_node), "interface %d refuses all kinds of transfers from node %d to node %d\n", handle->ops->interfaceid, src_node, dst_node);
  329. handling_nodes[0] = dst_node;
  330. }
  331. /* RAM -> GPU */
  332. src_nodes[1] = STARPU_MAIN_RAM;
  333. dst_nodes[1] = dst_node;
  334. if (starpu_node_get_kind(dst_node) == STARPU_DISK_RAM)
  335. /* Disks don't have their own driver thread */
  336. handling_nodes[1] = src_node;
  337. else if (!can_copy || can_copy(src_interface, src_node, dst_interface, dst_node, dst_node))
  338. {
  339. handling_nodes[1] = dst_node;
  340. }
  341. else
  342. {
  343. STARPU_ASSERT_MSG(can_copy(src_interface, src_node, dst_interface, dst_node, src_node), "interface %d refuses all kinds of transfers from node %d to node %d\n", handle->ops->interfaceid, src_node, dst_node);
  344. handling_nodes[1] = src_node;
  345. }
  346. return 2;
  347. }
  348. else
  349. {
  350. STARPU_ASSERT(max_len >= 1);
  351. src_nodes[0] = src_node;
  352. dst_nodes[0] = dst_node;
  353. handling_nodes[0] = handling_node;
  354. #if !defined(HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID)
  355. STARPU_ASSERT(!(mode & STARPU_R) || starpu_node_get_kind(src_node) != STARPU_CUDA_RAM || starpu_node_get_kind(dst_node) != STARPU_CUDA_RAM);
  356. #endif
  357. return 1;
  358. }
  359. }
  360. /* handle->lock should be taken. r is returned locked. The node parameter
  361. * indicate either the source of the request, or the destination for a
  362. * write-only request. */
  363. 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)
  364. {
  365. struct _starpu_data_request *r;
  366. r = replicate->request[node];
  367. if (r)
  368. {
  369. _starpu_spin_checklocked(&r->handle->header_lock);
  370. _starpu_spin_lock(&r->lock);
  371. /* perhaps we need to "upgrade" the request */
  372. if (is_prefetch < r->prefetch)
  373. _starpu_update_prefetch_status(r, is_prefetch);
  374. if (mode & STARPU_R)
  375. {
  376. /* in case the exisiting request did not imply a memory
  377. * transfer yet, we have to take a second refcnt now
  378. * for the source, in addition to the refcnt for the
  379. * destination
  380. * (so that the source remains valid) */
  381. if (!(r->mode & STARPU_R))
  382. {
  383. replicate->refcnt++;
  384. replicate->handle->busy_count++;
  385. }
  386. r->mode = (enum starpu_data_access_mode) ((int) r->mode | (int) STARPU_R);
  387. }
  388. if (mode & STARPU_W)
  389. r->mode = (enum starpu_data_access_mode) ((int) r->mode | (int) STARPU_W);
  390. }
  391. return r;
  392. }
  393. /*
  394. * This function is called when the data is needed on the local node, this
  395. * returns a pointer to the local copy
  396. *
  397. * R STARPU_W STARPU_RW
  398. * Owner OK OK OK
  399. * Shared OK 1 1
  400. * Invalid 2 3 4
  401. *
  402. * case 1 : shared + (read)write :
  403. * no data copy but shared->Invalid/Owner
  404. * case 2 : invalid + read :
  405. * data copy + invalid->shared + owner->shared (STARPU_ASSERT(there is a valid))
  406. * case 3 : invalid + write :
  407. * no data copy + invalid->owner + (owner,shared)->invalid
  408. * case 4 : invalid + R/STARPU_W :
  409. * data copy + if (STARPU_W) (invalid->owner + owner->invalid)
  410. * else (invalid,owner->shared)
  411. */
  412. struct _starpu_data_request *_starpu_create_request_to_fetch_data(starpu_data_handle_t handle,
  413. struct _starpu_data_replicate *dst_replicate,
  414. enum starpu_data_access_mode mode, unsigned is_prefetch,
  415. unsigned async,
  416. void (*callback_func)(void *), void *callback_arg, int prio, const char *origin)
  417. {
  418. /* We don't care about commuting for data requests, that was handled before. */
  419. mode &= ~STARPU_COMMUTE;
  420. /* This function is called with handle's header lock taken */
  421. _starpu_spin_checklocked(&handle->header_lock);
  422. int requesting_node = dst_replicate ? dst_replicate->memory_node : -1;
  423. unsigned nwait = 0;
  424. if (mode & STARPU_W)
  425. {
  426. /* We will write to the buffer. We will have to wait for all
  427. * existing requests before the last request which will
  428. * invalidate all their results (which were possibly spurious,
  429. * e.g. too aggressive eviction).
  430. */
  431. unsigned i, j;
  432. unsigned nnodes = starpu_memory_nodes_get_count();
  433. for (i = 0; i < nnodes; i++)
  434. for (j = 0; j < nnodes; j++)
  435. if (handle->per_node[i].request[j])
  436. nwait++;
  437. /* If the request is not detached (i.e. the caller really wants
  438. * proper ownership), no new requests will appear because a
  439. * reference will be kept on the dst replicate, which will
  440. * notably prevent data reclaiming.
  441. */
  442. }
  443. if ((!dst_replicate || dst_replicate->state != STARPU_INVALID) && (!nwait || is_prefetch))
  444. {
  445. if (dst_replicate)
  446. {
  447. #ifdef STARPU_MEMORY_STATS
  448. enum _starpu_cache_state old_state = dst_replicate->state;
  449. #endif
  450. /* the data is already available and we don't have to wait for
  451. * any request, so we can stop */
  452. _starpu_update_data_state(handle, dst_replicate, mode);
  453. _starpu_msi_cache_hit(requesting_node);
  454. #ifdef STARPU_MEMORY_STATS
  455. _starpu_memory_handle_stats_cache_hit(handle, requesting_node);
  456. /* XXX Broken ? */
  457. if (old_state == STARPU_SHARED
  458. && dst_replicate->state == STARPU_OWNER)
  459. _starpu_memory_handle_stats_shared_to_owner(handle, requesting_node);
  460. #endif
  461. if (dst_replicate->mc)
  462. _starpu_memchunk_recently_used(dst_replicate->mc, requesting_node);
  463. }
  464. _starpu_spin_unlock(&handle->header_lock);
  465. if (callback_func)
  466. callback_func(callback_arg);
  467. _STARPU_LOG_OUT_TAG("data available");
  468. return NULL;
  469. }
  470. if (dst_replicate)
  471. _starpu_msi_cache_miss(requesting_node);
  472. /* the only remaining situation is that the local copy was invalid */
  473. STARPU_ASSERT((dst_replicate && dst_replicate->state == STARPU_INVALID) || nwait);
  474. /* find someone who already has the data */
  475. int src_node = -1;
  476. if (dst_replicate && mode & STARPU_R)
  477. {
  478. if (dst_replicate->state == STARPU_INVALID)
  479. src_node = _starpu_select_src_node(handle, requesting_node);
  480. else
  481. src_node = requesting_node;
  482. if (src_node < 0)
  483. {
  484. /* We will create it, no need to read an existing value */
  485. mode &= ~STARPU_R;
  486. }
  487. }
  488. else if (dst_replicate)
  489. {
  490. /* if the data is in write only mode (and not SCRATCH or REDUX), there is no need for a source, data will be initialized by the task itself */
  491. if (mode & STARPU_W)
  492. dst_replicate->initialized = 1;
  493. if (starpu_node_get_kind(requesting_node) == STARPU_CPU_RAM && !nwait)
  494. {
  495. /* And this is the main RAM, really no need for a
  496. * request, just allocate */
  497. if (_starpu_allocate_memory_on_node(handle, dst_replicate, is_prefetch) == 0)
  498. {
  499. _starpu_update_data_state(handle, dst_replicate, mode);
  500. _starpu_spin_unlock(&handle->header_lock);
  501. if (callback_func)
  502. callback_func(callback_arg);
  503. _STARPU_LOG_OUT_TAG("data immediately allocated");
  504. return NULL;
  505. }
  506. }
  507. }
  508. #define MAX_REQUESTS 4
  509. /* We can safely assume that there won't be more than 2 hops in the
  510. * current implementation */
  511. unsigned src_nodes[MAX_REQUESTS], dst_nodes[MAX_REQUESTS], handling_nodes[MAX_REQUESTS];
  512. /* keep one slot for the last W request, if any */
  513. int write_invalidation = (mode & STARPU_W) && nwait && !is_prefetch;
  514. int nhops = determine_request_path(handle, src_node, requesting_node, mode, MAX_REQUESTS,
  515. src_nodes, dst_nodes, handling_nodes, write_invalidation);
  516. STARPU_ASSERT(nhops >= 0 && nhops <= MAX_REQUESTS-1);
  517. struct _starpu_data_request *requests[nhops + write_invalidation];
  518. /* Did we reuse a request for that hop ? */
  519. int reused_requests[nhops + write_invalidation];
  520. /* Construct an array with a list of requests, possibly reusing existing requests */
  521. int hop;
  522. for (hop = 0; hop < nhops; hop++)
  523. {
  524. struct _starpu_data_request *r;
  525. unsigned hop_src_node = src_nodes[hop];
  526. unsigned hop_dst_node = dst_nodes[hop];
  527. unsigned hop_handling_node = handling_nodes[hop];
  528. struct _starpu_data_replicate *hop_src_replicate;
  529. struct _starpu_data_replicate *hop_dst_replicate;
  530. /* Only the first request is independant */
  531. unsigned ndeps = (hop == 0)?0:1;
  532. hop_src_replicate = &handle->per_node[hop_src_node];
  533. hop_dst_replicate = (hop != nhops - 1)?&handle->per_node[hop_dst_node]:dst_replicate;
  534. /* Try to reuse a request if possible */
  535. r = _starpu_search_existing_data_request(hop_dst_replicate,
  536. (mode & STARPU_R)?hop_src_node:hop_dst_node,
  537. mode, is_prefetch);
  538. reused_requests[hop] = !!r;
  539. if (!r)
  540. {
  541. /* Create a new request if there was no request to reuse */
  542. r = _starpu_create_data_request(handle, hop_src_replicate,
  543. hop_dst_replicate, hop_handling_node,
  544. mode, ndeps, is_prefetch, prio, 0, origin);
  545. nwait++;
  546. }
  547. requests[hop] = r;
  548. }
  549. /* Chain these requests */
  550. for (hop = 0; hop < nhops; hop++)
  551. {
  552. struct _starpu_data_request *r;
  553. r = requests[hop];
  554. if (hop != nhops - 1)
  555. {
  556. if (!reused_requests[hop + 1])
  557. {
  558. r->next_req[r->next_req_count++] = requests[hop + 1];
  559. STARPU_ASSERT(r->next_req_count <= STARPU_MAXNODES);
  560. }
  561. }
  562. else if (!write_invalidation)
  563. /* The last request will perform the callback after termination */
  564. _starpu_data_request_append_callback(r, callback_func, callback_arg);
  565. if (reused_requests[hop])
  566. _starpu_spin_unlock(&r->lock);
  567. }
  568. if (write_invalidation)
  569. {
  570. /* Some requests were still pending, we have to add yet another
  571. * request, depending on them, which will invalidate their
  572. * result.
  573. */
  574. struct _starpu_data_request *r = _starpu_create_data_request(handle, dst_replicate,
  575. dst_replicate, requesting_node,
  576. STARPU_W, nwait, is_prefetch, prio, 1, origin);
  577. /* and perform the callback after termination */
  578. _starpu_data_request_append_callback(r, callback_func, callback_arg);
  579. /* We will write to the buffer. We will have to wait for all
  580. * existing requests before the last request which will
  581. * invalidate all their results (which were possibly spurious,
  582. * e.g. too aggressive eviction).
  583. */
  584. unsigned i, j;
  585. unsigned nnodes = starpu_memory_nodes_get_count();
  586. for (i = 0; i < nnodes; i++)
  587. for (j = 0; j < nnodes; j++)
  588. {
  589. struct _starpu_data_request *r2 = handle->per_node[i].request[j];
  590. if (r2)
  591. {
  592. _starpu_spin_lock(&r2->lock);
  593. if (is_prefetch < r2->prefetch)
  594. /* Hasten the request we will have to wait for */
  595. _starpu_update_prefetch_status(r2, is_prefetch);
  596. r2->next_req[r2->next_req_count++] = r;
  597. STARPU_ASSERT(r2->next_req_count <= STARPU_MAXNODES + 1);
  598. _starpu_spin_unlock(&r2->lock);
  599. nwait--;
  600. }
  601. }
  602. STARPU_ASSERT(nwait == 0);
  603. nhops++;
  604. requests[nhops - 1] = r;
  605. /* existing requests will post this one */
  606. reused_requests[nhops - 1] = 1;
  607. }
  608. STARPU_ASSERT(nhops);
  609. if (!async)
  610. requests[nhops - 1]->refcnt++;
  611. /* we only submit the first request, the remaining will be
  612. * automatically submitted afterward */
  613. if (!reused_requests[0])
  614. _starpu_post_data_request(requests[0], handling_nodes[0]);
  615. return requests[nhops - 1];
  616. }
  617. int _starpu_fetch_data_on_node(starpu_data_handle_t handle, int node, struct _starpu_data_replicate *dst_replicate,
  618. enum starpu_data_access_mode mode, unsigned detached, unsigned is_prefetch, unsigned async,
  619. void (*callback_func)(void *), void *callback_arg, int prio, const char *origin)
  620. {
  621. _STARPU_LOG_IN();
  622. int cpt = 0;
  623. while (cpt < STARPU_SPIN_MAXTRY && _starpu_spin_trylock(&handle->header_lock))
  624. {
  625. cpt++;
  626. _starpu_datawizard_progress(1);
  627. }
  628. if (cpt == STARPU_SPIN_MAXTRY)
  629. _starpu_spin_lock(&handle->header_lock);
  630. if (!detached)
  631. {
  632. /* Take references which will be released by _starpu_release_data_on_node */
  633. if (dst_replicate)
  634. dst_replicate->refcnt++;
  635. else if (node == STARPU_ACQUIRE_NO_NODE_LOCK_ALL)
  636. {
  637. int i;
  638. for (i = 0; i < STARPU_MAXNODES; i++)
  639. handle->per_node[i].refcnt++;
  640. }
  641. handle->busy_count++;
  642. }
  643. struct _starpu_data_request *r;
  644. r = _starpu_create_request_to_fetch_data(handle, dst_replicate, mode,
  645. is_prefetch, async, callback_func, callback_arg, prio, origin);
  646. /* If no request was created, the handle was already up-to-date on the
  647. * node. In this case, _starpu_create_request_to_fetch_data has already
  648. * unlocked the header. */
  649. if (!r)
  650. return 0;
  651. _starpu_spin_unlock(&handle->header_lock);
  652. int ret = async?0:_starpu_wait_data_request_completion(r, 1);
  653. _STARPU_LOG_OUT();
  654. return ret;
  655. }
  656. static int idle_prefetch_data_on_node(starpu_data_handle_t handle, int node, struct _starpu_data_replicate *replicate, enum starpu_data_access_mode mode, int prio)
  657. {
  658. return _starpu_fetch_data_on_node(handle, node, replicate, mode, 1, 2, 1, NULL, NULL, prio, "idle_prefetch_data_on_node");
  659. }
  660. static int prefetch_data_on_node(starpu_data_handle_t handle, int node, struct _starpu_data_replicate *replicate, enum starpu_data_access_mode mode, int prio)
  661. {
  662. return _starpu_fetch_data_on_node(handle, node, replicate, mode, 1, 1, 1, NULL, NULL, prio, "prefetch_data_on_node");
  663. }
  664. static int fetch_data(starpu_data_handle_t handle, int node, struct _starpu_data_replicate *replicate, enum starpu_data_access_mode mode, int prio)
  665. {
  666. return _starpu_fetch_data_on_node(handle, node, replicate, mode, 0, 0, 0, NULL, NULL, prio, "fetch_data");
  667. }
  668. uint32_t _starpu_get_data_refcnt(starpu_data_handle_t handle, unsigned node)
  669. {
  670. return handle->per_node[node].refcnt;
  671. }
  672. size_t _starpu_data_get_size(starpu_data_handle_t handle)
  673. {
  674. return handle->ops->get_size(handle);
  675. }
  676. uint32_t _starpu_data_get_footprint(starpu_data_handle_t handle)
  677. {
  678. return handle->footprint;
  679. }
  680. /* in case the data was accessed on a write mode, do not forget to
  681. * make it accessible again once it is possible ! */
  682. void _starpu_release_data_on_node(starpu_data_handle_t handle, uint32_t default_wt_mask, struct _starpu_data_replicate *replicate)
  683. {
  684. uint32_t wt_mask;
  685. wt_mask = default_wt_mask | handle->wt_mask;
  686. wt_mask &= (1<<starpu_memory_nodes_get_count())-1;
  687. /* Note that it is possible that there is no valid copy of the data (if
  688. * starpu_data_invalidate was called for instance). In that case, we do
  689. * not enforce any write-through mechanism. */
  690. unsigned memory_node = replicate->memory_node;
  691. if (replicate->state != STARPU_INVALID && handle->current_mode & STARPU_W)
  692. if ((wt_mask & ~(1<<memory_node)))
  693. _starpu_write_through_data(handle, memory_node, wt_mask);
  694. int cpt = 0;
  695. while (cpt < STARPU_SPIN_MAXTRY && _starpu_spin_trylock(&handle->header_lock))
  696. {
  697. cpt++;
  698. _starpu_datawizard_progress(1);
  699. }
  700. if (cpt == STARPU_SPIN_MAXTRY)
  701. _starpu_spin_lock(&handle->header_lock);
  702. /* Release refcnt taken by fetch_data_on_node */
  703. replicate->refcnt--;
  704. STARPU_ASSERT_MSG(replicate->refcnt >= 0, "handle %p released too many times", handle);
  705. STARPU_ASSERT_MSG(handle->busy_count > 0, "handle %p released too many times", handle);
  706. handle->busy_count--;
  707. if (!_starpu_notify_data_dependencies(handle))
  708. _starpu_spin_unlock(&handle->header_lock);
  709. }
  710. static void _starpu_set_data_requested_flag_if_needed(starpu_data_handle_t handle, struct _starpu_data_replicate *replicate)
  711. {
  712. int cpt = 0;
  713. while (cpt < STARPU_SPIN_MAXTRY && _starpu_spin_trylock(&handle->header_lock))
  714. {
  715. cpt++;
  716. _starpu_datawizard_progress(1);
  717. }
  718. if (cpt == STARPU_SPIN_MAXTRY)
  719. _starpu_spin_lock(&handle->header_lock);
  720. if (replicate->state == STARPU_INVALID)
  721. {
  722. unsigned dst_node = replicate->memory_node;
  723. replicate->requested |= 1UL << dst_node;
  724. }
  725. _starpu_spin_unlock(&handle->header_lock);
  726. }
  727. int starpu_prefetch_task_input_on_node_prio(struct starpu_task *task, unsigned node, int prio)
  728. {
  729. STARPU_ASSERT(!task->prefetched);
  730. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  731. unsigned index;
  732. for (index = 0; index < nbuffers; index++)
  733. {
  734. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, index);
  735. enum starpu_data_access_mode mode = STARPU_TASK_GET_MODE(task, index);
  736. if (mode & (STARPU_SCRATCH|STARPU_REDUX))
  737. continue;
  738. struct _starpu_data_replicate *replicate = &handle->per_node[node];
  739. prefetch_data_on_node(handle, node, replicate, mode, prio);
  740. _starpu_set_data_requested_flag_if_needed(handle, replicate);
  741. }
  742. return 0;
  743. }
  744. int starpu_prefetch_task_input_on_node(struct starpu_task *task, unsigned node)
  745. {
  746. int prio = task->priority;
  747. if (task->workerorder)
  748. prio = INT_MAX - task->workerorder;
  749. return starpu_prefetch_task_input_on_node_prio(task, node, prio);
  750. }
  751. int starpu_idle_prefetch_task_input_on_node_prio(struct starpu_task *task, unsigned node, int prio)
  752. {
  753. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  754. unsigned index;
  755. for (index = 0; index < nbuffers; index++)
  756. {
  757. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, index);
  758. enum starpu_data_access_mode mode = STARPU_TASK_GET_MODE(task, index);
  759. if (mode & (STARPU_SCRATCH|STARPU_REDUX))
  760. continue;
  761. struct _starpu_data_replicate *replicate = &handle->per_node[node];
  762. idle_prefetch_data_on_node(handle, node, replicate, mode, prio);
  763. }
  764. return 0;
  765. }
  766. int starpu_idle_prefetch_task_input_on_node(struct starpu_task *task, unsigned node)
  767. {
  768. int prio = task->priority;
  769. if (task->workerorder)
  770. prio = INT_MAX - task->workerorder;
  771. return starpu_idle_prefetch_task_input_on_node_prio(task, node, prio);
  772. }
  773. struct _starpu_data_replicate *get_replicate(starpu_data_handle_t handle, enum starpu_data_access_mode mode, int workerid, unsigned node)
  774. {
  775. if (mode & (STARPU_SCRATCH|STARPU_REDUX))
  776. {
  777. STARPU_ASSERT(workerid >= 0);
  778. if (!handle->per_worker)
  779. {
  780. _starpu_spin_lock(&handle->header_lock);
  781. if (!handle->per_worker)
  782. _starpu_data_initialize_per_worker(handle);
  783. _starpu_spin_unlock(&handle->header_lock);
  784. }
  785. return &handle->per_worker[workerid];
  786. }
  787. else
  788. /* That's a "normal" buffer (R/W) */
  789. return &handle->per_node[node];
  790. }
  791. /* Callback used when a buffer is send asynchronously to the sink */
  792. static void _starpu_fetch_task_input_cb(void *arg)
  793. {
  794. struct _starpu_worker * worker = (struct _starpu_worker *) arg;
  795. /* increase the number of buffer received */
  796. STARPU_WMB();
  797. (void)STARPU_ATOMIC_ADD(&worker->nb_buffers_transferred, 1);
  798. }
  799. /* Synchronously or asynchronously fetch data for a given task (if it's not there already)
  800. * Returns the number of data acquired here. */
  801. /* The synchronous version of _starpu_fetch_task_input must be called before
  802. * executing the task. __starpu_push_task_output but be called after the
  803. * execution of the task. */
  804. /* To improve overlapping, the driver can, before calling the synchronous
  805. * version of _starpu_fetch_task_input, call _starpu_fetch_task_input with
  806. * async==1, then wait for transfers to complete, then call
  807. * _starpu_release_fetch_task_input_async to release them before calling the
  808. * synchronous version of _starpu_fetch_task_input. */
  809. int _starpu_fetch_task_input(struct starpu_task *task, struct _starpu_job *j, int async)
  810. {
  811. struct _starpu_worker *worker = _starpu_get_local_worker_key();
  812. int workerid = worker->workerid;
  813. if (async)
  814. {
  815. worker->task_transferring = task;
  816. worker->nb_buffers_transferred = 0;
  817. if (worker->ntasks <= 1)
  818. _STARPU_TRACE_WORKER_START_FETCH_INPUT(NULL, workerid);
  819. }
  820. else
  821. _STARPU_TRACE_START_FETCH_INPUT(NULL);
  822. int profiling = starpu_profiling_status_get();
  823. if (profiling && task->profiling_info)
  824. _starpu_clock_gettime(&task->profiling_info->acquire_data_start_time);
  825. struct _starpu_data_descr *descrs = _STARPU_JOB_GET_ORDERED_BUFFERS(j);
  826. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  827. unsigned nacquires;
  828. unsigned local_memory_node = _starpu_memory_node_get_local_key();
  829. #ifdef STARPU_USE_FXT
  830. unsigned long total_size = 0;
  831. #endif
  832. unsigned index;
  833. nacquires = 0;
  834. for (index = 0; index < nbuffers; index++)
  835. {
  836. int ret;
  837. starpu_data_handle_t handle = descrs[index].handle;
  838. enum starpu_data_access_mode mode = descrs[index].mode;
  839. int node = descrs[index].node;
  840. if (node == -1)
  841. node = local_memory_node;
  842. if (mode == STARPU_NONE ||
  843. (mode & ((1<<STARPU_MODE_SHIFT) - 1)) >= STARPU_ACCESS_MODE_MAX ||
  844. (mode >> STARPU_MODE_SHIFT) >= (STARPU_SHIFTED_MODE_MAX >> STARPU_MODE_SHIFT))
  845. STARPU_ASSERT_MSG(0, "mode %d (0x%x) is bogus\n", mode, mode);
  846. struct _starpu_data_replicate *local_replicate;
  847. if (index && descrs[index-1].handle == descrs[index].handle)
  848. /* We have already took this data, skip it. This
  849. * depends on ordering putting writes before reads, see
  850. * _starpu_compar_handles */
  851. continue;
  852. local_replicate = get_replicate(handle, mode, workerid, node);
  853. if (async)
  854. {
  855. ret = _starpu_fetch_data_on_node(handle, node, local_replicate, mode, 0, 0, 1,
  856. _starpu_fetch_task_input_cb, worker, 0, "_starpu_src_common_worker_internal_work");
  857. if (STARPU_UNLIKELY(ret))
  858. {
  859. /* Ooops, not enough memory, make worker wait for these for now, and the synchronous call will finish by forcing eviction*/
  860. worker->nb_buffers_totransfer = nacquires;
  861. return 0;
  862. }
  863. }
  864. else
  865. {
  866. ret = fetch_data(handle, node, local_replicate, mode, 0);
  867. if (STARPU_UNLIKELY(ret))
  868. goto enomem;
  869. }
  870. #ifdef STARPU_USE_FXT
  871. total_size += _starpu_data_get_size(handle);
  872. #endif
  873. nacquires++;
  874. }
  875. if (async)
  876. {
  877. worker->nb_buffers_totransfer = nacquires;
  878. return 0;
  879. }
  880. _STARPU_TRACE_DATA_LOAD(workerid,total_size);
  881. /* Now that we have taken the data locks in locking order, fill the codelet interfaces in function order. */
  882. for (index = 0; index < nbuffers; index++)
  883. {
  884. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, index);
  885. enum starpu_data_access_mode mode = STARPU_TASK_GET_MODE(task, index);
  886. int node = descrs[index].node;
  887. if (node == -1)
  888. node = local_memory_node;
  889. struct _starpu_data_replicate *local_replicate;
  890. local_replicate = get_replicate(handle, mode, workerid, node);
  891. _STARPU_TASK_SET_INTERFACE(task , local_replicate->data_interface, index);
  892. /* If the replicate was not initialized yet, we have to do it now */
  893. if (!(mode & STARPU_SCRATCH) && !local_replicate->initialized)
  894. _starpu_redux_init_data_replicate(handle, local_replicate, workerid);
  895. }
  896. if (profiling && task->profiling_info)
  897. _starpu_clock_gettime(&task->profiling_info->acquire_data_end_time);
  898. _STARPU_TRACE_END_FETCH_INPUT(NULL);
  899. return 0;
  900. enomem:
  901. _STARPU_TRACE_END_FETCH_INPUT(NULL);
  902. _STARPU_DISP("something went wrong with buffer %u\n", index);
  903. /* try to unreference all the input that were successfully taken */
  904. unsigned index2;
  905. for (index2 = 0; index2 < index; index2++)
  906. {
  907. starpu_data_handle_t handle = descrs[index2].handle;
  908. enum starpu_data_access_mode mode = descrs[index2].mode;
  909. int node = descrs[index].node;
  910. if (node == -1)
  911. node = local_memory_node;
  912. struct _starpu_data_replicate *local_replicate;
  913. if (index2 && descrs[index2-1].handle == descrs[index2].handle)
  914. /* We have already released this data, skip it. This
  915. * depends on ordering putting writes before reads, see
  916. * _starpu_compar_handles */
  917. continue;
  918. local_replicate = get_replicate(handle, mode, workerid, node);
  919. _starpu_release_data_on_node(handle, 0, local_replicate);
  920. }
  921. return -1;
  922. }
  923. /* This is to be called after having called _starpu_fetch_task_input with async=1 and getting the cb called as many times as there are buffers. */
  924. int _starpu_release_fetch_task_input_async(struct _starpu_job *j, struct _starpu_worker *worker)
  925. {
  926. unsigned workerid = worker->workerid;
  927. unsigned nbtransfers = worker->nb_buffers_totransfer;
  928. STARPU_RMB();
  929. if (worker->ntasks <= 1)
  930. _STARPU_TRACE_WORKER_END_FETCH_INPUT(NULL, workerid);
  931. struct starpu_task *task = j->task;
  932. struct _starpu_data_descr *descrs = _STARPU_JOB_GET_ORDERED_BUFFERS(j);
  933. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  934. unsigned local_memory_node = _starpu_memory_node_get_local_key();
  935. unsigned index;
  936. unsigned nreleases;
  937. nreleases = 0;
  938. for (index = 0; index < nbuffers; index++)
  939. {
  940. if (nreleases == nbtransfers)
  941. /* That was a partial fetch */
  942. break;
  943. starpu_data_handle_t handle = descrs[index].handle;
  944. enum starpu_data_access_mode mode = descrs[index].mode;
  945. int node = descrs[index].node;
  946. if (node == -1)
  947. node = local_memory_node;
  948. struct _starpu_data_replicate *local_replicate;
  949. if (index && descrs[index-1].handle == descrs[index].handle)
  950. /* We have already took this data, skip it. This
  951. * depends on ordering putting writes before reads, see
  952. * _starpu_compar_handles */
  953. continue;
  954. local_replicate = get_replicate(handle, mode, workerid, node);
  955. /* Release our refcnt */
  956. _starpu_spin_lock(&handle->header_lock);
  957. local_replicate->refcnt--;
  958. STARPU_ASSERT(local_replicate->refcnt >= 0);
  959. STARPU_ASSERT(handle->busy_count > 0);
  960. handle->busy_count--;
  961. if (!_starpu_data_check_not_busy(handle))
  962. _starpu_spin_unlock(&handle->header_lock);
  963. }
  964. return 0;
  965. }
  966. /* Release task data dependencies */
  967. void __starpu_push_task_output(struct _starpu_job *j)
  968. {
  969. #ifdef STARPU_OPENMP
  970. STARPU_ASSERT(!j->continuation);
  971. #endif
  972. int profiling = starpu_profiling_status_get();
  973. struct starpu_task *task = j->task;
  974. if (profiling && task->profiling_info)
  975. _starpu_clock_gettime(&task->profiling_info->release_data_start_time);
  976. struct _starpu_data_descr *descrs = _STARPU_JOB_GET_ORDERED_BUFFERS(j);
  977. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  978. int workerid = starpu_worker_get_id();
  979. unsigned local_memory_node = _starpu_memory_node_get_local_key();
  980. unsigned index;
  981. for (index = 0; index < nbuffers; index++)
  982. {
  983. starpu_data_handle_t handle = descrs[index].handle;
  984. enum starpu_data_access_mode mode = descrs[index].mode;
  985. int node = descrs[index].node;
  986. if (node == -1 && task->cl->where != STARPU_NOWHERE)
  987. node = local_memory_node;
  988. struct _starpu_data_replicate *local_replicate = NULL;
  989. if (index && descrs[index-1].handle == descrs[index].handle)
  990. /* We have already released this data, skip it. This
  991. * depends on ordering putting writes before reads, see
  992. * _starpu_compar_handles */
  993. continue;
  994. if (node != -1)
  995. local_replicate = get_replicate(handle, mode, workerid, node);
  996. /* Keep a reference for future
  997. * _starpu_release_task_enforce_sequential_consistency call */
  998. _starpu_spin_lock(&handle->header_lock);
  999. handle->busy_count++;
  1000. if (node == -1)
  1001. {
  1002. /* NOWHERE case, just notify dependencies */
  1003. if (!_starpu_notify_data_dependencies(handle))
  1004. _starpu_spin_unlock(&handle->header_lock);
  1005. }
  1006. else
  1007. {
  1008. _starpu_spin_unlock(&handle->header_lock);
  1009. _starpu_release_data_on_node(handle, 0, local_replicate);
  1010. }
  1011. }
  1012. if (profiling && task->profiling_info)
  1013. _starpu_clock_gettime(&task->profiling_info->release_data_end_time);
  1014. }
  1015. /* Version for a driver running on a worker: we show the driver state in the trace */
  1016. void _starpu_push_task_output(struct _starpu_job *j)
  1017. {
  1018. _STARPU_TRACE_START_PUSH_OUTPUT(NULL);
  1019. __starpu_push_task_output(j);
  1020. _STARPU_TRACE_END_PUSH_OUTPUT(NULL);
  1021. }
  1022. struct fetch_nowhere_wrapper
  1023. {
  1024. struct _starpu_job *j;
  1025. unsigned pending;
  1026. };
  1027. static void _starpu_fetch_nowhere_task_input_cb(void *arg);
  1028. /* Asynchronously fetch data for a task which will have no content */
  1029. void _starpu_fetch_nowhere_task_input(struct _starpu_job *j)
  1030. {
  1031. int profiling = starpu_profiling_status_get();
  1032. struct starpu_task *task = j->task;
  1033. if (profiling && task->profiling_info)
  1034. _starpu_clock_gettime(&task->profiling_info->acquire_data_start_time);
  1035. struct _starpu_data_descr *descrs = _STARPU_JOB_GET_ORDERED_BUFFERS(j);
  1036. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  1037. unsigned nfetchbuffers = 0;
  1038. struct fetch_nowhere_wrapper *wrapper;
  1039. unsigned index;
  1040. for (index = 0; index < nbuffers; index++)
  1041. {
  1042. int node = descrs[index].node;
  1043. if (node != -1)
  1044. nfetchbuffers++;
  1045. }
  1046. if (!nfetchbuffers)
  1047. {
  1048. /* Nothing to fetch actually, already finished! */
  1049. __starpu_push_task_output(j);
  1050. _starpu_handle_job_termination(j);
  1051. _STARPU_LOG_OUT_TAG("handle_job_termination");
  1052. return;
  1053. }
  1054. _STARPU_MALLOC(wrapper, (sizeof(*wrapper)));
  1055. wrapper->j = j;
  1056. /* +1 for the call below */
  1057. wrapper->pending = nfetchbuffers + 1;
  1058. for (index = 0; index < nbuffers; index++)
  1059. {
  1060. starpu_data_handle_t handle = descrs[index].handle;
  1061. enum starpu_data_access_mode mode = descrs[index].mode;
  1062. int node = descrs[index].node;
  1063. if (node == -1)
  1064. continue;
  1065. if (mode == STARPU_NONE ||
  1066. (mode & ((1<<STARPU_MODE_SHIFT) - 1)) >= STARPU_ACCESS_MODE_MAX ||
  1067. (mode >> STARPU_MODE_SHIFT) >= (STARPU_SHIFTED_MODE_MAX >> STARPU_MODE_SHIFT))
  1068. STARPU_ASSERT_MSG(0, "mode %d (0x%x) is bogus\n", mode, mode);
  1069. STARPU_ASSERT(mode != STARPU_SCRATCH && mode != STARPU_REDUX);
  1070. struct _starpu_data_replicate *local_replicate;
  1071. local_replicate = get_replicate(handle, mode, -1, node);
  1072. _starpu_fetch_data_on_node(handle, node, local_replicate, mode, 0, 0, 1, _starpu_fetch_nowhere_task_input_cb, wrapper, 0, "_starpu_fetch_nowhere_task_input");
  1073. }
  1074. if (profiling && task->profiling_info)
  1075. _starpu_clock_gettime(&task->profiling_info->acquire_data_end_time);
  1076. /* Finished working with the task, release our reference */
  1077. _starpu_fetch_nowhere_task_input_cb(wrapper);
  1078. }
  1079. static void _starpu_fetch_nowhere_task_input_cb(void *arg)
  1080. {
  1081. /* One more transfer finished */
  1082. struct fetch_nowhere_wrapper *wrapper = arg;
  1083. unsigned pending = STARPU_ATOMIC_ADD(&wrapper->pending, -1);
  1084. ANNOTATE_HAPPENS_BEFORE(&wrapper->pending);
  1085. if (pending == 0)
  1086. {
  1087. ANNOTATE_HAPPENS_AFTER(&wrapper->pending);
  1088. /* Finished transferring, task is over */
  1089. struct _starpu_job *j = wrapper->j;
  1090. free(wrapper);
  1091. __starpu_push_task_output(j);
  1092. _starpu_handle_job_termination(j);
  1093. _STARPU_LOG_OUT_TAG("handle_job_termination");
  1094. }
  1095. }
  1096. /* NB : this value can only be an indication of the status of a data
  1097. at some point, but there is no strong garantee ! */
  1098. unsigned _starpu_is_data_present_or_requested(starpu_data_handle_t handle, unsigned node)
  1099. {
  1100. unsigned ret = 0;
  1101. // XXX : this is just a hint, so we don't take the lock ...
  1102. // STARPU_PTHREAD_SPIN_LOCK(&handle->header_lock);
  1103. if (handle->per_node[node].state != STARPU_INVALID)
  1104. {
  1105. ret = 1;
  1106. }
  1107. else
  1108. {
  1109. unsigned i;
  1110. unsigned nnodes = starpu_memory_nodes_get_count();
  1111. for (i = 0; i < nnodes; i++)
  1112. {
  1113. if ((handle->per_node[node].requested & (1UL << i)) || handle->per_node[node].request[i])
  1114. ret = 1;
  1115. }
  1116. }
  1117. // STARPU_PTHREAD_SPIN_UNLOCK(&handle->header_lock);
  1118. return ret;
  1119. }
  1120. void _starpu_data_set_unregister_hook(starpu_data_handle_t handle, _starpu_data_handle_unregister_hook func)
  1121. {
  1122. handle->unregister_hook = func;
  1123. }