320_scheduling.doxy 18 KB

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