filters.c 39 KB

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