320_scheduling.doxy 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. /*! \page Scheduling Scheduling
  17. \section TaskSchedulingPolicy Task Scheduling Policies
  18. The basics of the scheduling policy are the following:
  19. <ul>
  20. <li>The scheduler gets to schedule tasks (<c>push</c> operation) when they become
  21. ready to be executed, i.e. they are not waiting for some tags, data dependencies
  22. or task dependencies.</li>
  23. <li>Workers pull tasks (<c>pop</c> operation) one by one from the scheduler.
  24. </ul>
  25. This means scheduling policies usually contain at least one queue of tasks to
  26. store them between the time when they become available, and the time when a
  27. worker gets to grab them.
  28. By default, StarPU uses the work-stealing scheduler <c>lws</c>. This is
  29. because it provides correct load balance and locality even if the application codelets do
  30. not have performance models. Other non-modelling scheduling policies can be
  31. selected among the list below, thanks to the environment variable \ref
  32. STARPU_SCHED. For instance <c>export STARPU_SCHED=dmda</c> . Use <c>help</c> to
  33. get the list of available schedulers.
  34. <b>Non Performance Modelling Policies:</b>
  35. The <b>eager</b> scheduler uses a central task queue, from which all workers draw tasks
  36. to work on concurrently. This however does not permit to prefetch data since the scheduling
  37. decision is taken late. If a task has a non-0 priority, it is put at the front of the queue.
  38. The <b>random</b> scheduler uses a queue per worker, and distributes tasks randomly according to assumed worker
  39. overall performance.
  40. The <b>ws</b> (work stealing) scheduler uses a queue per worker, and schedules
  41. a task on the worker which released it by
  42. default. When a worker becomes idle, it steals a task from the most loaded
  43. worker.
  44. The <b>lws</b> (locality work stealing) scheduler uses a queue per worker, and schedules
  45. a task on the worker which released it by
  46. default. When a worker becomes idle, it steals a task from neighbour workers. It
  47. also takes into account priorities.
  48. The <b>prio</b> scheduler also uses a central task queue, but sorts tasks by
  49. priority specified by the programmer (between -5 and 5).
  50. The <b>heteroprio</b> scheduler uses different priorities for the different processing units.
  51. This scheduler must be configured to work correclty and to expect high-performance
  52. as described in the corresponding section.
  53. \section DMTaskSchedulingPolicy Performance Model-Based Task Scheduling Policies
  54. If (<b>and only if</b>) your application <b>codelets have performance models</b> (\ref
  55. PerformanceModelExample), you should change the scheduler thanks to the
  56. environment variable \ref STARPU_SCHED, to select one of the policies below, in
  57. order to take advantage of StarPU's performance modelling. For instance
  58. <c>export STARPU_SCHED=dmda</c> . Use <c>help</c> to get the list of available
  59. schedulers.
  60. <b>Note:</B> Depending on the performance model type chosen, some preliminary
  61. calibration runs may be needed for the model to converge. If the calibration
  62. has not been done, or is insufficient yet, or if no performance model is
  63. specified for a codelet, every task built from this codelet will be scheduled
  64. using an <b>eager</b> fallback policy.
  65. <b>Troubleshooting:</b> Configuring and recompiling StarPU using the
  66. \ref enable-verbose "--enable-verbose" \c configure option displays some statistics at the end of
  67. execution about the percentage of tasks which have been scheduled by a DM*
  68. family policy using performance model hints. A low or zero percentage may be
  69. the sign that performance models are not converging or that codelets do not
  70. have performance models enabled.
  71. <b>Performance Modelling Policies:</b>
  72. The <b>dm</b> (deque model) scheduler takes task execution performance models into account to
  73. perform a HEFT-similar scheduling strategy: it schedules tasks where their
  74. termination time will be minimal. The difference with HEFT is that <b>dm</b>
  75. schedules tasks as soon as they become available, and thus in the order they
  76. become available, without taking priorities into account.
  77. The <b>dmda</b> (deque model data aware) scheduler is similar to dm, but it also takes
  78. into account data transfer time.
  79. The <b>dmdap</b> (deque model data aware prio) scheduler is similar to dmda,
  80. except that it sorts tasks by priority order, which allows to become even closer
  81. to HEFT by respecting priorities after having made the scheduling decision (but
  82. it still schedules tasks in the order they become available).
  83. The <b>dmdar</b> (deque model data aware ready) scheduler is similar to dmda,
  84. but it also privileges tasks whose data buffers are already available
  85. on the target device.
  86. The <b>dmdas</b> combines dmdap and dmdas: it sorts tasks by priority order,
  87. but for a given priority it will privilege tasks whose data buffers are already
  88. available on the target device.
  89. The <b>dmdasd</b> (deque model data aware sorted decision) scheduler is similar
  90. to dmdas, except that when scheduling a task, it takes into account its priority
  91. when computing the minimum completion time, since this task may get executed
  92. before others, and thus the latter should be ignored.
  93. The <b>heft</b> (heterogeneous earliest finish time) scheduler is a deprecated
  94. alias for <b>dmda</b>.
  95. The <b>pheft</b> (parallel HEFT) scheduler is similar to dmda, it also supports
  96. parallel tasks (still experimental). Should not be used when several contexts using
  97. it are being executed simultaneously.
  98. The <b>peager</b> (parallel eager) scheduler is similar to eager, it also
  99. supports parallel tasks (still experimental). Should not be used when several
  100. contexts using it are being executed simultaneously.
  101. TODO: describe modular schedulers
  102. \section TaskDistributionVsDataTransfer Task Distribution Vs Data Transfer
  103. Distributing tasks to balance the load induces data transfer penalty. StarPU
  104. thus needs to find a balance between both. The target function that the
  105. scheduler <c>dmda</c> of StarPU
  106. tries to minimize is <c>alpha * T_execution + beta * T_data_transfer</c>, where
  107. <c>T_execution</c> is the estimated execution time of the codelet (usually
  108. accurate), and <c>T_data_transfer</c> is the estimated data transfer time. The
  109. latter is estimated based on bus calibration before execution start,
  110. i.e. with an idle machine, thus without contention. You can force bus
  111. re-calibration by running the tool <c>starpu_calibrate_bus</c>. The
  112. beta parameter defaults to <c>1</c>, but it can be worth trying to tweak it
  113. by using <c>export STARPU_SCHED_BETA=2</c> (\ref STARPU_SCHED_BETA) for instance, since during
  114. real application execution, contention makes transfer times bigger.
  115. This is of course imprecise, but in practice, a rough estimation
  116. already gives the good results that a precise estimation would give.
  117. \section Energy-basedScheduling Energy-based Scheduling
  118. If the application can provide some energy consumption performance model (through
  119. the field starpu_codelet::energy_model), StarPU will
  120. take it into account when distributing tasks. The target function that
  121. the scheduler <c>dmda</c> minimizes becomes <c>alpha * T_execution +
  122. beta * T_data_transfer + gamma * Consumption</c> , where <c>Consumption</c>
  123. is the estimated task consumption in Joules. To tune this parameter, use
  124. <c>export STARPU_SCHED_GAMMA=3000</c> (\ref STARPU_SCHED_GAMMA) for instance, to express that each Joule
  125. (i.e kW during 1000us) is worth 3000us execution time penalty. Setting
  126. <c>alpha</c> and <c>beta</c> to zero permits to only take into account energy consumption.
  127. This is however not sufficient to correctly optimize energy: the scheduler would
  128. simply tend to run all computations on the most energy-conservative processing
  129. unit. To account for the consumption of the whole machine (including idle
  130. processing units), the idle power of the machine should be given by setting
  131. <c>export STARPU_IDLE_POWER=200</c> (\ref STARPU_IDLE_POWER) for 200W, for instance. This value can often
  132. be obtained from the machine power supplier.
  133. The energy actually consumed by the total execution can be displayed by setting
  134. <c>export STARPU_PROFILING=1 STARPU_WORKER_STATS=1</c> (\ref STARPU_PROFILING and \ref STARPU_WORKER_STATS).
  135. For OpenCL devices, on-line task consumption measurement is currently supported through the
  136. <c>CL_PROFILING_POWER_CONSUMED</c> OpenCL extension, implemented in the MoviSim
  137. simulator.
  138. For CUDA devices, on-line task consumption measurement is supported on V100
  139. cards and beyond. This however only works for quite long tasks, since the
  140. measurement granularity is about 10ms.
  141. Applications can however provide explicit measurements by using the function
  142. starpu_perfmodel_update_history() (examplified in \ref PerformanceModelExample
  143. with the <c>energy_model</c> performance model). Fine-grain measurement
  144. is often not feasible with the feedback provided by the hardware, so the
  145. user can for instance run a given task a thousand times, measure the global
  146. consumption for that series of tasks, divide it by a thousand, repeat for
  147. varying kinds of tasks and task sizes, and eventually feed StarPU with these
  148. manual measurements through starpu_perfmodel_update_history(). For instance,
  149. for CUDA devices, <c>nvidia-smi -q -d POWER</c> can be used to get the current
  150. consumption in Watt. Multiplying this value by the average duration of a
  151. single task gives the consumption of the task in Joules, which can be given to
  152. starpu_perfmodel_update_history().
  153. Another way to provide the energy performance is to define a
  154. perfmodel with starpu_perfmodel::type ::STARPU_PER_ARCH or
  155. ::STARPU_PER_WORKER , and set the starpu_perfmodel::arch_cost_function or
  156. starpu_perfmodel::worker_cost_function field to a function which shall return
  157. the estimated consumption of the task in Joules. Such a function can for instance
  158. use starpu_task_expected_length() on the task (in µs), multiplied by the
  159. typical power consumption of the device, e.g. in W, and divided by 1000000. to
  160. get Joules.
  161. \section ExistingModularizedSchedulers Modularized Schedulers
  162. StarPU provides a powerful way to implement schedulers, as documented in \ref
  163. DefiningANewModularSchedulingPolicy . It is currently shipped with the following
  164. pre-defined Modularized Schedulers :
  165. - Eager-based Schedulers (with/without prefetching : \c modular-eager ,
  166. \c modular-eager-prefetching) : \n
  167. Naive scheduler, which tries to map a task on the first available resource
  168. it finds. The prefecthing variant queues several tasks in advance to be able to
  169. do data prefetching. This may however degrade load balancing a bit.
  170. - Prio-based Schedulers (with/without prefetching :
  171. \c modular-prio, \c modular-prio-prefetching , \c modular-eager-prio) : \n
  172. Similar to Eager-Based Schedulers. Can handle tasks which have a defined
  173. priority and schedule them accordingly.
  174. The \c modular-eager-prio variant integrates the eager and priority queue in a
  175. single component. This allows it to do a better job at pushing tasks.
  176. - Random-based Schedulers (with/without prefetching: \c modular-random,
  177. \c modular-random-prio, \c modular-random-prefetching, \c
  178. modular-random-prio-prefetching) : \n
  179. Selects randomly a resource to be mapped on for each task.
  180. - Work Stealing (\c modular-ws) : \n
  181. Maps tasks to workers in round robin, but allows workers to steal work from other workers.
  182. - HEFT Scheduler : \n
  183. Maps tasks to workers using a heuristic very close to
  184. Heterogeneous Earliest Finish Time.
  185. It needs that every task submitted to StarPU have a
  186. defined performance model (\ref PerformanceModelCalibration)
  187. to work efficiently, but can handle tasks without a performance
  188. model. \c modular-heft just takes tasks by priority order. \c modular-heft takes
  189. at most 5 tasks of the same priority and checks which one fits best. \c
  190. modular-heft-prio is similar to \c modular-heft, but only decides the memory
  191. node, not the exact worker, just pushing tasks to one central queue per memory
  192. node.
  193. - Heteroprio Scheduler: \n
  194. Maps tasks to worker similarly to HEFT, but first attribute accelerated tasks to
  195. GPUs, then not-so-accelerated tasks to CPUs.
  196. To use one of these schedulers, one can set the environment variable \ref STARPU_SCHED.
  197. \section StaticScheduling Static Scheduling
  198. In some cases, one may want to force some scheduling, for instance force a given
  199. set of tasks to GPU0, another set to GPU1, etc. while letting some other tasks
  200. be scheduled on any other device. This can indeed be useful to guide StarPU into
  201. some work distribution, while still letting some degree of dynamism. For
  202. instance, to force execution of a task on CUDA0:
  203. \code{.c}
  204. task->execute_on_a_specific_worker = 1;
  205. task->workerid = starpu_worker_get_by_type(STARPU_CUDA_WORKER, 0);
  206. \endcode
  207. or equivalently
  208. \code{.c}
  209. starpu_task_insert(&cl, ..., STARPU_EXECUTE_ON_WORKER, starpu_worker_get_by_type(STARPU_CUDA_WORKER, 0), ...);
  210. \endcode
  211. One can also specify a set worker(s) which are allowed to take the task, as an
  212. array of bit, for instance to allow workers 2 and 42:
  213. \code{.c}
  214. task->workerids = calloc(2,sizeof(uint32_t));
  215. task->workerids[2/32] |= (1 << (2%32));
  216. task->workerids[42/32] |= (1 << (42%32));
  217. task->workerids_len = 2;
  218. \endcode
  219. One can also specify the order in which tasks must be executed by setting the
  220. starpu_task::workerorder field. If this field is set to a non-zero value, it
  221. provides the per-worker consecutive order in which tasks will be executed,
  222. starting from 1. For a given of such task, the worker will thus not execute
  223. it before all the tasks with smaller order value have been executed, notably
  224. in case those tasks are not available yet due to some dependencies. This
  225. eventually gives total control of task scheduling, and StarPU will only serve as
  226. a "self-timed" task runtime. Of course, the provided order has to be runnable,
  227. i.e. a task should should not depend on another task bound to the same worker
  228. with a bigger order.
  229. Note however that using scheduling contexts while statically scheduling tasks on workers
  230. could be tricky. Be careful to schedule the tasks exactly on the workers of the corresponding
  231. contexts, otherwise the workers' corresponding scheduling structures may not be allocated or
  232. the execution of the application may deadlock. Moreover, the hypervisor should not be used when
  233. statically scheduling tasks.
  234. \section Configuring Heteroprio
  235. Within Heteroprio, one priority per processing unit type is assigned to each task, such that a task has several
  236. priorities. Each worker pops the task that has the highest priority for the hardware type it uses, which
  237. could be CPU or CUDA for example. Therefore, the priorities has to be used to manage the critical path,
  238. but also to promote the consumption of tasks by the more appropriate workers.
  239. The tasks are stored inside buckets, where each bucket corresponds to a priority set. Then each
  240. worker uses an indirect access array to know the order in which it should access the buckets. Moreover,
  241. all the tasks inside a bucket must be compatible with all the processing units that may access it (at least).
  242. As an example, see the following code where we have 5 types of tasks.
  243. CPU workers can compute all of them, but CUDA workers can only execute
  244. tasks of types 0 and 1, and is expected to go 20 and 30 time
  245. faster than the CPU, respectively.
  246. \code{.c}
  247. // In the file that init StarPU
  248. #include <starpu_heteroprio.h>
  249. ////////////////////////////////////////////////////
  250. // Before calling starpu_init
  251. struct starpu_conf conf;
  252. starpu_conf_init(&conf);
  253. // Inform StarPU to use Heteroprio
  254. conf.sched_policy_name = "heteroprio";
  255. // Inform StarPU about the function that will init the priorities in Heteroprio
  256. // where init_heteroprio is a function to implement
  257. conf.sched_policy_init = &init_heteroprio;
  258. // Do other things with conf if needed, then init StarPU
  259. starpu_init(&conf);
  260. ////////////////////////////////////////////////////
  261. void init_heteroprio(unsigned sched_ctx) {
  262. // CPU uses 5 buckets and visits them in the natural order
  263. starpu_heteroprio_set_nb_prios(ctx, STARPU_CPU_IDX, 5);
  264. // It uses direct mapping idx => idx
  265. for(unsigned idx = 0; idx < 5; ++idx){
  266. starpu_heteroprio_set_mapping(ctx, STARPU_CPU_IDX, idx, idx);
  267. // If there is no CUDA worker we must tell that CPU is faster
  268. starpu_heteroprio_set_faster_arch(ctx, STARPU_CPU_IDX, idx);
  269. }
  270. if(starpu_cuda_worker_get_count()){
  271. // CUDA is enabled and uses 2 buckets
  272. starpu_heteroprio_set_nb_prios(ctx, STARPU_CUDA_IDX, 2);
  273. // CUDA will first look at bucket 1
  274. starpu_heteroprio_set_mapping(ctx, STARPU_CUDA_IDX, 0, 1);
  275. // CUDA will then look at bucket 2
  276. starpu_heteroprio_set_mapping(ctx, STARPU_CUDA_IDX, 1, 2);
  277. // For bucket 1 CUDA is the fastest
  278. starpu_heteroprio_set_faster_arch(ctx, STARPU_CUDA_IDX, 1);
  279. // And CPU is 30 times slower
  280. starpu_heteroprio_set_arch_slow_factor(ctx, STARPU_CPU_IDX, 1, 30.0f);
  281. // For bucket 0 CUDA is the fastest
  282. starpu_heteroprio_set_faster_arch(ctx, STARPU_CUDA_IDX, 0);
  283. // And CPU is 20 times slower
  284. starpu_heteroprio_set_arch_slow_factor(ctx, STARPU_CPU_IDX, 0, 20.0f);
  285. }
  286. }
  287. \endcode
  288. Then, when a task is inserted <b>the priority of the task will be used to
  289. select in which bucket is has to be stored</b>.
  290. So, in the given example, the priority of a task will be between 0 and 4 included.
  291. However, tasks of priorities 0-1 must provide CPU and CUDA kernels, and
  292. tasks of priorities 2-4 must provide CPU kernels (at least).
  293. */