component_composed.c 7.5 KB

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