coherency.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2014 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014 Centre National de la Recherche Scientifique
  5. * Copyright (C) 2014 Inria
  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 <core/dependencies/data_concurrency.h>
  23. #include <core/disk.h>
  24. #include <profiling/profiling.h>
  25. #include <math.h>
  26. #include <core/task.h>
  27. #include <starpu_scheduler.h>
  28. #include <core/workers.h>
  29. #ifdef STARPU_SIMGRID
  30. #include <msg/msg.h>
  31. #include <core/simgrid.h>
  32. #endif
  33. static int link_supports_direct_transfers(starpu_data_handle_t handle, unsigned src_node, unsigned dst_node, unsigned *handling_node);
  34. int _starpu_select_src_node(starpu_data_handle_t handle, unsigned destination)
  35. {
  36. int src_node = -1;
  37. unsigned i;
  38. unsigned nnodes = starpu_memory_nodes_get_count();
  39. /* first find a valid copy, either a STARPU_OWNER or a STARPU_SHARED */
  40. unsigned node;
  41. size_t size = _starpu_data_get_size(handle);
  42. double cost = INFINITY;
  43. unsigned src_node_mask = 0;
  44. for (node = 0; node < nnodes; node++)
  45. {
  46. if (handle->per_node[node].state != STARPU_INVALID)
  47. {
  48. /* we found a copy ! */
  49. src_node_mask |= (1<<node);
  50. }
  51. }
  52. if (src_node_mask == 0 && handle->init_cl)
  53. {
  54. /* No copy yet, but applicationg told us how to build it. */
  55. return -1;
  56. }
  57. /* we should have found at least one copy ! */
  58. STARPU_ASSERT_MSG(src_node_mask != 0, "The data for this handle is requested, but this handle does not have a valid value. Perhaps some initialization task is missing?");
  59. /* Without knowing the size, we won't know the cost */
  60. if (!size)
  61. cost = 0;
  62. /* Check whether we have transfer cost for all nodes, if so, take the minimum */
  63. if (cost)
  64. for (i = 0; i < nnodes; i++)
  65. {
  66. if (src_node_mask & (1<<i))
  67. {
  68. double time = starpu_transfer_predict(i, destination, size);
  69. unsigned handling_node;
  70. /* Avoid indirect transfers */
  71. if (!link_supports_direct_transfers(handle, i, destination, &handling_node))
  72. continue;
  73. if (_STARPU_IS_ZERO(time))
  74. {
  75. /* No estimation, will have to revert to dumb strategy */
  76. cost = 0.0;
  77. break;
  78. }
  79. else if (time < cost)
  80. {
  81. cost = time;
  82. src_node = i;
  83. }
  84. }
  85. }
  86. if (cost && src_node != -1)
  87. /* Could estimate through cost, return that */
  88. return src_node;
  89. int i_ram = -1;
  90. int i_gpu = -1;
  91. int i_disk = -1;
  92. /* Revert to dumb strategy: take RAM unless only a GPU has it */
  93. for (i = 0; i < nnodes; i++)
  94. {
  95. if (src_node_mask & (1<<i))
  96. {
  97. int (*can_copy)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, unsigned handling_node) = handle->ops->copy_methods->can_copy;
  98. /* Avoid transfers which the interface does not want */
  99. if (can_copy)
  100. {
  101. void *src_interface = handle->per_node[i].data_interface;
  102. void *dst_interface = handle->per_node[destination].data_interface;
  103. unsigned handling_node;
  104. if (!link_supports_direct_transfers(handle, i, destination, &handling_node))
  105. {
  106. /* Avoid through RAM if the interface does not want it */
  107. void *ram_interface = handle->per_node[STARPU_MAIN_RAM].data_interface;
  108. if ((!can_copy(src_interface, i, ram_interface, STARPU_MAIN_RAM, i)
  109. && !can_copy(src_interface, i, ram_interface, STARPU_MAIN_RAM, STARPU_MAIN_RAM))
  110. || (!can_copy(ram_interface, STARPU_MAIN_RAM, dst_interface, destination, STARPU_MAIN_RAM)
  111. && !can_copy(ram_interface, STARPU_MAIN_RAM, dst_interface, destination, destination)))
  112. continue;
  113. }
  114. }
  115. /* however GPU are expensive sources, really !
  116. * Unless peer transfer is supported (and it would then have been selected above).
  117. * Other should be ok */
  118. if (starpu_node_get_kind(i) == STARPU_CUDA_RAM ||
  119. starpu_node_get_kind(i) == STARPU_OPENCL_RAM ||
  120. starpu_node_get_kind(i) == STARPU_MIC_RAM)
  121. i_gpu = i;
  122. if (starpu_node_get_kind(i) == STARPU_CPU_RAM ||
  123. starpu_node_get_kind(i) == STARPU_SCC_RAM ||
  124. starpu_node_get_kind(i) == STARPU_SCC_SHM)
  125. i_ram = i;
  126. if (starpu_node_get_kind(i) == STARPU_DISK_RAM)
  127. i_disk = i;
  128. }
  129. }
  130. /* we have to use cpu_ram in first */
  131. if (i_ram != -1)
  132. src_node = i_ram;
  133. /* no luck we have to use the disk memory */
  134. else if (i_gpu != -1)
  135. src_node = i_gpu;
  136. else
  137. src_node = i_disk;
  138. STARPU_ASSERT(src_node != -1);
  139. return src_node;
  140. }
  141. /* this may be called once the data is fetched with header and STARPU_RW-lock hold */
  142. void _starpu_update_data_state(starpu_data_handle_t handle,
  143. struct _starpu_data_replicate *requesting_replicate,
  144. enum starpu_data_access_mode mode)
  145. {
  146. /* There is nothing to do for relaxed coherency modes (scratch or
  147. * reductions) */
  148. if (!(mode & STARPU_RW))
  149. return;
  150. unsigned nnodes = starpu_memory_nodes_get_count();
  151. /* the data is present now */
  152. unsigned requesting_node = requesting_replicate->memory_node;
  153. requesting_replicate->requested &= ~(1UL << requesting_node);
  154. if (mode & STARPU_W)
  155. {
  156. /* the requesting node now has the only valid copy */
  157. unsigned node;
  158. for (node = 0; node < nnodes; node++)
  159. handle->per_node[node].state = STARPU_INVALID;
  160. requesting_replicate->state = STARPU_OWNER;
  161. }
  162. else
  163. { /* read only */
  164. if (requesting_replicate->state != STARPU_OWNER)
  165. {
  166. /* there was at least another copy of the data */
  167. unsigned node;
  168. for (node = 0; node < nnodes; node++)
  169. {
  170. struct _starpu_data_replicate *replicate = &handle->per_node[node];
  171. if (replicate->state != STARPU_INVALID)
  172. replicate->state = STARPU_SHARED;
  173. }
  174. requesting_replicate->state = STARPU_SHARED;
  175. }
  176. }
  177. }
  178. static int worker_supports_direct_access(unsigned node, unsigned handling_node)
  179. {
  180. /* only support disk <-> ram and disk <-> disk */
  181. if (starpu_node_get_kind(node) == STARPU_DISK_RAM || starpu_node_get_kind(handling_node) == STARPU_DISK_RAM)
  182. return 0;
  183. if (node == handling_node)
  184. return 1;
  185. if (!_starpu_memory_node_get_nworkers(handling_node))
  186. /* No worker to process the request from that node */
  187. return 0;
  188. int type = starpu_node_get_kind(node);
  189. switch (type)
  190. {
  191. case STARPU_CUDA_RAM:
  192. {
  193. /* GPUs not always allow direct remote access: if CUDA4
  194. * is enabled, we allow two CUDA devices to communicate. */
  195. #ifdef STARPU_SIMGRID
  196. if (starpu_node_get_kind(handling_node) == STARPU_CUDA_RAM)
  197. {
  198. char name[16];
  199. msg_host_t host;
  200. const char* cuda_memcpy_peer;
  201. snprintf(name, sizeof(name), "CUDA%d", _starpu_memory_node_get_devid(handling_node));
  202. host = _starpu_simgrid_get_host_by_name(name);
  203. cuda_memcpy_peer = MSG_host_get_property_value(host, "memcpy_peer");
  204. return cuda_memcpy_peer && atoll(cuda_memcpy_peer);
  205. }
  206. else
  207. return 0;
  208. #elif defined(HAVE_CUDA_MEMCPY_PEER)
  209. /* simgrid */
  210. enum starpu_node_kind kind = starpu_node_get_kind(handling_node);
  211. return kind == STARPU_CUDA_RAM;
  212. #else /* HAVE_CUDA_MEMCPY_PEER */
  213. /* Direct GPU-GPU transfers are not allowed in general */
  214. return 0;
  215. #endif /* HAVE_CUDA_MEMCPY_PEER */
  216. }
  217. case STARPU_OPENCL_RAM:
  218. return 0;
  219. case STARPU_MIC_RAM:
  220. /* We don't handle direct MIC-MIC transfers yet */
  221. return 0;
  222. case STARPU_SCC_RAM:
  223. return 1;
  224. default:
  225. return 1;
  226. }
  227. }
  228. static int link_supports_direct_transfers(starpu_data_handle_t handle, unsigned src_node, unsigned dst_node, unsigned *handling_node)
  229. {
  230. int (*can_copy)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, unsigned handling_node) = handle->ops->copy_methods->can_copy;
  231. void *src_interface = handle->per_node[src_node].data_interface;
  232. void *dst_interface = handle->per_node[dst_node].data_interface;
  233. /* XXX That's a hack until we fix cudaMemcpy3DPeerAsync in the block interface
  234. * Perhaps not all data interface provide a direct GPU-GPU transfer
  235. * method ! */
  236. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  237. if (src_node != dst_node && starpu_node_get_kind(src_node) == STARPU_CUDA_RAM && starpu_node_get_kind(dst_node) == STARPU_CUDA_RAM)
  238. {
  239. const struct starpu_data_copy_methods *copy_methods = handle->ops->copy_methods;
  240. if (!copy_methods->cuda_to_cuda_async && !copy_methods->any_to_any)
  241. return 0;
  242. }
  243. #endif
  244. /* Note: with CUDA, performance seems a bit better when issuing the transfer from the destination (tested without GPUDirect, but GPUDirect probably behave the same) */
  245. if (worker_supports_direct_access(src_node, dst_node) && (!can_copy || can_copy(src_interface, src_node, dst_interface, dst_node, dst_node)))
  246. {
  247. *handling_node = dst_node;
  248. return 1;
  249. }
  250. if (worker_supports_direct_access(dst_node, src_node) && (!can_copy || can_copy(src_interface, src_node, dst_interface, dst_node, src_node)))
  251. {
  252. *handling_node = src_node;
  253. return 1;
  254. }
  255. /* Link between disk and ram */
  256. if ((starpu_node_get_kind(src_node) == STARPU_DISK_RAM && starpu_node_get_kind(dst_node) == STARPU_CPU_RAM) ||
  257. (starpu_node_get_kind(src_node) == STARPU_CPU_RAM && starpu_node_get_kind(dst_node) == STARPU_DISK_RAM))
  258. {
  259. /* FIXME: not necessarily a worker :/ */
  260. *handling_node = STARPU_MAIN_RAM;
  261. return 1;
  262. }
  263. /* link between disk and disk, and they have the same kind */
  264. if (_starpu_is_same_kind_disk(src_node, dst_node))
  265. return 1;
  266. return 0;
  267. }
  268. /* Determines the path of a request : each hop is defined by (src,dst) and the
  269. * node that handles the hop. The returned value indicates the number of hops,
  270. * and the max_len is the maximum number of hops (ie. the size of the
  271. * src_nodes, dst_nodes and handling_nodes arrays. */
  272. static int determine_request_path(starpu_data_handle_t handle,
  273. unsigned src_node, unsigned dst_node,
  274. enum starpu_data_access_mode mode, int max_len,
  275. unsigned *src_nodes, unsigned *dst_nodes,
  276. unsigned *handling_nodes)
  277. {
  278. if (!(mode & STARPU_R))
  279. {
  280. /* The destination node should only allocate the data, no transfer is required */
  281. STARPU_ASSERT(max_len >= 1);
  282. src_nodes[0] = STARPU_MAIN_RAM; // ignored
  283. dst_nodes[0] = dst_node;
  284. handling_nodes[0] = dst_node;
  285. return 1;
  286. }
  287. unsigned handling_node;
  288. int link_is_valid = link_supports_direct_transfers(handle, src_node, dst_node, &handling_node);
  289. if (!link_is_valid)
  290. {
  291. int (*can_copy)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, unsigned handling_node) = handle->ops->copy_methods->can_copy;
  292. void *src_interface = handle->per_node[src_node].data_interface;
  293. void *dst_interface = handle->per_node[dst_node].data_interface;
  294. /* We need an intermediate hop to implement data staging
  295. * through main memory. */
  296. STARPU_ASSERT(max_len >= 2);
  297. /* GPU -> RAM */
  298. src_nodes[0] = src_node;
  299. dst_nodes[0] = STARPU_MAIN_RAM;
  300. if (starpu_node_get_kind(src_node) == STARPU_DISK_RAM)
  301. /* Disks don't have their own driver thread */
  302. handling_nodes[0] = dst_node;
  303. else if (!can_copy || can_copy(src_interface, src_node, dst_interface, dst_node, src_node))
  304. {
  305. handling_nodes[0] = src_node;
  306. }
  307. else
  308. {
  309. STARPU_ASSERT_MSG(can_copy(src_interface, src_node, dst_interface, dst_node, dst_node), "interface %d refuses all kinds of transfers from node %u to node %u\n", handle->ops->interfaceid, src_node, dst_node);
  310. handling_nodes[0] = dst_node;
  311. }
  312. /* RAM -> GPU */
  313. src_nodes[1] = STARPU_MAIN_RAM;
  314. dst_nodes[1] = dst_node;
  315. if (starpu_node_get_kind(dst_node) == STARPU_DISK_RAM)
  316. /* Disks don't have their own driver thread */
  317. handling_nodes[1] = src_node;
  318. else if (!can_copy || can_copy(src_interface, src_node, dst_interface, dst_node, dst_node))
  319. {
  320. handling_nodes[1] = dst_node;
  321. }
  322. else
  323. {
  324. STARPU_ASSERT_MSG(can_copy(src_interface, src_node, dst_interface, dst_node, src_node), "interface %d refuses all kinds of transfers from node %u to node %u\n", handle->ops->interfaceid, src_node, dst_node);
  325. handling_nodes[1] = src_node;
  326. }
  327. return 2;
  328. }
  329. else
  330. {
  331. STARPU_ASSERT(max_len >= 1);
  332. src_nodes[0] = src_node;
  333. dst_nodes[0] = dst_node;
  334. handling_nodes[0] = handling_node;
  335. #if !defined(HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID)
  336. STARPU_ASSERT(!(mode & STARPU_R) || starpu_node_get_kind(src_node) != STARPU_CUDA_RAM || starpu_node_get_kind(dst_node) != STARPU_CUDA_RAM);
  337. #endif
  338. return 1;
  339. }
  340. }
  341. /* handle->lock should be taken. r is returned locked. The node parameter
  342. * indicate either the source of the request, or the destination for a
  343. * write-only request. */
  344. 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)
  345. {
  346. struct _starpu_data_request *r;
  347. r = replicate->request[node];
  348. if (r)
  349. {
  350. _starpu_spin_checklocked(&r->handle->header_lock);
  351. _starpu_spin_lock(&r->lock);
  352. /* perhaps we need to "upgrade" the request */
  353. if (is_prefetch < r->prefetch)
  354. _starpu_update_prefetch_status(r);
  355. if (mode & STARPU_R)
  356. {
  357. /* in case the exisiting request did not imply a memory
  358. * transfer yet, we have to take a second refcnt now
  359. * for the source, in addition to the refcnt for the
  360. * destination
  361. * (so that the source remains valid) */
  362. if (!(r->mode & STARPU_R))
  363. {
  364. replicate->refcnt++;
  365. replicate->handle->busy_count++;
  366. }
  367. r->mode = (enum starpu_data_access_mode) ((int) r->mode | (int) STARPU_R);
  368. }
  369. if (mode & STARPU_W)
  370. r->mode = (enum starpu_data_access_mode) ((int) r->mode | (int) STARPU_W);
  371. }
  372. return r;
  373. }
  374. /*
  375. * This function is called when the data is needed on the local node, this
  376. * returns a pointer to the local copy
  377. *
  378. * R STARPU_W STARPU_RW
  379. * Owner OK OK OK
  380. * Shared OK 1 1
  381. * Invalid 2 3 4
  382. *
  383. * case 1 : shared + (read)write :
  384. * no data copy but shared->Invalid/Owner
  385. * case 2 : invalid + read :
  386. * data copy + invalid->shared + owner->shared (STARPU_ASSERT(there is a valid))
  387. * case 3 : invalid + write :
  388. * no data copy + invalid->owner + (owner,shared)->invalid
  389. * case 4 : invalid + R/STARPU_W :
  390. * data copy + if (STARPU_W) (invalid->owner + owner->invalid)
  391. * else (invalid,owner->shared)
  392. */
  393. struct _starpu_data_request *_starpu_create_request_to_fetch_data(starpu_data_handle_t handle,
  394. struct _starpu_data_replicate *dst_replicate,
  395. enum starpu_data_access_mode mode, unsigned is_prefetch,
  396. unsigned async,
  397. void (*callback_func)(void *), void *callback_arg)
  398. {
  399. /* We don't care about commuting for data requests, that was handled before. */
  400. mode &= ~STARPU_COMMUTE;
  401. /* This function is called with handle's header lock taken */
  402. _starpu_spin_checklocked(&handle->header_lock);
  403. unsigned requesting_node = dst_replicate->memory_node;
  404. if (dst_replicate->state != STARPU_INVALID)
  405. {
  406. #ifdef STARPU_MEMORY_STATS
  407. enum _starpu_cache_state old_state = dst_replicate->state;
  408. #endif
  409. /* the data is already available so we can stop */
  410. _starpu_update_data_state(handle, dst_replicate, mode);
  411. _starpu_msi_cache_hit(requesting_node);
  412. #ifdef STARPU_MEMORY_STATS
  413. _starpu_memory_handle_stats_cache_hit(handle, requesting_node);
  414. /* XXX Broken ? */
  415. if (old_state == STARPU_SHARED
  416. && dst_replicate->state == STARPU_OWNER)
  417. _starpu_memory_handle_stats_shared_to_owner(handle, requesting_node);
  418. #endif
  419. _starpu_memchunk_recently_used(dst_replicate->mc, requesting_node);
  420. _starpu_spin_unlock(&handle->header_lock);
  421. if (callback_func)
  422. callback_func(callback_arg);
  423. _STARPU_LOG_OUT_TAG("data available");
  424. return NULL;
  425. }
  426. _starpu_msi_cache_miss(requesting_node);
  427. /* the only remaining situation is that the local copy was invalid */
  428. STARPU_ASSERT(dst_replicate->state == STARPU_INVALID);
  429. /* find someone who already has the data */
  430. int src_node = 0;
  431. if (mode & STARPU_R)
  432. {
  433. src_node = _starpu_select_src_node(handle, requesting_node);
  434. STARPU_ASSERT(src_node != (int) requesting_node);
  435. if (src_node < 0)
  436. {
  437. /* We will create it, no need to read an existing value */
  438. mode &= ~STARPU_R;
  439. }
  440. }
  441. else
  442. {
  443. /* 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 */
  444. if (mode & STARPU_W)
  445. dst_replicate->initialized = 1;
  446. if (requesting_node == STARPU_MAIN_RAM) {
  447. /* And this is the main RAM, really no need for a
  448. * request, just allocate */
  449. if (_starpu_allocate_memory_on_node(handle, dst_replicate, is_prefetch) == 0)
  450. {
  451. _starpu_update_data_state(handle, dst_replicate, mode);
  452. _starpu_spin_unlock(&handle->header_lock);
  453. if (callback_func)
  454. callback_func(callback_arg);
  455. _STARPU_LOG_OUT_TAG("data immediately allocated");
  456. return NULL;
  457. }
  458. }
  459. }
  460. /* We can safely assume that there won't be more than 2 hops in the
  461. * current implementation */
  462. unsigned src_nodes[4], dst_nodes[4], handling_nodes[4];
  463. int nhops = determine_request_path(handle, src_node, requesting_node, mode, 4,
  464. src_nodes, dst_nodes, handling_nodes);
  465. STARPU_ASSERT(nhops >= 1 && nhops <= 4);
  466. struct _starpu_data_request *requests[nhops];
  467. /* Did we reuse a request for that hop ? */
  468. int reused_requests[nhops];
  469. /* Construct an array with a list of requests, possibly reusing existing requests */
  470. int hop;
  471. for (hop = 0; hop < nhops; hop++)
  472. {
  473. struct _starpu_data_request *r;
  474. unsigned hop_src_node = src_nodes[hop];
  475. unsigned hop_dst_node = dst_nodes[hop];
  476. unsigned hop_handling_node = handling_nodes[hop];
  477. struct _starpu_data_replicate *hop_src_replicate;
  478. struct _starpu_data_replicate *hop_dst_replicate;
  479. /* Only the first request is independant */
  480. unsigned ndeps = (hop == 0)?0:1;
  481. hop_src_replicate = &handle->per_node[hop_src_node];
  482. hop_dst_replicate = (hop != nhops - 1)?&handle->per_node[hop_dst_node]:dst_replicate;
  483. /* Try to reuse a request if possible */
  484. r = _starpu_search_existing_data_request(hop_dst_replicate,
  485. (mode & STARPU_R)?hop_src_node:hop_dst_node,
  486. mode, is_prefetch);
  487. reused_requests[hop] = !!r;
  488. if (!r)
  489. {
  490. /* Create a new request if there was no request to reuse */
  491. r = _starpu_create_data_request(handle, hop_src_replicate,
  492. hop_dst_replicate, hop_handling_node,
  493. mode, ndeps, is_prefetch);
  494. }
  495. requests[hop] = r;
  496. }
  497. /* Chain these requests */
  498. for (hop = 0; hop < nhops; hop++)
  499. {
  500. struct _starpu_data_request *r;
  501. r = requests[hop];
  502. if (hop != nhops - 1)
  503. {
  504. if (!reused_requests[hop + 1])
  505. {
  506. r->next_req[r->next_req_count++] = requests[hop + 1];
  507. STARPU_ASSERT(r->next_req_count <= STARPU_MAXNODES);
  508. }
  509. }
  510. else
  511. /* The last request will perform the callback after termination */
  512. _starpu_data_request_append_callback(r, callback_func, callback_arg);
  513. if (reused_requests[hop])
  514. _starpu_spin_unlock(&r->lock);
  515. }
  516. if (!async)
  517. requests[nhops - 1]->refcnt++;
  518. /* we only submit the first request, the remaining will be
  519. * automatically submitted afterward */
  520. if (!reused_requests[0])
  521. _starpu_post_data_request(requests[0], handling_nodes[0]);
  522. return requests[nhops - 1];
  523. }
  524. int _starpu_fetch_data_on_node(starpu_data_handle_t handle, struct _starpu_data_replicate *dst_replicate,
  525. enum starpu_data_access_mode mode, unsigned detached, unsigned async,
  526. void (*callback_func)(void *), void *callback_arg)
  527. {
  528. unsigned local_node = _starpu_memory_node_get_local_key();
  529. _STARPU_LOG_IN();
  530. int cpt = 0;
  531. while (cpt < STARPU_SPIN_MAXTRY && _starpu_spin_trylock(&handle->header_lock))
  532. {
  533. cpt++;
  534. _starpu_datawizard_progress(local_node, 1);
  535. }
  536. if (cpt == STARPU_SPIN_MAXTRY)
  537. _starpu_spin_lock(&handle->header_lock);
  538. if (!detached)
  539. {
  540. /* Take a reference which will be released by _starpu_release_data_on_node */
  541. dst_replicate->refcnt++;
  542. dst_replicate->handle->busy_count++;
  543. }
  544. struct _starpu_data_request *r;
  545. r = _starpu_create_request_to_fetch_data(handle, dst_replicate, mode,
  546. detached, async, callback_func, callback_arg);
  547. /* If no request was created, the handle was already up-to-date on the
  548. * node. In this case, _starpu_create_request_to_fetch_data has already
  549. * unlocked the header. */
  550. if (!r)
  551. return 0;
  552. _starpu_spin_unlock(&handle->header_lock);
  553. int ret = async?0:_starpu_wait_data_request_completion(r, 1);
  554. _STARPU_LOG_OUT();
  555. return ret;
  556. }
  557. static int prefetch_data_on_node(starpu_data_handle_t handle, struct _starpu_data_replicate *replicate, enum starpu_data_access_mode mode)
  558. {
  559. return _starpu_fetch_data_on_node(handle, replicate, mode, 1, 1, NULL, NULL);
  560. }
  561. static int fetch_data(starpu_data_handle_t handle, struct _starpu_data_replicate *replicate, enum starpu_data_access_mode mode)
  562. {
  563. return _starpu_fetch_data_on_node(handle, replicate, mode, 0, 0, NULL, NULL);
  564. }
  565. uint32_t _starpu_get_data_refcnt(starpu_data_handle_t handle, unsigned node)
  566. {
  567. return handle->per_node[node].refcnt;
  568. }
  569. size_t _starpu_data_get_size(starpu_data_handle_t handle)
  570. {
  571. return handle->ops->get_size(handle);
  572. }
  573. uint32_t _starpu_data_get_footprint(starpu_data_handle_t handle)
  574. {
  575. return handle->footprint;
  576. }
  577. /* in case the data was accessed on a write mode, do not forget to
  578. * make it accessible again once it is possible ! */
  579. void _starpu_release_data_on_node(starpu_data_handle_t handle, uint32_t default_wt_mask, struct _starpu_data_replicate *replicate)
  580. {
  581. uint32_t wt_mask;
  582. wt_mask = default_wt_mask | handle->wt_mask;
  583. wt_mask &= (1<<starpu_memory_nodes_get_count())-1;
  584. /* Note that it is possible that there is no valid copy of the data (if
  585. * starpu_data_invalidate was called for instance). In that case, we do
  586. * not enforce any write-through mechanism. */
  587. unsigned memory_node = replicate->memory_node;
  588. if (replicate->state != STARPU_INVALID && handle->current_mode & STARPU_W)
  589. if ((wt_mask & ~(1<<memory_node)))
  590. _starpu_write_through_data(handle, memory_node, wt_mask);
  591. unsigned local_node = _starpu_memory_node_get_local_key();
  592. int cpt = 0;
  593. while (cpt < STARPU_SPIN_MAXTRY && _starpu_spin_trylock(&handle->header_lock))
  594. {
  595. cpt++;
  596. _starpu_datawizard_progress(local_node, 1);
  597. }
  598. if (cpt == STARPU_SPIN_MAXTRY)
  599. _starpu_spin_lock(&handle->header_lock);
  600. /* Release refcnt taken by fetch_data_on_node */
  601. replicate->refcnt--;
  602. STARPU_ASSERT_MSG(replicate->refcnt >= 0, "handle %p released too many times", handle);
  603. STARPU_ASSERT_MSG(handle->busy_count > 0, "handle %p released too many times", handle);
  604. handle->busy_count--;
  605. if (!_starpu_notify_data_dependencies(handle))
  606. _starpu_spin_unlock(&handle->header_lock);
  607. }
  608. static void _starpu_set_data_requested_flag_if_needed(starpu_data_handle_t handle, struct _starpu_data_replicate *replicate)
  609. {
  610. unsigned local_node = _starpu_memory_node_get_local_key();
  611. int cpt = 0;
  612. while (cpt < STARPU_SPIN_MAXTRY && _starpu_spin_trylock(&handle->header_lock))
  613. {
  614. cpt++;
  615. _starpu_datawizard_progress(local_node, 1);
  616. }
  617. if (cpt == STARPU_SPIN_MAXTRY)
  618. _starpu_spin_lock(&handle->header_lock);
  619. if (replicate->state == STARPU_INVALID)
  620. {
  621. unsigned dst_node = replicate->memory_node;
  622. replicate->requested |= 1UL << dst_node;
  623. }
  624. _starpu_spin_unlock(&handle->header_lock);
  625. }
  626. int starpu_prefetch_task_input_on_node(struct starpu_task *task, unsigned node)
  627. {
  628. STARPU_ASSERT(!task->prefetched);
  629. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  630. unsigned index;
  631. for (index = 0; index < nbuffers; index++)
  632. {
  633. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, index);
  634. enum starpu_data_access_mode mode = STARPU_TASK_GET_MODE(task, index);
  635. if (mode & (STARPU_SCRATCH|STARPU_REDUX))
  636. continue;
  637. struct _starpu_data_replicate *replicate = &handle->per_node[node];
  638. prefetch_data_on_node(handle, replicate, mode);
  639. _starpu_set_data_requested_flag_if_needed(handle, replicate);
  640. }
  641. return 0;
  642. }
  643. static struct _starpu_data_replicate *get_replicate(starpu_data_handle_t handle, enum starpu_data_access_mode mode, int workerid, unsigned node)
  644. {
  645. if (mode & (STARPU_SCRATCH|STARPU_REDUX))
  646. return &handle->per_worker[workerid];
  647. else
  648. /* That's a "normal" buffer (R/W) */
  649. return &handle->per_node[node];
  650. }
  651. int _starpu_fetch_task_input(struct _starpu_job *j)
  652. {
  653. _STARPU_TRACE_START_FETCH_INPUT(NULL);
  654. int profiling = starpu_profiling_status_get();
  655. struct starpu_task *task = j->task;
  656. if (profiling && task->profiling_info)
  657. _starpu_clock_gettime(&task->profiling_info->acquire_data_start_time);
  658. struct _starpu_data_descr *descrs = _STARPU_JOB_GET_ORDERED_BUFFERS(j);
  659. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  660. unsigned local_memory_node = _starpu_memory_node_get_local_key();
  661. int workerid = starpu_worker_get_id();
  662. #ifdef STARPU_USE_FXT
  663. unsigned long total_size = 0;
  664. #endif
  665. unsigned index;
  666. for (index = 0; index < nbuffers; index++)
  667. {
  668. int ret;
  669. starpu_data_handle_t handle = descrs[index].handle;
  670. enum starpu_data_access_mode mode = descrs[index].mode;
  671. int node = descrs[index].node;
  672. if (node == -1)
  673. node = local_memory_node;
  674. struct _starpu_data_replicate *local_replicate;
  675. if (index && descrs[index-1].handle == descrs[index].handle)
  676. /* We have already took this data, skip it. This
  677. * depends on ordering putting writes before reads, see
  678. * _starpu_compar_handles */
  679. continue;
  680. local_replicate = get_replicate(handle, mode, workerid, node);
  681. ret = fetch_data(handle, local_replicate, mode);
  682. if (STARPU_UNLIKELY(ret))
  683. goto enomem;
  684. #ifdef STARPU_USE_FXT
  685. total_size += _starpu_data_get_size(handle);
  686. #endif
  687. }
  688. _STARPU_TRACE_DATA_LOAD(workerid,total_size);
  689. /* Now that we have taken the data locks in locking order, fill the codelet interfaces in function order. */
  690. for (index = 0; index < nbuffers; index++)
  691. {
  692. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, index);
  693. enum starpu_data_access_mode mode = STARPU_TASK_GET_MODE(task, index);
  694. int node = descrs[index].node;
  695. if (node == -1)
  696. node = local_memory_node;
  697. struct _starpu_data_replicate *local_replicate;
  698. local_replicate = get_replicate(handle, mode, workerid, node);
  699. _STARPU_TASK_SET_INTERFACE(task , local_replicate->data_interface, index);
  700. /* If the replicate was not initialized yet, we have to do it now */
  701. if (!(mode & STARPU_SCRATCH) && !local_replicate->initialized)
  702. _starpu_redux_init_data_replicate(handle, local_replicate, workerid);
  703. }
  704. if (profiling && task->profiling_info)
  705. _starpu_clock_gettime(&task->profiling_info->acquire_data_end_time);
  706. _STARPU_TRACE_END_FETCH_INPUT(NULL);
  707. return 0;
  708. enomem:
  709. _STARPU_TRACE_END_FETCH_INPUT(NULL);
  710. _STARPU_DISP("something went wrong with buffer %u\n", index);
  711. /* try to unreference all the input that were successfully taken */
  712. unsigned index2;
  713. for (index2 = 0; index2 < index; index2++)
  714. {
  715. starpu_data_handle_t handle = descrs[index2].handle;
  716. enum starpu_data_access_mode mode = descrs[index2].mode;
  717. int node = descrs[index].node;
  718. if (node == -1)
  719. node = local_memory_node;
  720. struct _starpu_data_replicate *local_replicate;
  721. if (index2 && descrs[index2-1].handle == descrs[index2].handle)
  722. /* We have already released this data, skip it. This
  723. * depends on ordering putting writes before reads, see
  724. * _starpu_compar_handles */
  725. continue;
  726. local_replicate = get_replicate(handle, mode, workerid, node);
  727. _starpu_release_data_on_node(handle, 0, local_replicate);
  728. }
  729. return -1;
  730. }
  731. void _starpu_push_task_output(struct _starpu_job *j)
  732. {
  733. #ifdef STARPU_OPENMP
  734. STARPU_ASSERT(!j->continuation);
  735. #endif
  736. _STARPU_TRACE_START_PUSH_OUTPUT(NULL);
  737. int profiling = starpu_profiling_status_get();
  738. struct starpu_task *task = j->task;
  739. if (profiling && task->profiling_info)
  740. _starpu_clock_gettime(&task->profiling_info->release_data_start_time);
  741. struct _starpu_data_descr *descrs = _STARPU_JOB_GET_ORDERED_BUFFERS(j);
  742. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  743. int workerid = starpu_worker_get_id();
  744. unsigned local_memory_node = _starpu_memory_node_get_local_key();
  745. unsigned index;
  746. for (index = 0; index < nbuffers; index++)
  747. {
  748. starpu_data_handle_t handle = descrs[index].handle;
  749. enum starpu_data_access_mode mode = descrs[index].mode;
  750. int node = descrs[index].node;
  751. if (node == -1)
  752. node = local_memory_node;
  753. struct _starpu_data_replicate *local_replicate;
  754. if (index && descrs[index-1].handle == descrs[index].handle)
  755. /* We have already released this data, skip it. This
  756. * depends on ordering putting writes before reads, see
  757. * _starpu_compar_handles */
  758. continue;
  759. local_replicate = get_replicate(handle, mode, workerid, node);
  760. /* Keep a reference for future
  761. * _starpu_release_task_enforce_sequential_consistency call */
  762. _starpu_spin_lock(&handle->header_lock);
  763. handle->busy_count++;
  764. _starpu_spin_unlock(&handle->header_lock);
  765. _starpu_release_data_on_node(handle, 0, local_replicate);
  766. }
  767. if (profiling && task->profiling_info)
  768. _starpu_clock_gettime(&task->profiling_info->release_data_end_time);
  769. _STARPU_TRACE_END_PUSH_OUTPUT(NULL);
  770. }
  771. /* NB : this value can only be an indication of the status of a data
  772. at some point, but there is no strong garantee ! */
  773. unsigned _starpu_is_data_present_or_requested(starpu_data_handle_t handle, unsigned node)
  774. {
  775. unsigned ret = 0;
  776. // XXX : this is just a hint, so we don't take the lock ...
  777. // STARPU_PTHREAD_SPIN_LOCK(&handle->header_lock);
  778. if (handle->per_node[node].state != STARPU_INVALID)
  779. {
  780. ret = 1;
  781. }
  782. else
  783. {
  784. unsigned i;
  785. unsigned nnodes = starpu_memory_nodes_get_count();
  786. for (i = 0; i < nnodes; i++)
  787. {
  788. if ((handle->per_node[node].requested & (1UL << i)) || handle->per_node[node].request[i])
  789. ret = 1;
  790. }
  791. }
  792. // STARPU_PTHREAD_SPIN_UNLOCK(&handle->header_lock);
  793. return ret;
  794. }
  795. void _starpu_data_set_unregister_hook(starpu_data_handle_t handle, _starpu_data_handle_unregister_hook func)
  796. {
  797. handle->unregister_hook = func;
  798. }