coherency.c 26 KB

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