coherency.c 38 KB

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