filters.c 7.8 KB

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