coherency.c 43 KB

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