perf-optimization.texi 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. @c -*-texinfo-*-
  2. @c This file is part of the StarPU Handbook.
  3. @c Copyright (C) 2009--2011 Universit@'e de Bordeaux 1
  4. @c Copyright (C) 2010, 2011, 2012 Centre National de la Recherche Scientifique
  5. @c Copyright (C) 2011 Institut National de Recherche en Informatique et Automatique
  6. @c See the file starpu.texi for copying conditions.
  7. TODO: improve!
  8. @menu
  9. * Data management::
  10. * Task submission::
  11. * Task priorities::
  12. * Task scheduling policy::
  13. * Performance model calibration::
  14. * Task distribution vs Data transfer::
  15. * Data prefetch::
  16. * Power-based scheduling::
  17. * Profiling::
  18. * CUDA-specific optimizations::
  19. @end menu
  20. Simply encapsulating application kernels into tasks already permits to
  21. seamlessly support CPU and GPUs at the same time. To achieve good performance, a
  22. few additional changes are needed.
  23. @node Data management
  24. @section Data management
  25. When the application allocates data, whenever possible it should use the
  26. @code{starpu_malloc} function, which will ask CUDA or
  27. OpenCL to make the allocation itself and pin the corresponding allocated
  28. memory. This is needed to permit asynchronous data transfer, i.e. permit data
  29. transfer to overlap with computations. Otherwise, the trace will show that the
  30. @code{DriverCopyAsync} state takes a lot of time, this is because CUDA or OpenCL
  31. then reverts to synchronous transfers.
  32. By default, StarPU leaves replicates of data wherever they were used, in case they
  33. will be re-used by other tasks, thus saving the data transfer time. When some
  34. task modifies some data, all the other replicates are invalidated, and only the
  35. processing unit which ran that task will have a valid replicate of the data. If the application knows
  36. that this data will not be re-used by further tasks, it should advise StarPU to
  37. immediately replicate it to a desired list of memory nodes (given through a
  38. bitmask). This can be understood like the write-through mode of CPU caches.
  39. @cartouche
  40. @smallexample
  41. starpu_data_set_wt_mask(img_handle, 1<<0);
  42. @end smallexample
  43. @end cartouche
  44. will for instance request to always automatically transfer a replicate into the
  45. main memory (node 0), as bit 0 of the write-through bitmask is being set.
  46. @cartouche
  47. @smallexample
  48. starpu_data_set_wt_mask(img_handle, ~0U);
  49. @end smallexample
  50. @end cartouche
  51. will request to always automatically broadcast the updated data to all memory
  52. nodes.
  53. @node Task submission
  54. @section Task submission
  55. To let StarPU make online optimizations, tasks should be submitted
  56. asynchronously as much as possible. Ideally, all the tasks should be
  57. submitted, and mere calls to @code{starpu_task_wait_for_all} or
  58. @code{starpu_data_unregister} be done to wait for
  59. termination. StarPU will then be able to rework the whole schedule, overlap
  60. computation with communication, manage accelerator local memory usage, etc.
  61. @node Task priorities
  62. @section Task priorities
  63. By default, StarPU will consider the tasks in the order they are submitted by
  64. the application. If the application programmer knows that some tasks should
  65. be performed in priority (for instance because their output is needed by many
  66. other tasks and may thus be a bottleneck if not executed early enough), the
  67. @code{priority} field of the task structure should be set to transmit the
  68. priority information to StarPU.
  69. @node Task scheduling policy
  70. @section Task scheduling policy
  71. By default, StarPU uses the @code{eager} simple greedy scheduler. This is
  72. because it provides correct load balance even if the application codelets do not
  73. have performance models. If your application codelets have performance models
  74. (@pxref{Performance model example} for examples showing how to do it),
  75. you should change the scheduler thanks to the @code{STARPU_SCHED} environment
  76. variable. For instance @code{export STARPU_SCHED=dmda} . Use @code{help} to get
  77. the list of available schedulers.
  78. The @b{eager} scheduler uses a central task queue, from which workers draw tasks
  79. to work on. This however does not permit to prefetch data since the scheduling
  80. decision is taken late. If a task has a non-0 priority, it is put at the front of the queue.
  81. The @b{prio} scheduler also uses a central task queue, but sorts tasks by
  82. priority (between -5 and 5).
  83. The @b{random} scheduler distributes tasks randomly according to assumed worker
  84. overall performance.
  85. The @b{ws} (work stealing) scheduler schedules tasks on the local worker by
  86. default. When a worker becomes idle, it steals a task from the most loaded
  87. worker.
  88. The @b{dm} (deque model) scheduler uses task execution performance models into account to
  89. perform an HEFT-similar scheduling strategy: it schedules tasks where their
  90. termination time will be minimal.
  91. The @b{dmda} (deque model data aware) scheduler is similar to dm, it also takes
  92. into account data transfer time.
  93. The @b{dmdar} (deque model data aware ready) scheduler is similar to dmda,
  94. it also sorts tasks on per-worker queues by number of already-available data
  95. buffers.
  96. The @b{dmdas} (deque model data aware sorted) scheduler is similar to dmda, it
  97. also supports arbitrary priority values.
  98. The @b{heft} (HEFT) scheduler is similar to dmda, it also supports task bundles.
  99. The @b{pheft} (parallel HEFT) scheduler is similar to heft, it also supports
  100. parallel tasks (still experimental).
  101. The @b{pgreedy} (parallel greedy) scheduler is similar to greedy, it also
  102. supports parallel tasks (still experimental).
  103. @node Performance model calibration
  104. @section Performance model calibration
  105. Most schedulers are based on an estimation of codelet duration on each kind
  106. of processing unit. For this to be possible, the application programmer needs
  107. to configure a performance model for the codelets of the application (see
  108. @ref{Performance model example} for instance). History-based performance models
  109. use on-line calibration. StarPU will automatically calibrate codelets
  110. which have never been calibrated yet, and save the result in
  111. @code{~/.starpu/sampling/codelets}.
  112. The models are indexed by machine name. To share the models between machines (e.g. for a homogeneous cluster), use @code{export STARPU_HOSTNAME=some_global_name}. To force continuing calibration, use
  113. @code{export STARPU_CALIBRATE=1} . This may be necessary if your application
  114. has not-so-stable performance. StarPU will force calibration (and thus ignore
  115. the current result) until 10 (_STARPU_CALIBRATION_MINIMUM) measurements have been
  116. made on each architecture, to avoid badly scheduling tasks just because the
  117. first measurements were not so good. Details on the current performance model status
  118. can be obtained from the @code{starpu_perfmodel_display} command: the @code{-l}
  119. option lists the available performance models, and the @code{-s} option permits
  120. to choose the performance model to be displayed. The result looks like:
  121. @example
  122. $ starpu_perfmodel_display -s starpu_dlu_lu_model_22
  123. performance model for cpu
  124. # hash size mean dev n
  125. 880805ba 98304 2.731309e+02 6.010210e+01 1240
  126. b50b6605 393216 1.469926e+03 1.088828e+02 1240
  127. 5c6c3401 1572864 1.125983e+04 3.265296e+03 1240
  128. @end example
  129. Which shows that for the LU 22 kernel with a 1.5MiB matrix, the average
  130. execution time on CPUs was about 11ms, with a 3ms standard deviation, over
  131. 1240 samples. It is a good idea to check this before doing actual performance
  132. measurements.
  133. A graph can be drawn by using the @code{starpu_perfmodel_plot}:
  134. @example
  135. $ starpu_perfmodel_plot -s starpu_dlu_lu_model_22
  136. 98304 393216 1572864
  137. $ gnuplot starpu_starpu_dlu_lu_model_22.gp
  138. $ gv starpu_starpu_dlu_lu_model_22.eps
  139. @end example
  140. If a kernel source code was modified (e.g. performance improvement), the
  141. calibration information is stale and should be dropped, to re-calibrate from
  142. start. This can be done by using @code{export STARPU_CALIBRATE=2}.
  143. Note: due to CUDA limitations, to be able to measure kernel duration,
  144. calibration mode needs to disable asynchronous data transfers. Calibration thus
  145. disables data transfer / computation overlapping, and should thus not be used
  146. for eventual benchmarks. Note 2: history-based performance models get calibrated
  147. only if a performance-model-based scheduler is chosen.
  148. @node Task distribution vs Data transfer
  149. @section Task distribution vs Data transfer
  150. Distributing tasks to balance the load induces data transfer penalty. StarPU
  151. thus needs to find a balance between both. The target function that the
  152. @code{dmda} scheduler of StarPU
  153. tries to minimize is @code{alpha * T_execution + beta * T_data_transfer}, where
  154. @code{T_execution} is the estimated execution time of the codelet (usually
  155. accurate), and @code{T_data_transfer} is the estimated data transfer time. The
  156. latter is estimated based on bus calibration before execution start,
  157. i.e. with an idle machine, thus without contention. You can force bus re-calibration by running
  158. @code{starpu_calibrate_bus}. The beta parameter defaults to 1, but it can be
  159. worth trying to tweak it by using @code{export STARPU_SCHED_BETA=2} for instance,
  160. since during real application execution, contention makes transfer times bigger.
  161. This is of course imprecise, but in practice, a rough estimation already gives
  162. the good results that a precise estimation would give.
  163. @node Data prefetch
  164. @section Data prefetch
  165. The @code{heft}, @code{dmda} and @code{pheft} scheduling policies perform data prefetch (see @ref{STARPU_PREFETCH}):
  166. as soon as a scheduling decision is taken for a task, requests are issued to
  167. transfer its required data to the target processing unit, if needeed, so that
  168. when the processing unit actually starts the task, its data will hopefully be
  169. already available and it will not have to wait for the transfer to finish.
  170. The application may want to perform some manual prefetching, for several reasons
  171. such as excluding initial data transfers from performance measurements, or
  172. setting up an initial statically-computed data distribution on the machine
  173. before submitting tasks, which will thus guide StarPU toward an initial task
  174. distribution (since StarPU will try to avoid further transfers).
  175. This can be achieved by giving the @code{starpu_data_prefetch_on_node} function
  176. the handle and the desired target memory node.
  177. @node Power-based scheduling
  178. @section Power-based scheduling
  179. If the application can provide some power performance model (through
  180. the @code{power_model} field of the codelet structure), StarPU will
  181. take it into account when distributing tasks. The target function that
  182. the @code{dmda} scheduler minimizes becomes @code{alpha * T_execution +
  183. beta * T_data_transfer + gamma * Consumption} , where @code{Consumption}
  184. is the estimated task consumption in Joules. To tune this parameter, use
  185. @code{export STARPU_SCHED_GAMMA=3000} for instance, to express that each Joule
  186. (i.e kW during 1000us) is worth 3000us execution time penalty. Setting
  187. @code{alpha} and @code{beta} to zero permits to only take into account power consumption.
  188. This is however not sufficient to correctly optimize power: the scheduler would
  189. simply tend to run all computations on the most energy-conservative processing
  190. unit. To account for the consumption of the whole machine (including idle
  191. processing units), the idle power of the machine should be given by setting
  192. @code{export STARPU_IDLE_POWER=200} for 200W, for instance. This value can often
  193. be obtained from the machine power supplier.
  194. The power actually consumed by the total execution can be displayed by setting
  195. @code{export STARPU_PROFILING=1 STARPU_WORKER_STATS=1} .
  196. @node Profiling
  197. @section Profiling
  198. A quick view of how many tasks each worker has executed can be obtained by setting
  199. @code{export STARPU_WORKER_STATS=1} This is a convenient way to check that
  200. execution did happen on accelerators without penalizing performance with
  201. the profiling overhead.
  202. A quick view of how much data transfers have been issued can be obtained by setting
  203. @code{export STARPU_BUS_STATS=1} .
  204. More detailed profiling information can be enabled by using @code{export STARPU_PROFILING=1} or by
  205. calling @code{starpu_profiling_status_set} from the source code.
  206. Statistics on the execution can then be obtained by using @code{export
  207. STARPU_BUS_STATS=1} and @code{export STARPU_WORKER_STATS=1} .
  208. More details on performance feedback are provided by the next chapter.
  209. @node CUDA-specific optimizations
  210. @section CUDA-specific optimizations
  211. Due to CUDA limitations, StarPU will have a hard time overlapping its own
  212. communications and the codelet computations if the application does not use a
  213. dedicated CUDA stream for its computations. StarPU provides one by the use of
  214. @code{starpu_cuda_get_local_stream()} which should be used by all CUDA codelet
  215. operations. For instance:
  216. @cartouche
  217. @smallexample
  218. func <<<grid,block,0,starpu_cuda_get_local_stream()>>> (foo, bar);
  219. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  220. @end smallexample
  221. @end cartouche
  222. StarPU already does appropriate calls for the CUBLAS library.
  223. Unfortunately, some CUDA libraries do not have stream variants of
  224. kernels. That will lower the potential for overlapping.