filters.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2008-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2010 Mehdi Juhoor
  5. * Copyright (C) 2013 Thibaut Lambert
  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. //#define STARPU_VERBOSE
  19. #include <datawizard/filters.h>
  20. #include <datawizard/footprint.h>
  21. #include <datawizard/interfaces/data_interface.h>
  22. #include <datawizard/memory_nodes.h>
  23. #include <core/task.h>
  24. /*
  25. * This function applies a data filter on all the elements of a partition
  26. */
  27. static void map_filter(starpu_data_handle_t root_handle, struct starpu_data_filter *f)
  28. {
  29. /* we need to apply the data filter on all leaf of the tree */
  30. if (root_handle->nchildren == 0)
  31. {
  32. /* this is a leaf */
  33. starpu_data_partition(root_handle, f);
  34. }
  35. else
  36. {
  37. /* try to apply the data filter recursively */
  38. unsigned child;
  39. for (child = 0; child < root_handle->nchildren; child++)
  40. {
  41. starpu_data_handle_t handle_child = starpu_data_get_child(root_handle, child);
  42. map_filter(handle_child, f);
  43. }
  44. }
  45. }
  46. void starpu_data_vmap_filters(starpu_data_handle_t root_handle, unsigned nfilters, va_list pa)
  47. {
  48. unsigned i;
  49. for (i = 0; i < nfilters; i++)
  50. {
  51. struct starpu_data_filter *next_filter;
  52. next_filter = va_arg(pa, struct starpu_data_filter *);
  53. STARPU_ASSERT(next_filter);
  54. map_filter(root_handle, next_filter);
  55. }
  56. }
  57. void starpu_data_map_filters(starpu_data_handle_t root_handle, unsigned nfilters, ...)
  58. {
  59. va_list pa;
  60. va_start(pa, nfilters);
  61. starpu_data_vmap_filters(root_handle, nfilters, pa);
  62. va_end(pa);
  63. }
  64. void fstarpu_data_map_filters(starpu_data_handle_t root_handle, int nfilters, struct starpu_data_filter **filters)
  65. {
  66. int i;
  67. assert(nfilters >= 0);
  68. for (i = 0; i < nfilters; i++)
  69. {
  70. struct starpu_data_filter *next_filter = filters[i];
  71. STARPU_ASSERT(next_filter);
  72. map_filter(root_handle, next_filter);
  73. }
  74. }
  75. int starpu_data_get_nb_children(starpu_data_handle_t handle)
  76. {
  77. return handle->nchildren;
  78. }
  79. starpu_data_handle_t starpu_data_get_child(starpu_data_handle_t handle, unsigned i)
  80. {
  81. STARPU_ASSERT_MSG(handle->nchildren != 0, "Data %p has to be partitioned before accessing children", handle);
  82. STARPU_ASSERT_MSG(i < handle->nchildren, "Invalid child index %u in handle %p, maximum %u", i, handle, handle->nchildren);
  83. return &handle->children[i];
  84. }
  85. /*
  86. * example starpu_data_get_sub_data(starpu_data_handle_t root_handle, 3, 42, 0, 1);
  87. */
  88. starpu_data_handle_t starpu_data_get_sub_data(starpu_data_handle_t root_handle, unsigned depth, ... )
  89. {
  90. va_list pa;
  91. va_start(pa, depth);
  92. starpu_data_handle_t handle = starpu_data_vget_sub_data(root_handle, depth, pa);
  93. va_end(pa);
  94. return handle;
  95. }
  96. starpu_data_handle_t starpu_data_vget_sub_data(starpu_data_handle_t root_handle, unsigned depth, va_list pa )
  97. {
  98. STARPU_ASSERT(root_handle);
  99. starpu_data_handle_t current_handle = root_handle;
  100. /* the variable number of argument must correlate the depth in the tree */
  101. unsigned i;
  102. for (i = 0; i < depth; i++)
  103. {
  104. unsigned next_child;
  105. next_child = va_arg(pa, unsigned);
  106. STARPU_ASSERT_MSG(current_handle->nchildren != 0, "Data %p has to be partitioned before accessing children", current_handle);
  107. STARPU_ASSERT_MSG(next_child < current_handle->nchildren, "Bogus child number %u, data %p only has %u children", next_child, current_handle, current_handle->nchildren);
  108. current_handle = &current_handle->children[next_child];
  109. }
  110. return current_handle;
  111. }
  112. starpu_data_handle_t fstarpu_data_get_sub_data(starpu_data_handle_t root_handle, int depth, int *indices)
  113. {
  114. STARPU_ASSERT(root_handle);
  115. starpu_data_handle_t current_handle = root_handle;
  116. STARPU_ASSERT(depth >= 0);
  117. /* the variable number of argument must correlate the depth in the tree */
  118. int i;
  119. for (i = 0; i < depth; i++)
  120. {
  121. int next_child;
  122. next_child = indices[i];
  123. STARPU_ASSERT(next_child >= 0);
  124. STARPU_ASSERT_MSG(current_handle->nchildren != 0, "Data %p has to be partitioned before accessing children", current_handle);
  125. STARPU_ASSERT_MSG((unsigned) next_child < current_handle->nchildren, "Bogus child number %d, data %p only has %u children", next_child, current_handle, current_handle->nchildren);
  126. current_handle = &current_handle->children[next_child];
  127. }
  128. return current_handle;
  129. }
  130. static unsigned _starpu_data_partition_nparts(starpu_data_handle_t initial_handle, struct starpu_data_filter *f)
  131. {
  132. /* how many parts ? */
  133. if (f->get_nchildren)
  134. return f->get_nchildren(f, initial_handle);
  135. else
  136. return f->nchildren;
  137. }
  138. static void _starpu_data_partition(starpu_data_handle_t initial_handle, starpu_data_handle_t *childrenp, unsigned nparts, struct starpu_data_filter *f, int inherit_state)
  139. {
  140. unsigned i;
  141. unsigned node;
  142. /* first take care to properly lock the data header */
  143. _starpu_spin_lock(&initial_handle->header_lock);
  144. initial_handle->nplans++;
  145. STARPU_ASSERT_MSG(nparts > 0, "Partitioning data %p in 0 piece does not make sense", initial_handle);
  146. /* allocate the children */
  147. if (inherit_state)
  148. {
  149. _STARPU_CALLOC(initial_handle->children, nparts, sizeof(struct _starpu_data_state));
  150. /* this handle now has children */
  151. initial_handle->nchildren = nparts;
  152. }
  153. for (node = 0; node < STARPU_MAXNODES; node++)
  154. {
  155. if (initial_handle->per_node[node].state != STARPU_INVALID)
  156. break;
  157. }
  158. if (node == STARPU_MAXNODES)
  159. {
  160. /* This is lazy allocation, allocate it now in main RAM, so as
  161. * to have somewhere to gather pieces later */
  162. /* FIXME: mark as unevictable! */
  163. int home_node = initial_handle->home_node;
  164. if (home_node < 0 || (starpu_node_get_kind(home_node) != STARPU_CPU_RAM))
  165. home_node = STARPU_MAIN_RAM;
  166. int ret = _starpu_allocate_memory_on_node(initial_handle, &initial_handle->per_node[home_node], STARPU_FETCH);
  167. #ifdef STARPU_DEVEL
  168. #warning we should reclaim memory if allocation failed
  169. #endif
  170. STARPU_ASSERT(!ret);
  171. }
  172. for (node = 0; node < STARPU_MAXNODES; node++)
  173. _starpu_data_unregister_ram_pointer(initial_handle, node);
  174. if (nparts && !inherit_state)
  175. {
  176. STARPU_ASSERT_MSG(childrenp, "Passing NULL pointer for parameter childrenp while parameter inherit_state is 0");
  177. }
  178. for (i = 0; i < nparts; i++)
  179. {
  180. starpu_data_handle_t child;
  181. if (inherit_state)
  182. child = &initial_handle->children[i];
  183. else
  184. child = childrenp[i];
  185. STARPU_ASSERT(child);
  186. struct starpu_data_interface_ops *ops;
  187. /* each child may have his own interface type */
  188. /* what's this child's interface ? */
  189. if (f->get_child_ops)
  190. ops = f->get_child_ops(f, i);
  191. else
  192. ops = initial_handle->ops;
  193. /* As most of the fields must be initialized at NULL, let's put
  194. * 0 everywhere */
  195. memset(child, 0, sizeof(*child));
  196. _starpu_data_handle_init(child, ops, initial_handle->mf_node);
  197. //child->nchildren = 0;
  198. //child->nplans = 0;
  199. //child->switch_cl = NULL;
  200. //child->partitioned = 0;
  201. //child->readonly = 0;
  202. child->active = inherit_state;
  203. //child->active_ro = 0;
  204. //child->mpi_data = NULL;
  205. child->root_handle = initial_handle->root_handle;
  206. child->father_handle = initial_handle;
  207. //child->active_children = NULL;
  208. //child->active_readonly_children = NULL;
  209. //child->nactive_readonly_children = 0;
  210. child->nsiblings = nparts;
  211. if (inherit_state)
  212. {
  213. //child->siblings = NULL;
  214. }
  215. else
  216. child->siblings = childrenp;
  217. child->sibling_index = i;
  218. child->depth = initial_handle->depth + 1;
  219. child->is_not_important = initial_handle->is_not_important;
  220. child->wt_mask = initial_handle->wt_mask;
  221. child->home_node = initial_handle->home_node;
  222. /* initialize the chunk lock */
  223. _starpu_data_requester_prio_list_init(&child->req_list);
  224. _starpu_data_requester_prio_list_init(&child->reduction_req_list);
  225. //child->reduction_tmp_handles = NULL;
  226. //child->write_invalidation_req = NULL;
  227. //child->refcnt = 0;
  228. //child->unlocking_reqs = 0;
  229. //child->busy_count = 0;
  230. //child->busy_waiting = 0;
  231. STARPU_PTHREAD_MUTEX_INIT0(&child->busy_mutex, NULL);
  232. STARPU_PTHREAD_COND_INIT0(&child->busy_cond, NULL);
  233. //child->reduction_refcnt = 0;
  234. _starpu_spin_init(&child->header_lock);
  235. child->sequential_consistency = initial_handle->sequential_consistency;
  236. child->initialized = initial_handle->initialized;
  237. child->ooc = initial_handle->ooc;
  238. STARPU_PTHREAD_MUTEX_INIT0(&child->sequential_consistency_mutex, NULL);
  239. child->last_submitted_mode = STARPU_R;
  240. //child->last_sync_task = NULL;
  241. //child->last_submitted_accessors.task = NULL;
  242. child->last_submitted_accessors.next = &child->last_submitted_accessors;
  243. child->last_submitted_accessors.prev = &child->last_submitted_accessors;
  244. //child->post_sync_tasks = NULL;
  245. /* Tell helgrind that the race in _starpu_unlock_post_sync_tasks is fine */
  246. STARPU_HG_DISABLE_CHECKING(child->post_sync_tasks_cnt);
  247. //child->post_sync_tasks_cnt = 0;
  248. /* The methods used for reduction are propagated to the
  249. * children. */
  250. child->redux_cl = initial_handle->redux_cl;
  251. child->init_cl = initial_handle->init_cl;
  252. #ifdef STARPU_USE_FXT
  253. //child->last_submitted_ghost_sync_id_is_valid = 0;
  254. //child->last_submitted_ghost_sync_id = 0;
  255. //child->last_submitted_ghost_accessors_id = NULL;
  256. #endif
  257. if (_starpu_global_arbiter)
  258. /* Just for testing purpose */
  259. starpu_data_assign_arbiter(child, _starpu_global_arbiter);
  260. else
  261. {
  262. //child->arbiter = NULL;
  263. }
  264. _starpu_data_requester_prio_list_init0(&child->arbitered_req_list);
  265. for (node = 0; node < STARPU_MAXNODES; node++)
  266. {
  267. struct _starpu_data_replicate *initial_replicate;
  268. struct _starpu_data_replicate *child_replicate;
  269. initial_replicate = &initial_handle->per_node[node];
  270. child_replicate = &child->per_node[node];
  271. if (inherit_state)
  272. child_replicate->state = initial_replicate->state;
  273. else
  274. child_replicate->state = STARPU_INVALID;
  275. if (inherit_state || !initial_replicate->automatically_allocated)
  276. child_replicate->allocated = initial_replicate->allocated;
  277. else
  278. {
  279. //child_replicate->allocated = 0;
  280. }
  281. /* Do not allow memory reclaiming within the child for parent bits */
  282. //child_replicate->automatically_allocated = 0;
  283. //child_replicate->refcnt = 0;
  284. child_replicate->memory_node = node;
  285. //child_replicate->relaxed_coherency = 0;
  286. if (inherit_state)
  287. child_replicate->initialized = initial_replicate->initialized;
  288. else
  289. {
  290. //child_replicate->initialized = 0;
  291. }
  292. /* update the interface */
  293. void *initial_interface = starpu_data_get_interface_on_node(initial_handle, node);
  294. void *child_interface = starpu_data_get_interface_on_node(child, node);
  295. STARPU_ASSERT_MSG(!(!inherit_state && child_replicate->automatically_allocated && child_replicate->allocated), "partition planning is currently not supported when handle has some automatically allocated buffers");
  296. f->filter_func(initial_interface, child_interface, f, i, nparts);
  297. }
  298. //child->per_worker = NULL;
  299. //child->user_data = NULL;
  300. /* We compute the size and the footprint of the child once and
  301. * store it in the handle */
  302. child->footprint = _starpu_compute_data_footprint(child);
  303. for (node = 0; node < STARPU_MAXNODES; node++)
  304. {
  305. if (starpu_node_get_kind(node) != STARPU_CPU_RAM)
  306. continue;
  307. void *ptr = starpu_data_handle_to_pointer(child, node);
  308. if (ptr != NULL)
  309. _starpu_data_register_ram_pointer(child, ptr);
  310. }
  311. _STARPU_TRACE_HANDLE_DATA_REGISTER(child);
  312. }
  313. /* now let the header */
  314. _starpu_spin_unlock(&initial_handle->header_lock);
  315. }
  316. static
  317. void _starpu_empty_codelet_function(void *buffers[], void *args)
  318. {
  319. (void) buffers; // unused;
  320. (void) args; // unused;
  321. }
  322. void starpu_data_unpartition(starpu_data_handle_t root_handle, unsigned gathering_node)
  323. {
  324. unsigned child;
  325. unsigned worker;
  326. unsigned nworkers = starpu_worker_get_count();
  327. unsigned node;
  328. unsigned sizes[root_handle->nchildren];
  329. void *ptr;
  330. _STARPU_TRACE_START_UNPARTITION(root_handle, gathering_node);
  331. _starpu_spin_lock(&root_handle->header_lock);
  332. STARPU_ASSERT_MSG(root_handle->nchildren != 0, "data %p is not partitioned, can not unpartition it", root_handle);
  333. /* first take all the children lock (in order !) */
  334. for (child = 0; child < root_handle->nchildren; child++)
  335. {
  336. starpu_data_handle_t child_handle = starpu_data_get_child(root_handle, child);
  337. /* make sure the intermediate children is unpartitionned as well */
  338. if (child_handle->nchildren > 0)
  339. starpu_data_unpartition(child_handle, gathering_node);
  340. /* If this is a multiformat handle, we must convert the data now */
  341. #ifdef STARPU_DEVEL
  342. #warning TODO: _starpu_fetch_data_on_node should be doing it
  343. #endif
  344. if (_starpu_data_is_multiformat_handle(child_handle) &&
  345. starpu_node_get_kind(child_handle->mf_node) != STARPU_CPU_RAM)
  346. {
  347. struct starpu_codelet cl =
  348. {
  349. .where = STARPU_CPU,
  350. .cpu_funcs = { _starpu_empty_codelet_function },
  351. .modes = { STARPU_RW },
  352. .nbuffers = 1
  353. };
  354. struct starpu_task *task = starpu_task_create();
  355. task->name = "convert_data";
  356. STARPU_TASK_SET_HANDLE(task, child_handle, 0);
  357. task->cl = &cl;
  358. task->synchronous = 1;
  359. if (_starpu_task_submit_internally(task) != 0)
  360. _STARPU_ERROR("Could not submit the conversion task while unpartitionning\n");
  361. }
  362. int ret;
  363. /* for now we pretend that the RAM is almost unlimited and that gathering
  364. * data should be possible from the node that does the unpartionning ... we
  365. * don't want to have the programming deal with memory shortage at that time,
  366. * really */
  367. /* Acquire the child data on the gathering node. This will trigger collapsing any reduction */
  368. ret = starpu_data_acquire_on_node(child_handle, gathering_node, STARPU_RW);
  369. STARPU_ASSERT(ret == 0);
  370. starpu_data_release_on_node(child_handle, gathering_node);
  371. _starpu_spin_lock(&child_handle->header_lock);
  372. child_handle->busy_waiting = 1;
  373. _starpu_spin_unlock(&child_handle->header_lock);
  374. /* Wait for all requests to finish (notably WT requests) */
  375. STARPU_PTHREAD_MUTEX_LOCK(&child_handle->busy_mutex);
  376. while (1)
  377. {
  378. /* Here helgrind would shout that this an unprotected access,
  379. * but this is actually fine: all threads who do busy_count--
  380. * are supposed to call _starpu_data_check_not_busy, which will
  381. * wake us up through the busy_mutex/busy_cond. */
  382. if (!child_handle->busy_count)
  383. break;
  384. /* This is woken by _starpu_data_check_not_busy, always called
  385. * after decrementing busy_count */
  386. STARPU_PTHREAD_COND_WAIT(&child_handle->busy_cond, &child_handle->busy_mutex);
  387. }
  388. STARPU_PTHREAD_MUTEX_UNLOCK(&child_handle->busy_mutex);
  389. _starpu_spin_lock(&child_handle->header_lock);
  390. sizes[child] = _starpu_data_get_alloc_size(child_handle);
  391. if (child_handle->unregister_hook)
  392. {
  393. child_handle->unregister_hook(child_handle);
  394. }
  395. for (node = 0; node < STARPU_MAXNODES; node++)
  396. _starpu_data_unregister_ram_pointer(child_handle, node);
  397. if (child_handle->per_worker)
  398. {
  399. for (worker = 0; worker < nworkers; worker++)
  400. {
  401. struct _starpu_data_replicate *local = &child_handle->per_worker[worker];
  402. STARPU_ASSERT(local->state == STARPU_INVALID);
  403. if (local->allocated && local->automatically_allocated)
  404. _starpu_request_mem_chunk_removal(child_handle, local, starpu_worker_get_memory_node(worker), sizes[child]);
  405. }
  406. }
  407. _starpu_memory_stats_free(child_handle);
  408. }
  409. for (node = 0; node < STARPU_MAXNODES; node++)
  410. {
  411. if (starpu_node_get_kind(node) != STARPU_CPU_RAM)
  412. continue;
  413. ptr = starpu_data_handle_to_pointer(root_handle, node);
  414. if (ptr != NULL)
  415. _starpu_data_register_ram_pointer(root_handle, ptr);
  416. }
  417. /* the gathering_node should now have a valid copy of all the children.
  418. * For all nodes, if the node had all copies and none was locally
  419. * allocated then the data is still valid there, else, it's invalidated
  420. * for the gathering node, if we have some locally allocated data, we
  421. * copy all the children (XXX this should not happen so we just do not
  422. * do anything since this is transparent ?) */
  423. unsigned still_valid[STARPU_MAXNODES];
  424. /* we do 2 passes : the first pass determines wether the data is still
  425. * valid or not, the second pass is needed to choose between STARPU_SHARED and
  426. * STARPU_OWNER */
  427. unsigned nvalids = 0;
  428. /* still valid ? */
  429. for (node = 0; node < STARPU_MAXNODES; node++)
  430. {
  431. struct _starpu_data_replicate *local;
  432. /* until an issue is found the data is assumed to be valid */
  433. unsigned isvalid = 1;
  434. for (child = 0; child < root_handle->nchildren; child++)
  435. {
  436. starpu_data_handle_t child_handle = starpu_data_get_child(root_handle, child);
  437. local = &child_handle->per_node[node];
  438. if (local->state == STARPU_INVALID || local->automatically_allocated == 1)
  439. {
  440. /* One of the bits is missing or is not inside the parent */
  441. isvalid = 0;
  442. }
  443. if (local->mc && local->allocated && local->automatically_allocated)
  444. /* free the child data copy in a lazy fashion */
  445. _starpu_request_mem_chunk_removal(child_handle, local, node, sizes[child]);
  446. }
  447. local = &root_handle->per_node[node];
  448. if (!local->allocated)
  449. /* Even if we have all the bits, if we don't have the
  450. * whole data, it's not valid */
  451. isvalid = 0;
  452. if (!isvalid && local->mc && local->allocated && local->automatically_allocated)
  453. /* free the data copy in a lazy fashion */
  454. _starpu_request_mem_chunk_removal(root_handle, local, node, _starpu_data_get_alloc_size(root_handle));
  455. /* if there was no invalid copy, the node still has a valid copy */
  456. still_valid[node] = isvalid;
  457. if (isvalid)
  458. nvalids++;
  459. }
  460. /* either shared or owned */
  461. STARPU_ASSERT(nvalids > 0);
  462. enum _starpu_cache_state newstate = (nvalids == 1)?STARPU_OWNER:STARPU_SHARED;
  463. for (node = 0; node < STARPU_MAXNODES; node++)
  464. {
  465. root_handle->per_node[node].state = still_valid[node]?newstate:STARPU_INVALID;
  466. }
  467. for (child = 0; child < root_handle->nchildren; child++)
  468. {
  469. starpu_data_handle_t child_handle = starpu_data_get_child(root_handle, child);
  470. _starpu_data_free_interfaces(child_handle);
  471. _starpu_spin_unlock(&child_handle->header_lock);
  472. _starpu_spin_destroy(&child_handle->header_lock);
  473. }
  474. /* Set the initialized state */
  475. starpu_data_handle_t first_child = starpu_data_get_child(root_handle, 0);
  476. root_handle->initialized = first_child->initialized;
  477. for (child = 1; child < root_handle->nchildren; child++)
  478. {
  479. starpu_data_handle_t child_handle = starpu_data_get_child(root_handle, child);
  480. STARPU_ASSERT_MSG(child_handle->initialized == root_handle->initialized, "Inconsistent state between children initialization");
  481. }
  482. if (root_handle->initialized)
  483. {
  484. for (node = 0; node < STARPU_MAXNODES; node++)
  485. {
  486. struct _starpu_data_replicate *root_replicate;
  487. root_replicate = &root_handle->per_node[node];
  488. root_replicate->initialized = still_valid[node];
  489. }
  490. }
  491. for (child = 0; child < root_handle->nchildren; child++)
  492. {
  493. starpu_data_handle_t child_handle = starpu_data_get_child(root_handle, child);
  494. _starpu_data_clear_implicit(child_handle);
  495. STARPU_PTHREAD_MUTEX_DESTROY(&child_handle->busy_mutex);
  496. STARPU_PTHREAD_COND_DESTROY(&child_handle->busy_cond);
  497. STARPU_PTHREAD_MUTEX_DESTROY(&child_handle->sequential_consistency_mutex);
  498. _STARPU_TRACE_HANDLE_DATA_UNREGISTER(child_handle);
  499. }
  500. /* there is no child anymore */
  501. starpu_data_handle_t children = root_handle->children;
  502. root_handle->children = NULL;
  503. root_handle->nchildren = 0;
  504. root_handle->nplans--;
  505. /* now the parent may be used again so we release the lock */
  506. _starpu_spin_unlock(&root_handle->header_lock);
  507. free(children);
  508. _STARPU_TRACE_END_UNPARTITION(root_handle, gathering_node);
  509. }
  510. void starpu_data_partition(starpu_data_handle_t initial_handle, struct starpu_data_filter *f)
  511. {
  512. unsigned nparts = _starpu_data_partition_nparts(initial_handle, f);
  513. STARPU_ASSERT_MSG(initial_handle->nchildren == 0, "there should not be mutiple filters applied on the same data %p, futher filtering has to be done on children", initial_handle);
  514. STARPU_ASSERT_MSG(initial_handle->nplans == 0, "partition planning and synchronous partitioning is not supported");
  515. initial_handle->children = NULL;
  516. /* Make sure to wait for previous tasks working on the whole data */
  517. starpu_data_acquire_on_node(initial_handle, STARPU_ACQUIRE_NO_NODE, initial_handle->initialized?STARPU_RW:STARPU_W);
  518. starpu_data_release_on_node(initial_handle, STARPU_ACQUIRE_NO_NODE);
  519. _starpu_data_partition(initial_handle, NULL, nparts, f, 1);
  520. }
  521. void starpu_data_partition_plan(starpu_data_handle_t initial_handle, struct starpu_data_filter *f, starpu_data_handle_t *childrenp)
  522. {
  523. unsigned i;
  524. unsigned nparts = _starpu_data_partition_nparts(initial_handle, f);
  525. STARPU_ASSERT_MSG(initial_handle->nchildren == 0, "partition planning and synchronous partitioning is not supported");
  526. STARPU_ASSERT_MSG(initial_handle->sequential_consistency, "partition planning is currently only supported for data with sequential consistency");
  527. struct starpu_codelet *cl = initial_handle->switch_cl;
  528. int home_node = initial_handle->home_node;
  529. starpu_data_handle_t *children;
  530. if (home_node == -1)
  531. /* Nothing better for now */
  532. /* TODO: pass -1, and make _starpu_fetch_nowhere_task_input
  533. * really call _starpu_fetch_data_on_node, and make that update
  534. * the coherency.
  535. */
  536. home_node = STARPU_MAIN_RAM;
  537. _STARPU_MALLOC(children, nparts * sizeof(*children));
  538. for (i = 0; i < nparts; i++)
  539. {
  540. _STARPU_CALLOC(children[i], 1, sizeof(struct _starpu_data_state));
  541. childrenp[i] = children[i];
  542. }
  543. _starpu_data_partition(initial_handle, children, nparts, f, 0);
  544. if (!cl)
  545. {
  546. /* Create a codelet that will make the coherency on the home node */
  547. _STARPU_CALLOC(initial_handle->switch_cl, 1, sizeof(*initial_handle->switch_cl));
  548. cl = initial_handle->switch_cl;
  549. cl->where = STARPU_NOWHERE;
  550. cl->nbuffers = STARPU_VARIABLE_NBUFFERS;
  551. cl->flags = STARPU_CODELET_NOPLANS;
  552. cl->name = "data_partition_switch";
  553. cl->specific_nodes = 1;
  554. }
  555. if (initial_handle->switch_cl_nparts < nparts)
  556. {
  557. /* First initialization, or previous initialization was with fewer parts, enlarge it */
  558. _STARPU_REALLOC(cl->dyn_nodes, (nparts+1) * sizeof(*cl->dyn_nodes));
  559. for (i = initial_handle->switch_cl_nparts; i < nparts+1; i++)
  560. cl->dyn_nodes[i] = home_node;
  561. initial_handle->switch_cl_nparts = nparts;
  562. }
  563. }
  564. void starpu_data_partition_clean(starpu_data_handle_t root_handle, unsigned nparts, starpu_data_handle_t *children)
  565. {
  566. unsigned i;
  567. if (children[0]->active)
  568. {
  569. #ifdef STARPU_DEVEL
  570. #warning FIXME: better choose gathering node
  571. #endif
  572. starpu_data_unpartition_submit(root_handle, nparts, children, STARPU_MAIN_RAM);
  573. }
  574. free(children[0]->siblings);
  575. for (i = 0; i < nparts; i++)
  576. starpu_data_unregister_submit(children[i]);
  577. _starpu_spin_lock(&root_handle->header_lock);
  578. root_handle->nplans--;
  579. _starpu_spin_unlock(&root_handle->header_lock);
  580. }
  581. static
  582. void _starpu_data_partition_submit(starpu_data_handle_t initial_handle, unsigned nparts, starpu_data_handle_t *children, unsigned char *handles_sequential_consistency)
  583. {
  584. unsigned i;
  585. STARPU_ASSERT_MSG(initial_handle->sequential_consistency, "partition planning is currently only supported for data with sequential consistency");
  586. _starpu_spin_lock(&initial_handle->header_lock);
  587. STARPU_ASSERT_MSG(initial_handle->partitioned == 0, "One can't submit several partition plannings at the same time");
  588. STARPU_ASSERT_MSG(initial_handle->readonly == 0, "One can't submit a partition planning while a readonly partitioning is active");
  589. STARPU_ASSERT_MSG(nparts > 0, "One can't partition into 0 parts");
  590. initial_handle->partitioned++;
  591. initial_handle->active_children = children[0]->siblings;
  592. _starpu_spin_unlock(&initial_handle->header_lock);
  593. for (i = 0; i < nparts; i++)
  594. {
  595. _starpu_spin_lock(&children[i]->header_lock);
  596. children[i]->active = 1;
  597. _starpu_spin_unlock(&children[i]->header_lock);
  598. }
  599. if (!initial_handle->initialized)
  600. /* No need for coherency, it is not initialized */
  601. return;
  602. struct starpu_data_descr descr[nparts];
  603. for (i = 0; i < nparts; i++)
  604. {
  605. STARPU_ASSERT_MSG(children[i]->father_handle == initial_handle, "child(%d) %p is partitioned from %p and not from the given parameter %p", i, children[i], children[i]->father_handle, initial_handle);
  606. descr[i].handle = children[i];
  607. descr[i].mode = STARPU_W;
  608. }
  609. /* TODO: assert nparts too */
  610. int ret;
  611. if (handles_sequential_consistency)
  612. ret = starpu_task_insert(initial_handle->switch_cl, STARPU_RW, initial_handle, STARPU_DATA_MODE_ARRAY, descr, nparts,
  613. STARPU_NAME, "partition",
  614. STARPU_HANDLES_SEQUENTIAL_CONSISTENCY, handles_sequential_consistency,
  615. 0);
  616. else
  617. ret = starpu_task_insert(initial_handle->switch_cl, STARPU_RW, initial_handle, STARPU_DATA_MODE_ARRAY, descr, nparts,
  618. STARPU_NAME, "partition",
  619. 0);
  620. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  621. if (!handles_sequential_consistency || handles_sequential_consistency[0])
  622. starpu_data_invalidate_submit(initial_handle);
  623. }
  624. void starpu_data_partition_submit_sequential_consistency(starpu_data_handle_t initial_handle, unsigned nparts, starpu_data_handle_t *children, int sequential_consistency)
  625. {
  626. unsigned i;
  627. unsigned char handles_sequential_consistency[nparts+1];
  628. handles_sequential_consistency[0] = sequential_consistency;
  629. for(i=1 ; i<nparts+1 ; i++) handles_sequential_consistency[i] = children[i-1]->sequential_consistency;
  630. _starpu_data_partition_submit(initial_handle, nparts, children, handles_sequential_consistency);
  631. }
  632. void starpu_data_partition_submit(starpu_data_handle_t initial_handle, unsigned nparts, starpu_data_handle_t *children)
  633. {
  634. _starpu_data_partition_submit(initial_handle, nparts, children, NULL);
  635. }
  636. void starpu_data_partition_readonly_submit(starpu_data_handle_t initial_handle, unsigned nparts, starpu_data_handle_t *children)
  637. {
  638. unsigned i;
  639. STARPU_ASSERT_MSG(initial_handle->sequential_consistency, "partition planning is currently only supported for data with sequential consistency");
  640. _starpu_spin_lock(&initial_handle->header_lock);
  641. STARPU_ASSERT_MSG(initial_handle->partitioned == 0 || initial_handle->readonly, "One can't submit a readonly partition planning at the same time as a readwrite partition planning");
  642. STARPU_ASSERT_MSG(nparts > 0, "One can't partition into 0 parts");
  643. initial_handle->partitioned++;
  644. initial_handle->readonly = 1;
  645. if (initial_handle->nactive_readonly_children < initial_handle->partitioned)
  646. {
  647. _STARPU_REALLOC(initial_handle->active_readonly_children, initial_handle->partitioned * sizeof(initial_handle->active_readonly_children[0]));
  648. initial_handle->nactive_readonly_children = initial_handle->partitioned;
  649. }
  650. initial_handle->active_readonly_children[initial_handle->partitioned-1] = children[0]->siblings;
  651. _starpu_spin_unlock(&initial_handle->header_lock);
  652. for (i = 0; i < nparts; i++)
  653. {
  654. _starpu_spin_lock(&children[i]->header_lock);
  655. children[i]->active = 1;
  656. children[i]->active_ro = 1;
  657. _starpu_spin_unlock(&children[i]->header_lock);
  658. }
  659. STARPU_ASSERT_MSG(initial_handle->initialized, "It is odd to read-only-partition a data which does not have a value yet");
  660. struct starpu_data_descr descr[nparts];
  661. for (i = 0; i < nparts; i++)
  662. {
  663. STARPU_ASSERT_MSG(children[i]->father_handle == initial_handle, "child(%d) %p is partitioned from %p and not from the given parameter %p", i, children[i], children[i]->father_handle, initial_handle);
  664. descr[i].handle = children[i];
  665. descr[i].mode = STARPU_W;
  666. }
  667. /* TODO: assert nparts too */
  668. starpu_task_insert(initial_handle->switch_cl, STARPU_R, initial_handle, STARPU_DATA_MODE_ARRAY, descr, nparts, 0);
  669. }
  670. void starpu_data_partition_readwrite_upgrade_submit(starpu_data_handle_t initial_handle, unsigned nparts, starpu_data_handle_t *children)
  671. {
  672. STARPU_ASSERT_MSG(initial_handle->sequential_consistency, "partition planning is currently only supported for data with sequential consistency");
  673. _starpu_spin_lock(&initial_handle->header_lock);
  674. STARPU_ASSERT_MSG(initial_handle->partitioned == 1, "One can't upgrade a readonly partition planning to readwrite while other readonly partition plannings are active");
  675. STARPU_ASSERT_MSG(initial_handle->readonly == 1, "One can only upgrade a readonly partition planning");
  676. STARPU_ASSERT_MSG(nparts > 0, "One can't partition into 0 parts");
  677. initial_handle->readonly = 0;
  678. initial_handle->active_children = initial_handle->active_readonly_children[0];
  679. initial_handle->active_readonly_children[0] = NULL;
  680. _starpu_spin_unlock(&initial_handle->header_lock);
  681. unsigned i;
  682. struct starpu_data_descr descr[nparts];
  683. for (i = 0; i < nparts; i++)
  684. {
  685. STARPU_ASSERT_MSG(children[i]->father_handle == initial_handle, "child(%d) %p is partitioned from %p and not from the given parameter %p", i, children[i], children[i]->father_handle, initial_handle);
  686. children[i]->active_ro = 0;
  687. descr[i].handle = children[i];
  688. descr[i].mode = STARPU_W;
  689. }
  690. /* TODO: assert nparts too */
  691. starpu_task_insert(initial_handle->switch_cl, STARPU_RW, initial_handle, STARPU_DATA_MODE_ARRAY, descr, nparts, 0);
  692. starpu_data_invalidate_submit(initial_handle);
  693. }
  694. void _starpu_data_unpartition_submit(starpu_data_handle_t initial_handle, unsigned nparts, starpu_data_handle_t *children, int gather_node, unsigned char *handles_sequential_consistency, void (*callback_func)(void *), void *callback_arg)
  695. {
  696. unsigned i;
  697. STARPU_ASSERT_MSG(initial_handle->sequential_consistency, "partition planning is currently only supported for data with sequential consistency");
  698. STARPU_ASSERT_MSG(gather_node == initial_handle->home_node || gather_node == -1, "gathering node different from home node is currently not supported");
  699. _starpu_spin_lock(&initial_handle->header_lock);
  700. STARPU_ASSERT_MSG(initial_handle->partitioned >= 1, "No partition planning is active for handle %p", initial_handle);
  701. STARPU_ASSERT_MSG(nparts > 0, "One can't partition into 0 parts");
  702. if (initial_handle->readonly)
  703. {
  704. /* Replace this children set with the last set in the list of readonly children sets */
  705. for (i = 0; i < initial_handle->partitioned-1; i++)
  706. {
  707. if (initial_handle->active_readonly_children[i] == children[0]->siblings)
  708. {
  709. initial_handle->active_readonly_children[i] = initial_handle->active_readonly_children[initial_handle->partitioned-1];
  710. initial_handle->active_readonly_children[initial_handle->partitioned-1] = NULL;
  711. break;
  712. }
  713. }
  714. }
  715. else
  716. {
  717. initial_handle->active_children = NULL;
  718. }
  719. initial_handle->partitioned--;
  720. if (!initial_handle->partitioned)
  721. initial_handle->readonly = 0;
  722. initial_handle->active_children = NULL;
  723. _starpu_spin_unlock(&initial_handle->header_lock);
  724. for (i = 0; i < nparts; i++)
  725. {
  726. _starpu_spin_lock(&children[i]->header_lock);
  727. children[i]->active = 0;
  728. children[i]->active_ro = 0;
  729. _starpu_spin_unlock(&children[i]->header_lock);
  730. }
  731. unsigned n;
  732. struct starpu_data_descr descr[nparts];
  733. for (i = 0, n = 0; i < nparts; i++)
  734. {
  735. STARPU_ASSERT_MSG(children[i]->father_handle == initial_handle, "child(%d) %p is partitioned from %p and not from the given parameter %p", i, children[i], children[i]->father_handle, initial_handle);
  736. if (!children[i]->initialized)
  737. /* Dropped value, do not care about coherency for this one */
  738. continue;
  739. descr[n].handle = children[i];
  740. descr[n].mode = STARPU_RW;
  741. n++;
  742. }
  743. /* TODO: assert nparts too */
  744. int ret;
  745. if (handles_sequential_consistency)
  746. ret = starpu_task_insert(initial_handle->switch_cl, STARPU_W, initial_handle, STARPU_DATA_MODE_ARRAY, descr, n,
  747. STARPU_NAME, "unpartition",
  748. STARPU_HANDLES_SEQUENTIAL_CONSISTENCY, handles_sequential_consistency,
  749. STARPU_CALLBACK_WITH_ARG_NFREE, callback_func, callback_arg,
  750. 0);
  751. else
  752. ret = starpu_task_insert(initial_handle->switch_cl, STARPU_W, initial_handle, STARPU_DATA_MODE_ARRAY, descr, n,
  753. STARPU_NAME, "unpartition",
  754. STARPU_CALLBACK_WITH_ARG_NFREE, callback_func, callback_arg,
  755. 0);
  756. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  757. for (i = 0; i < nparts; i++)
  758. {
  759. if (!handles_sequential_consistency || handles_sequential_consistency[i+1])
  760. starpu_data_invalidate_submit(children[i]);
  761. }
  762. }
  763. void starpu_data_unpartition_submit(starpu_data_handle_t initial_handle, unsigned nparts, starpu_data_handle_t *children, int gather_node)
  764. {
  765. _starpu_data_unpartition_submit(initial_handle, nparts, children, gather_node, NULL, NULL, NULL);
  766. }
  767. void starpu_data_unpartition_submit_sequential_consistency_cb(starpu_data_handle_t initial_handle, unsigned nparts, starpu_data_handle_t *children, int gather_node, int sequential_consistency, void (*callback_func)(void *), void *callback_arg)
  768. {
  769. unsigned i;
  770. unsigned char handles_sequential_consistency[nparts+1];
  771. handles_sequential_consistency[0] = sequential_consistency;
  772. for(i=1 ; i<nparts+1 ; i++) handles_sequential_consistency[i] = children[i-1]->sequential_consistency;
  773. _starpu_data_unpartition_submit(initial_handle, nparts, children, gather_node, handles_sequential_consistency, callback_func, callback_arg);
  774. }
  775. void starpu_data_unpartition_submit_sequential_consistency(starpu_data_handle_t initial_handle, unsigned nparts, starpu_data_handle_t *children, int gather_node, int sequential_consistency)
  776. {
  777. unsigned i;
  778. unsigned char handles_sequential_consistency[nparts+1];
  779. handles_sequential_consistency[0] = sequential_consistency;
  780. for(i=1 ; i<nparts+1 ; i++) handles_sequential_consistency[i] = children[i-1]->sequential_consistency;
  781. _starpu_data_unpartition_submit(initial_handle, nparts, children, gather_node, handles_sequential_consistency, NULL, NULL);
  782. }
  783. void starpu_data_unpartition_readonly_submit(starpu_data_handle_t initial_handle, unsigned nparts, starpu_data_handle_t *children, int gather_node)
  784. {
  785. STARPU_ASSERT_MSG(initial_handle->sequential_consistency, "partition planning is currently only supported for data with sequential consistency");
  786. STARPU_ASSERT_MSG(gather_node == initial_handle->home_node || gather_node == -1, "gathering node different from home node is currently not supported");
  787. _starpu_spin_lock(&initial_handle->header_lock);
  788. STARPU_ASSERT_MSG(initial_handle->partitioned >= 1, "No partition planning is active for handle %p", initial_handle);
  789. STARPU_ASSERT_MSG(nparts > 0, "One can't partition into 0 parts");
  790. initial_handle->readonly = 1;
  791. _starpu_spin_unlock(&initial_handle->header_lock);
  792. unsigned i, n;
  793. struct starpu_data_descr descr[nparts];
  794. for (i = 0, n = 0; i < nparts; i++)
  795. {
  796. STARPU_ASSERT_MSG(children[i]->father_handle == initial_handle, "child(%d) %p is partitioned from %p and not from the given parameter %p", i, children[i], children[i]->father_handle, initial_handle);
  797. if (!children[i]->initialized)
  798. /* Dropped value, do not care about coherency for this one */
  799. continue;
  800. descr[n].handle = children[i];
  801. descr[n].mode = STARPU_R;
  802. n++;
  803. }
  804. /* TODO: assert nparts too */
  805. starpu_task_insert(initial_handle->switch_cl, STARPU_W, initial_handle, STARPU_DATA_MODE_ARRAY, descr, n, 0);
  806. }
  807. /* Unpartition everything below ancestor */
  808. void starpu_data_unpartition_submit_r(starpu_data_handle_t ancestor, int gathering_node)
  809. {
  810. unsigned i, j, nsiblings;
  811. if (!ancestor->partitioned)
  812. /* It's already unpartitioned */
  813. return;
  814. _STARPU_DEBUG("ancestor %p needs unpartitioning\n", ancestor);
  815. if (ancestor->readonly)
  816. {
  817. unsigned n = ancestor->partitioned;
  818. /* Uh, has to go through all read-only partitions */
  819. for (i = 0; i < n; i++)
  820. {
  821. /* Note: active_readonly_children is emptied by starpu_data_unpartition_submit calls */
  822. starpu_data_handle_t *children = ancestor->active_readonly_children[0];
  823. _STARPU_DEBUG("unpartition readonly children %p etc.\n", children[0]);
  824. nsiblings = children[0]->nsiblings;
  825. for (j = 0; j < nsiblings; j++)
  826. {
  827. /* Make sure our children are unpartitioned */
  828. starpu_data_unpartition_submit_r(children[j], gathering_node);
  829. }
  830. /* And unpartition them */
  831. starpu_data_unpartition_submit(ancestor, nsiblings, children, gathering_node);
  832. }
  833. }
  834. else
  835. {
  836. _STARPU_DEBUG("unpartition children %p\n", ancestor->active_children);
  837. /* Only one partition */
  838. nsiblings = ancestor->active_children[0]->nsiblings;
  839. for (i = 0; i < nsiblings; i++)
  840. starpu_data_unpartition_submit_r(ancestor->active_children[i], gathering_node);
  841. /* And unpartition ourself */
  842. starpu_data_unpartition_submit(ancestor, nsiblings, ancestor->active_children, gathering_node);
  843. }
  844. }
  845. /* Make ancestor partition itself properly for target */
  846. static void _starpu_data_partition_access_look_up(starpu_data_handle_t ancestor, starpu_data_handle_t target, int write)
  847. {
  848. /* First make sure ancestor has proper state, if not, ask father */
  849. if (!ancestor->active || (write && ancestor->active_ro))
  850. {
  851. /* (The root is always active-rw) */
  852. STARPU_ASSERT(ancestor->father_handle);
  853. _STARPU_DEBUG("ancestor %p is not ready: %s, asking father %p\n", ancestor, ancestor->active ? ancestor->active_ro ? "RO" : "RW" : "NONE", ancestor->father_handle);
  854. _starpu_data_partition_access_look_up(ancestor->father_handle, ancestor, write);
  855. _STARPU_DEBUG("ancestor %p is now ready\n", ancestor);
  856. }
  857. else
  858. _STARPU_DEBUG("ancestor %p was ready\n", ancestor);
  859. /* We shouldn't be called for nothing */
  860. STARPU_ASSERT(!ancestor->partitioned || !target || ancestor->active_children != target->siblings || (ancestor->readonly && write));
  861. /* Then unpartition ancestor if needed */
  862. if (ancestor->partitioned &&
  863. /* Not the right children, unpartition ourself */
  864. ((target && write && ancestor->active_children != target->siblings) ||
  865. (target && !write && !ancestor->readonly) ||
  866. /* We are partitioned and we want to write or some child
  867. * is writing and we want to read, unpartition ourself*/
  868. (!target && (write || !ancestor->readonly))))
  869. {
  870. #ifdef STARPU_DEVEL
  871. #warning FIXME: better choose gathering node
  872. #endif
  873. starpu_data_unpartition_submit_r(ancestor, STARPU_MAIN_RAM);
  874. }
  875. if (!target)
  876. {
  877. _STARPU_DEBUG("ancestor %p is done\n", ancestor);
  878. /* No child target, nothing more to do actually. */
  879. return;
  880. }
  881. /* Then partition ancestor towards target, if needed */
  882. if (ancestor->partitioned)
  883. {
  884. /* That must be readonly, otherwise we would have unpartitioned it */
  885. STARPU_ASSERT(ancestor->readonly);
  886. if (write)
  887. {
  888. _STARPU_DEBUG("ancestor %p is already partitioned RO, turn RW\n", ancestor);
  889. /* Already partitioned, normally it's already for the target */
  890. STARPU_ASSERT(ancestor->active_children == target->siblings);
  891. /* And we are here just because we haven't partitioned rw */
  892. STARPU_ASSERT(ancestor->readonly && write);
  893. /* So we just need to upgrade ro to rw */
  894. starpu_data_partition_readwrite_upgrade_submit(ancestor, target->nsiblings, target->siblings);
  895. }
  896. else
  897. {
  898. _STARPU_DEBUG("ancestor %p is already partitioned RO, but not to target, partition towards target too\n", ancestor);
  899. /* So we just need to upgrade ro to rw */
  900. starpu_data_partition_readonly_submit(ancestor, target->nsiblings, target->siblings);
  901. }
  902. }
  903. else
  904. {
  905. /* Just need to partition properly for the child */
  906. if (write)
  907. {
  908. _STARPU_DEBUG("partition ancestor %p RW\n", ancestor);
  909. starpu_data_partition_submit(ancestor, target->nsiblings, target->siblings);
  910. }
  911. else
  912. {
  913. _STARPU_DEBUG("partition ancestor %p RO\n", ancestor);
  914. starpu_data_partition_readonly_submit(ancestor, target->nsiblings, target->siblings);
  915. }
  916. }
  917. }
  918. void _starpu_data_partition_access_submit(starpu_data_handle_t target, int write)
  919. {
  920. _STARPU_DEBUG("accessing %p %s\n", target, write ? "RW" : "RO");
  921. _starpu_data_partition_access_look_up(target, NULL, write);
  922. }
  923. void
  924. starpu_filter_nparts_compute_chunk_size_and_offset(unsigned n, unsigned nparts,
  925. size_t elemsize, unsigned id,
  926. unsigned ld, unsigned *chunk_size,
  927. size_t *offset)
  928. {
  929. *chunk_size = n/nparts;
  930. unsigned remainder = n % nparts;
  931. if (id < remainder)
  932. (*chunk_size)++;
  933. /*
  934. * Computing the total offset. The formula may not be really clear, but
  935. * it really just is:
  936. *
  937. * total = 0;
  938. * for (i = 0; i < id; i++)
  939. * {
  940. * total += n/nparts;
  941. * if (i < n%nparts)
  942. * total++;
  943. * }
  944. * offset = total * elemsize * ld;
  945. */
  946. if (offset != NULL)
  947. *offset = (id *(n/nparts) + STARPU_MIN(remainder, id)) * ld * elemsize;
  948. }
  949. void starpu_data_partition_not_automatic(starpu_data_handle_t handle)
  950. {
  951. handle->partition_automatic_disabled = 1;
  952. }