filters.c 10 KB

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