coherency.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2008-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2018 Federal University of Rio Grande do Sul (UFRGS)
  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 <limits.h>
  18. #include <math.h>
  19. #include <common/config.h>
  20. #include <datawizard/coherency.h>
  21. #include <datawizard/copy_driver.h>
  22. #include <datawizard/write_back.h>
  23. #include <datawizard/memory_nodes.h>
  24. #include <core/dependencies/data_concurrency.h>
  25. #include <core/disk.h>
  26. #include <profiling/profiling.h>
  27. #include <core/task.h>
  28. #include <starpu_scheduler.h>
  29. #include <core/workers.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_CPU_RAM ||
  123. starpu_node_get_kind(i) == STARPU_MPI_MS_RAM)
  124. i_ram = i;
  125. else if (starpu_node_get_kind(i) == STARPU_DISK_RAM)
  126. i_disk = i;
  127. else
  128. i_gpu = i;
  129. }
  130. }
  131. /* we have to use cpu_ram in first */
  132. if (i_ram != -1)
  133. src_node = i_ram;
  134. else if (i_gpu != -1)
  135. /* otherwise a gpu */
  136. src_node = i_gpu;
  137. else
  138. /* no luck we have to use the disk memory */
  139. src_node = i_disk;
  140. STARPU_ASSERT(src_node != -1);
  141. STARPU_ASSERT(handle->per_node[src_node].allocated);
  142. STARPU_ASSERT(handle->per_node[src_node].initialized);
  143. return src_node;
  144. }
  145. /* this may be called once the data is fetched with header and STARPU_RW-lock hold */
  146. void _starpu_update_data_state(starpu_data_handle_t handle,
  147. struct _starpu_data_replicate *requesting_replicate,
  148. enum starpu_data_access_mode mode)
  149. {
  150. /* There is nothing to do for relaxed coherency modes (scratch or
  151. * reductions) */
  152. if (!(mode & STARPU_RW))
  153. return;
  154. unsigned nnodes = starpu_memory_nodes_get_count();
  155. /* the data is present now */
  156. unsigned requesting_node = requesting_replicate->memory_node;
  157. requesting_replicate->requested &= ~(1UL << requesting_node);
  158. if (mode & STARPU_W)
  159. {
  160. /* the requesting node now has the only valid copy */
  161. unsigned node;
  162. for (node = 0; node < nnodes; node++)
  163. {
  164. if (handle->per_node[node].state != STARPU_INVALID)
  165. _STARPU_TRACE_DATA_STATE_INVALID(handle, node);
  166. handle->per_node[node].state = STARPU_INVALID;
  167. }
  168. if (requesting_replicate->state != STARPU_OWNER)
  169. _STARPU_TRACE_DATA_STATE_OWNER(handle, requesting_node);
  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. {
  177. /* read only */
  178. if (requesting_replicate->state != STARPU_OWNER)
  179. {
  180. /* there was at least another copy of the data */
  181. unsigned node;
  182. for (node = 0; node < nnodes; node++)
  183. {
  184. struct _starpu_data_replicate *replicate = &handle->per_node[node];
  185. if (replicate->state != STARPU_INVALID)
  186. {
  187. if (replicate->state != STARPU_SHARED)
  188. _STARPU_TRACE_DATA_STATE_SHARED(handle, node);
  189. replicate->state = STARPU_SHARED;
  190. }
  191. }
  192. if (requesting_replicate->state != STARPU_SHARED)
  193. _STARPU_TRACE_DATA_STATE_SHARED(handle, requesting_node);
  194. requesting_replicate->state = STARPU_SHARED;
  195. }
  196. }
  197. }
  198. static int worker_supports_direct_access(unsigned node, unsigned handling_node)
  199. {
  200. if (node == handling_node)
  201. return 1;
  202. if (!_starpu_memory_node_get_nworkers(handling_node))
  203. /* No worker to process the request from that node */
  204. return 0;
  205. struct _starpu_node_ops *node_ops = _starpu_memory_node_get_node_ops(node);
  206. if (node_ops && node_ops->is_direct_access_supported)
  207. return node_ops->is_direct_access_supported(node, handling_node);
  208. else
  209. {
  210. STARPU_ABORT_MSG("Node %s does not define the operation 'is_direct_access_supported'", _starpu_node_get_prefix(starpu_node_get_kind(node)));
  211. return 1;
  212. }
  213. }
  214. static int link_supports_direct_transfers(starpu_data_handle_t handle, unsigned src_node, unsigned dst_node, unsigned *handling_node)
  215. {
  216. int (*can_copy)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, unsigned handling_node) = handle->ops->copy_methods->can_copy;
  217. void *src_interface = handle->per_node[src_node].data_interface;
  218. void *dst_interface = handle->per_node[dst_node].data_interface;
  219. /* XXX That's a hack until we fix cudaMemcpy3DPeerAsync in the block interface
  220. * Perhaps not all data interface provide a direct GPU-GPU transfer
  221. * method ! */
  222. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  223. if (src_node != dst_node && starpu_node_get_kind(src_node) == STARPU_CUDA_RAM && starpu_node_get_kind(dst_node) == STARPU_CUDA_RAM)
  224. {
  225. const struct starpu_data_copy_methods *copy_methods = handle->ops->copy_methods;
  226. if (!copy_methods->cuda_to_cuda_async && !copy_methods->any_to_any)
  227. return 0;
  228. }
  229. #endif
  230. /* Note: with CUDA, performance seems a bit better when issuing the transfer from the destination (tested without GPUDirect, but GPUDirect probably behave the same) */
  231. if (worker_supports_direct_access(src_node, dst_node) && (!can_copy || can_copy(src_interface, src_node, dst_interface, dst_node, dst_node)))
  232. {
  233. *handling_node = dst_node;
  234. return 1;
  235. }
  236. if (worker_supports_direct_access(dst_node, src_node) && (!can_copy || can_copy(src_interface, src_node, dst_interface, dst_node, src_node)))
  237. {
  238. *handling_node = src_node;
  239. return 1;
  240. }
  241. return 0;
  242. }
  243. /* Now, we use slowness/bandwidth to compare numa nodes, is it better to use latency ? */
  244. static unsigned chose_best_numa_between_src_and_dest(int src, int dst)
  245. {
  246. double timing_best;
  247. int best_numa = -1;
  248. unsigned numa;
  249. const unsigned nb_numa_nodes = starpu_memory_nodes_get_numa_count();
  250. for(numa = 0; numa < nb_numa_nodes; numa++)
  251. {
  252. double actual = 1.0/starpu_transfer_bandwidth(src, numa) + 1.0/starpu_transfer_bandwidth(numa, dst);
  253. /* Compare slowness : take the lowest */
  254. if (best_numa < 0 || actual < timing_best)
  255. {
  256. best_numa = numa;
  257. timing_best = actual;
  258. }
  259. }
  260. STARPU_ASSERT(best_numa >= 0);
  261. return best_numa;
  262. }
  263. /* Determines the path of a request : each hop is defined by (src,dst) and the
  264. * node that handles the hop. The returned value indicates the number of hops,
  265. * and the max_len is the maximum number of hops (ie. the size of the
  266. * src_nodes, dst_nodes and handling_nodes arrays. */
  267. int _starpu_determine_request_path(starpu_data_handle_t handle,
  268. int src_node, int dst_node,
  269. enum starpu_data_access_mode mode, int max_len,
  270. unsigned *src_nodes, unsigned *dst_nodes,
  271. unsigned *handling_nodes, unsigned write_invalidation)
  272. {
  273. if (src_node == dst_node || !(mode & STARPU_R))
  274. {
  275. if (dst_node == -1 || starpu_node_get_kind(dst_node) == STARPU_DISK_RAM)
  276. handling_nodes[0] = src_node;
  277. else
  278. handling_nodes[0] = dst_node;
  279. if (write_invalidation)
  280. /* The invalidation request will be enough */
  281. return 0;
  282. /* The destination node should only allocate the data, no transfer is required */
  283. STARPU_ASSERT(max_len >= 1);
  284. src_nodes[0] = STARPU_MAIN_RAM; // ignored
  285. dst_nodes[0] = dst_node;
  286. return 1;
  287. }
  288. if (src_node < 0)
  289. {
  290. /* Will just initialize the destination */
  291. STARPU_ASSERT(max_len >= 1);
  292. src_nodes[0] = src_node; // ignored
  293. dst_nodes[0] = dst_node;
  294. return 1;
  295. }
  296. unsigned handling_node;
  297. int link_is_valid = link_supports_direct_transfers(handle, src_node, dst_node, &handling_node);
  298. if (!link_is_valid)
  299. {
  300. int (*can_copy)(void *, unsigned, void *, unsigned, unsigned) = handle->ops->copy_methods->can_copy;
  301. void *src_interface = handle->per_node[src_node].data_interface;
  302. void *dst_interface = handle->per_node[dst_node].data_interface;
  303. /* We need an intermediate hop to implement data staging
  304. * through main memory. */
  305. STARPU_ASSERT(max_len >= 2);
  306. STARPU_ASSERT(src_node >= 0);
  307. unsigned numa = chose_best_numa_between_src_and_dest(src_node, dst_node);
  308. /* GPU -> RAM */
  309. src_nodes[0] = src_node;
  310. dst_nodes[0] = numa;
  311. if (starpu_node_get_kind(src_node) == STARPU_DISK_RAM)
  312. /* Disks don't have their own driver thread */
  313. handling_nodes[0] = dst_node;
  314. else if (!can_copy || can_copy(src_interface, src_node, dst_interface, dst_node, src_node))
  315. {
  316. handling_nodes[0] = src_node;
  317. }
  318. else
  319. {
  320. 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);
  321. handling_nodes[0] = dst_node;
  322. }
  323. /* RAM -> GPU */
  324. src_nodes[1] = numa;
  325. dst_nodes[1] = dst_node;
  326. if (starpu_node_get_kind(dst_node) == STARPU_DISK_RAM)
  327. /* Disks don't have their own driver thread */
  328. handling_nodes[1] = src_node;
  329. else if (!can_copy || can_copy(src_interface, src_node, dst_interface, dst_node, dst_node))
  330. {
  331. handling_nodes[1] = dst_node;
  332. }
  333. else
  334. {
  335. 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);
  336. handling_nodes[1] = src_node;
  337. }
  338. return 2;
  339. }
  340. else
  341. {
  342. STARPU_ASSERT(max_len >= 1);
  343. src_nodes[0] = src_node;
  344. dst_nodes[0] = dst_node;
  345. handling_nodes[0] = handling_node;
  346. #if !defined(STARPU_HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID)
  347. STARPU_ASSERT(!(mode & STARPU_R) || starpu_node_get_kind(src_node) != STARPU_CUDA_RAM || starpu_node_get_kind(dst_node) != STARPU_CUDA_RAM);
  348. #endif
  349. return 1;
  350. }
  351. }
  352. /* handle->lock should be taken. r is returned locked. The node parameter
  353. * indicate either the source of the request, or the destination for a
  354. * write-only request. */
  355. static struct _starpu_data_request *_starpu_search_existing_data_request(struct _starpu_data_replicate *replicate, unsigned node, enum starpu_data_access_mode mode, enum starpu_is_prefetch is_prefetch)
  356. {
  357. struct _starpu_data_request *r;
  358. r = replicate->request[node];
  359. if (r)
  360. {
  361. _starpu_spin_checklocked(&r->handle->header_lock);
  362. _starpu_spin_lock(&r->lock);
  363. /* perhaps we need to "upgrade" the request */
  364. if (is_prefetch < r->prefetch)
  365. _starpu_update_prefetch_status(r, is_prefetch);
  366. if (mode & STARPU_R)
  367. {
  368. /* in case the exisiting request did not imply a memory
  369. * transfer yet, we have to take a second refcnt now
  370. * for the source, in addition to the refcnt for the
  371. * destination
  372. * (so that the source remains valid) */
  373. if (!(r->mode & STARPU_R))
  374. {
  375. replicate->refcnt++;
  376. replicate->handle->busy_count++;
  377. }
  378. r->mode = (enum starpu_data_access_mode) ((int) r->mode | (int) STARPU_R);
  379. }
  380. if (mode & STARPU_W)
  381. r->mode = (enum starpu_data_access_mode) ((int) r->mode | (int) STARPU_W);
  382. }
  383. return r;
  384. }
  385. /*
  386. * This function is called when the data is needed on the local node, this
  387. * returns a pointer to the local copy
  388. *
  389. * R STARPU_W STARPU_RW
  390. * Owner OK OK OK
  391. * Shared OK 1 1
  392. * Invalid 2 3 4
  393. *
  394. * case 1 : shared + (read)write :
  395. * no data copy but shared->Invalid/Owner
  396. * case 2 : invalid + read :
  397. * data copy + invalid->shared + owner->shared (STARPU_ASSERT(there is a valid))
  398. * case 3 : invalid + write :
  399. * no data copy + invalid->owner + (owner,shared)->invalid
  400. * case 4 : invalid + R/STARPU_W :
  401. * data copy + if (STARPU_W) (invalid->owner + owner->invalid)
  402. * else (invalid,owner->shared)
  403. */
  404. struct _starpu_data_request *_starpu_create_request_to_fetch_data(starpu_data_handle_t handle,
  405. struct _starpu_data_replicate *dst_replicate,
  406. enum starpu_data_access_mode mode, enum starpu_is_prefetch is_prefetch,
  407. unsigned async,
  408. void (*callback_func)(void *), void *callback_arg, int prio, const char *origin)
  409. {
  410. /* We don't care about commuting for data requests, that was handled before. */
  411. mode &= ~STARPU_COMMUTE;
  412. /* This function is called with handle's header lock taken */
  413. _starpu_spin_checklocked(&handle->header_lock);
  414. int requesting_node = dst_replicate ? dst_replicate->memory_node : -1;
  415. unsigned nwait = 0;
  416. if (mode & STARPU_W)
  417. {
  418. /* We will write to the buffer. We will have to wait for all
  419. * existing requests before the last request which will
  420. * invalidate all their results (which were possibly spurious,
  421. * e.g. too aggressive eviction).
  422. */
  423. unsigned i, j;
  424. unsigned nnodes = starpu_memory_nodes_get_count();
  425. for (i = 0; i < nnodes; i++)
  426. for (j = 0; j < nnodes; j++)
  427. if (handle->per_node[i].request[j])
  428. nwait++;
  429. /* If the request is not detached (i.e. the caller really wants
  430. * proper ownership), no new requests will appear because a
  431. * reference will be kept on the dst replicate, which will
  432. * notably prevent data reclaiming.
  433. */
  434. }
  435. if ((!dst_replicate || dst_replicate->state != STARPU_INVALID) && (!nwait || is_prefetch))
  436. {
  437. if (dst_replicate)
  438. {
  439. #ifdef STARPU_MEMORY_STATS
  440. enum _starpu_cache_state old_state = dst_replicate->state;
  441. #endif
  442. /* the data is already available and we don't have to wait for
  443. * any request, so we can stop */
  444. _starpu_update_data_state(handle, dst_replicate, mode);
  445. _starpu_msi_cache_hit(requesting_node);
  446. #ifdef STARPU_MEMORY_STATS
  447. _starpu_memory_handle_stats_cache_hit(handle, requesting_node);
  448. /* XXX Broken ? */
  449. if (old_state == STARPU_SHARED
  450. && dst_replicate->state == STARPU_OWNER)
  451. _starpu_memory_handle_stats_shared_to_owner(handle, requesting_node);
  452. #endif
  453. if (dst_replicate->mc)
  454. {
  455. if (is_prefetch == STARPU_TASK_PREFETCH)
  456. /* Make sure it stays there */
  457. dst_replicate->nb_tasks_prefetch++;
  458. _starpu_memchunk_recently_used(dst_replicate->mc, requesting_node);
  459. }
  460. }
  461. _starpu_spin_unlock(&handle->header_lock);
  462. if (callback_func)
  463. callback_func(callback_arg);
  464. _STARPU_LOG_OUT_TAG("data available");
  465. return NULL;
  466. }
  467. if (dst_replicate)
  468. _starpu_msi_cache_miss(requesting_node);
  469. /* the only remaining situation is that the local copy was invalid */
  470. STARPU_ASSERT((dst_replicate && dst_replicate->state == STARPU_INVALID) || nwait);
  471. /* find someone who already has the data */
  472. int src_node = -1;
  473. if (dst_replicate && mode & STARPU_R)
  474. {
  475. if (dst_replicate->state == STARPU_INVALID)
  476. src_node = _starpu_select_src_node(handle, requesting_node);
  477. else
  478. src_node = requesting_node;
  479. if (src_node < 0)
  480. {
  481. /* We will create it, no need to read an existing value */
  482. mode &= ~STARPU_R;
  483. }
  484. }
  485. else if (dst_replicate)
  486. {
  487. /* 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 */
  488. if (mode & STARPU_W)
  489. dst_replicate->initialized = 1;
  490. if (starpu_node_get_kind(requesting_node) == STARPU_CPU_RAM && !nwait
  491. && !_starpu_malloc_willpin_on_node(requesting_node))
  492. {
  493. /* And this is the main RAM without pinning, really no need for a
  494. * request, just quickly allocate and be done */
  495. if (_starpu_allocate_memory_on_node(handle, dst_replicate, is_prefetch) == 0)
  496. {
  497. _starpu_update_data_state(handle, dst_replicate, mode);
  498. if (dst_replicate->mc)
  499. {
  500. if (is_prefetch == STARPU_TASK_PREFETCH)
  501. /* Make sure it stays there */
  502. dst_replicate->nb_tasks_prefetch++;
  503. _starpu_memchunk_recently_used(dst_replicate->mc, requesting_node);
  504. }
  505. _starpu_spin_unlock(&handle->header_lock);
  506. if (callback_func)
  507. callback_func(callback_arg);
  508. _STARPU_LOG_OUT_TAG("data immediately allocated");
  509. return NULL;
  510. }
  511. }
  512. }
  513. #define MAX_REQUESTS 4
  514. /* We can safely assume that there won't be more than 2 hops in the
  515. * current implementation */
  516. unsigned src_nodes[MAX_REQUESTS], dst_nodes[MAX_REQUESTS], handling_nodes[MAX_REQUESTS];
  517. /* keep one slot for the last W request, if any */
  518. int write_invalidation = (mode & STARPU_W) && nwait && !is_prefetch;
  519. int nhops = _starpu_determine_request_path(handle, src_node, requesting_node, mode, MAX_REQUESTS,
  520. src_nodes, dst_nodes, handling_nodes, write_invalidation);
  521. STARPU_ASSERT(nhops >= 0 && nhops <= MAX_REQUESTS-1);
  522. struct _starpu_data_request *requests[nhops + write_invalidation];
  523. /* Did we reuse a request for that hop ? */
  524. int reused_requests[nhops + write_invalidation];
  525. /* Construct an array with a list of requests, possibly reusing existing requests */
  526. int hop;
  527. for (hop = 0; hop < nhops; hop++)
  528. {
  529. struct _starpu_data_request *r;
  530. unsigned hop_src_node = src_nodes[hop];
  531. unsigned hop_dst_node = dst_nodes[hop];
  532. unsigned hop_handling_node = handling_nodes[hop];
  533. struct _starpu_data_replicate *hop_src_replicate;
  534. struct _starpu_data_replicate *hop_dst_replicate;
  535. /* Only the first request is independant */
  536. unsigned ndeps = (hop == 0)?0:1;
  537. hop_src_replicate = &handle->per_node[hop_src_node];
  538. hop_dst_replicate = (hop != nhops - 1)?&handle->per_node[hop_dst_node]:dst_replicate;
  539. /* Try to reuse a request if possible */
  540. r = _starpu_search_existing_data_request(hop_dst_replicate,
  541. (mode & STARPU_R)?hop_src_node:hop_dst_node,
  542. mode, is_prefetch);
  543. reused_requests[hop] = !!r;
  544. if (!r)
  545. {
  546. /* Create a new request if there was no request to reuse */
  547. r = _starpu_create_data_request(handle, hop_src_replicate,
  548. hop_dst_replicate, hop_handling_node,
  549. mode, ndeps, is_prefetch, prio, 0, origin);
  550. nwait++;
  551. }
  552. requests[hop] = r;
  553. }
  554. /* Chain these requests */
  555. for (hop = 0; hop < nhops; hop++)
  556. {
  557. struct _starpu_data_request *r;
  558. r = requests[hop];
  559. if (hop != nhops - 1)
  560. {
  561. if (!reused_requests[hop + 1])
  562. {
  563. r->next_req[r->next_req_count++] = requests[hop + 1];
  564. STARPU_ASSERT(r->next_req_count <= STARPU_MAXNODES);
  565. }
  566. }
  567. else
  568. {
  569. if (is_prefetch == STARPU_TASK_PREFETCH)
  570. /* Make last request add the prefetch count on the mc to keep the data
  571. * there until the task gets to execute. */
  572. r->nb_tasks_prefetch++;
  573. if (!write_invalidation)
  574. /* The last request will perform the callback after termination */
  575. _starpu_data_request_append_callback(r, callback_func, callback_arg);
  576. }
  577. if (reused_requests[hop])
  578. _starpu_spin_unlock(&r->lock);
  579. }
  580. if (write_invalidation)
  581. {
  582. /* Some requests were still pending, we have to add yet another
  583. * request, depending on them, which will invalidate their
  584. * result.
  585. */
  586. struct _starpu_data_request *r = _starpu_create_data_request(handle, dst_replicate,
  587. dst_replicate, requesting_node,
  588. STARPU_W, nwait, is_prefetch, prio, 1, origin);
  589. /* and perform the callback after termination */
  590. _starpu_data_request_append_callback(r, callback_func, callback_arg);
  591. /* We will write to the buffer. We will have to wait for all
  592. * existing requests before the last request which will
  593. * invalidate all their results (which were possibly spurious,
  594. * e.g. too aggressive eviction).
  595. */
  596. unsigned i, j;
  597. unsigned nnodes = starpu_memory_nodes_get_count();
  598. for (i = 0; i < nnodes; i++)
  599. for (j = 0; j < nnodes; j++)
  600. {
  601. struct _starpu_data_request *r2 = handle->per_node[i].request[j];
  602. if (r2)
  603. {
  604. _starpu_spin_lock(&r2->lock);
  605. if (is_prefetch < r2->prefetch)
  606. /* Hasten the request we will have to wait for */
  607. _starpu_update_prefetch_status(r2, is_prefetch);
  608. r2->next_req[r2->next_req_count++] = r;
  609. STARPU_ASSERT(r2->next_req_count <= STARPU_MAXNODES + 1);
  610. _starpu_spin_unlock(&r2->lock);
  611. nwait--;
  612. }
  613. }
  614. STARPU_ASSERT(nwait == 0);
  615. nhops++;
  616. requests[nhops - 1] = r;
  617. /* existing requests will post this one */
  618. reused_requests[nhops - 1] = 1;
  619. }
  620. STARPU_ASSERT(nhops);
  621. if (!async)
  622. requests[nhops - 1]->refcnt++;
  623. /* we only submit the first request, the remaining will be
  624. * automatically submitted afterward */
  625. if (!reused_requests[0])
  626. _starpu_post_data_request(requests[0]);
  627. return requests[nhops - 1];
  628. }
  629. int _starpu_fetch_data_on_node(starpu_data_handle_t handle, int node, struct _starpu_data_replicate *dst_replicate,
  630. enum starpu_data_access_mode mode, unsigned detached, enum starpu_is_prefetch is_prefetch, unsigned async,
  631. void (*callback_func)(void *), void *callback_arg, int prio, const char *origin)
  632. {
  633. _STARPU_LOG_IN();
  634. int cpt = 0;
  635. while (cpt < STARPU_SPIN_MAXTRY && _starpu_spin_trylock(&handle->header_lock))
  636. {
  637. cpt++;
  638. _starpu_datawizard_progress(1);
  639. }
  640. if (cpt == STARPU_SPIN_MAXTRY)
  641. _starpu_spin_lock(&handle->header_lock);
  642. if (mode & STARPU_R && is_prefetch > STARPU_FETCH)
  643. {
  644. unsigned src_node_mask = 0;
  645. unsigned nnodes = starpu_memory_nodes_get_count();
  646. unsigned n;
  647. for (n = 0; n < nnodes; n++)
  648. {
  649. if (handle->per_node[n].state != STARPU_INVALID)
  650. {
  651. /* we found a copy ! */
  652. src_node_mask |= (1<<n);
  653. }
  654. }
  655. if (src_node_mask == 0)
  656. {
  657. /* no valid copy, nothing to prefetch */
  658. STARPU_ASSERT_MSG(handle->init_cl, "Could not find a valid copy of the data, and no handle initialization function");
  659. _starpu_spin_unlock(&handle->header_lock);
  660. return 0;
  661. }
  662. }
  663. if (!detached)
  664. {
  665. /* Take references which will be released by _starpu_release_data_on_node */
  666. if (dst_replicate)
  667. dst_replicate->refcnt++;
  668. else if (node == STARPU_ACQUIRE_NO_NODE_LOCK_ALL)
  669. {
  670. int i;
  671. for (i = 0; i < STARPU_MAXNODES; i++)
  672. handle->per_node[i].refcnt++;
  673. }
  674. handle->busy_count++;
  675. }
  676. struct _starpu_data_request *r;
  677. r = _starpu_create_request_to_fetch_data(handle, dst_replicate, mode,
  678. is_prefetch, async, callback_func, callback_arg, prio, origin);
  679. /* If no request was created, the handle was already up-to-date on the
  680. * node. In this case, _starpu_create_request_to_fetch_data has already
  681. * unlocked the header. */
  682. if (!r)
  683. return 0;
  684. _starpu_spin_unlock(&handle->header_lock);
  685. int ret = async?0:_starpu_wait_data_request_completion(r, 1);
  686. _STARPU_LOG_OUT();
  687. return ret;
  688. }
  689. 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)
  690. {
  691. return _starpu_fetch_data_on_node(handle, node, replicate, mode, 1, STARPU_IDLEFETCH, 1, NULL, NULL, prio, "idle_prefetch_data_on_node");
  692. }
  693. static int task_prefetch_data_on_node(starpu_data_handle_t handle, int node, struct _starpu_data_replicate *replicate, enum starpu_data_access_mode mode, int prio)
  694. {
  695. return _starpu_fetch_data_on_node(handle, node, replicate, mode, 1, STARPU_TASK_PREFETCH, 1, NULL, NULL, prio, "task_prefetch_data_on_node");
  696. }
  697. static int STARPU_ATTRIBUTE_UNUSED prefetch_data_on_node(starpu_data_handle_t handle, int node, struct _starpu_data_replicate *replicate, enum starpu_data_access_mode mode, int prio)
  698. {
  699. return _starpu_fetch_data_on_node(handle, node, replicate, mode, 1, STARPU_PREFETCH, 1, NULL, NULL, prio, "prefetch_data_on_node");
  700. }
  701. static int fetch_data(starpu_data_handle_t handle, int node, struct _starpu_data_replicate *replicate, enum starpu_data_access_mode mode, int prio)
  702. {
  703. return _starpu_fetch_data_on_node(handle, node, replicate, mode, 0, STARPU_FETCH, 0, NULL, NULL, prio, "fetch_data");
  704. }
  705. uint32_t _starpu_get_data_refcnt(starpu_data_handle_t handle, unsigned node)
  706. {
  707. return handle->per_node[node].refcnt;
  708. }
  709. size_t _starpu_data_get_size(starpu_data_handle_t handle)
  710. {
  711. return handle->ops->get_size(handle);
  712. }
  713. size_t _starpu_data_get_alloc_size(starpu_data_handle_t handle)
  714. {
  715. if (handle->ops->get_alloc_size)
  716. return handle->ops->get_alloc_size(handle);
  717. else
  718. return handle->ops->get_size(handle);
  719. }
  720. starpu_ssize_t _starpu_data_get_max_size(starpu_data_handle_t handle)
  721. {
  722. if (handle->ops->get_max_size)
  723. return handle->ops->get_max_size(handle);
  724. else
  725. return -1;
  726. }
  727. uint32_t _starpu_data_get_footprint(starpu_data_handle_t handle)
  728. {
  729. return handle->footprint;
  730. }
  731. /* in case the data was accessed on a write mode, do not forget to
  732. * make it accessible again once it is possible ! */
  733. void _starpu_release_data_on_node(starpu_data_handle_t handle, uint32_t default_wt_mask, enum starpu_data_access_mode down_to_mode, struct _starpu_data_replicate *replicate)
  734. {
  735. uint32_t wt_mask;
  736. wt_mask = default_wt_mask | handle->wt_mask;
  737. wt_mask &= (1<<starpu_memory_nodes_get_count())-1;
  738. /* Note that it is possible that there is no valid copy of the data (if
  739. * starpu_data_invalidate was called for instance). In that case, we do
  740. * not enforce any write-through mechanism. */
  741. unsigned memory_node = replicate->memory_node;
  742. if (replicate->state != STARPU_INVALID && handle->current_mode & STARPU_W)
  743. if (wt_mask & ~(1<<memory_node))
  744. _starpu_write_through_data(handle, memory_node, wt_mask);
  745. int cpt = 0;
  746. while (cpt < STARPU_SPIN_MAXTRY && _starpu_spin_trylock(&handle->header_lock))
  747. {
  748. cpt++;
  749. _starpu_datawizard_progress(1);
  750. }
  751. if (cpt == STARPU_SPIN_MAXTRY)
  752. _starpu_spin_lock(&handle->header_lock);
  753. if (down_to_mode == STARPU_NONE)
  754. {
  755. /* Release refcnt taken by fetch_data_on_node */
  756. replicate->refcnt--;
  757. STARPU_ASSERT_MSG(replicate->refcnt >= 0, "handle %p released too many times", handle);
  758. STARPU_ASSERT_MSG(handle->busy_count > 0, "handle %p released too many times", handle);
  759. handle->busy_count--;
  760. }
  761. if (!_starpu_notify_data_dependencies(handle, down_to_mode))
  762. _starpu_spin_unlock(&handle->header_lock);
  763. }
  764. static void _starpu_set_data_requested_flag_if_needed(starpu_data_handle_t handle, struct _starpu_data_replicate *replicate)
  765. {
  766. int cpt = 0;
  767. while (cpt < STARPU_SPIN_MAXTRY && _starpu_spin_trylock(&handle->header_lock))
  768. {
  769. cpt++;
  770. _starpu_datawizard_progress(1);
  771. }
  772. if (cpt == STARPU_SPIN_MAXTRY)
  773. _starpu_spin_lock(&handle->header_lock);
  774. if (replicate->state == STARPU_INVALID)
  775. {
  776. unsigned dst_node = replicate->memory_node;
  777. replicate->requested |= 1UL << dst_node;
  778. }
  779. _starpu_spin_unlock(&handle->header_lock);
  780. }
  781. int _starpu_prefetch_task_input_prio(struct starpu_task *task, int target_node, int worker, int prio, enum starpu_is_prefetch prefetch)
  782. {
  783. #ifdef STARPU_OPENMP
  784. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  785. /* do not attempt to prefetch task input if this is an OpenMP task resuming after blocking */
  786. if (j->discontinuous != 0)
  787. return 0;
  788. #endif
  789. STARPU_ASSERT_MSG(prefetch != STARPU_PREFETCH || !task->prefetched, "Prefetching was already requested for this task! Did you set 'prefetches' to 1 in the starpu_sched_policy structure?");
  790. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  791. unsigned index;
  792. for (index = 0; index < nbuffers; index++)
  793. {
  794. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, index);
  795. enum starpu_data_access_mode mode = STARPU_TASK_GET_MODE(task, index);
  796. if (mode & (STARPU_SCRATCH|STARPU_REDUX))
  797. continue;
  798. int node;
  799. if (target_node >= 0)
  800. node = _starpu_task_data_get_node_on_node(task, index, target_node);
  801. else
  802. node = _starpu_task_data_get_node_on_worker(task, index, worker);
  803. struct _starpu_data_replicate *replicate = &handle->per_node[node];
  804. if (prefetch == STARPU_PREFETCH)
  805. {
  806. task_prefetch_data_on_node(handle, node, replicate, mode, prio);
  807. _starpu_set_data_requested_flag_if_needed(handle, replicate);
  808. }
  809. else
  810. idle_prefetch_data_on_node(handle, node, replicate, mode, prio);
  811. }
  812. if (prefetch == STARPU_PREFETCH)
  813. task->prefetched = 1;
  814. return 0;
  815. }
  816. int starpu_prefetch_task_input_prio(struct starpu_task *task, int target_node, int worker, int prio)
  817. {
  818. return _starpu_prefetch_task_input_prio(task, target_node, worker, prio, STARPU_PREFETCH);
  819. }
  820. int starpu_prefetch_task_input_on_node_prio(struct starpu_task *task, unsigned target_node, int prio)
  821. {
  822. return starpu_prefetch_task_input_prio(task, target_node, -1, prio);
  823. }
  824. int starpu_prefetch_task_input_on_node(struct starpu_task *task, unsigned node)
  825. {
  826. int prio = task->priority;
  827. if (task->workerorder)
  828. prio = INT_MAX - task->workerorder;
  829. return starpu_prefetch_task_input_on_node_prio(task, node, prio);
  830. }
  831. int starpu_idle_prefetch_task_input_prio(struct starpu_task *task, int target_node, int worker, int prio)
  832. {
  833. return _starpu_prefetch_task_input_prio(task, target_node, worker, prio, STARPU_IDLEFETCH);
  834. }
  835. int starpu_idle_prefetch_task_input_on_node_prio(struct starpu_task *task, unsigned target_node, int prio)
  836. {
  837. return starpu_idle_prefetch_task_input_prio(task, target_node, -1, prio);
  838. }
  839. int starpu_idle_prefetch_task_input_on_node(struct starpu_task *task, unsigned node)
  840. {
  841. int prio = task->priority;
  842. if (task->workerorder)
  843. prio = INT_MAX - task->workerorder;
  844. return starpu_idle_prefetch_task_input_on_node_prio(task, node, prio);
  845. }
  846. int starpu_prefetch_task_input_for_prio(struct starpu_task *task, unsigned worker, int prio)
  847. {
  848. return starpu_prefetch_task_input_prio(task, -1, worker, prio);
  849. }
  850. int starpu_prefetch_task_input_for(struct starpu_task *task, unsigned worker)
  851. {
  852. int prio = task->priority;
  853. if (task->workerorder)
  854. prio = INT_MAX - task->workerorder;
  855. return starpu_prefetch_task_input_for_prio(task, worker, prio);
  856. }
  857. int starpu_idle_prefetch_task_input_for_prio(struct starpu_task *task, unsigned worker, int prio)
  858. {
  859. return starpu_idle_prefetch_task_input_prio(task, -1, worker, prio);
  860. }
  861. int starpu_idle_prefetch_task_input_for(struct starpu_task *task, unsigned worker)
  862. {
  863. int prio = task->priority;
  864. if (task->workerorder)
  865. prio = INT_MAX - task->workerorder;
  866. return starpu_idle_prefetch_task_input_for_prio(task, worker, prio);
  867. }
  868. struct _starpu_data_replicate *get_replicate(starpu_data_handle_t handle, enum starpu_data_access_mode mode, int workerid, unsigned node)
  869. {
  870. if (mode & (STARPU_SCRATCH|STARPU_REDUX))
  871. {
  872. STARPU_ASSERT(workerid >= 0);
  873. if (!handle->per_worker)
  874. {
  875. _starpu_spin_lock(&handle->header_lock);
  876. if (!handle->per_worker)
  877. _starpu_data_initialize_per_worker(handle);
  878. _starpu_spin_unlock(&handle->header_lock);
  879. }
  880. return &handle->per_worker[workerid];
  881. }
  882. else
  883. /* That's a "normal" buffer (R/W) */
  884. return &handle->per_node[node];
  885. }
  886. /* Callback used when a buffer is send asynchronously to the sink */
  887. static void _starpu_fetch_task_input_cb(void *arg)
  888. {
  889. struct _starpu_worker * worker = (struct _starpu_worker *) arg;
  890. /* increase the number of buffer received */
  891. STARPU_WMB();
  892. (void)STARPU_ATOMIC_ADD(&worker->nb_buffers_transferred, 1);
  893. #ifdef STARPU_SIMGRID
  894. starpu_pthread_queue_broadcast(&_starpu_simgrid_transfer_queue[worker->memory_node]);
  895. #endif
  896. }
  897. /* Synchronously or asynchronously fetch data for a given task (if it's not there already)
  898. * Returns the number of data acquired here. */
  899. /* _starpu_fetch_task_input must be called before
  900. * executing the task. __starpu_push_task_output but be called after the
  901. * execution of the task. */
  902. /* The driver can either just call _starpu_fetch_task_input with async==0,
  903. * or to improve overlapping, it can call _starpu_fetch_task_input with
  904. * async==1, then wait for transfers to complete, then call
  905. * _starpu_fetch_task_input_tail to complete the fetch. */
  906. int _starpu_fetch_task_input(struct starpu_task *task, struct _starpu_job *j, int async)
  907. {
  908. struct _starpu_worker *worker = _starpu_get_local_worker_key();
  909. int workerid = worker->workerid;
  910. if (async)
  911. {
  912. worker->task_transferring = task;
  913. worker->nb_buffers_transferred = 0;
  914. if (worker->ntasks <= 1)
  915. _STARPU_TRACE_WORKER_START_FETCH_INPUT(NULL, workerid);
  916. }
  917. else
  918. _STARPU_TRACE_START_FETCH_INPUT(NULL);
  919. int profiling = starpu_profiling_status_get();
  920. if (profiling && task->profiling_info)
  921. _starpu_clock_gettime(&task->profiling_info->acquire_data_start_time);
  922. struct _starpu_data_descr *descrs = _STARPU_JOB_GET_ORDERED_BUFFERS(j);
  923. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  924. unsigned nacquires;
  925. unsigned index;
  926. nacquires = 0;
  927. for (index = 0; index < nbuffers; index++)
  928. {
  929. int ret;
  930. starpu_data_handle_t handle = descrs[index].handle;
  931. enum starpu_data_access_mode mode = descrs[index].mode;
  932. int node = _starpu_task_data_get_node_on_worker(task, descrs[index].index, workerid);
  933. /* We set this here for coherency with __starpu_push_task_output */
  934. descrs[index].node = node;
  935. if (mode == STARPU_NONE ||
  936. (mode & ((1<<STARPU_MODE_SHIFT) - 1)) >= STARPU_ACCESS_MODE_MAX ||
  937. (mode >> STARPU_MODE_SHIFT) >= (STARPU_SHIFTED_MODE_MAX >> STARPU_MODE_SHIFT))
  938. STARPU_ASSERT_MSG(0, "mode %d (0x%x) is bogus\n", mode, mode);
  939. struct _starpu_data_replicate *local_replicate;
  940. if (index && descrs[index-1].handle == descrs[index].handle)
  941. /* We have already took this data, skip it. This
  942. * depends on ordering putting writes before reads, see
  943. * _starpu_compar_handles */
  944. continue;
  945. local_replicate = get_replicate(handle, mode, workerid, node);
  946. if (async)
  947. {
  948. ret = _starpu_fetch_data_on_node(handle, node, local_replicate, mode, 0, STARPU_FETCH, 1,
  949. _starpu_fetch_task_input_cb, worker, 0, "_starpu_fetch_task_input");
  950. #ifdef STARPU_SIMGRID
  951. if (_starpu_simgrid_fetching_input_cost())
  952. starpu_sleep(0.000001);
  953. #endif
  954. if (STARPU_UNLIKELY(ret))
  955. {
  956. /* Ooops, not enough memory, make worker wait for these for now, and the synchronous call will finish by forcing eviction*/
  957. worker->nb_buffers_totransfer = nacquires;
  958. _starpu_set_worker_status(worker, STATUS_WAITING);
  959. return 0;
  960. }
  961. }
  962. else
  963. {
  964. ret = fetch_data(handle, node, local_replicate, mode, 0);
  965. #ifdef STARPU_SIMGRID
  966. if (_starpu_simgrid_fetching_input_cost())
  967. starpu_sleep(0.000001);
  968. #endif
  969. if (STARPU_UNLIKELY(ret))
  970. goto enomem;
  971. }
  972. nacquires++;
  973. }
  974. if (async)
  975. {
  976. worker->nb_buffers_totransfer = nacquires;
  977. _starpu_set_worker_status(worker, STATUS_WAITING);
  978. return 0;
  979. }
  980. _starpu_fetch_task_input_tail(task, j, worker);
  981. return 0;
  982. enomem:
  983. _STARPU_TRACE_END_FETCH_INPUT(NULL);
  984. _STARPU_DISP("something went wrong with buffer %u\n", index);
  985. /* try to unreference all the input that were successfully taken */
  986. unsigned index2;
  987. for (index2 = 0; index2 < index; index2++)
  988. {
  989. starpu_data_handle_t handle = descrs[index2].handle;
  990. enum starpu_data_access_mode mode = descrs[index2].mode;
  991. int node = descrs[index].node;
  992. struct _starpu_data_replicate *local_replicate;
  993. if (index2 && descrs[index2-1].handle == descrs[index2].handle)
  994. /* We have already released this data, skip it. This
  995. * depends on ordering putting writes before reads, see
  996. * _starpu_compar_handles */
  997. continue;
  998. local_replicate = get_replicate(handle, mode, workerid, node);
  999. _starpu_release_data_on_node(handle, 0, STARPU_NONE, local_replicate);
  1000. }
  1001. return -1;
  1002. }
  1003. /* Now that we have taken the data locks in locking order, fill the codelet interfaces in function order. */
  1004. void _starpu_fetch_task_input_tail(struct starpu_task *task, struct _starpu_job *j, struct _starpu_worker *worker)
  1005. {
  1006. int workerid = worker->workerid;
  1007. int profiling = starpu_profiling_status_get();
  1008. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  1009. struct _starpu_data_descr *descrs = _STARPU_JOB_GET_ORDERED_BUFFERS(j);
  1010. unsigned index;
  1011. unsigned long total_size = 0;
  1012. for (index = 0; index < nbuffers; index++)
  1013. {
  1014. starpu_data_handle_t handle = descrs[index].handle;
  1015. enum starpu_data_access_mode mode = descrs[index].mode;
  1016. int node = descrs[index].node;
  1017. struct _starpu_data_replicate *local_replicate;
  1018. local_replicate = get_replicate(handle, mode, workerid, node);
  1019. _starpu_spin_lock(&handle->header_lock);
  1020. if (local_replicate->mc)
  1021. {
  1022. if (task->prefetched && local_replicate->initialized &&
  1023. /* See prefetch conditions in
  1024. * starpu_prefetch_task_input_on_node_prio and alike */
  1025. !(mode & (STARPU_SCRATCH|STARPU_REDUX)) &&
  1026. (mode & STARPU_R))
  1027. {
  1028. /* Allocations or transfer prefetchs should have been done by now and marked
  1029. * this mc as needed for us.
  1030. * Now that we added a reference for the task, we can relieve that. */
  1031. /* Note: the replicate might have been evicted in between, thus not 100% sure
  1032. * that our prefetch request is still recorded here. */
  1033. if (local_replicate->nb_tasks_prefetch > 0)
  1034. local_replicate->nb_tasks_prefetch--;
  1035. }
  1036. }
  1037. _starpu_spin_unlock(&handle->header_lock);
  1038. _STARPU_TASK_SET_INTERFACE(task , local_replicate->data_interface, descrs[index].index);
  1039. /* If the replicate was not initialized yet, we have to do it now */
  1040. if (!(mode & STARPU_SCRATCH) && !local_replicate->initialized)
  1041. _starpu_redux_init_data_replicate(handle, local_replicate, workerid);
  1042. #ifdef STARPU_USE_FXT
  1043. total_size += _starpu_data_get_size(handle);
  1044. #endif
  1045. }
  1046. _STARPU_TRACE_DATA_LOAD(workerid,total_size);
  1047. if (profiling && task->profiling_info)
  1048. _starpu_clock_gettime(&task->profiling_info->acquire_data_end_time);
  1049. _STARPU_TRACE_END_FETCH_INPUT(NULL);
  1050. }
  1051. /* Release task data dependencies */
  1052. void __starpu_push_task_output(struct _starpu_job *j)
  1053. {
  1054. #ifdef STARPU_OPENMP
  1055. STARPU_ASSERT(!j->continuation);
  1056. #endif
  1057. int profiling = starpu_profiling_status_get();
  1058. struct starpu_task *task = j->task;
  1059. if (profiling && task->profiling_info)
  1060. _starpu_clock_gettime(&task->profiling_info->release_data_start_time);
  1061. struct _starpu_data_descr *descrs = _STARPU_JOB_GET_ORDERED_BUFFERS(j);
  1062. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  1063. int workerid = starpu_worker_get_id();
  1064. unsigned index;
  1065. for (index = 0; index < nbuffers; index++)
  1066. {
  1067. starpu_data_handle_t handle = descrs[index].handle;
  1068. enum starpu_data_access_mode mode = descrs[index].mode;
  1069. int node = descrs[index].node;
  1070. struct _starpu_data_replicate *local_replicate = NULL;
  1071. if (index && descrs[index-1].handle == descrs[index].handle)
  1072. /* We have already released this data, skip it. This
  1073. * depends on ordering putting writes before reads, see
  1074. * _starpu_compar_handles */
  1075. continue;
  1076. if (node != -1)
  1077. local_replicate = get_replicate(handle, mode, workerid, node);
  1078. /* Keep a reference for future
  1079. * _starpu_release_task_enforce_sequential_consistency call */
  1080. _starpu_spin_lock(&handle->header_lock);
  1081. handle->busy_count++;
  1082. if (node == -1)
  1083. {
  1084. /* NOWHERE case, just notify dependencies */
  1085. if (!_starpu_notify_data_dependencies(handle, STARPU_NONE))
  1086. _starpu_spin_unlock(&handle->header_lock);
  1087. }
  1088. else
  1089. {
  1090. _starpu_spin_unlock(&handle->header_lock);
  1091. _starpu_release_data_on_node(handle, 0, STARPU_NONE, local_replicate);
  1092. }
  1093. }
  1094. if (profiling && task->profiling_info)
  1095. _starpu_clock_gettime(&task->profiling_info->release_data_end_time);
  1096. }
  1097. /* Version for a driver running on a worker: we show the driver state in the trace */
  1098. void _starpu_push_task_output(struct _starpu_job *j)
  1099. {
  1100. _STARPU_TRACE_START_PUSH_OUTPUT(NULL);
  1101. __starpu_push_task_output(j);
  1102. _STARPU_TRACE_END_PUSH_OUTPUT(NULL);
  1103. }
  1104. struct fetch_nowhere_wrapper
  1105. {
  1106. struct _starpu_job *j;
  1107. unsigned pending;
  1108. };
  1109. static void _starpu_fetch_nowhere_task_input_cb(void *arg);
  1110. /* Asynchronously fetch data for a task which will have no content */
  1111. void _starpu_fetch_nowhere_task_input(struct _starpu_job *j)
  1112. {
  1113. int profiling = starpu_profiling_status_get();
  1114. struct starpu_task *task = j->task;
  1115. if (profiling && task->profiling_info)
  1116. _starpu_clock_gettime(&task->profiling_info->acquire_data_start_time);
  1117. struct _starpu_data_descr *descrs = _STARPU_JOB_GET_ORDERED_BUFFERS(j);
  1118. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  1119. unsigned nfetchbuffers = 0;
  1120. struct fetch_nowhere_wrapper *wrapper;
  1121. unsigned index;
  1122. for (index = 0; index < nbuffers; index++)
  1123. {
  1124. /* Note here we just follow what was requested, and not use _starpu_task_data_get_node* */
  1125. int node = -1;
  1126. if (task->cl->specific_nodes)
  1127. node = STARPU_CODELET_GET_NODE(task->cl, descrs[index].index);
  1128. descrs[index].node = node;
  1129. if (node != -1)
  1130. nfetchbuffers++;
  1131. }
  1132. if (!nfetchbuffers)
  1133. {
  1134. /* Nothing to fetch actually, already finished! */
  1135. __starpu_push_task_output(j);
  1136. _starpu_handle_job_termination(j);
  1137. _STARPU_LOG_OUT_TAG("handle_job_termination");
  1138. return;
  1139. }
  1140. _STARPU_MALLOC(wrapper, (sizeof(*wrapper)));
  1141. wrapper->j = j;
  1142. /* +1 for the call below */
  1143. wrapper->pending = nfetchbuffers + 1;
  1144. for (index = 0; index < nbuffers; index++)
  1145. {
  1146. starpu_data_handle_t handle = descrs[index].handle;
  1147. enum starpu_data_access_mode mode = descrs[index].mode;
  1148. int node = descrs[index].node;
  1149. if (node == -1)
  1150. continue;
  1151. if (mode == STARPU_NONE ||
  1152. (mode & ((1<<STARPU_MODE_SHIFT) - 1)) >= STARPU_ACCESS_MODE_MAX ||
  1153. (mode >> STARPU_MODE_SHIFT) >= (STARPU_SHIFTED_MODE_MAX >> STARPU_MODE_SHIFT))
  1154. STARPU_ASSERT_MSG(0, "mode %d (0x%x) is bogus\n", mode, mode);
  1155. STARPU_ASSERT(mode != STARPU_SCRATCH && mode != STARPU_REDUX);
  1156. struct _starpu_data_replicate *local_replicate;
  1157. local_replicate = get_replicate(handle, mode, -1, node);
  1158. _starpu_fetch_data_on_node(handle, node, local_replicate, mode, 0, STARPU_FETCH, 1, _starpu_fetch_nowhere_task_input_cb, wrapper, 0, "_starpu_fetch_nowhere_task_input");
  1159. }
  1160. if (profiling && task->profiling_info)
  1161. _starpu_clock_gettime(&task->profiling_info->acquire_data_end_time);
  1162. /* Finished working with the task, release our reference */
  1163. _starpu_fetch_nowhere_task_input_cb(wrapper);
  1164. }
  1165. static void _starpu_fetch_nowhere_task_input_cb(void *arg)
  1166. {
  1167. /* One more transfer finished */
  1168. struct fetch_nowhere_wrapper *wrapper = arg;
  1169. unsigned pending = STARPU_ATOMIC_ADD(&wrapper->pending, -1);
  1170. ANNOTATE_HAPPENS_BEFORE(&wrapper->pending);
  1171. if (pending == 0)
  1172. {
  1173. ANNOTATE_HAPPENS_AFTER(&wrapper->pending);
  1174. /* Finished transferring, task is over */
  1175. struct _starpu_job *j = wrapper->j;
  1176. free(wrapper);
  1177. __starpu_push_task_output(j);
  1178. _starpu_handle_job_termination(j);
  1179. _STARPU_LOG_OUT_TAG("handle_job_termination");
  1180. }
  1181. }
  1182. /* NB : this value can only be an indication of the status of a data
  1183. at some point, but there is no strong garantee ! */
  1184. unsigned starpu_data_is_on_node(starpu_data_handle_t handle, unsigned node)
  1185. {
  1186. unsigned ret = 0;
  1187. // XXX : this is just a hint, so we don't take the lock ...
  1188. // STARPU_PTHREAD_SPIN_LOCK(&handle->header_lock);
  1189. if (handle->per_node[node].state != STARPU_INVALID)
  1190. {
  1191. ret = 1;
  1192. }
  1193. else
  1194. {
  1195. unsigned i;
  1196. unsigned nnodes = starpu_memory_nodes_get_count();
  1197. for (i = 0; i < nnodes; i++)
  1198. {
  1199. if ((handle->per_node[node].requested & (1UL << i)) || handle->per_node[node].request[i])
  1200. ret = 1;
  1201. }
  1202. }
  1203. // STARPU_PTHREAD_SPIN_UNLOCK(&handle->header_lock);
  1204. return ret;
  1205. }