filters.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2016 Université de Bordeaux
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010, 2011, 2012, 2013, 2015 CNRS
  6. * Copyright (C) 2012 INRIA
  7. *
  8. * StarPU is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * StarPU is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  18. */
  19. #include <datawizard/filters.h>
  20. #include <datawizard/footprint.h>
  21. #include <datawizard/interfaces/data_interface.h>
  22. #include <core/task.h>
  23. /*
  24. * This function applies a data filter on all the elements of a partition
  25. */
  26. static void map_filter(starpu_data_handle_t root_handle, struct starpu_data_filter *f)
  27. {
  28. /* we need to apply the data filter on all leaf of the tree */
  29. if (root_handle->nchildren == 0)
  30. {
  31. /* this is a leaf */
  32. starpu_data_partition(root_handle, f);
  33. }
  34. else
  35. {
  36. /* try to apply the data filter recursively */
  37. unsigned child;
  38. for (child = 0; child < root_handle->nchildren; child++)
  39. {
  40. starpu_data_handle_t handle_child = starpu_data_get_child(root_handle, child);
  41. map_filter(handle_child, f);
  42. }
  43. }
  44. }
  45. void starpu_data_vmap_filters(starpu_data_handle_t root_handle, unsigned nfilters, va_list pa)
  46. {
  47. unsigned i;
  48. for (i = 0; i < nfilters; i++)
  49. {
  50. struct starpu_data_filter *next_filter;
  51. next_filter = va_arg(pa, struct starpu_data_filter *);
  52. STARPU_ASSERT(next_filter);
  53. map_filter(root_handle, next_filter);
  54. }
  55. }
  56. void starpu_data_map_filters(starpu_data_handle_t root_handle, unsigned nfilters, ...)
  57. {
  58. va_list pa;
  59. va_start(pa, nfilters);
  60. starpu_data_vmap_filters(root_handle, nfilters, pa);
  61. va_end(pa);
  62. }
  63. int starpu_data_get_nb_children(starpu_data_handle_t handle)
  64. {
  65. return handle->nchildren;
  66. }
  67. starpu_data_handle_t starpu_data_get_child(starpu_data_handle_t handle, unsigned i)
  68. {
  69. STARPU_ASSERT_MSG(handle->nchildren != 0, "Data %p has to be partitioned before accessing children", handle);
  70. STARPU_ASSERT_MSG(i < handle->nchildren, "Invalid child index %u in handle %p, maximum %u", i, handle, handle->nchildren);
  71. return &handle->children[i];
  72. }
  73. /*
  74. * example starpu_data_get_sub_data(starpu_data_handle_t root_handle, 3, 42, 0, 1);
  75. */
  76. starpu_data_handle_t starpu_data_get_sub_data(starpu_data_handle_t root_handle, unsigned depth, ... )
  77. {
  78. va_list pa;
  79. va_start(pa, depth);
  80. starpu_data_handle_t handle = starpu_data_vget_sub_data(root_handle, depth, pa);
  81. va_end(pa);
  82. return handle;
  83. }
  84. starpu_data_handle_t starpu_data_vget_sub_data(starpu_data_handle_t root_handle, unsigned depth, va_list pa )
  85. {
  86. STARPU_ASSERT(root_handle);
  87. starpu_data_handle_t current_handle = root_handle;
  88. /* the variable number of argument must correlate the depth in the tree */
  89. unsigned i;
  90. for (i = 0; i < depth; i++)
  91. {
  92. unsigned next_child;
  93. next_child = va_arg(pa, unsigned);
  94. STARPU_ASSERT_MSG(current_handle->nchildren != 0, "Data %p has to be partitioned before accessing children", current_handle);
  95. 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);
  96. current_handle = &current_handle->children[next_child];
  97. }
  98. return current_handle;
  99. }
  100. static unsigned _starpu_data_partition_nparts(starpu_data_handle_t initial_handle, struct starpu_data_filter *f)
  101. {
  102. /* how many parts ? */
  103. if (f->get_nchildren)
  104. return f->get_nchildren(f, initial_handle);
  105. else
  106. return f->nchildren;
  107. }
  108. 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)
  109. {
  110. unsigned i;
  111. unsigned node;
  112. /* Make sure to wait for previous tasks working on the whole data */
  113. starpu_data_acquire_on_node(initial_handle, -1, STARPU_RW);
  114. starpu_data_release_on_node(initial_handle, -1);
  115. /* first take care to properly lock the data header */
  116. _starpu_spin_lock(&initial_handle->header_lock);
  117. initial_handle->nplans++;
  118. STARPU_ASSERT_MSG(nparts > 0, "Partitioning data %p in 0 piece does not make sense", initial_handle);
  119. /* allocate the children */
  120. if (inherit_state)
  121. {
  122. initial_handle->children = (struct _starpu_data_state *) calloc(nparts, sizeof(struct _starpu_data_state));
  123. STARPU_ASSERT(initial_handle->children);
  124. /* this handle now has children */
  125. initial_handle->nchildren = nparts;
  126. }
  127. for (node = 0; node < STARPU_MAXNODES; node++)
  128. {
  129. if (initial_handle->per_node[node].state != STARPU_INVALID)
  130. break;
  131. }
  132. if (node == STARPU_MAXNODES)
  133. {
  134. /* This is lazy allocation, allocate it now in main RAM, so as
  135. * to have somewhere to gather pieces later */
  136. /* FIXME: mark as unevictable! */
  137. int ret = _starpu_allocate_memory_on_node(initial_handle, &initial_handle->per_node[STARPU_MAIN_RAM], 0);
  138. #ifdef STARPU_DEVEL
  139. #warning we should reclaim memory if allocation failed
  140. #endif
  141. STARPU_ASSERT(!ret);
  142. }
  143. for (i = 0; i < nparts; i++)
  144. {
  145. starpu_data_handle_t child;
  146. if (inherit_state)
  147. child = &initial_handle->children[i];
  148. else
  149. child = childrenp[i];
  150. STARPU_ASSERT(child);
  151. struct starpu_data_interface_ops *ops;
  152. /* each child may have his own interface type */
  153. /* what's this child's interface ? */
  154. if (f->get_child_ops)
  155. ops = f->get_child_ops(f, i);
  156. else
  157. ops = initial_handle->ops;
  158. _starpu_data_handle_init(child, ops, initial_handle->mf_node);
  159. child->nchildren = 0;
  160. child->nplans = 0;
  161. child->switch_cl = NULL;
  162. child->partitioned = 0;
  163. child->readonly = 0;
  164. child->mpi_data = initial_handle->mpi_data;
  165. child->root_handle = initial_handle->root_handle;
  166. child->father_handle = initial_handle;
  167. child->sibling_index = i;
  168. child->depth = initial_handle->depth + 1;
  169. child->is_not_important = initial_handle->is_not_important;
  170. child->wt_mask = initial_handle->wt_mask;
  171. child->home_node = initial_handle->home_node;
  172. child->is_readonly = initial_handle->is_readonly;
  173. /* initialize the chunk lock */
  174. _starpu_data_requester_list_init(&child->req_list);
  175. _starpu_data_requester_list_init(&child->reduction_req_list);
  176. child->reduction_tmp_handles = NULL;
  177. child->write_invalidation_req = NULL;
  178. child->refcnt = 0;
  179. child->unlocking_reqs = 0;
  180. child->busy_count = 0;
  181. child->busy_waiting = 0;
  182. STARPU_PTHREAD_MUTEX_INIT(&child->busy_mutex, NULL);
  183. STARPU_PTHREAD_COND_INIT(&child->busy_cond, NULL);
  184. child->reduction_refcnt = 0;
  185. _starpu_spin_init(&child->header_lock);
  186. child->sequential_consistency = initial_handle->sequential_consistency;
  187. STARPU_PTHREAD_MUTEX_INIT(&child->sequential_consistency_mutex, NULL);
  188. child->last_submitted_mode = STARPU_R;
  189. child->last_sync_task = NULL;
  190. child->last_submitted_accessors.task = NULL;
  191. child->last_submitted_accessors.next = &child->last_submitted_accessors;
  192. child->last_submitted_accessors.prev = &child->last_submitted_accessors;
  193. child->post_sync_tasks = NULL;
  194. /* Tell helgrind that the race in _starpu_unlock_post_sync_tasks is fine */
  195. STARPU_HG_DISABLE_CHECKING(child->post_sync_tasks_cnt);
  196. child->post_sync_tasks_cnt = 0;
  197. /* The methods used for reduction are propagated to the
  198. * children. */
  199. child->redux_cl = initial_handle->redux_cl;
  200. child->init_cl = initial_handle->init_cl;
  201. #ifdef STARPU_USE_FXT
  202. child->last_submitted_ghost_sync_id_is_valid = 0;
  203. child->last_submitted_ghost_sync_id = 0;
  204. child->last_submitted_ghost_accessors_id = NULL;
  205. #endif
  206. if (_starpu_global_arbiter)
  207. /* Just for testing purpose */
  208. starpu_data_assign_arbiter(child, _starpu_global_arbiter);
  209. else
  210. child->arbiter = NULL;
  211. _starpu_data_requester_list_init(&child->arbitered_req_list);
  212. for (node = 0; node < STARPU_MAXNODES; node++)
  213. {
  214. struct _starpu_data_replicate *initial_replicate;
  215. struct _starpu_data_replicate *child_replicate;
  216. initial_replicate = &initial_handle->per_node[node];
  217. child_replicate = &child->per_node[node];
  218. if (inherit_state)
  219. child_replicate->state = initial_replicate->state;
  220. else
  221. child_replicate->state = STARPU_INVALID;
  222. if (inherit_state || !initial_replicate->automatically_allocated)
  223. child_replicate->allocated = initial_replicate->allocated;
  224. else
  225. child_replicate->allocated = 0;
  226. /* Do not allow memory reclaiming within the child for parent bits */
  227. child_replicate->automatically_allocated = 0;
  228. child_replicate->refcnt = 0;
  229. child_replicate->memory_node = node;
  230. child_replicate->relaxed_coherency = 0;
  231. if (inherit_state)
  232. child_replicate->initialized = initial_replicate->initialized;
  233. else
  234. child_replicate->initialized = 0;
  235. /* update the interface */
  236. void *initial_interface = starpu_data_get_interface_on_node(initial_handle, node);
  237. void *child_interface = starpu_data_get_interface_on_node(child, node);
  238. 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");
  239. f->filter_func(initial_interface, child_interface, f, i, nparts);
  240. }
  241. child->per_worker = NULL;
  242. /* We compute the size and the footprint of the child once and
  243. * store it in the handle */
  244. child->footprint = _starpu_compute_data_footprint(child);
  245. void *ptr;
  246. ptr = starpu_data_handle_to_pointer(child, STARPU_MAIN_RAM);
  247. if (ptr != NULL)
  248. _starpu_data_register_ram_pointer(child, ptr);
  249. }
  250. /* now let the header */
  251. _starpu_spin_unlock(&initial_handle->header_lock);
  252. }
  253. static
  254. void _starpu_empty_codelet_function(void *buffers[], void *args)
  255. {
  256. (void) buffers; // unused;
  257. (void) args; // unused;
  258. }
  259. void starpu_data_unpartition(starpu_data_handle_t root_handle, unsigned gathering_node)
  260. {
  261. unsigned child;
  262. unsigned worker;
  263. unsigned nworkers = starpu_worker_get_count();
  264. unsigned node;
  265. unsigned sizes[root_handle->nchildren];
  266. _STARPU_TRACE_START_UNPARTITION(root_handle, gathering_node);
  267. _starpu_spin_lock(&root_handle->header_lock);
  268. STARPU_ASSERT_MSG(root_handle->nchildren != 0, "data %p is not partitioned, can not unpartition it", root_handle);
  269. /* first take all the children lock (in order !) */
  270. for (child = 0; child < root_handle->nchildren; child++)
  271. {
  272. starpu_data_handle_t child_handle = starpu_data_get_child(root_handle, child);
  273. /* make sure the intermediate children is unpartitionned as well */
  274. if (child_handle->nchildren > 0)
  275. starpu_data_unpartition(child_handle, gathering_node);
  276. /* If this is a multiformat handle, we must convert the data now */
  277. #ifdef STARPU_DEVEL
  278. #warning TODO: _starpu_fetch_data_on_node should be doing it
  279. #endif
  280. if (_starpu_data_is_multiformat_handle(child_handle) &&
  281. starpu_node_get_kind(child_handle->mf_node) != STARPU_CPU_RAM)
  282. {
  283. struct starpu_codelet cl =
  284. {
  285. .where = STARPU_CPU,
  286. .cpu_funcs = { _starpu_empty_codelet_function },
  287. .modes = { STARPU_RW },
  288. .nbuffers = 1
  289. };
  290. struct starpu_task *task = starpu_task_create();
  291. task->name = "convert_data";
  292. STARPU_TASK_SET_HANDLE(task, child_handle, 0);
  293. task->cl = &cl;
  294. task->synchronous = 1;
  295. if (_starpu_task_submit_internally(task) != 0)
  296. _STARPU_ERROR("Could not submit the conversion task while unpartitionning\n");
  297. }
  298. int ret;
  299. /* for now we pretend that the RAM is almost unlimited and that gathering
  300. * data should be possible from the node that does the unpartionning ... we
  301. * don't want to have the programming deal with memory shortage at that time,
  302. * really */
  303. /* Acquire the child data on the gathering node. This will trigger collapsing any reduction */
  304. ret = starpu_data_acquire_on_node(child_handle, gathering_node, STARPU_RW);
  305. STARPU_ASSERT(ret == 0);
  306. starpu_data_release_on_node(child_handle, gathering_node);
  307. _starpu_spin_lock(&child_handle->header_lock);
  308. child_handle->busy_waiting = 1;
  309. _starpu_spin_unlock(&child_handle->header_lock);
  310. /* Wait for all requests to finish (notably WT requests) */
  311. STARPU_PTHREAD_MUTEX_LOCK(&child_handle->busy_mutex);
  312. while (1)
  313. {
  314. /* Here helgrind would shout that this an unprotected access,
  315. * but this is actually fine: all threads who do busy_count--
  316. * are supposed to call _starpu_data_check_not_busy, which will
  317. * wake us up through the busy_mutex/busy_cond. */
  318. if (!child_handle->busy_count)
  319. break;
  320. /* This is woken by _starpu_data_check_not_busy, always called
  321. * after decrementing busy_count */
  322. STARPU_PTHREAD_COND_WAIT(&child_handle->busy_cond, &child_handle->busy_mutex);
  323. }
  324. STARPU_PTHREAD_MUTEX_UNLOCK(&child_handle->busy_mutex);
  325. _starpu_spin_lock(&child_handle->header_lock);
  326. sizes[child] = _starpu_data_get_size(child_handle);
  327. _starpu_data_unregister_ram_pointer(child_handle);
  328. if (child_handle->per_worker)
  329. for (worker = 0; worker < nworkers; worker++)
  330. {
  331. struct _starpu_data_replicate *local = &child_handle->per_worker[worker];
  332. STARPU_ASSERT(local->state == STARPU_INVALID);
  333. if (local->allocated && local->automatically_allocated)
  334. _starpu_request_mem_chunk_removal(child_handle, local, starpu_worker_get_memory_node(worker), sizes[child]);
  335. }
  336. _starpu_memory_stats_free(child_handle);
  337. }
  338. /* the gathering_node should now have a valid copy of all the children.
  339. * For all nodes, if the node had all copies and none was locally
  340. * allocated then the data is still valid there, else, it's invalidated
  341. * for the gathering node, if we have some locally allocated data, we
  342. * copy all the children (XXX this should not happen so we just do not
  343. * do anything since this is transparent ?) */
  344. unsigned still_valid[STARPU_MAXNODES];
  345. /* we do 2 passes : the first pass determines wether the data is still
  346. * valid or not, the second pass is needed to choose between STARPU_SHARED and
  347. * STARPU_OWNER */
  348. unsigned nvalids = 0;
  349. /* still valid ? */
  350. for (node = 0; node < STARPU_MAXNODES; node++)
  351. {
  352. struct _starpu_data_replicate *local;
  353. /* until an issue is found the data is assumed to be valid */
  354. unsigned isvalid = 1;
  355. for (child = 0; child < root_handle->nchildren; child++)
  356. {
  357. starpu_data_handle_t child_handle = starpu_data_get_child(root_handle, child);
  358. local = &child_handle->per_node[node];
  359. if (local->state == STARPU_INVALID || local->automatically_allocated == 1)
  360. {
  361. /* One of the bits is missing or is not inside the parent */
  362. isvalid = 0;
  363. }
  364. if (local->mc && local->allocated && local->automatically_allocated)
  365. /* free the child data copy in a lazy fashion */
  366. _starpu_request_mem_chunk_removal(child_handle, local, node, sizes[child]);
  367. }
  368. local = &root_handle->per_node[node];
  369. if (!local->allocated)
  370. /* Even if we have all the bits, if we don't have the
  371. * whole data, it's not valid */
  372. isvalid = 0;
  373. if (!isvalid && local->mc && local->allocated && local->automatically_allocated)
  374. /* free the data copy in a lazy fashion */
  375. _starpu_request_mem_chunk_removal(root_handle, local, node, _starpu_data_get_size(root_handle));
  376. /* if there was no invalid copy, the node still has a valid copy */
  377. still_valid[node] = isvalid;
  378. if (isvalid)
  379. nvalids++;
  380. }
  381. /* either shared or owned */
  382. STARPU_ASSERT(nvalids > 0);
  383. enum _starpu_cache_state newstate = (nvalids == 1)?STARPU_OWNER:STARPU_SHARED;
  384. for (node = 0; node < STARPU_MAXNODES; node++)
  385. {
  386. root_handle->per_node[node].state =
  387. still_valid[node]?newstate:STARPU_INVALID;
  388. }
  389. for (child = 0; child < root_handle->nchildren; child++)
  390. {
  391. starpu_data_handle_t child_handle = starpu_data_get_child(root_handle, child);
  392. _starpu_data_free_interfaces(child_handle);
  393. _starpu_spin_unlock(&child_handle->header_lock);
  394. _starpu_spin_destroy(&child_handle->header_lock);
  395. }
  396. for (child = 0; child < root_handle->nchildren; child++)
  397. {
  398. starpu_data_handle_t child_handle = starpu_data_get_child(root_handle, child);
  399. _starpu_data_clear_implicit(child_handle);
  400. STARPU_PTHREAD_MUTEX_DESTROY(&child_handle->busy_mutex);
  401. STARPU_PTHREAD_COND_DESTROY(&child_handle->busy_cond);
  402. STARPU_PTHREAD_MUTEX_DESTROY(&child_handle->sequential_consistency_mutex);
  403. }
  404. /* there is no child anymore */
  405. starpu_data_handle_t children = root_handle->children;
  406. root_handle->children = NULL;
  407. root_handle->nchildren = 0;
  408. root_handle->nplans--;
  409. /* now the parent may be used again so we release the lock */
  410. _starpu_spin_unlock(&root_handle->header_lock);
  411. free(children);
  412. _STARPU_TRACE_END_UNPARTITION(root_handle, gathering_node);
  413. }
  414. void starpu_data_partition(starpu_data_handle_t initial_handle, struct starpu_data_filter *f)
  415. {
  416. unsigned nparts = _starpu_data_partition_nparts(initial_handle, f);
  417. 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);
  418. STARPU_ASSERT_MSG(initial_handle->nplans == 0, "partition planning and synchronous partitioning is not supported");
  419. initial_handle->children = NULL;
  420. _starpu_data_partition(initial_handle, NULL, nparts, f, 1);
  421. }
  422. void starpu_data_partition_plan(starpu_data_handle_t initial_handle, struct starpu_data_filter *f, starpu_data_handle_t *childrenp)
  423. {
  424. unsigned i;
  425. unsigned nparts = _starpu_data_partition_nparts(initial_handle, f);
  426. STARPU_ASSERT_MSG(initial_handle->nchildren == 0, "partition planning and synchronous partitioning is not supported");
  427. STARPU_ASSERT_MSG(initial_handle->sequential_consistency, "partition planning is currently only supported for data with sequential consistency");
  428. for (i = 0; i < nparts; i++)
  429. childrenp[i] = calloc(1, sizeof(struct _starpu_data_state));
  430. _starpu_data_partition(initial_handle, childrenp, nparts, f, 0);
  431. if (!initial_handle->switch_cl)
  432. {
  433. /* Create a codelet that will make the coherency on the home node */
  434. struct starpu_codelet *cl = initial_handle->switch_cl = calloc(1, sizeof(*initial_handle->switch_cl));
  435. cl->where = STARPU_NOWHERE;
  436. cl->nbuffers = STARPU_VARIABLE_NBUFFERS;
  437. cl->name = "data_partition_switch";
  438. cl->specific_nodes = 1;
  439. cl->dyn_nodes = malloc((nparts+1) * sizeof(*cl->dyn_nodes));
  440. for (i = 0; i < nparts+1; i++)
  441. cl->dyn_nodes[i] = initial_handle->home_node;
  442. }
  443. }
  444. void starpu_data_partition_clean(starpu_data_handle_t root_handle, unsigned nparts, starpu_data_handle_t *children)
  445. {
  446. unsigned i;
  447. for (i = 0; i < nparts; i++)
  448. starpu_data_unregister_submit(children[i]);
  449. _starpu_spin_lock(&root_handle->header_lock);
  450. root_handle->nplans--;
  451. _starpu_spin_unlock(&root_handle->header_lock);
  452. }
  453. void starpu_data_partition_submit(starpu_data_handle_t initial_handle, unsigned nparts, starpu_data_handle_t *children)
  454. {
  455. STARPU_ASSERT_MSG(initial_handle->sequential_consistency, "partition planning is currently only supported for data with sequential consistency");
  456. _starpu_spin_lock(&initial_handle->header_lock);
  457. STARPU_ASSERT_MSG(initial_handle->partitioned == 0, "One can't submit several partition plannings at the same time");
  458. STARPU_ASSERT_MSG(initial_handle->readonly == 0, "One can't submit a partition planning while a readonly partitioning is active");
  459. initial_handle->partitioned++;
  460. _starpu_spin_unlock(&initial_handle->header_lock);
  461. unsigned i;
  462. struct starpu_data_descr descr[nparts];
  463. for (i = 0; i < nparts; i++)
  464. {
  465. STARPU_ASSERT_MSG(children[i]->father_handle == initial_handle, "children parameter of starpu_data_partition_submit must be the children of the parent parameter");
  466. descr[i].handle = children[i];
  467. descr[i].mode = STARPU_W;
  468. }
  469. /* TODO: assert nparts too */
  470. starpu_task_insert(initial_handle->switch_cl, STARPU_RW, initial_handle, STARPU_DATA_MODE_ARRAY, descr, nparts, 0);
  471. starpu_data_invalidate_submit(initial_handle);
  472. }
  473. void starpu_data_partition_readonly_submit(starpu_data_handle_t initial_handle, unsigned nparts, starpu_data_handle_t *children)
  474. {
  475. STARPU_ASSERT_MSG(initial_handle->sequential_consistency, "partition planning is currently only supported for data with sequential consistency");
  476. _starpu_spin_lock(&initial_handle->header_lock);
  477. 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");
  478. initial_handle->partitioned++;
  479. initial_handle->readonly = 1;
  480. _starpu_spin_unlock(&initial_handle->header_lock);
  481. unsigned i;
  482. struct starpu_data_descr descr[nparts];
  483. for (i = 0; i < nparts; i++)
  484. {
  485. STARPU_ASSERT_MSG(children[i]->father_handle == initial_handle, "children parameter of starpu_data_partition_submit must be the children of the parent parameter");
  486. descr[i].handle = children[i];
  487. descr[i].mode = STARPU_W;
  488. }
  489. /* TODO: assert nparts too */
  490. starpu_task_insert(initial_handle->switch_cl, STARPU_R, initial_handle, STARPU_DATA_MODE_ARRAY, descr, nparts, 0);
  491. }
  492. void starpu_data_partition_readwrite_upgrade_submit(starpu_data_handle_t initial_handle, unsigned nparts, starpu_data_handle_t *children)
  493. {
  494. STARPU_ASSERT_MSG(initial_handle->sequential_consistency, "partition planning is currently only supported for data with sequential consistency");
  495. _starpu_spin_lock(&initial_handle->header_lock);
  496. STARPU_ASSERT_MSG(initial_handle->partitioned == 1, "One can't upgrade a readonly partition planning to readwrite while other readonly partition plannings are active");
  497. STARPU_ASSERT_MSG(initial_handle->readonly == 1, "One can only upgrade a readonly partition planning");
  498. initial_handle->readonly = 0;
  499. _starpu_spin_unlock(&initial_handle->header_lock);
  500. unsigned i;
  501. struct starpu_data_descr descr[nparts];
  502. for (i = 0; i < nparts; i++)
  503. {
  504. STARPU_ASSERT_MSG(children[i]->father_handle == initial_handle, "children parameter of starpu_data_partition_submit must be the children of the parent parameter");
  505. descr[i].handle = children[i];
  506. descr[i].mode = STARPU_W;
  507. }
  508. /* TODO: assert nparts too */
  509. starpu_task_insert(initial_handle->switch_cl, STARPU_RW, initial_handle, STARPU_DATA_MODE_ARRAY, descr, nparts, 0);
  510. starpu_data_invalidate_submit(initial_handle);
  511. }
  512. void starpu_data_unpartition_submit(starpu_data_handle_t initial_handle, unsigned nparts, starpu_data_handle_t *children, int gather_node)
  513. {
  514. STARPU_ASSERT_MSG(initial_handle->sequential_consistency, "partition planning is currently only supported for data with sequential consistency");
  515. STARPU_ASSERT_MSG(gather_node == initial_handle->home_node || gather_node == -1, "gathering node different from home node is currently not supported");
  516. _starpu_spin_lock(&initial_handle->header_lock);
  517. STARPU_ASSERT_MSG(initial_handle->partitioned >= 1, "No partition planning is active for this handle");
  518. initial_handle->partitioned--;
  519. if (!initial_handle->partitioned)
  520. initial_handle->readonly = 0;
  521. _starpu_spin_unlock(&initial_handle->header_lock);
  522. unsigned i;
  523. struct starpu_data_descr descr[nparts];
  524. for (i = 0; i < nparts; i++)
  525. {
  526. STARPU_ASSERT_MSG(children[i]->father_handle == initial_handle, "children parameter of starpu_data_partition_submit must be the children of the parent parameter");
  527. descr[i].handle = children[i];
  528. descr[i].mode = STARPU_RW;
  529. }
  530. /* TODO: assert nparts too */
  531. starpu_task_insert(initial_handle->switch_cl, STARPU_W, initial_handle, STARPU_DATA_MODE_ARRAY, descr, nparts, 0);
  532. for (i = 0; i < nparts; i++)
  533. starpu_data_invalidate_submit(children[i]);
  534. }
  535. void starpu_data_unpartition_readonly_submit(starpu_data_handle_t initial_handle, unsigned nparts, starpu_data_handle_t *children, int gather_node)
  536. {
  537. STARPU_ASSERT_MSG(initial_handle->sequential_consistency, "partition planning is currently only supported for data with sequential consistency");
  538. STARPU_ASSERT_MSG(gather_node == initial_handle->home_node || gather_node == -1, "gathering node different from home node is currently not supported");
  539. _starpu_spin_lock(&initial_handle->header_lock);
  540. STARPU_ASSERT_MSG(initial_handle->partitioned >= 1, "No partition planning is active for this handle");
  541. initial_handle->readonly = 1;
  542. _starpu_spin_unlock(&initial_handle->header_lock);
  543. unsigned i;
  544. struct starpu_data_descr descr[nparts];
  545. for (i = 0; i < nparts; i++)
  546. {
  547. STARPU_ASSERT_MSG(children[i]->father_handle == initial_handle, "children parameter of starpu_data_partition_submit must be the children of the parent parameter");
  548. descr[i].handle = children[i];
  549. descr[i].mode = STARPU_R;
  550. }
  551. /* TODO: assert nparts too */
  552. starpu_task_insert(initial_handle->switch_cl, STARPU_W, initial_handle, STARPU_DATA_MODE_ARRAY, descr, nparts, 0);
  553. }
  554. /*
  555. * Given an integer N, NPARTS the number of parts it must be divided in, ID the
  556. * part currently considered, determines the CHUNK_SIZE and the OFFSET, taking
  557. * into account the size of the elements stored in the data structure ELEMSIZE
  558. * and LD, the leading dimension.
  559. */
  560. void
  561. _starpu_filter_nparts_compute_chunk_size_and_offset(unsigned n, unsigned nparts,
  562. size_t elemsize, unsigned id,
  563. unsigned ld, unsigned *chunk_size,
  564. size_t *offset)
  565. {
  566. *chunk_size = n/nparts;
  567. unsigned remainder = n % nparts;
  568. if (id < remainder)
  569. (*chunk_size)++;
  570. /*
  571. * Computing the total offset. The formula may not be really clear, but
  572. * it really just is:
  573. *
  574. * total = 0;
  575. * for (i = 0; i < id; i++)
  576. * {
  577. * total += n/nparts;
  578. * if (i < n%nparts)
  579. * total++;
  580. * }
  581. * offset = total * elemsize * ld;
  582. */
  583. if (offset != NULL)
  584. *offset = (id *(n/nparts) + STARPU_MIN(remainder, id)) * ld * elemsize;
  585. }