filters.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2011 Université de Bordeaux 1
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <datawizard/filters.h>
  19. #include <datawizard/footprint.h>
  20. static void starpu_data_create_children(starpu_data_handle_t handle, unsigned nchildren, struct starpu_data_filter *f);
  21. /*
  22. * This function applies a data filter on all the elements of a partition
  23. */
  24. static void map_filter(starpu_data_handle_t root_handle, struct starpu_data_filter *f)
  25. {
  26. /* we need to apply the data filter on all leaf of the tree */
  27. if (root_handle->nchildren == 0)
  28. {
  29. /* this is a leaf */
  30. starpu_data_partition(root_handle, f);
  31. }
  32. else
  33. {
  34. /* try to apply the data filter recursively */
  35. unsigned child;
  36. for (child = 0; child < root_handle->nchildren; child++)
  37. {
  38. map_filter(&root_handle->children[child], f);
  39. }
  40. }
  41. }
  42. void starpu_data_vmap_filters(starpu_data_handle_t root_handle, unsigned nfilters, va_list pa)
  43. {
  44. unsigned i;
  45. for (i = 0; i < nfilters; i++)
  46. {
  47. struct starpu_data_filter *next_filter;
  48. next_filter = va_arg(pa, struct starpu_data_filter *);
  49. STARPU_ASSERT(next_filter);
  50. map_filter(root_handle, next_filter);
  51. }
  52. }
  53. void starpu_data_map_filters(starpu_data_handle_t root_handle, unsigned nfilters, ...)
  54. {
  55. va_list pa;
  56. va_start(pa, nfilters);
  57. starpu_data_vmap_filters(root_handle, nfilters, pa);
  58. va_end(pa);
  59. }
  60. int starpu_data_get_nb_children(starpu_data_handle_t handle)
  61. {
  62. return handle->nchildren;
  63. }
  64. starpu_data_handle_t starpu_data_get_child(starpu_data_handle_t handle, unsigned i)
  65. {
  66. STARPU_ASSERT(i < handle->nchildren);
  67. return &handle->children[i];
  68. }
  69. /*
  70. * example starpu_data_get_sub_data(starpu_data_handle_t root_handle, 3, 42, 0, 1);
  71. */
  72. starpu_data_handle_t starpu_data_get_sub_data(starpu_data_handle_t root_handle, unsigned depth, ... )
  73. {
  74. va_list pa;
  75. va_start(pa, depth);
  76. starpu_data_handle_t handle = starpu_data_vget_sub_data(root_handle, depth, pa);
  77. va_end(pa);
  78. return handle;
  79. }
  80. starpu_data_handle_t starpu_data_vget_sub_data(starpu_data_handle_t root_handle, unsigned depth, va_list pa )
  81. {
  82. STARPU_ASSERT(root_handle);
  83. starpu_data_handle_t current_handle = root_handle;
  84. /* the variable number of argument must correlate the depth in the tree */
  85. unsigned i;
  86. for (i = 0; i < depth; i++)
  87. {
  88. unsigned next_child;
  89. next_child = va_arg(pa, unsigned);
  90. STARPU_ASSERT(next_child < current_handle->nchildren);
  91. current_handle = &current_handle->children[next_child];
  92. }
  93. return current_handle;
  94. }
  95. void starpu_data_partition(starpu_data_handle_t initial_handle, struct starpu_data_filter *f)
  96. {
  97. unsigned nparts;
  98. unsigned i;
  99. /* first take care to properly lock the data header */
  100. _starpu_spin_lock(&initial_handle->header_lock);
  101. /* there should not be mutiple filters applied on the same data */
  102. STARPU_ASSERT(initial_handle->nchildren == 0);
  103. /* how many parts ? */
  104. if (f->get_nchildren)
  105. nparts = f->get_nchildren(f, initial_handle);
  106. else
  107. nparts = f->nchildren;
  108. STARPU_ASSERT(nparts > 0);
  109. /* allocate the children */
  110. starpu_data_create_children(initial_handle, nparts, f);
  111. unsigned nworkers = starpu_worker_get_count();
  112. for (i = 0; i < nparts; i++)
  113. {
  114. starpu_data_handle_t child =
  115. starpu_data_get_child(initial_handle, i);
  116. STARPU_ASSERT(child);
  117. child->nchildren = 0;
  118. child->rank = initial_handle->rank;
  119. child->root_handle = initial_handle->root_handle;
  120. child->father_handle = initial_handle;
  121. child->sibling_index = i;
  122. child->depth = initial_handle->depth + 1;
  123. child->is_not_important = initial_handle->is_not_important;
  124. child->wt_mask = initial_handle->wt_mask;
  125. child->home_node = initial_handle->home_node;
  126. child->is_readonly = initial_handle->is_readonly;
  127. /* initialize the chunk lock */
  128. child->req_list = _starpu_data_requester_list_new();
  129. child->reduction_req_list = _starpu_data_requester_list_new();
  130. child->refcnt = 0;
  131. child->busy_count = 0;
  132. child->busy_waiting = 0;
  133. _STARPU_PTHREAD_MUTEX_INIT(&child->busy_mutex, NULL);
  134. _STARPU_PTHREAD_COND_INIT(&child->busy_cond, NULL);
  135. child->reduction_refcnt = 0;
  136. _starpu_spin_init(&child->header_lock);
  137. child->sequential_consistency = initial_handle->sequential_consistency;
  138. _STARPU_PTHREAD_MUTEX_INIT(&child->sequential_consistency_mutex, NULL);
  139. child->last_submitted_mode = STARPU_R;
  140. child->last_submitted_writer = NULL;
  141. child->last_submitted_readers = NULL;
  142. child->post_sync_tasks = NULL;
  143. child->post_sync_tasks_cnt = 0;
  144. /* The methods used for reduction are propagated to the
  145. * children. */
  146. child->redux_cl = initial_handle->redux_cl;
  147. child->init_cl = initial_handle->init_cl;
  148. #ifdef STARPU_USE_FXT
  149. child->last_submitted_ghost_writer_id_is_valid = 0;
  150. child->last_submitted_ghost_writer_id = 0;
  151. child->last_submitted_ghost_readers_id = NULL;
  152. #endif
  153. unsigned node;
  154. for (node = 0; node < STARPU_MAXNODES; node++)
  155. {
  156. struct _starpu_data_replicate *initial_replicate;
  157. struct _starpu_data_replicate *child_replicate;
  158. initial_replicate = &initial_handle->per_node[node];
  159. child_replicate = &child->per_node[node];
  160. child_replicate->state = initial_replicate->state;
  161. child_replicate->allocated = initial_replicate->allocated;
  162. child_replicate->automatically_allocated = initial_replicate->automatically_allocated;
  163. child_replicate->refcnt = 0;
  164. child_replicate->memory_node = node;
  165. child_replicate->relaxed_coherency = 0;
  166. /* update the interface */
  167. void *initial_interface = starpu_data_get_interface_on_node(initial_handle, node);
  168. void *child_interface = starpu_data_get_interface_on_node(child, node);
  169. f->filter_func(initial_interface, child_interface, f, i, nparts);
  170. }
  171. unsigned worker;
  172. for (worker = 0; worker < nworkers; worker++)
  173. {
  174. struct _starpu_data_replicate *child_replicate;
  175. child_replicate = &child->per_worker[worker];
  176. child_replicate->state = STARPU_INVALID;
  177. child_replicate->allocated = 0;
  178. child_replicate->automatically_allocated = 0;
  179. child_replicate->refcnt = 0;
  180. child_replicate->memory_node = starpu_worker_get_memory_node(worker);
  181. for (node = 0; node < STARPU_MAXNODES; node++)
  182. {
  183. child_replicate->requested[node] = 0;
  184. child_replicate->request[node] = NULL;
  185. }
  186. child_replicate->relaxed_coherency = 1;
  187. child_replicate->initialized = 0;
  188. /* duplicate the content of the interface on node 0 */
  189. memcpy(child_replicate->data_interface, child->per_node[0].data_interface, child->ops->interface_size);
  190. }
  191. /* We compute the size and the footprint of the child once and
  192. * store it in the handle */
  193. child->data_size = child->ops->get_size(child);
  194. child->footprint = _starpu_compute_data_footprint(child);
  195. void *ptr;
  196. ptr = starpu_handle_to_pointer(child, 0);
  197. if (ptr != NULL)
  198. {
  199. _starpu_data_register_ram_pointer(child, ptr);
  200. }
  201. }
  202. /* now let the header */
  203. _starpu_spin_unlock(&initial_handle->header_lock);
  204. }
  205. void starpu_data_unpartition(starpu_data_handle_t root_handle, uint32_t gathering_node)
  206. {
  207. unsigned child;
  208. unsigned node;
  209. _starpu_spin_lock(&root_handle->header_lock);
  210. /* first take all the children lock (in order !) */
  211. for (child = 0; child < root_handle->nchildren; child++)
  212. {
  213. struct _starpu_data_state *child_handle = &root_handle->children[child];
  214. /* make sure the intermediate children is unpartitionned as well */
  215. if (child_handle->nchildren > 0)
  216. starpu_data_unpartition(child_handle, gathering_node);
  217. int ret;
  218. ret = _starpu_fetch_data_on_node(child_handle, &child_handle->per_node[gathering_node], STARPU_R, 0, NULL, NULL);
  219. /* for now we pretend that the RAM is almost unlimited and that gathering
  220. * data should be possible from the node that does the unpartionning ... we
  221. * don't want to have the programming deal with memory shortage at that time,
  222. * really */
  223. STARPU_ASSERT(ret == 0);
  224. _starpu_data_free_interfaces(&root_handle->children[child]);
  225. _starpu_data_requester_list_delete(child_handle->req_list);
  226. _starpu_data_requester_list_delete(child_handle->reduction_req_list);
  227. }
  228. /* the gathering_node should now have a valid copy of all the children.
  229. * For all nodes, if the node had all copies and none was locally
  230. * allocated then the data is still valid there, else, it's invalidated
  231. * for the gathering node, if we have some locally allocated data, we
  232. * copy all the children (XXX this should not happen so we just do not
  233. * do anything since this is transparent ?) */
  234. unsigned still_valid[STARPU_MAXNODES];
  235. /* we do 2 passes : the first pass determines wether the data is still
  236. * valid or not, the second pass is needed to choose between STARPU_SHARED and
  237. * STARPU_OWNER */
  238. unsigned nvalids = 0;
  239. /* still valid ? */
  240. for (node = 0; node < STARPU_MAXNODES; node++)
  241. {
  242. /* until an issue is found the data is assumed to be valid */
  243. unsigned isvalid = 1;
  244. for (child = 0; child < root_handle->nchildren; child++)
  245. {
  246. struct _starpu_data_replicate *local = &root_handle->children[child].per_node[node];
  247. if (local->state == STARPU_INVALID)
  248. {
  249. /* One of the bits is missing */
  250. isvalid = 0;
  251. }
  252. if (local->allocated && local->automatically_allocated)
  253. /* free the child data copy in a lazy fashion */
  254. _starpu_request_mem_chunk_removal(&root_handle->children[child], node);
  255. }
  256. if (!root_handle->per_node[node].allocated)
  257. /* Even if we have all the bits, if we don't have the
  258. * whole data, it's not valid */
  259. isvalid = 0;
  260. if (!isvalid && root_handle->per_node[node].allocated && root_handle->per_node[node].automatically_allocated)
  261. /* free the data copy in a lazy fashion */
  262. _starpu_request_mem_chunk_removal(root_handle, node);
  263. /* if there was no invalid copy, the node still has a valid copy */
  264. still_valid[node] = isvalid;
  265. if (isvalid)
  266. nvalids++;
  267. }
  268. /* either shared or owned */
  269. STARPU_ASSERT(nvalids > 0);
  270. enum _starpu_cache_state newstate = (nvalids == 1)?STARPU_OWNER:STARPU_SHARED;
  271. for (node = 0; node < STARPU_MAXNODES; node++)
  272. {
  273. root_handle->per_node[node].state =
  274. still_valid[node]?newstate:STARPU_INVALID;
  275. }
  276. /* there is no child anymore */
  277. free(root_handle->children);
  278. root_handle->children = NULL;
  279. root_handle->nchildren = 0;
  280. /* now the parent may be used again so we release the lock */
  281. _starpu_spin_unlock(&root_handle->header_lock);
  282. }
  283. /* each child may have his own interface type */
  284. static void starpu_data_create_children(starpu_data_handle_t handle, unsigned nchildren, struct starpu_data_filter *f)
  285. {
  286. handle->children = (struct _starpu_data_state *) calloc(nchildren, sizeof(struct _starpu_data_state));
  287. STARPU_ASSERT(handle->children);
  288. unsigned node;
  289. unsigned worker;
  290. unsigned child;
  291. unsigned nworkers = starpu_worker_get_count();
  292. for (child = 0; child < nchildren; child++)
  293. {
  294. starpu_data_handle_t handle_child = &handle->children[child];
  295. struct starpu_data_interface_ops *ops;
  296. /* what's this child's interface ? */
  297. if (f->get_child_ops)
  298. ops = f->get_child_ops(f, child);
  299. else
  300. ops = handle->ops;
  301. handle_child->ops = ops;
  302. size_t interfacesize = ops->interface_size;
  303. for (node = 0; node < STARPU_MAXNODES; node++)
  304. {
  305. /* relaxed_coherency = 0 */
  306. handle_child->per_node[node].handle = handle_child;
  307. handle_child->per_node[node].data_interface = calloc(1, interfacesize);
  308. STARPU_ASSERT(handle_child->per_node[node].data_interface);
  309. }
  310. for (worker = 0; worker < nworkers; worker++)
  311. {
  312. handle_child->per_worker[worker].handle = handle_child;
  313. handle_child->per_worker[worker].data_interface = calloc(1, interfacesize);
  314. STARPU_ASSERT(handle_child->per_worker[worker].data_interface);
  315. }
  316. }
  317. /* this handle now has children */
  318. handle->nchildren = nchildren;
  319. }