filters.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. /*
  18. * This function applies a starpu_filter on all the elements of a partition
  19. */
  20. static void map_filter(starpu_data_handle root_handle, starpu_filter *f)
  21. {
  22. /* we need to apply the starpu_filter on all leaf of the tree */
  23. if (root_handle->nchildren == 0)
  24. {
  25. /* this is a leaf */
  26. starpu_partition_data(root_handle, f);
  27. }
  28. else {
  29. /* try to apply the starpu_filter recursively */
  30. unsigned child;
  31. for (child = 0; child < root_handle->nchildren; child++)
  32. {
  33. map_filter(&root_handle->children[child], f);
  34. }
  35. }
  36. }
  37. void starpu_map_filters(starpu_data_handle root_handle, unsigned nfilters, ...)
  38. {
  39. unsigned i;
  40. va_list pa;
  41. va_start(pa, nfilters);
  42. for (i = 0; i < nfilters; i++)
  43. {
  44. starpu_filter *next_filter;
  45. next_filter = va_arg(pa, starpu_filter *);
  46. STARPU_ASSERT(next_filter);
  47. map_filter(root_handle, next_filter);
  48. }
  49. va_end(pa);
  50. }
  51. /*
  52. * example starpu_get_sub_data(starpu_data_handle root_handle, 3, 42, 0, 1);
  53. */
  54. starpu_data_handle starpu_data_get_child(starpu_data_handle handle, unsigned i)
  55. {
  56. STARPU_ASSERT(i < handle->nchildren);
  57. return &handle->children[i];
  58. }
  59. starpu_data_handle starpu_get_sub_data(starpu_data_handle root_handle, unsigned depth, ... )
  60. {
  61. STARPU_ASSERT(root_handle);
  62. starpu_data_handle current_handle = root_handle;
  63. /* the variable number of argument must correlate the depth in the tree */
  64. unsigned i;
  65. va_list pa;
  66. va_start(pa, depth);
  67. for (i = 0; i < depth; i++)
  68. {
  69. unsigned next_child;
  70. next_child = va_arg(pa, unsigned);
  71. STARPU_ASSERT(next_child < current_handle->nchildren);
  72. current_handle = &current_handle->children[next_child];
  73. }
  74. va_end(pa);
  75. return current_handle;
  76. }
  77. /*
  78. * For now, we assume that partitionned_data is already properly allocated;
  79. * at least by the starpu_filter function !
  80. */
  81. void starpu_partition_data(starpu_data_handle initial_handle, starpu_filter *f)
  82. {
  83. int nparts;
  84. int i;
  85. /* first take care to properly lock the data header */
  86. _starpu_spin_lock(&initial_handle->header_lock);
  87. /* there should not be mutiple filters applied on the same data */
  88. STARPU_ASSERT(initial_handle->nchildren == 0);
  89. /* this should update the pointers and size of the chunk */
  90. f->filter_func(f, initial_handle);
  91. nparts = initial_handle->nchildren;
  92. STARPU_ASSERT(nparts > 0);
  93. for (i = 0; i < nparts; i++)
  94. {
  95. starpu_data_handle children =
  96. starpu_data_get_child(initial_handle, i);
  97. STARPU_ASSERT(children);
  98. children->nchildren = 0;
  99. children->root_handle = initial_handle->root_handle;
  100. children->father_handle = initial_handle;
  101. children->sibling_index = i;
  102. children->depth = initial_handle->depth + 1;
  103. children->is_not_important = initial_handle->is_not_important;
  104. children->wb_mask = initial_handle->wb_mask;
  105. /* initialize the chunk lock */
  106. children->req_list = starpu_data_requester_list_new();
  107. children->refcnt = 0;
  108. _starpu_spin_init(&children->header_lock);
  109. children->sequential_consistency = initial_handle->sequential_consistency;
  110. unsigned node;
  111. for (node = 0; node < STARPU_MAXNODES; node++)
  112. {
  113. children->per_node[node].state =
  114. initial_handle->per_node[node].state;
  115. children->per_node[node].allocated =
  116. initial_handle->per_node[node].allocated;
  117. children->per_node[node].automatically_allocated = initial_handle->per_node[node].automatically_allocated;
  118. children->per_node[node].refcnt = 0;
  119. }
  120. }
  121. /* now let the header */
  122. _starpu_spin_unlock(&initial_handle->header_lock);
  123. }
  124. void starpu_unpartition_data(starpu_data_handle root_handle, uint32_t gathering_node)
  125. {
  126. unsigned child;
  127. unsigned node;
  128. _starpu_spin_lock(&root_handle->header_lock);
  129. #warning starpu_unpartition_data is not supported with NO_DATA_RW_LOCK yet ...
  130. /* first take all the children lock (in order !) */
  131. for (child = 0; child < root_handle->nchildren; child++)
  132. {
  133. /* make sure the intermediate children is unpartitionned as well */
  134. if (root_handle->children[child].nchildren > 0)
  135. starpu_unpartition_data(&root_handle->children[child], gathering_node);
  136. int ret;
  137. ret = _starpu_fetch_data_on_node(&root_handle->children[child], gathering_node, 1, 0, 0);
  138. /* for now we pretend that the RAM is almost unlimited and that gathering
  139. * data should be possible from the node that does the unpartionning ... we
  140. * don't want to have the programming deal with memory shortage at that time,
  141. * really */
  142. STARPU_ASSERT(ret == 0);
  143. starpu_data_liberate_interfaces(&root_handle->children[child]);
  144. }
  145. /* the gathering_node should now have a valid copy of all the children.
  146. * For all nodes, if the node had all copies and none was locally
  147. * allocated then the data is still valid there, else, it's invalidated
  148. * for the gathering node, if we have some locally allocated data, we
  149. * copy all the children (XXX this should not happen so we just do not
  150. * do anything since this is transparent ?) */
  151. unsigned still_valid[STARPU_MAXNODES];
  152. /* we do 2 passes : the first pass determines wether the data is still
  153. * valid or not, the second pass is needed to choose between STARPU_SHARED and
  154. * STARPU_OWNER */
  155. unsigned nvalids = 0;
  156. /* still valid ? */
  157. for (node = 0; node < STARPU_MAXNODES; node++)
  158. {
  159. /* until an issue is found the data is assumed to be valid */
  160. unsigned isvalid = 1;
  161. for (child = 0; child < root_handle->nchildren; child++)
  162. {
  163. starpu_local_data_state *local = &root_handle->children[child].per_node[node];
  164. if (local->state == STARPU_INVALID) {
  165. isvalid = 0;
  166. }
  167. if (local->allocated && local->automatically_allocated){
  168. /* free the data copy in a lazy fashion */
  169. _starpu_request_mem_chunk_removal(root_handle, node);
  170. isvalid = 0;
  171. }
  172. }
  173. /* no problem was found so the node still has a valid copy */
  174. still_valid[node] = isvalid;
  175. nvalids++;
  176. }
  177. /* either shared or owned */
  178. STARPU_ASSERT(nvalids > 0);
  179. starpu_cache_state newstate = (nvalids == 1)?STARPU_OWNER:STARPU_SHARED;
  180. for (node = 0; node < STARPU_MAXNODES; node++)
  181. {
  182. root_handle->per_node[node].state =
  183. still_valid[node]?newstate:STARPU_INVALID;
  184. }
  185. /* there is no child anymore */
  186. root_handle->nchildren = 0;
  187. /* now the parent may be used again so we release the lock */
  188. _starpu_spin_unlock(&root_handle->header_lock);
  189. }
  190. /* TODO create an alternative version of that function which takes an array of
  191. * data interface ops in case each child may have its own interface type */
  192. void starpu_data_create_children(starpu_data_handle handle,
  193. unsigned nchildren, struct starpu_data_interface_ops_t *children_interface_ops)
  194. {
  195. handle->children = calloc(nchildren, sizeof(struct starpu_data_state_t));
  196. STARPU_ASSERT(handle->children);
  197. unsigned node;
  198. unsigned child;
  199. for (child = 0; child < nchildren; child++)
  200. {
  201. starpu_data_handle handle_child = &handle->children[child];
  202. handle_child->ops = children_interface_ops;
  203. size_t interfacesize = children_interface_ops->interface_size;
  204. for (node = 0; node < STARPU_MAXNODES; node++)
  205. {
  206. handle_child->interface[node] = calloc(1, interfacesize);
  207. STARPU_ASSERT(handle->children->interface[node]);
  208. }
  209. }
  210. handle->nchildren = nchildren;
  211. }