08scheduling.doxy 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 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 Policy
  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 not
  22. have performance models. If your application codelets have performance models
  23. (\ref PerformanceModelExample), you should change the scheduler thanks
  24. to the environment variable \ref STARPU_SCHED. For instance <c>export
  25. STARPU_SCHED=dmda</c> . Use <c>help</c> to get the list of available schedulers.
  26. The <b>eager</b> scheduler uses a central task queue, from which all workers draw tasks
  27. to work on concurrently. This however does not permit to prefetch data since the scheduling
  28. decision is taken late. If a task has a non-0 priority, it is put at the front of the queue.
  29. The <b>prio</b> scheduler also uses a central task queue, but sorts tasks by
  30. priority (between -5 and 5).
  31. The <b>random</b> scheduler uses a queue per worker, and distributes tasks randomly according to assumed worker
  32. overall performance.
  33. The <b>ws</b> (work stealing) scheduler uses a queue per worker, and schedules
  34. a task on the worker which released it by
  35. default. When a worker becomes idle, it steals a task from the most loaded
  36. worker.
  37. The <b>lws</b> (locality work stealing) scheduler uses a queue per worker, and schedules
  38. a task on the worker which released it by
  39. default. When a worker becomes idle, it steals a task from neighbour workers. It
  40. also takes into account priorities.
  41. The <b>dm</b> (deque model) scheduler uses task execution performance models into account to
  42. perform a HEFT-similar scheduling strategy: it schedules tasks where their
  43. termination time will be minimal. The difference with HEFT is that <b>dm</b>
  44. schedules tasks as soon as they become available, and thus in the order they
  45. become available, without taking priorities into account.
  46. The <b>dmda</b> (deque model data aware) scheduler is similar to dm, but it also takes
  47. into account data transfer time.
  48. The <b>dmdar</b> (deque model data aware ready) scheduler is similar to dmda,
  49. but it also sorts tasks on per-worker queues by number of already-available data
  50. buffers on the target device.
  51. The <b>dmdas</b> (deque model data aware sorted) scheduler is similar to dmdar,
  52. except that it sorts tasks by priority order, which allows to become even closer
  53. to HEFT by respecting priorities after having made the scheduling decision (but
  54. it still schedules tasks in the order they become available).
  55. The <b>heft</b> (heterogeneous earliest finish time) scheduler is a deprecated
  56. alias for <b>dmda</b>.
  57. The <b>pheft</b> (parallel HEFT) scheduler is similar to dmda, it also supports
  58. parallel tasks (still experimental). Should not be used when several contexts using
  59. it are being executed simultaneously.
  60. The <b>peager</b> (parallel eager) scheduler is similar to eager, it also
  61. supports parallel tasks (still experimental). Should not be used when several
  62. contexts using it are being executed simultaneously.
  63. TODO: describe modular schedulers
  64. \section TaskDistributionVsDataTransfer Task Distribution Vs Data Transfer
  65. Distributing tasks to balance the load induces data transfer penalty. StarPU
  66. thus needs to find a balance between both. The target function that the
  67. scheduler <c>dmda</c> of StarPU
  68. tries to minimize is <c>alpha * T_execution + beta * T_data_transfer</c>, where
  69. <c>T_execution</c> is the estimated execution time of the codelet (usually
  70. accurate), and <c>T_data_transfer</c> is the estimated data transfer time. The
  71. latter is estimated based on bus calibration before execution start,
  72. i.e. with an idle machine, thus without contention. You can force bus
  73. re-calibration by running the tool <c>starpu_calibrate_bus</c>. The
  74. beta parameter defaults to <c>1</c>, but it can be worth trying to tweak it
  75. by using <c>export STARPU_SCHED_BETA=2</c> for instance, since during
  76. real application execution, contention makes transfer times bigger.
  77. This is of course imprecise, but in practice, a rough estimation
  78. already gives the good results that a precise estimation would give.
  79. \section Power-basedScheduling Power-based Scheduling
  80. If the application can provide some power performance model (through
  81. the field starpu_codelet::power_model), StarPU will
  82. take it into account when distributing tasks. The target function that
  83. the scheduler <c>dmda</c> minimizes becomes <c>alpha * T_execution +
  84. beta * T_data_transfer + gamma * Consumption</c> , where <c>Consumption</c>
  85. is the estimated task consumption in Joules. To tune this parameter, use
  86. <c>export STARPU_SCHED_GAMMA=3000</c> for instance, to express that each Joule
  87. (i.e kW during 1000us) is worth 3000us execution time penalty. Setting
  88. <c>alpha</c> and <c>beta</c> to zero permits to only take into account power consumption.
  89. This is however not sufficient to correctly optimize power: the scheduler would
  90. simply tend to run all computations on the most energy-conservative processing
  91. unit. To account for the consumption of the whole machine (including idle
  92. processing units), the idle power of the machine should be given by setting
  93. <c>export STARPU_IDLE_POWER=200</c> for 200W, for instance. This value can often
  94. be obtained from the machine power supplier.
  95. The power actually consumed by the total execution can be displayed by setting
  96. <c>export STARPU_PROFILING=1 STARPU_WORKER_STATS=1</c> .
  97. On-line task consumption measurement is currently only supported through the
  98. <c>CL_PROFILING_POWER_CONSUMED</c> OpenCL extension, implemented in the MoviSim
  99. simulator. Applications can however provide explicit measurements by
  100. using the function starpu_perfmodel_update_history() (examplified in \ref PerformanceModelExample
  101. with the <c>power_model</c> performance model). Fine-grain
  102. measurement is often not feasible with the feedback provided by the hardware, so
  103. the user can for instance run a given task a thousand times, measure the global
  104. consumption for that series of tasks, divide it by a thousand, repeat for
  105. varying kinds of tasks and task sizes, and eventually feed StarPU
  106. with these manual measurements through starpu_perfmodel_update_history().
  107. For instance, for CUDA devices, <c>nvidia-smi -q -d POWER</c> can be used to get
  108. the current consumption in Watt. Multiplying that value by the average duration
  109. of a single task gives the consumption of the task in Joules, which can be given
  110. to starpu_perfmodel_update_history().
  111. \section StaticScheduling Static Scheduling
  112. In some cases, one may want to force some scheduling, for instance force a given
  113. set of tasks to GPU0, another set to GPU1, etc. while letting some other tasks
  114. be scheduled on any other device. This can indeed be useful to guide StarPU into
  115. some work distribution, while still letting some degree of dynamism. For
  116. instance, to force execution of a task on CUDA0:
  117. \code{.c}
  118. task->execute_on_a_specific_worker = 1;
  119. task->worker = starpu_worker_get_by_type(STARPU_CUDA_WORKER, 0);
  120. \endcode
  121. One can also specify the order in which tasks must be executed by setting the
  122. starpu_task::workerorder field. If this field is set to a non-zero value, it
  123. provides the per-worker consecutive order in which tasks will be executed,
  124. starting from 1. For a given of such task, the worker will thus not execute
  125. it before all the tasks with smaller order value have been executed, notably
  126. in case those tasks are not available yet due to some dependencies. This
  127. eventually gives total control of task scheduling, and StarPU will only serve as
  128. a "self-timed" task runtime. Of course, the provided order has to be runnable,
  129. i.e. a task should should not depend on another task bound to the same worker
  130. with a bigger order.
  131. Note however that using scheduling contexts while statically scheduling tasks on workers
  132. could be tricky. Be careful to schedule the tasks exactly on the workers of the corresponding
  133. contexts, otherwise the workers' corresponding scheduling structures may not be allocated or
  134. the execution of the application may deadlock. Moreover, the hypervisor should not be used when
  135. statically scheduling tasks.
  136. \section DefiningANewSchedulingPolicy Defining A New Scheduling Policy
  137. A full example showing how to define a new scheduling policy is available in
  138. the StarPU sources in the directory <c>examples/scheduler/</c>.
  139. The scheduler has to provide methods:
  140. \code{.c}
  141. static struct starpu_sched_policy dummy_sched_policy = {
  142. .init_sched = init_dummy_sched,
  143. .deinit_sched = deinit_dummy_sched,
  144. .add_workers = dummy_sched_add_workers,
  145. .remove_workers = dummy_sched_remove_workers,
  146. .push_task = push_task_dummy,
  147. .pop_task = pop_task_dummy,
  148. .policy_name = "dummy",
  149. .policy_description = "dummy scheduling strategy"
  150. };
  151. \endcode
  152. The idea is that when a task becomes ready for execution, the
  153. starpu_sched_policy::push_task method is called. When a worker is idle, the
  154. starpu_sched_policy::pop_task method is called to get a task. It is up to the
  155. scheduler to implement what is between. A simple eager scheduler is for instance
  156. to make starpu_sched_policy::push_task push the task to a global list, and make
  157. starpu_sched_policy::pop_task pop from that list.
  158. The \ref starpu_sched_policy section provides the exact rules that govern the
  159. methods of the policy.
  160. Make sure to have a look at the \ref API_Scheduling_Policy section, which
  161. provides a list of the available functions for writing advanced schedulers, such
  162. as starpu_task_expected_length, starpu_task_expected_data_transfer_time,
  163. starpu_task_expected_power, starpu_prefetch_task_input_node, etc. Other
  164. useful functions include starpu_transfer_bandwidth, starpu_transfer_latency,
  165. starpu_transfer_predict, ...
  166. Usual functions can also be used on tasks, for instance one can do
  167. \code{.c}
  168. size = 0;
  169. write = 0;
  170. if (task->cl)
  171. for (i = 0; i < STARPU_TASK_GET_NBUFFERS(task); i++)
  172. {
  173. starpu_data_handle_t data = STARPU_TASK_GET_HANDLE(task, i)
  174. size_t datasize = starpu_data_get_size(data);
  175. size += datasize;
  176. if (STARPU_TASK_GET_MODE(task, i) & STARPU_W)
  177. write += datasize;
  178. }
  179. \endcode
  180. And various queues can be used in schedulers. A variety of examples of
  181. schedulers can be read in <c>src/sched_policies</c>, for
  182. instance <c>random_policy.c</c>, <c>eager_central_policy.c</c>,
  183. <c>work_stealing_policy.c</c>
  184. \section GraphScheduling Graph-based scheduling
  185. For performance reasons, most of the schedulers shipped with StarPU use simple
  186. list-scheduling heuristics, assuming that the application has already set
  187. priorities. That is why they do their scheduling between when tasks become
  188. available for execution and when a worker becomes idle, without looking at the
  189. task graph.
  190. Other heuristics can however look at the task graph. Recording the task graph
  191. is expensive, so it is not available by default, the scheduling heuristic has
  192. to set _starpu_graph_record to 1 from the initialization function, to make it
  193. available. Then the <c>_starpu_graph*</c> functions can be used.
  194. <c>src/sched_policies/graph_test_policy.c</c> is an example of simple greedy
  195. policy which automatically computes priorities by bottom-up rank.
  196. The idea is that while the application submits tasks, they are only pushed
  197. to a bag of tasks. When the application is finished with submitting tasks,
  198. it calls starpu_do_schedule (or starpu_task_wait_for_all, which calls
  199. starpu_do_schedule), and the starpu_sched_policy::do_schedule method of the
  200. scheduler is called. This method calls _starpu_graph_compute_depths to compute
  201. the bottom-up ranks, and then uses these rank to set priorities over tasks.
  202. It then has two priority queues, one for CPUs, and one for GPUs, and uses a dumb
  203. heuristic based on the duration of the task over CPUs and GPUs to decide between
  204. the two queues. CPU workers can then pop from the CPU priority queue, and GPU
  205. workers from the GPU priority queue.
  206. */