hierarchy.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (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 "hierarchy.h"
  17. /*
  18. * Stop monitoring a data
  19. */
  20. /* TODO : move in a more appropriate file */
  21. void starpu_delete_data(data_state *state)
  22. {
  23. unsigned node;
  24. STARPU_ASSERT(state);
  25. for (node = 0; node < MAXNODES; node++)
  26. {
  27. local_data_state *local = &state->per_node[node];
  28. if (local->allocated && local->automatically_allocated){
  29. /* free the data copy in a lazy fashion */
  30. request_mem_chunk_removal(state, node);
  31. }
  32. }
  33. data_requester_list_delete(state->req_list);
  34. free(state);
  35. }
  36. void register_new_data(data_state *state, uint32_t home_node, uint32_t wb_mask)
  37. {
  38. STARPU_ASSERT(state);
  39. /* initialize the new lock */
  40. state->req_list = data_requester_list_new();
  41. state->refcnt = 0;
  42. starpu_spin_init(&state->header_lock);
  43. /* first take care to properly lock the data */
  44. starpu_spin_lock(&state->header_lock);
  45. /* we assume that all nodes may use that data */
  46. state->nnodes = MAXNODES;
  47. /* there is no hierarchy yet */
  48. state->nchildren = 0;
  49. state->is_not_important = 0;
  50. state->wb_mask = wb_mask;
  51. /* that new data is invalid from all nodes perpective except for the
  52. * home node */
  53. unsigned node;
  54. for (node = 0; node < MAXNODES; node++)
  55. {
  56. if (node == home_node) {
  57. /* this is the home node with the only valid copy */
  58. state->per_node[node].state = OWNER;
  59. state->per_node[node].allocated = 1;
  60. state->per_node[node].automatically_allocated = 0;
  61. state->per_node[node].refcnt = 0;
  62. }
  63. else {
  64. /* the value is not available here yet */
  65. state->per_node[node].state = INVALID;
  66. state->per_node[node].allocated = 0;
  67. state->per_node[node].refcnt = 0;
  68. }
  69. }
  70. /* now the data is available ! */
  71. starpu_spin_unlock(&state->header_lock);
  72. }
  73. /*
  74. * This function applies a starpu_filter on all the elements of a partition
  75. */
  76. static void map_filter(data_state *root_data, starpu_filter *f)
  77. {
  78. /* we need to apply the starpu_filter on all leaf of the tree */
  79. if (root_data->nchildren == 0)
  80. {
  81. /* this is a leaf */
  82. starpu_partition_data(root_data, f);
  83. }
  84. else {
  85. /* try to apply the starpu_filter recursively */
  86. int child;
  87. for (child = 0; child < root_data->nchildren; child++)
  88. {
  89. map_filter(&root_data->children[child], f);
  90. }
  91. }
  92. }
  93. void starpu_map_filters(data_state *root_data, unsigned nfilters, ...)
  94. {
  95. unsigned i;
  96. va_list pa;
  97. va_start(pa, nfilters);
  98. for (i = 0; i < nfilters; i++)
  99. {
  100. starpu_filter *next_filter;
  101. next_filter = va_arg(pa, starpu_filter *);
  102. STARPU_ASSERT(next_filter);
  103. map_filter(root_data, next_filter);
  104. }
  105. va_end(pa);
  106. }
  107. /*
  108. * example get_sub_data(data_state *root_data, 3, 42, 0, 1);
  109. */
  110. starpu_data_handle starpu_data_get_child(starpu_data_handle handle, unsigned i)
  111. {
  112. #warning TODO nchildren should not be an int
  113. STARPU_ASSERT(i < (unsigned)handle->nchildren);
  114. return &handle->children[i];
  115. }
  116. data_state *get_sub_data(data_state *root_data, unsigned depth, ... )
  117. {
  118. STARPU_ASSERT(root_data);
  119. data_state *current_data = root_data;
  120. /* the variable number of argument must correlate the depth in the tree */
  121. unsigned i;
  122. va_list pa;
  123. va_start(pa, depth);
  124. for (i = 0; i < depth; i++)
  125. {
  126. unsigned next_child;
  127. next_child = va_arg(pa, unsigned);
  128. STARPU_ASSERT((int)next_child < current_data->nchildren);
  129. current_data = &current_data->children[next_child];
  130. }
  131. va_end(pa);
  132. return current_data;
  133. }
  134. /*
  135. * For now, we assume that partitionned_data is already properly allocated;
  136. * at least by the starpu_filter function !
  137. */
  138. void starpu_partition_data(data_state *initial_data, starpu_filter *f)
  139. {
  140. int nparts;
  141. int i;
  142. /* first take care to properly lock the data header */
  143. starpu_spin_lock(&initial_data->header_lock);
  144. /* there should not be mutiple filters applied on the same data */
  145. STARPU_ASSERT(initial_data->nchildren == 0);
  146. /* this should update the pointers and size of the chunk */
  147. nparts = f->filter_func(f, initial_data);
  148. STARPU_ASSERT(nparts > 0);
  149. initial_data->nchildren = nparts;
  150. for (i = 0; i < nparts; i++)
  151. {
  152. data_state *children = &initial_data->children[i];
  153. STARPU_ASSERT(children);
  154. children->nchildren = 0;
  155. children->is_not_important = initial_data->is_not_important;
  156. /* it is possible that the children does not use the same interface as the parent,
  157. * in that case, the starpu_filter must set the proper methods */
  158. if (!children->ops)
  159. children->ops = initial_data->ops;
  160. children->wb_mask = initial_data->wb_mask;
  161. /* initialize the chunk lock */
  162. children->req_list = data_requester_list_new();
  163. children->refcnt = 0;
  164. starpu_spin_init(&children->header_lock);
  165. unsigned node;
  166. for (node = 0; node < MAXNODES; node++)
  167. {
  168. children->per_node[node].state =
  169. initial_data->per_node[node].state;
  170. children->per_node[node].allocated =
  171. initial_data->per_node[node].allocated;
  172. children->per_node[node].automatically_allocated = initial_data->per_node[node].automatically_allocated;
  173. children->per_node[node].refcnt = 0;
  174. }
  175. }
  176. /* now let the header */
  177. starpu_spin_unlock(&initial_data->header_lock);
  178. }
  179. void starpu_unpartition_data(data_state *root_data, uint32_t gathering_node)
  180. {
  181. int child;
  182. unsigned node;
  183. starpu_spin_lock(&root_data->header_lock);
  184. #warning starpu_unpartition_data is not supported with NO_DATA_RW_LOCK yet ...
  185. /* first take all the children lock (in order !) */
  186. for (child = 0; child < root_data->nchildren; child++)
  187. {
  188. /* make sure the intermediate children is unpartitionned as well */
  189. if (root_data->children[child].nchildren > 0)
  190. starpu_unpartition_data(&root_data->children[child], gathering_node);
  191. int ret;
  192. ret = fetch_data_on_node(&root_data->children[child], gathering_node, 1, 0, 0);
  193. /* for now we pretend that the RAM is almost unlimited and that gathering
  194. * data should be possible from the node that does the unpartionning ... we
  195. * don't want to have the programming deal with memory shortage at that time,
  196. * really */
  197. STARPU_ASSERT(ret == 0);
  198. }
  199. /* the gathering_node should now have a valid copy of all the children.
  200. * For all nodes, if the node had all copies and none was locally
  201. * allocated then the data is still valid there, else, it's invalidated
  202. * for the gathering node, if we have some locally allocated data, we
  203. * copy all the children (XXX this should not happen so we just do not
  204. * do anything since this is transparent ?) */
  205. unsigned still_valid[MAXNODES];
  206. /* we do 2 passes : the first pass determines wether the data is still
  207. * valid or not, the second pass is needed to choose between SHARED and
  208. * OWNER */
  209. unsigned nvalids = 0;
  210. /* still valid ? */
  211. for (node = 0; node < MAXNODES; node++)
  212. {
  213. /* until an issue is found the data is assumed to be valid */
  214. unsigned isvalid = 1;
  215. for (child = 0; child < root_data->nchildren; child++)
  216. {
  217. local_data_state *local = &root_data->children[child].per_node[node];
  218. if (local->state == INVALID) {
  219. isvalid = 0;
  220. }
  221. if (local->allocated && local->automatically_allocated){
  222. /* free the data copy in a lazy fashion */
  223. request_mem_chunk_removal(root_data, node);
  224. isvalid = 0;
  225. }
  226. }
  227. /* no problem was found so the node still has a valid copy */
  228. still_valid[node] = isvalid;
  229. nvalids++;
  230. }
  231. /* either shared or owned */
  232. STARPU_ASSERT(nvalids > 0);
  233. cache_state newstate = (nvalids == 1)?OWNER:SHARED;
  234. for (node = 0; node < MAXNODES; node++)
  235. {
  236. root_data->per_node[node].state =
  237. still_valid[node]?newstate:INVALID;
  238. }
  239. /* there is no child anymore */
  240. root_data->nchildren = 0;
  241. /* now the parent may be used again so we release the lock */
  242. starpu_spin_unlock(&root_data->header_lock);
  243. }
  244. /* TODO move ! */
  245. void starpu_advise_if_data_is_important(data_state *state, unsigned is_important)
  246. {
  247. starpu_spin_lock(&state->header_lock);
  248. /* first take all the children lock (in order !) */
  249. int child;
  250. for (child = 0; child < state->nchildren; child++)
  251. {
  252. /* make sure the intermediate children is advised as well */
  253. if (state->children[child].nchildren > 0)
  254. starpu_advise_if_data_is_important(&state->children[child], is_important);
  255. }
  256. state->is_not_important = !is_important;
  257. /* now the parent may be used again so we release the lock */
  258. starpu_spin_unlock(&state->header_lock);
  259. }
  260. void starpu_data_create_children(starpu_data_handle handle, unsigned nchildren)
  261. {
  262. handle->children = calloc(nchildren, sizeof(data_state));
  263. STARPU_ASSERT(handle->children);
  264. handle->nchildren = nchildren;
  265. }