hierarchical_scheduler.doxy 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * This file is part of the StarPU Handbook.
  3. * Copyright (C) 2013 Simon Archipoff
  4. * Copyright (C) 2013 INRIA
  5. * See the file version.doxy for copying conditions.
  6. */
  7. /*! \page HierarchicalScheduler Hierarchical Schedulers
  8. \section Introduction
  9. StarPU's Hierarchical Schedulers are made of individual Scheduling Components
  10. hierarchically assembled as a Scheduling Tree. Each Scheduling Component has an
  11. unique purpose, such as prioritizing tasks or mapping tasks over resources.
  12. A typical Scheduling Tree is shown below.
  13. <pre>
  14. |
  15. starpu_push_task |
  16. |
  17. v
  18. Fifo_Component
  19. | ^
  20. | |
  21. v |
  22. Eager_Component
  23. | ^
  24. | |
  25. v |
  26. --------><--------------><--------
  27. | ^ | ^
  28. | | | |
  29. v | v |
  30. Fifo_Component Fifo_Component
  31. | ^ | ^
  32. | | | |
  33. v | v |
  34. Worker_Component Worker_Component
  35. </pre>
  36. When a task is pushed by StarPU in a Hierarchical Scheduler, the task moves from
  37. a Scheduling Component to an other, following the hierarchy of the
  38. Scheduling Tree, and is stored in one of the Scheduling Components of the
  39. strategy.
  40. When a worker wants to pop a task from the Hierarchical Scheduler, the
  41. corresponding Worker Component of the Scheduling Tree tries to pull a task from
  42. its parents, following the hierarchy, and gives it to the worker if it succeded
  43. to get one.
  44. \section UsingHierarchicalSchedulers Using Hierarchical Schedulers
  45. \subsection ExistingHierarchicalSchedulers Existing Hierarchical Schedulers
  46. StarPU is currently shipped with the following pre-defined Hierarchical
  47. Schedulers :
  48. - Eager-based Schedulers (with/without prefetching) : \n
  49. Naive scheduler, which tries to map a task on the first available resource
  50. it finds.
  51. - Prio-based Schedulers (with/without prefetching) : \n
  52. Similar to Eager-Based Schedulers. Can handle tasks which have a defined
  53. priority and schedule them accordingly.
  54. - Random-based Schedulers (with/without prefetching) : \n
  55. Selects randomly a resource to be mapped on for each task.
  56. - HEFT Scheduler : \n
  57. Heterogeneous Earliest Finish Time Scheduler.
  58. This scheduler needs that every task submitted to StarPU have a
  59. defined performance model (\ref PerformanceModelCalibration)
  60. to work efficiently, but can handle tasks without a performance
  61. model.
  62. It is currently needed to set the environment variable \ref STARPU_SCHED
  63. to use those Schedulers. Hierarchical Schedulers' naming is tree-*
  64. \subsection ExampleTreeEagerPrefetchingStrategy An Example : The Tree-Eager-Prefetching Strategy
  65. <pre>
  66. |
  67. starpu_push_task |
  68. |
  69. v
  70. Fifo_Component
  71. | ^
  72. Push | | Can_Push
  73. v |
  74. Eager_Component
  75. | ^
  76. | |
  77. v |
  78. --------><-------------------><---------
  79. | ^ | ^
  80. Push | | Can_Push Push | | Can_Push
  81. v | v |
  82. Fifo_Component Fifo_Component
  83. | ^ | ^
  84. Pull | | Can_Pull Pull | | Can_Pull
  85. v | v |
  86. Worker_Component Worker_Component
  87. </pre>
  88. \subsection Interface
  89. Each Scheduling Component must follow the following pre-defined Interface
  90. to be able to interact with other Scheduling Components.
  91. - Push (Caller_Component, Child_Component, Task) \n
  92. The calling Scheduling Component transfers a task to its
  93. Child Component. When the Push function returns, the task no longer
  94. belongs to the calling Component. The Hierarchical Schedulers'
  95. model relies on this function to perform prefetching.
  96. - Pull (Caller_Component, Parent_Component) -> Task \n
  97. The calling Scheduling Component requests a task from
  98. its Parent Component. When the Pull function ends, the returned
  99. task belongs to the calling Component.
  100. - Can_Push (Caller_Component, Parent_Component) \n
  101. The calling Scheduling Component notifies its Parent Component that
  102. it is ready to accept new tasks.
  103. - Can_Pull (Caller_Component, Child_Component) \n
  104. The calling Scheduling Component notifies its Child Component
  105. that it is ready to give new tasks.
  106. \section BuildAHierarchicalScheduler Build a Hierarchical Scheduler
  107. \subsection PreImplementedComponents Pre-implemented Components
  108. StarPU is currently shipped with the following four Scheduling Components :
  109. - Flow-control Components : Fifo, Prio \n
  110. Components which store tasks. They can also prioritize them if
  111. they have a defined priority. It is possible to define a threshold
  112. for those Components following two criterias : the number of tasks
  113. stored in the Component, or the sum of the expected length of all
  114. tasks stored in the Component.
  115. - Resource-Mapping Components : Mct, Heft, Eager, Random, Work-Stealing \n
  116. "Core" of the Scheduling Strategy, those Components are the
  117. ones who make scheduling choices.
  118. - Worker Components : Worker \n
  119. Each Worker Component modelize a concrete worker.
  120. - Special-Purpose Components : Perfmodel_Select, Best_Implementation \n
  121. Components dedicated to original purposes. The Perfmodel_Select
  122. Component decides which Resource-Mapping Component should be used to
  123. schedule a task. The Best_Implementation Component chooses which
  124. implementation of a task should be used on the choosen resource.
  125. \subsection ProgressionAndValidationRules Progression And Validation Rules
  126. Some rules must be followed to ensure the correctness of a Hierarchical
  127. Scheduler :
  128. - At least one Flow-control Component without threshold per Worker Component
  129. is needed in a Hierarchical Scheduler, to store incoming tasks from StarPU
  130. and to give tasks to Worker Components who asks for it. It is possible to
  131. use one Flow-control Component per Worker Component, or one for all Worker
  132. Components, depending on how the Scheduling Tree is defined.
  133. - At least one Resource-Mapping Component is needed in a Hierarchical
  134. Scheduler. Resource-Mapping Components are the only ones who can make
  135. scheduling choices, and so the only ones who can have several child.
  136. \subsection ImplementAHierarchicalScheduler Implement a Hierarchical Scheduler
  137. The following code shows how the Tree-Eager-Prefetching Scheduler
  138. shown in Section \ref ExampleTreeEagerPrefetchingStrategy is implemented :
  139. \code{.c}
  140. #define _STARPU_SCHED_NTASKS_THRESHOLD_DEFAULT 2
  141. #define _STARPU_SCHED_EXP_LEN_THRESHOLD_DEFAULT 1000000000.0
  142. static void initialize_eager_prefetching_center_policy(unsigned sched_ctx_id)
  143. {
  144. unsigned ntasks_threshold = _STARPU_SCHED_NTASKS_THRESHOLD_DEFAULT;
  145. double exp_len_threshold = _STARPU_SCHED_EXP_LEN_THRESHOLD_DEFAULT;
  146. [...]
  147. starpu_sched_ctx_create_worker_collection
  148. (sched_ctx_id, STARPU_WORKER_LIST);
  149. /* Create the Scheduling Tree */
  150. struct starpu_sched_tree * t =
  151. starpu_sched_tree_create(sched_ctx_id);
  152. /* The Root Component is a Flow-control Fifo Component */
  153. t->root = starpu_sched_component_fifo_create(NULL);
  154. /* The Resource-mapping Component of the strategy is an Eager Component
  155. */
  156. struct starpu_sched_component * eager_component =
  157. starpu_sched_component_eager_create(NULL);
  158. /* Create links between Components : the Eager Component is the child
  159. * of the Root Component */
  160. t->root->add_child
  161. (t->root, eager_component);
  162. eager_component->add_father
  163. (eager_component, t->root);
  164. /* A task threshold is set for the Flow-control Components which will
  165. * be connected to Worker Components. By doing so, this Hierarchical
  166. * Scheduler will be able to perform some prefetching on the resources
  167. */
  168. struct starpu_fifo_data fifo_data =
  169. {
  170. .ntasks_threshold = ntasks_threshold,
  171. .exp_len_threshold = exp_len_threshold,
  172. };
  173. unsigned i;
  174. for(i = 0;
  175. i < starpu_worker_get_count() +
  176. starpu_combined_worker_get_count();
  177. i++)
  178. {
  179. /* Each Worker Component has a Flow-control Fifo Component as
  180. * father */
  181. struct starpu_sched_component * worker_component =
  182. starpu_sched_component_worker_get(i);
  183. struct starpu_sched_component * fifo_component =
  184. starpu_sched_component_fifo_create(&fifo_data);
  185. fifo_component->add_child
  186. (fifo_component, worker_component);
  187. worker_component->add_father
  188. (worker_component, fifo_component);
  189. /* Each Flow-control Fifo Component associated to a Worker
  190. * Component is linked to the Eager Component as one of its
  191. * children */
  192. eager_component->add_child
  193. (eager_component, fifo_component);
  194. fifo_component->add_father
  195. (fifo_component, eager_component);
  196. }
  197. starpu_sched_tree_update_workers(t);
  198. starpu_sched_ctx_set_policy_data
  199. (sched_ctx_id, (void*)t);
  200. }
  201. /* Properly destroy the Scheduling Tree and all its Components */
  202. static void deinitialize_eager_prefetching_center_policy(unsigned sched_ctx_id)
  203. {
  204. struct starpu_sched_tree * tree =
  205. (struct starpu_sched_tree*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  206. starpu_sched_tree_destroy(tree);
  207. starpu_sched_ctx_delete_worker_collection
  208. (sched_ctx_id);
  209. }
  210. /* Initializing the starpu_sched_policy struct associated to the Hierarchical
  211. * Scheduler : only the init_sched and deinit_sched needs to be defined to
  212. * implement a Hierarchical Scheduler */
  213. struct starpu_sched_policy _starpu_sched_tree_eager_prefetching_policy =
  214. {
  215. .init_sched = initialize_eager_prefetching_center_policy,
  216. .deinit_sched = deinitialize_eager_prefetching_center_policy,
  217. .add_workers = starpu_sched_tree_add_workers,
  218. .remove_workers = starpu_sched_tree_remove_workers,
  219. .push_task = starpu_sched_tree_push_task,
  220. .pop_task = starpu_sched_tree_pop_task,
  221. .pre_exec_hook = starpu_sched_component_worker_pre_exec_hook,
  222. .post_exec_hook = starpu_sched_component_worker_post_exec_hook,
  223. .pop_every_task = NULL,
  224. .policy_name = "tree-eager-prefetching",
  225. .policy_description = "eager with prefetching tree policy"
  226. };
  227. \endcode
  228. \section WriteASchedulingComponent Write a Scheduling Component
  229. \subsection GenericSchedulingComponent Generic Scheduling Component
  230. Each Scheduling Component is instantiated from a Generic Scheduling Component,
  231. which implements a generic version of the Interface. The generic implementation
  232. of Pull, Can_Pull and Can_Push functions are recursive calls to their parents
  233. (respectively to their children). However, as a Generic Scheduling Component do
  234. not know how much children it will have when it will be instantiated, it do not
  235. implements the Push function.
  236. \subsection InstanciationRedefineInterface Instanciation : Redefine the Interface
  237. A Scheduling Component must implement all the functions of the Interface. It is
  238. so necessary to implement a Push function to instantiate a Scheduling Component.
  239. The implemented Push function is the "fingerprint" of a Scheduling Component.
  240. Depending on how functionalities or properties the programmer wants to give
  241. to the Scheduling Component he is implementing, it is possible to reimplement
  242. all the functions of the Interface. For example, a Flow-control Component
  243. reimplements the Pull and the Can_Push functions of the Interface, allowing him
  244. to catch the generic recursive calls of these functions. The Pull function of
  245. a Flow-control Component can, for example, pop a task from the local storage
  246. queue of the Component, and give it to the calling Component which asks for it.
  247. \subsection DetailedProgressionAndValidationRules Detailed Progression and Validation Rules
  248. */