filters.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <datawizard/filters.h>
  17. #include <datawizard/footprint.h>
  18. static void starpu_data_create_children(starpu_data_handle handle, unsigned nchildren, struct starpu_data_filter *f);
  19. /*
  20. * This function applies a data filter on all the elements of a partition
  21. */
  22. static void map_filter(starpu_data_handle root_handle, struct starpu_data_filter *f)
  23. {
  24. /* we need to apply the data filter on all leaf of the tree */
  25. if (root_handle->nchildren == 0)
  26. {
  27. /* this is a leaf */
  28. starpu_data_partition(root_handle, f);
  29. }
  30. else {
  31. /* try to apply the data filter recursively */
  32. unsigned child;
  33. for (child = 0; child < root_handle->nchildren; child++)
  34. {
  35. map_filter(&root_handle->children[child], f);
  36. }
  37. }
  38. }
  39. void starpu_data_map_filters(starpu_data_handle root_handle, unsigned nfilters, ...)
  40. {
  41. unsigned i;
  42. va_list pa;
  43. va_start(pa, nfilters);
  44. for (i = 0; i < nfilters; i++)
  45. {
  46. struct starpu_data_filter *next_filter;
  47. next_filter = va_arg(pa, struct starpu_data_filter *);
  48. STARPU_ASSERT(next_filter);
  49. map_filter(root_handle, next_filter);
  50. }
  51. va_end(pa);
  52. }
  53. int starpu_data_get_nb_children(starpu_data_handle handle)
  54. {
  55. return handle->nchildren;
  56. }
  57. starpu_data_handle starpu_data_get_child(starpu_data_handle handle, unsigned i)
  58. {
  59. STARPU_ASSERT(i < handle->nchildren);
  60. return &handle->children[i];
  61. }
  62. /*
  63. * example starpu_data_get_sub_data(starpu_data_handle root_handle, 3, 42, 0, 1);
  64. */
  65. starpu_data_handle starpu_data_get_sub_data(starpu_data_handle root_handle, unsigned depth, ... )
  66. {
  67. STARPU_ASSERT(root_handle);
  68. starpu_data_handle current_handle = root_handle;
  69. /* the variable number of argument must correlate the depth in the tree */
  70. unsigned i;
  71. va_list pa;
  72. va_start(pa, depth);
  73. for (i = 0; i < depth; i++)
  74. {
  75. unsigned next_child;
  76. next_child = va_arg(pa, unsigned);
  77. STARPU_ASSERT(next_child < current_handle->nchildren);
  78. current_handle = &current_handle->children[next_child];
  79. }
  80. va_end(pa);
  81. return current_handle;
  82. }
  83. void starpu_data_partition(starpu_data_handle initial_handle, struct starpu_data_filter *f)
  84. {
  85. unsigned nparts;
  86. unsigned i;
  87. /* first take care to properly lock the data header */
  88. _starpu_spin_lock(&initial_handle->header_lock);
  89. /* there should not be mutiple filters applied on the same data */
  90. STARPU_ASSERT(initial_handle->nchildren == 0);
  91. /* how many parts ? */
  92. if (f->get_nchildren)
  93. nparts = f->get_nchildren(f, initial_handle);
  94. else
  95. nparts = f->nchildren;
  96. STARPU_ASSERT(nparts > 0);
  97. /* allocate the children */
  98. starpu_data_create_children(initial_handle, nparts, f);
  99. for (i = 0; i < nparts; i++)
  100. {
  101. starpu_data_handle child =
  102. starpu_data_get_child(initial_handle, i);
  103. STARPU_ASSERT(child);
  104. child->nchildren = 0;
  105. child->root_handle = initial_handle->root_handle;
  106. child->father_handle = initial_handle;
  107. child->sibling_index = i;
  108. child->depth = initial_handle->depth + 1;
  109. child->is_not_important = initial_handle->is_not_important;
  110. child->wt_mask = initial_handle->wt_mask;
  111. child->home_node = initial_handle->home_node;
  112. /* initialize the chunk lock */
  113. child->req_list = starpu_data_requester_list_new();
  114. child->refcnt = 0;
  115. _starpu_spin_init(&child->header_lock);
  116. child->sequential_consistency = initial_handle->sequential_consistency;
  117. /* The methods used for reduction are propagated to the
  118. * children. */
  119. child->redux_func = initial_handle->redux_func;
  120. child->init_func = initial_handle->init_func;
  121. unsigned node;
  122. for (node = 0; node < STARPU_MAXNODES; node++)
  123. {
  124. struct starpu_data_replicate_s *initial_replicate;
  125. struct starpu_data_replicate_s *child_replicate;
  126. initial_replicate = &initial_handle->per_node[node];
  127. child_replicate = &child->per_node[node];
  128. child_replicate->state = initial_replicate->state;
  129. child_replicate->allocated = initial_replicate->allocated;
  130. child_replicate->automatically_allocated = initial_replicate->automatically_allocated;
  131. child_replicate->refcnt = 0;
  132. child_replicate->memory_node = node;
  133. /* update the interface */
  134. void *initial_interface = starpu_data_get_interface_on_node(initial_handle, node);
  135. void *child_interface = starpu_data_get_interface_on_node(child, node);
  136. f->filter_func(initial_interface, child_interface, f, i, nparts);
  137. }
  138. unsigned worker;
  139. for (worker = 0; worker < STARPU_NMAXWORKERS; worker++)
  140. {
  141. struct starpu_data_replicate_s *child_replicate;
  142. child_replicate = &child->per_worker[worker];
  143. child_replicate->state = STARPU_INVALID;
  144. child_replicate->allocated = 0;
  145. child_replicate->automatically_allocated = 0;
  146. child_replicate->refcnt = 0;
  147. child_replicate->memory_node = starpu_worker_get_memory_node(worker);
  148. child_replicate->requested = 0;
  149. child_replicate->request = NULL;
  150. child_replicate->relaxed_coherency = 1;
  151. /* duplicate the content of the interface on node 0 */
  152. memcpy(child_replicate->interface, child->per_node[0].interface, child->ops->interface_size);
  153. }
  154. /* We compute the size and the footprint of the child once and
  155. * store it in the handle */
  156. child->data_size = child->ops->get_size(child);
  157. child->footprint = _starpu_compute_data_footprint(child);
  158. }
  159. /* now let the header */
  160. _starpu_spin_unlock(&initial_handle->header_lock);
  161. }
  162. void starpu_data_unpartition(starpu_data_handle root_handle, uint32_t gathering_node)
  163. {
  164. unsigned child;
  165. unsigned node;
  166. _starpu_spin_lock(&root_handle->header_lock);
  167. #warning starpu_data_unpartition is not supported with NO_DATA_RW_LOCK yet ...
  168. /* first take all the children lock (in order !) */
  169. for (child = 0; child < root_handle->nchildren; child++)
  170. {
  171. struct starpu_data_state_t *child_handle = &root_handle->children[child];
  172. /* make sure the intermediate children is unpartitionned as well */
  173. if (child_handle->nchildren > 0)
  174. starpu_data_unpartition(child_handle, gathering_node);
  175. int ret;
  176. ret = _starpu_fetch_data_on_node(child_handle, &child_handle->per_node[gathering_node], STARPU_R, 0, NULL, NULL);
  177. /* for now we pretend that the RAM is almost unlimited and that gathering
  178. * data should be possible from the node that does the unpartionning ... we
  179. * don't want to have the programming deal with memory shortage at that time,
  180. * really */
  181. STARPU_ASSERT(ret == 0);
  182. _starpu_data_free_interfaces(&root_handle->children[child]);
  183. }
  184. /* the gathering_node should now have a valid copy of all the children.
  185. * For all nodes, if the node had all copies and none was locally
  186. * allocated then the data is still valid there, else, it's invalidated
  187. * for the gathering node, if we have some locally allocated data, we
  188. * copy all the children (XXX this should not happen so we just do not
  189. * do anything since this is transparent ?) */
  190. unsigned still_valid[STARPU_MAXNODES];
  191. /* we do 2 passes : the first pass determines wether the data is still
  192. * valid or not, the second pass is needed to choose between STARPU_SHARED and
  193. * STARPU_OWNER */
  194. unsigned nvalids = 0;
  195. /* still valid ? */
  196. for (node = 0; node < STARPU_MAXNODES; node++)
  197. {
  198. /* until an issue is found the data is assumed to be valid */
  199. unsigned isvalid = 1;
  200. for (child = 0; child < root_handle->nchildren; child++)
  201. {
  202. struct starpu_data_replicate_s *local = &root_handle->children[child].per_node[node];
  203. if (local->state == STARPU_INVALID) {
  204. isvalid = 0;
  205. }
  206. if (local->allocated && local->automatically_allocated){
  207. /* free the data copy in a lazy fashion */
  208. _starpu_request_mem_chunk_removal(root_handle, node);
  209. isvalid = 0;
  210. }
  211. #warning free the data replicate if needed
  212. }
  213. /* if there was no invalid copy, the node still has a valid copy */
  214. still_valid[node] = isvalid;
  215. nvalids++;
  216. }
  217. /* either shared or owned */
  218. STARPU_ASSERT(nvalids > 0);
  219. starpu_cache_state newstate = (nvalids == 1)?STARPU_OWNER:STARPU_SHARED;
  220. for (node = 0; node < STARPU_MAXNODES; node++)
  221. {
  222. root_handle->per_node[node].state =
  223. still_valid[node]?newstate:STARPU_INVALID;
  224. }
  225. /* there is no child anymore */
  226. root_handle->nchildren = 0;
  227. /* now the parent may be used again so we release the lock */
  228. _starpu_spin_unlock(&root_handle->header_lock);
  229. }
  230. /* each child may have his own interface type */
  231. static void starpu_data_create_children(starpu_data_handle handle, unsigned nchildren, struct starpu_data_filter *f)
  232. {
  233. handle->children = calloc(nchildren, sizeof(struct starpu_data_state_t));
  234. STARPU_ASSERT(handle->children);
  235. unsigned node;
  236. unsigned worker;
  237. unsigned child;
  238. for (child = 0; child < nchildren; child++)
  239. {
  240. starpu_data_handle handle_child = &handle->children[child];
  241. struct starpu_data_interface_ops_t *ops;
  242. /* what's this child's interface ? */
  243. if (f->get_child_ops)
  244. ops = f->get_child_ops(f, child);
  245. else
  246. ops = handle->ops;
  247. handle_child->ops = ops;
  248. size_t interfacesize = ops->interface_size;
  249. for (node = 0; node < STARPU_MAXNODES; node++)
  250. {
  251. /* relaxed_coherency = 0 */
  252. handle_child->per_node[node].handle = handle_child;
  253. handle_child->per_node[node].interface = calloc(1, interfacesize);
  254. STARPU_ASSERT(handle_child->per_node[node].interface);
  255. }
  256. for (worker = 0; worker < STARPU_NMAXWORKERS; worker++)
  257. {
  258. handle_child->per_worker[worker].handle = handle_child;
  259. handle_child->per_worker[worker].interface = calloc(1, interfacesize);
  260. STARPU_ASSERT(handle_child->per_node[node].interface);
  261. }
  262. }
  263. /* this handle now has children */
  264. handle->nchildren = nchildren;
  265. }