filters.c 25 KB

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