coherency.c 42 KB

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