350_modularized_scheduler.doxy 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 ModularizedScheduler Modularized Schedulers
  8. \section Introduction
  9. StarPU's Modularized Schedulers are made of individual Scheduling Components
  10. Modularizedly 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 Modularized 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 Modularized 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 UsingModularizedSchedulers Using Modularized Schedulers
  45. \subsection ExistingModularizedSchedulers Existing Modularized Schedulers
  46. StarPU is currently shipped with the following pre-defined Modularized
  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. To use one of these schedulers, one can set the environment variable \ref STARPU_SCHED.
  63. All modularized schedulers are named following the RE <c>tree-*</c>
  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 Modularized Schedulers'
  95. model relies on this function to perform prefetching.
  96. See starpu_sched_component::push_task for more details
  97. - Pull (Caller_Component, Parent_Component) -> Task \n
  98. The calling Scheduling Component requests a task from
  99. its Parent Component. When the Pull function ends, the returned
  100. task belongs to the calling Component.
  101. See starpu_sched_component::pull_task for more details
  102. - Can_Push (Caller_Component, Parent_Component) \n
  103. The calling Scheduling Component notifies its Parent Component that
  104. it is ready to accept new tasks.
  105. See starpu_sched_component::can_push for more details
  106. - Can_Pull (Caller_Component, Child_Component) \n
  107. The calling Scheduling Component notifies its Child Component
  108. that it is ready to give new tasks.
  109. See starpu_sched_component::can_pull for more details
  110. \section BuildAModularizedScheduler Building a Modularized Scheduler
  111. \subsection PreImplementedComponents Pre-implemented Components
  112. StarPU is currently shipped with the following four Scheduling Components :
  113. - Flow-control Components : Fifo, Prio \n
  114. Components which store tasks. They can also prioritize them if
  115. they have a defined priority. It is possible to define a threshold
  116. for those Components following two criterias : the number of tasks
  117. stored in the Component, or the sum of the expected length of all
  118. tasks stored in the Component.
  119. - Resource-Mapping Components : Mct, Heft, Eager, Random, Work-Stealing \n
  120. "Core" of the Scheduling Strategy, those Components are the
  121. ones who make scheduling choices.
  122. - Worker Components : Worker \n
  123. Each Worker Component modelize a concrete worker.
  124. - Special-Purpose Components : Perfmodel_Select, Best_Implementation \n
  125. Components dedicated to original purposes. The Perfmodel_Select
  126. Component decides which Resource-Mapping Component should be used to
  127. schedule a task. The Best_Implementation Component chooses which
  128. implementation of a task should be used on the choosen resource.
  129. \subsection ProgressionAndValidationRules Progression And Validation Rules
  130. Some rules must be followed to ensure the correctness of a Modularized
  131. Scheduler :
  132. - At least one Flow-control Component without threshold per Worker Component
  133. is needed in a Modularized Scheduler, to store incoming tasks from StarPU
  134. and to give tasks to Worker Components who asks for it. It is possible to
  135. use one Flow-control Component per Worker Component, or one for all Worker
  136. Components, depending on how the Scheduling Tree is defined.
  137. - At least one Resource-Mapping Component is needed in a Modularized
  138. Scheduler. Resource-Mapping Components are the only ones who can make
  139. scheduling choices, and so the only ones who can have several child.
  140. \subsection ImplementAModularizedScheduler Implementing a Modularized Scheduler
  141. The following code shows how the Tree-Eager-Prefetching Scheduler
  142. shown in Section \ref ExampleTreeEagerPrefetchingStrategy is implemented :
  143. \code{.c}
  144. #define _STARPU_SCHED_NTASKS_THRESHOLD_DEFAULT 2
  145. #define _STARPU_SCHED_EXP_LEN_THRESHOLD_DEFAULT 1000000000.0
  146. static void initialize_eager_prefetching_center_policy(unsigned sched_ctx_id)
  147. {
  148. unsigned ntasks_threshold = _STARPU_SCHED_NTASKS_THRESHOLD_DEFAULT;
  149. double exp_len_threshold = _STARPU_SCHED_EXP_LEN_THRESHOLD_DEFAULT;
  150. [...]
  151. starpu_sched_ctx_create_worker_collection
  152. (sched_ctx_id, STARPU_WORKER_LIST);
  153. /* Create the Scheduling Tree */
  154. struct starpu_sched_tree * t =
  155. starpu_sched_tree_create(sched_ctx_id);
  156. /* The Root Component is a Flow-control Fifo Component */
  157. t->root = starpu_sched_component_fifo_create(NULL);
  158. /* The Resource-mapping Component of the strategy is an Eager Component
  159. */
  160. struct starpu_sched_component * eager_component =
  161. starpu_sched_component_eager_create(NULL);
  162. /* Create links between Components : the Eager Component is the child
  163. * of the Root Component */
  164. t->root->add_child
  165. (t->root, eager_component);
  166. eager_component->add_father
  167. (eager_component, t->root);
  168. /* A task threshold is set for the Flow-control Components which will
  169. * be connected to Worker Components. By doing so, this Modularized
  170. * Scheduler will be able to perform some prefetching on the resources
  171. */
  172. struct starpu_sched_component_fifo_data fifo_data =
  173. {
  174. .ntasks_threshold = ntasks_threshold,
  175. .exp_len_threshold = exp_len_threshold,
  176. };
  177. unsigned i;
  178. for(i = 0;
  179. i < starpu_worker_get_count() +
  180. starpu_combined_worker_get_count();
  181. i++)
  182. {
  183. /* Each Worker Component has a Flow-control Fifo Component as
  184. * father */
  185. struct starpu_sched_component * worker_component =
  186. starpu_sched_component_worker_new(i);
  187. struct starpu_sched_component * fifo_component =
  188. starpu_sched_component_fifo_create(&fifo_data);
  189. fifo_component->add_child
  190. (fifo_component, worker_component);
  191. worker_component->add_father
  192. (worker_component, fifo_component);
  193. /* Each Flow-control Fifo Component associated to a Worker
  194. * Component is linked to the Eager Component as one of its
  195. * children */
  196. eager_component->add_child
  197. (eager_component, fifo_component);
  198. fifo_component->add_father
  199. (fifo_component, eager_component);
  200. }
  201. starpu_sched_tree_update_workers(t);
  202. starpu_sched_ctx_set_policy_data
  203. (sched_ctx_id, (void*)t);
  204. }
  205. /* Properly destroy the Scheduling Tree and all its Components */
  206. static void deinitialize_eager_prefetching_center_policy(unsigned sched_ctx_id)
  207. {
  208. struct starpu_sched_tree * tree =
  209. (struct starpu_sched_tree*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  210. starpu_sched_tree_destroy(tree);
  211. starpu_sched_ctx_delete_worker_collection
  212. (sched_ctx_id);
  213. }
  214. /* Initializing the starpu_sched_policy struct associated to the Modularized
  215. * Scheduler : only the init_sched and deinit_sched needs to be defined to
  216. * implement a Modularized Scheduler */
  217. struct starpu_sched_policy _starpu_sched_tree_eager_prefetching_policy =
  218. {
  219. .init_sched = initialize_eager_prefetching_center_policy,
  220. .deinit_sched = deinitialize_eager_prefetching_center_policy,
  221. .add_workers = starpu_sched_tree_add_workers,
  222. .remove_workers = starpu_sched_tree_remove_workers,
  223. .push_task = starpu_sched_tree_push_task,
  224. .pop_task = starpu_sched_tree_pop_task,
  225. .pre_exec_hook = starpu_sched_component_worker_pre_exec_hook,
  226. .post_exec_hook = starpu_sched_component_worker_post_exec_hook,
  227. .pop_every_task = NULL,
  228. .policy_name = "tree-eager-prefetching",
  229. .policy_description = "eager with prefetching tree policy"
  230. };
  231. \endcode
  232. \section WriteASchedulingComponent Writing a Scheduling Component
  233. \subsection GenericSchedulingComponent Generic Scheduling Component
  234. Each Scheduling Component is instantiated from a Generic Scheduling Component,
  235. which implements a generic version of the Interface. The generic implementation
  236. of Pull, Can_Pull and Can_Push functions are recursive calls to their parents
  237. (respectively to their children). However, as a Generic Scheduling Component do
  238. not know how much children it will have when it will be instantiated, it does
  239. not implement the Push function.
  240. \subsection InstantiationRedefineInterface Instantiation : Redefining the Interface
  241. A Scheduling Component must implement all the functions of the Interface. It is
  242. so necessary to implement a Push function to instantiate a Scheduling Component.
  243. The implemented Push function is the "fingerprint" of a Scheduling Component.
  244. Depending on how functionalities or properties the programmer wants to give
  245. to the Scheduling Component he is implementing, it is possible to reimplement
  246. all the functions of the Interface. For example, a Flow-control Component
  247. reimplements the Pull and the Can_Push functions of the Interface, allowing him
  248. to catch the generic recursive calls of these functions. The Pull function of
  249. a Flow-control Component can, for example, pop a task from the local storage
  250. queue of the Component, and give it to the calling Component which asks for it.
  251. \subsection DetailedProgressionAndValidationRules Detailed Progression and Validation Rules
  252. - A Reservoir is a Scheduling Component which redefines a Push and a Pull
  253. function, in order to store tasks into it. A Reservoir delimit Scheduling
  254. Areas in the Scheduling Tree.
  255. - A Pump is the engine source of the Scheduler : it pushes/pulls tasks
  256. to/from a Scheduling Component to an other. Native Pumps of a Scheduling
  257. Tree are located at the root of the Tree (incoming Push calls from StarPU),
  258. and at the leafs of the Tree (Pop calls coming from StarPU Workers).
  259. Pre-implemented Scheduling Components currently shipped with Pumps are
  260. Flow-Control Components and the Resource-Mapping Component Heft, within
  261. their defined Can_Push functions.
  262. - A correct Scheduling Tree requires a Pump per Scheduling Area and per
  263. Execution Flow.
  264. The Tree-Eager-Prefetching Scheduler shown in Section
  265. \ref ExampleTreeEagerPrefetchingStrategy follows the previous assumptions :
  266. <pre>
  267. starpu_push_task
  268. <b>Pump</b>
  269. |
  270. Area 1 |
  271. |
  272. v
  273. -----------------------Fifo_Component-----------------------------
  274. <b>Pump</b>
  275. | ^
  276. Push | | Can_Push
  277. v |
  278. Area 2 Eager_Component
  279. | ^
  280. | |
  281. v |
  282. --------><-------------------><---------
  283. | ^ | ^
  284. Push | | Can_Push Push | | Can_Push
  285. v | v |
  286. -----Fifo_Component-----------------------Fifo_Component----------
  287. | ^ | ^
  288. Pull | | Can_Pull Pull | | Can_Pull
  289. Area 3 v | v |
  290. <b>Pump</b> <b>Pump</b>
  291. Worker_Component Worker_Component
  292. </pre>
  293. */