320_scheduling.doxy 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2017 CNRS
  4. * Copyright (C) 2011-2012,2016 Inria
  5. * Copyright (C) 2009-2011,2014-2017 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 that:
  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. \section DMTaskSchedulingPolicy Performance Model-Based Task Scheduling Policies
  53. If (<b>and only if</b>) your application <b>codelets have performance models</b> (\ref
  54. PerformanceModelExample), you should change the scheduler thanks to the
  55. environment variable \ref STARPU_SCHED, to select one of the policies below, in
  56. order to take advantage of StarPU's performance modelling. For instance
  57. <c>export STARPU_SCHED=dmda</c> . Use <c>help</c> to get the list of available
  58. schedulers.
  59. <b>Note:</B> Depending on the performance model type chosen, some preliminary
  60. calibration runs may be needed for the model to converge. If the calibration
  61. has not been done, or is insufficient yet, or if no performance model is
  62. specified for a codelet, every task built from this codelet will be scheduled
  63. using an <b>eager</b> fallback policy.
  64. <b>Troubleshooting:</b> Configuring and recompiling StarPU using the
  65. <c>--enable-verbose</c> configure flag displays some statistics at the end of
  66. execution about the percentage of tasks that have been scheduled by a DM*
  67. family policy using performance model hints. A low or zero percentage may be
  68. the sign that performance models are not converging or that codelets do not
  69. have performance models enabled.
  70. <b>Performance Modelling Policies:</b>
  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>dmdar</b> (deque model data aware ready) scheduler is similar to dmda,
  79. but it also sorts tasks on per-worker queues by number of already-available data
  80. buffers on the target device.
  81. The <b>dmdas</b> (deque model data aware sorted) scheduler is similar to dmdar,
  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>dmdasd</b> (deque model data aware sorted decision) scheduler is similar
  86. to dmdas, except that when scheduling a task, it takes into account its priority
  87. when computing the minimum completion time, since this task may get executed
  88. before others, and thus the latter should be ignored.
  89. The <b>heft</b> (heterogeneous earliest finish time) scheduler is a deprecated
  90. alias for <b>dmda</b>.
  91. The <b>pheft</b> (parallel HEFT) scheduler is similar to dmda, it also supports
  92. parallel tasks (still experimental). Should not be used when several contexts using
  93. it are being executed simultaneously.
  94. The <b>peager</b> (parallel eager) scheduler is similar to eager, it also
  95. supports parallel tasks (still experimental). Should not be used when several
  96. contexts using it are being executed simultaneously.
  97. TODO: describe modular schedulers
  98. \section TaskDistributionVsDataTransfer Task Distribution Vs Data Transfer
  99. Distributing tasks to balance the load induces data transfer penalty. StarPU
  100. thus needs to find a balance between both. The target function that the
  101. scheduler <c>dmda</c> of StarPU
  102. tries to minimize is <c>alpha * T_execution + beta * T_data_transfer</c>, where
  103. <c>T_execution</c> is the estimated execution time of the codelet (usually
  104. accurate), and <c>T_data_transfer</c> is the estimated data transfer time. The
  105. latter is estimated based on bus calibration before execution start,
  106. i.e. with an idle machine, thus without contention. You can force bus
  107. re-calibration by running the tool <c>starpu_calibrate_bus</c>. The
  108. beta parameter defaults to <c>1</c>, but it can be worth trying to tweak it
  109. by using <c>export STARPU_SCHED_BETA=2</c> (\ref STARPU_SCHED_BETA) for instance, since during
  110. real application execution, contention makes transfer times bigger.
  111. This is of course imprecise, but in practice, a rough estimation
  112. already gives the good results that a precise estimation would give.
  113. \section Energy-basedScheduling Energy-based Scheduling
  114. If the application can provide some energy consumption performance model (through
  115. the field starpu_codelet::energy_model), StarPU will
  116. take it into account when distributing tasks. The target function that
  117. the scheduler <c>dmda</c> minimizes becomes <c>alpha * T_execution +
  118. beta * T_data_transfer + gamma * Consumption</c> , where <c>Consumption</c>
  119. is the estimated task consumption in Joules. To tune this parameter, use
  120. <c>export STARPU_SCHED_GAMMA=3000</c> (\ref STARPU_SCHED_GAMMA) for instance, to express that each Joule
  121. (i.e kW during 1000us) is worth 3000us execution time penalty. Setting
  122. <c>alpha</c> and <c>beta</c> to zero permits to only take into account energy consumption.
  123. This is however not sufficient to correctly optimize energy: the scheduler would
  124. simply tend to run all computations on the most energy-conservative processing
  125. unit. To account for the consumption of the whole machine (including idle
  126. processing units), the idle power of the machine should be given by setting
  127. <c>export STARPU_IDLE_POWER=200</c> (\ref STARPU_IDLE_POWER) for 200W, for instance. This value can often
  128. be obtained from the machine power supplier.
  129. The energy actually consumed by the total execution can be displayed by setting
  130. <c>export STARPU_PROFILING=1 STARPU_WORKER_STATS=1</c> .
  131. On-line task consumption measurement is currently only supported through the
  132. <c>CL_PROFILING_POWER_CONSUMED</c> OpenCL extension, implemented in the MoviSim
  133. simulator. Applications can however provide explicit measurements by
  134. using the function starpu_perfmodel_update_history() (examplified in \ref PerformanceModelExample
  135. with the <c>energy_model</c> performance model). Fine-grain
  136. measurement is often not feasible with the feedback provided by the hardware, so
  137. the user can for instance run a given task a thousand times, measure the global
  138. consumption for that series of tasks, divide it by a thousand, repeat for
  139. varying kinds of tasks and task sizes, and eventually feed StarPU
  140. with these manual measurements through starpu_perfmodel_update_history().
  141. For instance, for CUDA devices, <c>nvidia-smi -q -d POWER</c> can be used to get
  142. the current consumption in Watt. Multiplying that value by the average duration
  143. of a single task gives the consumption of the task in Joules, which can be given
  144. to starpu_perfmodel_update_history().
  145. \section StaticScheduling Static Scheduling
  146. In some cases, one may want to force some scheduling, for instance force a given
  147. set of tasks to GPU0, another set to GPU1, etc. while letting some other tasks
  148. be scheduled on any other device. This can indeed be useful to guide StarPU into
  149. some work distribution, while still letting some degree of dynamism. For
  150. instance, to force execution of a task on CUDA0:
  151. \code{.c}
  152. task->execute_on_a_specific_worker = 1;
  153. task->workerid = starpu_worker_get_by_type(STARPU_CUDA_WORKER, 0);
  154. \endcode
  155. One can also specify a set worker(s) which are allowed to take the task, as an
  156. array of bit, for instance to allow workers 2 and 42:
  157. \code{.c}
  158. task->workerids = calloc(2,sizeof(uint32_t));
  159. task->workerids[2/32] |= (1 << (2%32));
  160. task->workerids[42/32] |= (1 << (42%32));
  161. task->workerids_len = 2;
  162. \endcode
  163. One can also specify the order in which tasks must be executed by setting the
  164. starpu_task::workerorder field. If this field is set to a non-zero value, it
  165. provides the per-worker consecutive order in which tasks will be executed,
  166. starting from 1. For a given of such task, the worker will thus not execute
  167. it before all the tasks with smaller order value have been executed, notably
  168. in case those tasks are not available yet due to some dependencies. This
  169. eventually gives total control of task scheduling, and StarPU will only serve as
  170. a "self-timed" task runtime. Of course, the provided order has to be runnable,
  171. i.e. a task should should not depend on another task bound to the same worker
  172. with a bigger order.
  173. Note however that using scheduling contexts while statically scheduling tasks on workers
  174. could be tricky. Be careful to schedule the tasks exactly on the workers of the corresponding
  175. contexts, otherwise the workers' corresponding scheduling structures may not be allocated or
  176. the execution of the application may deadlock. Moreover, the hypervisor should not be used when
  177. statically scheduling tasks.
  178. \section DefiningANewSchedulingPolicy Defining A New Scheduling Policy
  179. A full example showing how to define a new scheduling policy is available in
  180. the StarPU sources in the directory <c>examples/scheduler/</c>.
  181. The scheduler has to provide methods:
  182. \code{.c}
  183. static struct starpu_sched_policy dummy_sched_policy =
  184. {
  185. .init_sched = init_dummy_sched,
  186. .deinit_sched = deinit_dummy_sched,
  187. .add_workers = dummy_sched_add_workers,
  188. .remove_workers = dummy_sched_remove_workers,
  189. .push_task = push_task_dummy,
  190. .pop_task = pop_task_dummy,
  191. .policy_name = "dummy",
  192. .policy_description = "dummy scheduling strategy"
  193. };
  194. \endcode
  195. The idea is that when a task becomes ready for execution, the
  196. starpu_sched_policy::push_task method is called. When a worker is idle, the
  197. starpu_sched_policy::pop_task method is called to get a task. It is up to the
  198. scheduler to implement what is between. A simple eager scheduler is for instance
  199. to make starpu_sched_policy::push_task push the task to a global list, and make
  200. starpu_sched_policy::pop_task pop from that list.
  201. The \ref starpu_sched_policy section provides the exact rules that govern the
  202. methods of the policy.
  203. Make sure to have a look at the \ref API_Scheduling_Policy section, which
  204. provides a list of the available functions for writing advanced schedulers, such
  205. as starpu_task_expected_length(), starpu_task_expected_data_transfer_time(),
  206. starpu_task_expected_energy(), etc. Other
  207. useful functions include starpu_transfer_bandwidth(), starpu_transfer_latency(),
  208. starpu_transfer_predict(), ...
  209. Usual functions can also be used on tasks, for instance one can do
  210. \code{.c}
  211. size = 0;
  212. write = 0;
  213. if (task->cl)
  214. for (i = 0; i < STARPU_TASK_GET_NBUFFERS(task); i++)
  215. {
  216. starpu_data_handle_t data = STARPU_TASK_GET_HANDLE(task, i)
  217. size_t datasize = starpu_data_get_size(data);
  218. size += datasize;
  219. if (STARPU_TASK_GET_MODE(task, i) & STARPU_W)
  220. write += datasize;
  221. }
  222. \endcode
  223. And various queues can be used in schedulers. A variety of examples of
  224. schedulers can be read in <c>src/sched_policies</c>, for
  225. instance <c>random_policy.c</c>, <c>eager_central_policy.c</c>,
  226. <c>work_stealing_policy.c</c>
  227. \section GraphScheduling Graph-based Scheduling
  228. For performance reasons, most of the schedulers shipped with StarPU use simple
  229. list-scheduling heuristics, assuming that the application has already set
  230. priorities. That is why they do their scheduling between when tasks become
  231. available for execution and when a worker becomes idle, without looking at the
  232. task graph.
  233. Other heuristics can however look at the task graph. Recording the task graph
  234. is expensive, so it is not available by default, the scheduling heuristic has
  235. to set _starpu_graph_record to 1 from the initialization function, to make it
  236. available. Then the <c>_starpu_graph*</c> functions can be used.
  237. <c>src/sched_policies/graph_test_policy.c</c> is an example of simple greedy
  238. policy which automatically computes priorities by bottom-up rank.
  239. The idea is that while the application submits tasks, they are only pushed
  240. to a bag of tasks. When the application is finished with submitting tasks,
  241. it calls starpu_do_schedule() (or starpu_task_wait_for_all(), which calls
  242. starpu_do_schedule()), and the starpu_sched_policy::do_schedule method of the
  243. scheduler is called. This method calls _starpu_graph_compute_depths to compute
  244. the bottom-up ranks, and then uses these rank to set priorities over tasks.
  245. It then has two priority queues, one for CPUs, and one for GPUs, and uses a dumb
  246. heuristic based on the duration of the task over CPUs and GPUs to decide between
  247. the two queues. CPU workers can then pop from the CPU priority queue, and GPU
  248. workers from the GPU priority queue.
  249. \section DebuggingScheduling Debugging Scheduling
  250. All the \ref OnlinePerformanceTools and \ref OfflinePerformanceTools can
  251. be used to get information about how well the execution proceeded, and thus the
  252. overall quality of the execution.
  253. Precise debugging can also be performed by using the
  254. \ref STARPU_TASK_BREAK_ON_PUSH, \ref STARPU_TASK_BREAK_ON_SCHED,
  255. \ref STARPU_TASK_BREAK_ON_POP, and \ref STARPU_TASK_BREAK_ON_EXEC environment variables.
  256. By setting the job_id of a task
  257. in these environment variables, StarPU will raise <c>SIGTRAP</c> when the task is being
  258. scheduled, pushed, or popped by the scheduler. That means that when one notices
  259. that a task is being scheduled in a seemingly odd way, one can just reexecute
  260. the application in a debugger, with some of those variables set, and the
  261. execution will stop exactly at the scheduling points of that task, thus allowing
  262. to inspect the scheduler state, etc.
  263. */