filters.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 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, starpu_filter *f);
  19. /*
  20. * This function applies a starpu_filter on all the elements of a partition
  21. */
  22. static void map_filter(starpu_data_handle root_handle, starpu_filter *f)
  23. {
  24. /* we need to apply the starpu_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 starpu_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_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. starpu_filter *next_filter;
  47. next_filter = va_arg(pa, starpu_filter *);
  48. STARPU_ASSERT(next_filter);
  49. map_filter(root_handle, next_filter);
  50. }
  51. va_end(pa);
  52. }
  53. /*
  54. * example starpu_data_get_sub_data(starpu_data_handle root_handle, 3, 42, 0, 1);
  55. */
  56. starpu_data_handle starpu_data_get_child(starpu_data_handle handle, unsigned i)
  57. {
  58. STARPU_ASSERT(i < handle->nchildren);
  59. return &handle->children[i];
  60. }
  61. starpu_data_handle starpu_data_get_sub_data(starpu_data_handle root_handle, unsigned depth, ... )
  62. {
  63. STARPU_ASSERT(root_handle);
  64. starpu_data_handle current_handle = root_handle;
  65. /* the variable number of argument must correlate the depth in the tree */
  66. unsigned i;
  67. va_list pa;
  68. va_start(pa, depth);
  69. for (i = 0; i < depth; i++)
  70. {
  71. unsigned next_child;
  72. next_child = va_arg(pa, unsigned);
  73. STARPU_ASSERT(next_child < current_handle->nchildren);
  74. current_handle = &current_handle->children[next_child];
  75. }
  76. va_end(pa);
  77. return current_handle;
  78. }
  79. void starpu_data_partition(starpu_data_handle initial_handle, starpu_filter *f)
  80. {
  81. unsigned nparts;
  82. unsigned i;
  83. /* first take care to properly lock the data header */
  84. _starpu_spin_lock(&initial_handle->header_lock);
  85. /* there should not be mutiple filters applied on the same data */
  86. STARPU_ASSERT(initial_handle->nchildren == 0);
  87. /* how many parts ? */
  88. if (f->get_nchildren)
  89. nparts = f->get_nchildren(f, initial_handle);
  90. else
  91. nparts = f->nchildren;
  92. STARPU_ASSERT(nparts > 0);
  93. /* allocate the children */
  94. starpu_data_create_children(initial_handle, nparts, f);
  95. for (i = 0; i < nparts; i++)
  96. {
  97. starpu_data_handle child =
  98. starpu_data_get_child(initial_handle, i);
  99. STARPU_ASSERT(child);
  100. child->nchildren = 0;
  101. child->root_handle = initial_handle->root_handle;
  102. child->father_handle = initial_handle;
  103. child->sibling_index = i;
  104. child->depth = initial_handle->depth + 1;
  105. child->is_not_important = initial_handle->is_not_important;
  106. child->wb_mask = initial_handle->wb_mask;
  107. child->home_node = initial_handle->home_node;
  108. /* We compute the size and the footprint of the child once and
  109. * store it in the handle */
  110. child->data_size = child->ops->get_size(child);
  111. child->footprint = _starpu_compute_data_footprint(child);
  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. unsigned node;
  118. for (node = 0; node < STARPU_MAXNODES; node++)
  119. {
  120. child->per_node[node].state =
  121. initial_handle->per_node[node].state;
  122. child->per_node[node].allocated =
  123. initial_handle->per_node[node].allocated;
  124. child->per_node[node].automatically_allocated = initial_handle->per_node[node].automatically_allocated;
  125. child->per_node[node].refcnt = 0;
  126. /* update the interface */
  127. f->filter_func(initial_handle->interface[node], child->interface[node], f, i, nparts);
  128. }
  129. }
  130. /* now let the header */
  131. _starpu_spin_unlock(&initial_handle->header_lock);
  132. }
  133. void starpu_data_unpartition(starpu_data_handle root_handle, uint32_t gathering_node)
  134. {
  135. unsigned child;
  136. unsigned node;
  137. _starpu_spin_lock(&root_handle->header_lock);
  138. #warning starpu_data_unpartition is not supported with NO_DATA_RW_LOCK yet ...
  139. /* first take all the children lock (in order !) */
  140. for (child = 0; child < root_handle->nchildren; child++)
  141. {
  142. /* make sure the intermediate children is unpartitionned as well */
  143. if (root_handle->children[child].nchildren > 0)
  144. starpu_data_unpartition(&root_handle->children[child], gathering_node);
  145. int ret;
  146. ret = _starpu_fetch_data_on_node(&root_handle->children[child], gathering_node, STARPU_R, 0, NULL, NULL);
  147. /* for now we pretend that the RAM is almost unlimited and that gathering
  148. * data should be possible from the node that does the unpartionning ... we
  149. * don't want to have the programming deal with memory shortage at that time,
  150. * really */
  151. STARPU_ASSERT(ret == 0);
  152. _starpu_data_free_interfaces(&root_handle->children[child]);
  153. }
  154. /* the gathering_node should now have a valid copy of all the children.
  155. * For all nodes, if the node had all copies and none was locally
  156. * allocated then the data is still valid there, else, it's invalidated
  157. * for the gathering node, if we have some locally allocated data, we
  158. * copy all the children (XXX this should not happen so we just do not
  159. * do anything since this is transparent ?) */
  160. unsigned still_valid[STARPU_MAXNODES];
  161. /* we do 2 passes : the first pass determines wether the data is still
  162. * valid or not, the second pass is needed to choose between STARPU_SHARED and
  163. * STARPU_OWNER */
  164. unsigned nvalids = 0;
  165. /* still valid ? */
  166. for (node = 0; node < STARPU_MAXNODES; node++)
  167. {
  168. /* until an issue is found the data is assumed to be valid */
  169. unsigned isvalid = 1;
  170. for (child = 0; child < root_handle->nchildren; child++)
  171. {
  172. starpu_local_data_state *local = &root_handle->children[child].per_node[node];
  173. if (local->state == STARPU_INVALID) {
  174. isvalid = 0;
  175. }
  176. if (local->allocated && local->automatically_allocated){
  177. /* free the data copy in a lazy fashion */
  178. _starpu_request_mem_chunk_removal(root_handle, node);
  179. isvalid = 0;
  180. }
  181. }
  182. /* no problem was found so the node still has a valid copy */
  183. still_valid[node] = isvalid;
  184. nvalids++;
  185. }
  186. /* either shared or owned */
  187. STARPU_ASSERT(nvalids > 0);
  188. starpu_cache_state newstate = (nvalids == 1)?STARPU_OWNER:STARPU_SHARED;
  189. for (node = 0; node < STARPU_MAXNODES; node++)
  190. {
  191. root_handle->per_node[node].state =
  192. still_valid[node]?newstate:STARPU_INVALID;
  193. }
  194. /* there is no child anymore */
  195. root_handle->nchildren = 0;
  196. /* now the parent may be used again so we release the lock */
  197. _starpu_spin_unlock(&root_handle->header_lock);
  198. }
  199. /* each child may have his own interface type */
  200. static void starpu_data_create_children(starpu_data_handle handle, unsigned nchildren, starpu_filter *f)
  201. {
  202. handle->children = calloc(nchildren, sizeof(struct starpu_data_state_t));
  203. STARPU_ASSERT(handle->children);
  204. unsigned node;
  205. unsigned child;
  206. for (child = 0; child < nchildren; child++)
  207. {
  208. starpu_data_handle handle_child = &handle->children[child];
  209. struct starpu_data_interface_ops_t *ops;
  210. /* what's this child's interface ? */
  211. if (f->get_child_ops)
  212. ops = f->get_child_ops(f, child);
  213. else
  214. ops = handle->ops;
  215. handle_child->ops = ops;
  216. size_t interfacesize = ops->interface_size;
  217. for (node = 0; node < STARPU_MAXNODES; node++)
  218. {
  219. handle_child->interface[node] = calloc(1, interfacesize);
  220. STARPU_ASSERT(handle->children->interface[node]);
  221. }
  222. }
  223. /* this handle now has children */
  224. handle->nchildren = nchildren;
  225. }