filters.c 39 KB

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