320_scheduling.doxy 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. \subsection NonPerformanceModelingPolicies Non Performance Modelling Policies
  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. \subsection 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. - The <b>dm</b> (deque model) scheduler takes task execution performance models into account to
  72. perform a HEFT-similar scheduling strategy: it schedules tasks where their
  73. termination time will be minimal. The difference with HEFT is that <b>dm</b>
  74. schedules tasks as soon as they become available, and thus in the order they
  75. become available, without taking priorities into account.
  76. - The <b>dmda</b> (deque model data aware) scheduler is similar to dm, but it also takes
  77. into account data transfer time.
  78. - The <b>dmdap</b> (deque model data aware prio) scheduler is similar to dmda,
  79. except that it sorts tasks by priority order, which allows to become even closer
  80. to HEFT by respecting priorities after having made the scheduling decision (but
  81. it still schedules tasks in the order they become available).
  82. - The <b>dmdar</b> (deque model data aware ready) scheduler is similar to dmda,
  83. but it also privileges tasks whose data buffers are already available
  84. on the target device.
  85. - The <b>dmdas</b> combines dmdap and dmdas: it sorts tasks by priority order,
  86. but for a given priority it will privilege tasks whose data buffers are already
  87. available on the target device.
  88. - The <b>dmdasd</b> (deque model data aware sorted decision) scheduler is similar
  89. to dmdas, except that when scheduling a task, it takes into account its priority
  90. when computing the minimum completion time, since this task may get executed
  91. before others, and thus the latter should be ignored.
  92. - The <b>heft</b> (heterogeneous earliest finish time) scheduler is a deprecated
  93. alias for <b>dmda</b>.
  94. - The <b>pheft</b> (parallel HEFT) scheduler is similar to dmda, it also supports
  95. parallel tasks (still experimental). Should not be used when several contexts using
  96. it are being executed simultaneously.
  97. - The <b>peager</b> (parallel eager) scheduler is similar to eager, it also
  98. supports parallel tasks (still experimental). Should not be used when several
  99. contexts using it are being executed simultaneously.
  100. \subsection ExistingModularizedSchedulers Modularized Schedulers
  101. StarPU provides a powerful way to implement schedulers, as documented in \ref
  102. DefiningANewModularSchedulingPolicy . It is currently shipped with the following
  103. pre-defined Modularized Schedulers :
  104. - <b>modular-eager</b> , <b>modular-eager-prefetching</b> are eager-based Schedulers (without and with prefetching)), they are \n
  105. naive schedulers, which try to map a task on the first available resource
  106. they find. The prefetching variant queues several tasks in advance to be able to
  107. do data prefetching. This may however degrade load balancing a bit.
  108. - <b>modular-prio</b>, <b>modular-prio-prefetching</b>, <b>modular-eager-prio</b> are prio-based Schedulers (without / with prefetching):,
  109. similar to Eager-Based Schedulers. Can handle tasks which have a defined
  110. priority and schedule them accordingly.
  111. The <b>modular-eager-prio</b> variant integrates the eager and priority queue in a
  112. single component. This allows it to do a better job at pushing tasks.
  113. - <b>modular-random</b>, <b>modular-random-prio</b>, <b>modular-random-prefetching</b>, <b>modular-random-prio-prefetching</b> are random-based Schedulers (without/with prefetching) : \n
  114. Select randomly a resource to be mapped on for each task.
  115. - <b>modular-ws</b>) implements Work Stealing:
  116. Maps tasks to workers in round robin, but allows workers to steal work from other workers.
  117. - <b>modular-heft</b>, <b>modular-heft2</b>, and <b>modular-heft-prio</b> are
  118. HEFT Schedulers : \n
  119. Maps tasks to workers using a heuristic very close to
  120. Heterogeneous Earliest Finish Time.
  121. It 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. <b>modular-heft</b> just takes tasks by priority order. <b>modular-heft2</b> takes
  125. at most 5 tasks of the same priority and checks which one fits best.
  126. <b>modular-heft-prio</b> is similar to <b>modular-heft</b>, but only decides the memory
  127. node, not the exact worker, just pushing tasks to one central queue per memory
  128. node.
  129. - <b>modular-heteroprio</b> is a Heteroprio Scheduler: \n
  130. Maps tasks to worker similarly to HEFT, but first attribute accelerated tasks to
  131. GPUs, then not-so-accelerated tasks to CPUs.
  132. \section TaskDistributionVsDataTransfer Task Distribution Vs Data Transfer
  133. Distributing tasks to balance the load induces data transfer penalty. StarPU
  134. thus needs to find a balance between both. The target function that the
  135. scheduler <c>dmda</c> of StarPU
  136. tries to minimize is <c>alpha * T_execution + beta * T_data_transfer</c>, where
  137. <c>T_execution</c> is the estimated execution time of the codelet (usually
  138. accurate), and <c>T_data_transfer</c> is the estimated data transfer time. The
  139. latter is estimated based on bus calibration before execution start,
  140. i.e. with an idle machine, thus without contention. You can force bus
  141. re-calibration by running the tool <c>starpu_calibrate_bus</c>. The
  142. beta parameter defaults to <c>1</c>, but it can be worth trying to tweak it
  143. by using <c>export STARPU_SCHED_BETA=2</c> (\ref STARPU_SCHED_BETA) for instance, since during
  144. real application execution, contention makes transfer times bigger.
  145. This is of course imprecise, but in practice, a rough estimation
  146. already gives the good results that a precise estimation would give.
  147. \section Energy-basedScheduling Energy-based Scheduling
  148. If the application can provide some energy consumption performance model (through
  149. the field starpu_codelet::energy_model), StarPU will
  150. take it into account when distributing tasks. The target function that
  151. the scheduler <c>dmda</c> minimizes becomes <c>alpha * T_execution +
  152. beta * T_data_transfer + gamma * Consumption</c> , where <c>Consumption</c>
  153. is the estimated task consumption in Joules. To tune this parameter, use
  154. <c>export STARPU_SCHED_GAMMA=3000</c> (\ref STARPU_SCHED_GAMMA) for instance, to express that each Joule
  155. (i.e kW during 1000us) is worth 3000us execution time penalty. Setting
  156. <c>alpha</c> and <c>beta</c> to zero permits to only take into account energy consumption.
  157. This is however not sufficient to correctly optimize energy: the scheduler would
  158. simply tend to run all computations on the most energy-conservative processing
  159. unit. To account for the consumption of the whole machine (including idle
  160. processing units), the idle power of the machine should be given by setting
  161. <c>export STARPU_IDLE_POWER=200</c> (\ref STARPU_IDLE_POWER) for 200W, for instance. This value can often
  162. be obtained from the machine power supplier.
  163. The energy actually consumed by the total execution can be displayed by setting
  164. <c>export STARPU_PROFILING=1 STARPU_WORKER_STATS=1</c> (\ref STARPU_PROFILING and \ref STARPU_WORKER_STATS).
  165. For OpenCL devices, on-line task consumption measurement is currently supported through the
  166. <c>CL_PROFILING_POWER_CONSUMED</c> OpenCL extension, implemented in the MoviSim
  167. simulator.
  168. For CUDA devices, on-line task consumption measurement is supported on V100
  169. cards and beyond. This however only works for quite long tasks, since the
  170. measurement granularity is about 10ms.
  171. Applications can however provide explicit measurements by using the function
  172. starpu_perfmodel_update_history() (examplified in \ref PerformanceModelExample
  173. with the <c>energy_model</c> performance model). Fine-grain measurement
  174. is often not feasible with the feedback provided by the hardware, so the
  175. user can for instance run a given task a thousand times, measure the global
  176. consumption for that series of tasks, divide it by a thousand, repeat for
  177. varying kinds of tasks and task sizes, and eventually feed StarPU with these
  178. manual measurements through starpu_perfmodel_update_history(). For instance,
  179. for CUDA devices, <c>nvidia-smi -q -d POWER</c> can be used to get the current
  180. consumption in Watt. Multiplying this value by the average duration of a
  181. single task gives the consumption of the task in Joules, which can be given to
  182. starpu_perfmodel_update_history().
  183. Another way to provide the energy performance is to define a
  184. perfmodel with starpu_perfmodel::type ::STARPU_PER_ARCH or
  185. ::STARPU_PER_WORKER , and set the starpu_perfmodel::arch_cost_function or
  186. starpu_perfmodel::worker_cost_function field to a function which shall return
  187. the estimated consumption of the task in Joules. Such a function can for instance
  188. use starpu_task_expected_length() on the task (in µs), multiplied by the
  189. typical power consumption of the device, e.g. in W, and divided by 1000000. to
  190. get Joules.
  191. \section StaticScheduling Static Scheduling
  192. In some cases, one may want to force some scheduling, for instance force a given
  193. set of tasks to GPU0, another set to GPU1, etc. while letting some other tasks
  194. be scheduled on any other device. This can indeed be useful to guide StarPU into
  195. some work distribution, while still letting some degree of dynamism. For
  196. instance, to force execution of a task on CUDA0:
  197. \code{.c}
  198. task->execute_on_a_specific_worker = 1;
  199. task->workerid = starpu_worker_get_by_type(STARPU_CUDA_WORKER, 0);
  200. \endcode
  201. or equivalently
  202. \code{.c}
  203. starpu_task_insert(&cl, ..., STARPU_EXECUTE_ON_WORKER, starpu_worker_get_by_type(STARPU_CUDA_WORKER, 0), ...);
  204. \endcode
  205. One can also specify a set worker(s) which are allowed to take the task, as an
  206. array of bit, for instance to allow workers 2 and 42:
  207. \code{.c}
  208. task->workerids = calloc(2,sizeof(uint32_t));
  209. task->workerids[2/32] |= (1 << (2%32));
  210. task->workerids[42/32] |= (1 << (42%32));
  211. task->workerids_len = 2;
  212. \endcode
  213. One can also specify the order in which tasks must be executed by setting the
  214. starpu_task::workerorder field. If this field is set to a non-zero value, it
  215. provides the per-worker consecutive order in which tasks will be executed,
  216. starting from 1. For a given of such task, the worker will thus not execute
  217. it before all the tasks with smaller order value have been executed, notably
  218. in case those tasks are not available yet due to some dependencies. This
  219. eventually gives total control of task scheduling, and StarPU will only serve as
  220. a "self-timed" task runtime. Of course, the provided order has to be runnable,
  221. i.e. a task should should not depend on another task bound to the same worker
  222. with a bigger order.
  223. Note however that using scheduling contexts while statically scheduling tasks on workers
  224. could be tricky. Be careful to schedule the tasks exactly on the workers of the corresponding
  225. contexts, otherwise the workers' corresponding scheduling structures may not be allocated or
  226. the execution of the application may deadlock. Moreover, the hypervisor should not be used when
  227. statically scheduling tasks.
  228. \section Configuring Heteroprio
  229. Within Heteroprio, one priority per processing unit type is assigned to each task, such that a task has several
  230. priorities. Each worker pops the task that has the highest priority for the hardware type it uses, which
  231. could be CPU or CUDA for example. Therefore, the priorities has to be used to manage the critical path,
  232. but also to promote the consumption of tasks by the more appropriate workers.
  233. The tasks are stored inside buckets, where each bucket corresponds to a priority set. Then each
  234. worker uses an indirect access array to know the order in which it should access the buckets. Moreover,
  235. all the tasks inside a bucket must be compatible with all the processing units that may access it (at least).
  236. As an example, see the following code where we have 5 types of tasks.
  237. CPU workers can compute all of them, but CUDA workers can only execute
  238. tasks of types 0 and 1, and is expected to go 20 and 30 time
  239. faster than the CPU, respectively.
  240. \code{.c}
  241. // In the file that init StarPU
  242. #include <starpu_heteroprio.h>
  243. ////////////////////////////////////////////////////
  244. // Before calling starpu_init
  245. struct starpu_conf conf;
  246. starpu_conf_init(&conf);
  247. // Inform StarPU to use Heteroprio
  248. conf.sched_policy_name = "heteroprio";
  249. // Inform StarPU about the function that will init the priorities in Heteroprio
  250. // where init_heteroprio is a function to implement
  251. conf.sched_policy_init = &init_heteroprio;
  252. // Do other things with conf if needed, then init StarPU
  253. starpu_init(&conf);
  254. ////////////////////////////////////////////////////
  255. void init_heteroprio(unsigned sched_ctx) {
  256. // CPU uses 5 buckets and visits them in the natural order
  257. starpu_heteroprio_set_nb_prios(ctx, STARPU_CPU_IDX, 5);
  258. // It uses direct mapping idx => idx
  259. for(unsigned idx = 0; idx < 5; ++idx){
  260. starpu_heteroprio_set_mapping(ctx, STARPU_CPU_IDX, idx, idx);
  261. // If there is no CUDA worker we must tell that CPU is faster
  262. starpu_heteroprio_set_faster_arch(ctx, STARPU_CPU_IDX, idx);
  263. }
  264. if(starpu_cuda_worker_get_count()){
  265. // CUDA is enabled and uses 2 buckets
  266. starpu_heteroprio_set_nb_prios(ctx, STARPU_CUDA_IDX, 2);
  267. // CUDA will first look at bucket 1
  268. starpu_heteroprio_set_mapping(ctx, STARPU_CUDA_IDX, 0, 1);
  269. // CUDA will then look at bucket 2
  270. starpu_heteroprio_set_mapping(ctx, STARPU_CUDA_IDX, 1, 2);
  271. // For bucket 1 CUDA is the fastest
  272. starpu_heteroprio_set_faster_arch(ctx, STARPU_CUDA_IDX, 1);
  273. // And CPU is 30 times slower
  274. starpu_heteroprio_set_arch_slow_factor(ctx, STARPU_CPU_IDX, 1, 30.0f);
  275. // For bucket 0 CUDA is the fastest
  276. starpu_heteroprio_set_faster_arch(ctx, STARPU_CUDA_IDX, 0);
  277. // And CPU is 20 times slower
  278. starpu_heteroprio_set_arch_slow_factor(ctx, STARPU_CPU_IDX, 0, 20.0f);
  279. }
  280. }
  281. \endcode
  282. Then, when a task is inserted <b>the priority of the task will be used to
  283. select in which bucket is has to be stored</b>.
  284. So, in the given example, the priority of a task will be between 0 and 4 included.
  285. However, tasks of priorities 0-1 must provide CPU and CUDA kernels, and
  286. tasks of priorities 2-4 must provide CPU kernels (at least).
  287. */