component_composed.c 7.6 KB

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