coherency.c 45 KB

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