350_scheduling_policy_definition.doxy 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013 Inria
  4. * Copyright (C) 2014,2016-2018 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. /*! \page HowToDefineANewSchedulingPolicy How To Define A New Scheduling Policy
  20. \section Introduction Introduction
  21. presentation of both types of schedulers: the basic and the modular.
  22. Explain why it is easier to define a new scheduler by using the modular approach
  23. \section DefiningANewBasicSchedulingPolicy Defining A New Basic Scheduling Policy
  24. A full example showing how to define a new scheduling policy is available in
  25. the StarPU sources in the directory <c>examples/scheduler/</c>.
  26. The scheduler has to provide methods:
  27. \code{.c}
  28. static struct starpu_sched_policy dummy_sched_policy =
  29. {
  30. .init_sched = init_dummy_sched,
  31. .deinit_sched = deinit_dummy_sched,
  32. .add_workers = dummy_sched_add_workers,
  33. .remove_workers = dummy_sched_remove_workers,
  34. .push_task = push_task_dummy,
  35. .pop_task = pop_task_dummy,
  36. .policy_name = "dummy",
  37. .policy_description = "dummy scheduling strategy"
  38. };
  39. \endcode
  40. The idea is that when a task becomes ready for execution, the
  41. starpu_sched_policy::push_task method is called. When a worker is idle, the
  42. starpu_sched_policy::pop_task method is called to get a task. It is up to the
  43. scheduler to implement what is between. A simple eager scheduler is for instance
  44. to make starpu_sched_policy::push_task push the task to a global list, and make
  45. starpu_sched_policy::pop_task pop from this list.
  46. The \ref starpu_sched_policy section provides the exact rules that govern the
  47. methods of the policy.
  48. Make sure to have a look at the \ref API_Scheduling_Policy section, which
  49. provides a list of the available functions for writing advanced schedulers, such
  50. as starpu_task_expected_length(), starpu_task_expected_data_transfer_time_for(),
  51. starpu_task_expected_energy(), etc. Other
  52. useful functions include starpu_transfer_bandwidth(), starpu_transfer_latency(),
  53. starpu_transfer_predict(), ...
  54. Usual functions can also be used on tasks, for instance one can do
  55. \code{.c}
  56. size = 0;
  57. write = 0;
  58. if (task->cl)
  59. for (i = 0; i < STARPU_TASK_GET_NBUFFERS(task); i++)
  60. {
  61. starpu_data_handle_t data = STARPU_TASK_GET_HANDLE(task, i)
  62. size_t datasize = starpu_data_get_size(data);
  63. size += datasize;
  64. if (STARPU_TASK_GET_MODE(task, i) & STARPU_W)
  65. write += datasize;
  66. }
  67. \endcode
  68. And various queues can be used in schedulers. A variety of examples of
  69. schedulers can be read in <c>src/sched_policies</c>, for
  70. instance <c>random_policy.c</c>, <c>eager_central_policy.c</c>,
  71. <c>work_stealing_policy.c</c>
  72. \section DefiningANewModularSchedulingPolicy Defining A New Modular Scheduling Policy
  73. StarPU's Modularized Schedulers are made of individual Scheduling Components
  74. Modularizedly assembled as a Scheduling Tree. Each Scheduling Component has an
  75. unique purpose, such as prioritizing tasks or mapping tasks over resources.
  76. A typical Scheduling Tree is shown below.
  77. <pre>
  78. |
  79. starpu_push_task |
  80. |
  81. v
  82. Fifo_Component
  83. | ^
  84. | |
  85. v |
  86. Eager_Component
  87. | ^
  88. | |
  89. v |
  90. --------><--------------><--------
  91. | ^ | ^
  92. | | | |
  93. v | v |
  94. Fifo_Component Fifo_Component
  95. | ^ | ^
  96. | | | |
  97. v | v |
  98. Worker_Component Worker_Component
  99. </pre>
  100. When a task is pushed by StarPU in a Modularized Scheduler, the task moves from
  101. a Scheduling Component to an other, following the hierarchy of the
  102. Scheduling Tree, and is stored in one of the Scheduling Components of the
  103. strategy.
  104. When a worker wants to pop a task from the Modularized Scheduler, the
  105. corresponding Worker Component of the Scheduling Tree tries to pull a task from
  106. its parents, following the hierarchy, and gives it to the worker if it succeded
  107. to get one.
  108. \subsection ExistingModularizedSchedulers Existing Modularized Schedulers
  109. StarPU is currently shipped with the following pre-defined Modularized
  110. Schedulers :
  111. - Eager-based Schedulers (with/without prefetching) : \n
  112. Naive scheduler, which tries to map a task on the first available resource
  113. it finds.
  114. - Prio-based Schedulers (with/without prefetching) : \n
  115. Similar to Eager-Based Schedulers. Can handle tasks which have a defined
  116. priority and schedule them accordingly.
  117. - Random-based Schedulers (with/without prefetching) : \n
  118. Selects randomly a resource to be mapped on for each task.
  119. - HEFT Scheduler : \n
  120. Heterogeneous Earliest Finish Time Scheduler.
  121. This scheduler needs that every task submitted to StarPU have a
  122. defined performance model (\ref PerformanceModelCalibration)
  123. to work efficiently, but can handle tasks without a performance
  124. model.
  125. To use one of these schedulers, one can set the environment variable \ref STARPU_SCHED.
  126. All modularized schedulers are named following the RE <c>tree-*</c>
  127. \subsection ExampleTreeEagerPrefetchingStrategy An Example : The Tree-Eager-Prefetching Strategy
  128. <pre>
  129. |
  130. starpu_push_task |
  131. |
  132. v
  133. Fifo_Component
  134. | ^
  135. Push | | Can_Push
  136. v |
  137. Eager_Component
  138. | ^
  139. | |
  140. v |
  141. --------><-------------------><---------
  142. | ^ | ^
  143. Push | | Can_Push Push | | Can_Push
  144. v | v |
  145. Fifo_Component Fifo_Component
  146. | ^ | ^
  147. Pull | | Can_Pull Pull | | Can_Pull
  148. v | v |
  149. Worker_Component Worker_Component
  150. </pre>
  151. \subsection Interface
  152. Each Scheduling Component must follow the following pre-defined Interface
  153. to be able to interact with other Scheduling Components.
  154. - Push (Caller_Component, Child_Component, Task) \n
  155. The calling Scheduling Component transfers a task to its
  156. Child Component. When the Push function returns, the task no longer
  157. belongs to the calling Component. The Modularized Schedulers'
  158. model relies on this function to perform prefetching.
  159. See starpu_sched_component::push_task for more details
  160. - Pull (Caller_Component, Parent_Component) -> Task \n
  161. The calling Scheduling Component requests a task from
  162. its Parent Component. When the Pull function ends, the returned
  163. task belongs to the calling Component.
  164. See starpu_sched_component::pull_task for more details
  165. - Can_Push (Caller_Component, Parent_Component) \n
  166. The calling Scheduling Component notifies its Parent Component that
  167. it is ready to accept new tasks.
  168. See starpu_sched_component::can_push for more details
  169. - Can_Pull (Caller_Component, Child_Component) \n
  170. The calling Scheduling Component notifies its Child Component
  171. that it is ready to give new tasks.
  172. See starpu_sched_component::can_pull for more details
  173. \subsection BuildAModularizedScheduler Building a Modularized Scheduler
  174. \subsubsection PreImplementedComponents Pre-implemented Components
  175. StarPU is currently shipped with the following four Scheduling Components :
  176. - Flow-control Components : Fifo, Prio \n
  177. Components which store tasks. They can also prioritize them if
  178. they have a defined priority. It is possible to define a threshold
  179. for those Components following two criterias : the number of tasks
  180. stored in the Component, or the sum of the expected length of all
  181. tasks stored in the Component.
  182. - Resource-Mapping Components : Mct, Heft, Eager, Random, Work-Stealing \n
  183. "Core" of the Scheduling Strategy, those Components are the
  184. ones who make scheduling choices.
  185. - Worker Components : Worker \n
  186. Each Worker Component modelize a concrete worker.
  187. - Special-Purpose Components : Perfmodel_Select, Best_Implementation \n
  188. Components dedicated to original purposes. The Perfmodel_Select
  189. Component decides which Resource-Mapping Component should be used to
  190. schedule a task. The Best_Implementation Component chooses which
  191. implementation of a task should be used on the choosen resource.
  192. \subsubsection ProgressionAndValidationRules Progression And Validation Rules
  193. Some rules must be followed to ensure the correctness of a Modularized
  194. Scheduler :
  195. - At least one Flow-control Component without threshold per Worker Component
  196. is needed in a Modularized Scheduler, to store incoming tasks from StarPU
  197. and to give tasks to Worker Components who asks for it. It is possible to
  198. use one Flow-control Component per Worker Component, or one for all Worker
  199. Components, depending on how the Scheduling Tree is defined.
  200. - At least one Resource-Mapping Component is needed in a Modularized
  201. Scheduler. Resource-Mapping Components are the only ones who can make
  202. scheduling choices, and so the only ones who can have several child.
  203. \subsubsection ImplementAModularizedScheduler Implementing a Modularized Scheduler
  204. The following code shows how the Tree-Eager-Prefetching Scheduler
  205. shown in Section \ref ExampleTreeEagerPrefetchingStrategy is implemented :
  206. \code{.c}
  207. #define _STARPU_SCHED_NTASKS_THRESHOLD_DEFAULT 2
  208. #define _STARPU_SCHED_EXP_LEN_THRESHOLD_DEFAULT 1000000000.0
  209. static void initialize_eager_prefetching_center_policy(unsigned sched_ctx_id)
  210. {
  211. unsigned ntasks_threshold = _STARPU_SCHED_NTASKS_THRESHOLD_DEFAULT;
  212. double exp_len_threshold = _STARPU_SCHED_EXP_LEN_THRESHOLD_DEFAULT;
  213. [...]
  214. starpu_sched_ctx_create_worker_collection
  215. (sched_ctx_id, STARPU_WORKER_LIST);
  216. /* Create the Scheduling Tree */
  217. struct starpu_sched_tree * t = starpu_sched_tree_create(sched_ctx_id);
  218. /* The Root Component is a Flow-control Fifo Component */
  219. t->root = starpu_sched_component_fifo_create(NULL);
  220. /* The Resource-mapping Component of the strategy is an Eager Component
  221. */
  222. struct starpu_sched_component *eager_component = starpu_sched_component_eager_create(NULL);
  223. /* Create links between Components : the Eager Component is the child
  224. * of the Root Component */
  225. t->root->add_child(t->root, eager_component);
  226. eager_component->add_father(eager_component, t->root);
  227. /* A task threshold is set for the Flow-control Components which will
  228. * be connected to Worker Components. By doing so, this Modularized
  229. * Scheduler will be able to perform some prefetching on the resources
  230. */
  231. struct starpu_sched_component_fifo_data fifo_data =
  232. {
  233. .ntasks_threshold = ntasks_threshold,
  234. .exp_len_threshold = exp_len_threshold,
  235. };
  236. unsigned i;
  237. for(i = 0; i < starpu_worker_get_count() + starpu_combined_worker_get_count(); i++)
  238. {
  239. /* Each Worker Component has a Flow-control Fifo Component as
  240. * father */
  241. struct starpu_sched_component * worker_component = starpu_sched_component_worker_new(i);
  242. struct starpu_sched_component * fifo_component = starpu_sched_component_fifo_create(&fifo_data);
  243. fifo_component->add_child(fifo_component, worker_component);
  244. worker_component->add_father(worker_component, fifo_component);
  245. /* Each Flow-control Fifo Component associated to a Worker
  246. * Component is linked to the Eager Component as one of its
  247. * children */
  248. eager_component->add_child(eager_component, fifo_component);
  249. fifo_component->add_father(fifo_component, eager_component);
  250. }
  251. starpu_sched_tree_update_workers(t);
  252. starpu_sched_ctx_set_policy_data(sched_ctx_id, (void*)t);
  253. }
  254. /* Properly destroy the Scheduling Tree and all its Components */
  255. static void deinitialize_eager_prefetching_center_policy(unsigned sched_ctx_id)
  256. {
  257. struct starpu_sched_tree * tree = (struct starpu_sched_tree*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  258. starpu_sched_tree_destroy(tree);
  259. starpu_sched_ctx_delete_worker_collection(sched_ctx_id);
  260. }
  261. /* Initializing the starpu_sched_policy struct associated to the Modularized
  262. * Scheduler : only the init_sched and deinit_sched needs to be defined to
  263. * implement a Modularized Scheduler */
  264. struct starpu_sched_policy _starpu_sched_tree_eager_prefetching_policy =
  265. {
  266. .init_sched = initialize_eager_prefetching_center_policy,
  267. .deinit_sched = deinitialize_eager_prefetching_center_policy,
  268. .add_workers = starpu_sched_tree_add_workers,
  269. .remove_workers = starpu_sched_tree_remove_workers,
  270. .push_task = starpu_sched_tree_push_task,
  271. .pop_task = starpu_sched_tree_pop_task,
  272. .pre_exec_hook = starpu_sched_component_worker_pre_exec_hook,
  273. .post_exec_hook = starpu_sched_component_worker_post_exec_hook,
  274. .pop_every_task = NULL,
  275. .policy_name = "tree-eager-prefetching",
  276. .policy_description = "eager with prefetching tree policy"
  277. };
  278. \endcode
  279. \subsection WriteASchedulingComponent Writing a Scheduling Component
  280. \subsubsection GenericSchedulingComponent Generic Scheduling Component
  281. Each Scheduling Component is instantiated from a Generic Scheduling Component,
  282. which implements a generic version of the Interface. The generic implementation
  283. of Pull, Can_Pull and Can_Push functions are recursive calls to their parents
  284. (respectively to their children). However, as a Generic Scheduling Component do
  285. not know how much children it will have when it will be instantiated, it does
  286. not implement the Push function.
  287. \subsubsection InstantiationRedefineInterface Instantiation : Redefining the Interface
  288. A Scheduling Component must implement all the functions of the Interface. It is
  289. so necessary to implement a Push function to instantiate a Scheduling Component.
  290. The implemented Push function is the "fingerprint" of a Scheduling Component.
  291. Depending on how functionalities or properties the programmer wants to give
  292. to the Scheduling Component he is implementing, it is possible to reimplement
  293. all the functions of the Interface. For example, a Flow-control Component
  294. reimplements the Pull and the Can_Push functions of the Interface, allowing him
  295. to catch the generic recursive calls of these functions. The Pull function of
  296. a Flow-control Component can, for example, pop a task from the local storage
  297. queue of the Component, and give it to the calling Component which asks for it.
  298. \subsubsection DetailedProgressionAndValidationRules Detailed Progression and Validation Rules
  299. - A Reservoir is a Scheduling Component which redefines a Push and a Pull
  300. function, in order to store tasks into it. A Reservoir delimit Scheduling
  301. Areas in the Scheduling Tree.
  302. - A Pump is the engine source of the Scheduler : it pushes/pulls tasks
  303. to/from a Scheduling Component to an other. Native Pumps of a Scheduling
  304. Tree are located at the root of the Tree (incoming Push calls from StarPU),
  305. and at the leafs of the Tree (Pop calls coming from StarPU Workers).
  306. Pre-implemented Scheduling Components currently shipped with Pumps are
  307. Flow-Control Components and the Resource-Mapping Component Heft, within
  308. their defined Can_Push functions.
  309. - A correct Scheduling Tree requires a Pump per Scheduling Area and per
  310. Execution Flow.
  311. The Tree-Eager-Prefetching Scheduler shown in Section
  312. \ref ExampleTreeEagerPrefetchingStrategy follows the previous assumptions :
  313. <pre>
  314. starpu_push_task
  315. <b>Pump</b>
  316. |
  317. Area 1 |
  318. |
  319. v
  320. -----------------------Fifo_Component-----------------------------
  321. <b>Pump</b>
  322. | ^
  323. Push | | Can_Push
  324. v |
  325. Area 2 Eager_Component
  326. | ^
  327. | |
  328. v |
  329. --------><-------------------><---------
  330. | ^ | ^
  331. Push | | Can_Push Push | | Can_Push
  332. v | v |
  333. -----Fifo_Component-----------------------Fifo_Component----------
  334. | ^ | ^
  335. Pull | | Can_Pull Pull | | Can_Pull
  336. Area 3 v | v |
  337. <b>Pump</b> <b>Pump</b>
  338. Worker_Component Worker_Component
  339. </pre>
  340. */