320_scheduling.doxy 15 KB

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