filters.c 40 KB

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