component_composed.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013 Inria
  4. * Copyright (C) 2014-2017 CNRS
  5. * Copyright (C) 2014-2017 Université de Bordeaux
  6. * Copyright (C) 2013 Simon Archipoff
  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 <starpu_sched_component.h>
  20. #include <common/list.h>
  21. /* a composed component is parametred by a list of pair
  22. * (create_component_function(arg), arg)
  23. */
  24. LIST_TYPE(fun_create_component,
  25. struct starpu_sched_component *(*create_component)(struct starpu_sched_tree *tree, void * arg);
  26. void * arg;
  27. );
  28. struct starpu_sched_component_composed_recipe
  29. {
  30. struct fun_create_component_list list;
  31. };
  32. struct starpu_sched_component_composed_recipe * starpu_sched_component_composed_recipe_create(void)
  33. {
  34. struct starpu_sched_component_composed_recipe *recipe;
  35. _STARPU_MALLOC(recipe, sizeof(*recipe));
  36. fun_create_component_list_init(&recipe->list);
  37. return recipe;
  38. }
  39. void starpu_sched_component_composed_recipe_add(struct starpu_sched_component_composed_recipe * recipe,
  40. struct starpu_sched_component *(*create_component)(struct starpu_sched_tree *tree, void * arg),
  41. void * arg)
  42. {
  43. struct fun_create_component * e = fun_create_component_new();
  44. e->create_component = create_component;
  45. e->arg = arg;
  46. fun_create_component_list_push_back(&recipe->list, e);
  47. }
  48. struct starpu_sched_component_composed_recipe * starpu_sched_component_composed_recipe_create_singleton(struct starpu_sched_component *(*create_component)(struct starpu_sched_tree *tree, void * arg),
  49. void * arg)
  50. {
  51. struct starpu_sched_component_composed_recipe * r = starpu_sched_component_composed_recipe_create();
  52. starpu_sched_component_composed_recipe_add(r, create_component, arg);
  53. return r;
  54. }
  55. void starpu_sched_component_composed_recipe_destroy(struct starpu_sched_component_composed_recipe * recipe)
  56. {
  57. if(!recipe)
  58. return;
  59. while(!fun_create_component_list_empty(&recipe->list))
  60. fun_create_component_delete(fun_create_component_list_pop_back(&recipe->list));
  61. free(recipe);
  62. }
  63. struct composed_component
  64. {
  65. struct starpu_sched_component *top,*bottom;
  66. };
  67. /* this function actualy build the composed component data by changing the list of
  68. * (component_create_fun, arg_create_fun) into a tree where all components have 1 childs
  69. */
  70. struct composed_component create_composed_component(struct starpu_sched_tree *tree, struct starpu_sched_component_composed_recipe * recipe
  71. #ifdef STARPU_HAVE_HWLOC
  72. ,hwloc_obj_t obj
  73. #endif
  74. )
  75. {
  76. struct composed_component c;
  77. STARPU_ASSERT(recipe);
  78. struct fun_create_component_list * list = &recipe->list;
  79. struct fun_create_component * i = fun_create_component_list_begin(list);
  80. STARPU_ASSERT(i);
  81. STARPU_ASSERT(i->create_component);
  82. c.top = c.bottom = i->create_component(tree, i->arg);
  83. #ifdef STARPU_HAVE_HWLOC
  84. c.top->obj = obj;
  85. #endif
  86. for(i = fun_create_component_list_next(i);
  87. i != fun_create_component_list_end(list);
  88. i = fun_create_component_list_next(i))
  89. {
  90. STARPU_ASSERT(i->create_component);
  91. struct starpu_sched_component * component = i->create_component(tree, i->arg);
  92. #ifdef STARPU_HAVE_HWLOC
  93. component->obj = obj;
  94. #endif
  95. c.bottom->add_child(c.bottom, component);
  96. /* we want to be able to traverse scheduler bottom up for all sched ctxs
  97. * when a worker call pop()
  98. */
  99. unsigned j;
  100. for(j = 0; j < STARPU_NMAX_SCHED_CTXS; j++)
  101. component->add_parent(component, c.bottom);
  102. c.bottom = component;
  103. }
  104. STARPU_ASSERT(!starpu_sched_component_is_worker(c.bottom));
  105. return c;
  106. }
  107. static int composed_component_push_task(struct starpu_sched_component * component, struct starpu_task * task)
  108. {
  109. struct composed_component *c = component->data;
  110. return starpu_sched_component_push_task(component,c->top,task);
  111. }
  112. struct starpu_task * composed_component_pull_task(struct starpu_sched_component *component)
  113. {
  114. struct composed_component *c = component->data;
  115. struct starpu_task * task = NULL;
  116. task = starpu_sched_component_pull_task(c->bottom,component);
  117. if(task)
  118. return task;
  119. unsigned i;
  120. for(i=0; i < component->nparents; i++)
  121. {
  122. if(component->parents[i] == NULL)
  123. continue;
  124. else
  125. {
  126. task = starpu_sched_component_pull_task(component->parents[i],component);
  127. if(task)
  128. break;
  129. }
  130. }
  131. return task;
  132. }
  133. double composed_component_estimated_load(struct starpu_sched_component * component)
  134. {
  135. struct composed_component * c = component->data;
  136. return c->top->estimated_load(c->top);
  137. }
  138. static void composed_component_add_child(struct starpu_sched_component * component, struct starpu_sched_component * child)
  139. {
  140. struct composed_component * c = component->data;
  141. component->add_child(component, child);
  142. c->bottom->add_child(c->bottom, child);
  143. }
  144. static void composed_component_remove_child(struct starpu_sched_component * component, struct starpu_sched_component * child)
  145. {
  146. struct composed_component * c = component->data;
  147. component->remove_child(component, child);
  148. c->bottom->remove_child(c->bottom, child);
  149. }
  150. static void composed_component_notify_change_workers(struct starpu_sched_component * component)
  151. {
  152. struct composed_component * c = component->data;
  153. struct starpu_bitmap * workers = component->workers;
  154. struct starpu_bitmap * workers_in_ctx = component->workers_in_ctx;
  155. struct starpu_sched_component * n;
  156. for(n = c->top; ;n = n->children[0])
  157. {
  158. starpu_bitmap_unset_all(n->workers);
  159. starpu_bitmap_or(n->workers, workers);
  160. starpu_bitmap_unset_all(n->workers_in_ctx);
  161. starpu_bitmap_or(n->workers_in_ctx, workers_in_ctx);
  162. n->properties = component->properties;
  163. if(n == c->bottom)
  164. break;
  165. }
  166. }
  167. void composed_component_deinit_data(struct starpu_sched_component * _component)
  168. {
  169. struct composed_component *c = _component->data;
  170. c->bottom->children = NULL;
  171. c->bottom->nchildren = 0;
  172. struct starpu_sched_component * component;
  173. struct starpu_sched_component * next = c->top;
  174. do
  175. {
  176. component = next;
  177. component->workers = NULL;
  178. next = component->children ? component->children[0] : NULL;
  179. starpu_sched_component_destroy(component);
  180. }
  181. while(next);
  182. free(c);
  183. _component->data = NULL;
  184. }
  185. struct starpu_sched_component * starpu_sched_component_composed_component_create(struct starpu_sched_tree *tree, struct starpu_sched_component_composed_recipe * recipe)
  186. {
  187. STARPU_ASSERT(!fun_create_component_list_empty(&recipe->list));
  188. struct fun_create_component_list * l = &recipe->list;
  189. if(l->_head == l->_tail)
  190. return l->_head->create_component(tree, l->_head->arg);
  191. struct starpu_sched_component * component = starpu_sched_component_create(tree, "composed");
  192. struct composed_component *c;
  193. _STARPU_MALLOC(c, sizeof(struct composed_component));
  194. *c = create_composed_component(tree, recipe
  195. #ifdef STARPU_HAVE_HWLOC
  196. ,component->obj
  197. #endif
  198. );
  199. c->bottom->nchildren = component->nchildren;
  200. c->bottom->children = component->children;
  201. c->bottom->nparents = component->nparents;
  202. c->bottom->parents = component->parents;
  203. component->data = c;
  204. component->push_task = composed_component_push_task;
  205. component->pull_task = composed_component_pull_task;
  206. component->estimated_load = composed_component_estimated_load;
  207. component->estimated_end = starpu_sched_component_estimated_end_min;
  208. component->add_child = composed_component_add_child;
  209. component->remove_child = composed_component_remove_child;
  210. component->notify_change_workers = composed_component_notify_change_workers;
  211. return component;
  212. }