filters.c 14 KB

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