filters.c 40 KB

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