hierarchy.c 10 KB

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