coherency.c 26 KB

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