filters.c 40 KB

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