perf-optimization.texi 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 granularity::
  11. * Task submission::
  12. * Task priorities::
  13. * Task scheduling policy::
  14. * Task scheduling contexts::
  15. * Performance model calibration::
  16. * Task distribution vs Data transfer::
  17. * Data prefetch::
  18. * Power-based scheduling::
  19. * Profiling::
  20. * CUDA-specific optimizations::
  21. * Performance debugging::
  22. @end menu
  23. Simply encapsulating application kernels into tasks already permits to
  24. seamlessly support CPU and GPUs at the same time. To achieve good performance, a
  25. few additional changes are needed.
  26. @node Data management
  27. @section Data management
  28. When the application allocates data, whenever possible it should use the
  29. @code{starpu_malloc} function, which will ask CUDA or
  30. OpenCL to make the allocation itself and pin the corresponding allocated
  31. memory. This is needed to permit asynchronous data transfer, i.e. permit data
  32. transfer to overlap with computations. Otherwise, the trace will show that the
  33. @code{DriverCopyAsync} state takes a lot of time, this is because CUDA or OpenCL
  34. then reverts to synchronous transfers.
  35. By default, StarPU leaves replicates of data wherever they were used, in case they
  36. will be re-used by other tasks, thus saving the data transfer time. When some
  37. task modifies some data, all the other replicates are invalidated, and only the
  38. processing unit which ran that task will have a valid replicate of the data. If the application knows
  39. that this data will not be re-used by further tasks, it should advise StarPU to
  40. immediately replicate it to a desired list of memory nodes (given through a
  41. bitmask). This can be understood like the write-through mode of CPU caches.
  42. @cartouche
  43. @smallexample
  44. starpu_data_set_wt_mask(img_handle, 1<<0);
  45. @end smallexample
  46. @end cartouche
  47. will for instance request to always automatically transfer a replicate into the
  48. main memory (node 0), as bit 0 of the write-through bitmask is being set.
  49. @cartouche
  50. @smallexample
  51. starpu_data_set_wt_mask(img_handle, ~0U);
  52. @end smallexample
  53. @end cartouche
  54. will request to always automatically broadcast the updated data to all memory
  55. nodes.
  56. Setting the write-through mask to @code{~0U} can also be useful to make sure all
  57. memory nodes always have a copy of the data, so that it is never evicted when
  58. memory gets scarse.
  59. Implicit data dependency computation can become expensive if a lot
  60. of tasks access the same piece of data. If no dependency is required
  61. on some piece of data (e.g. because it is only accessed in read-only
  62. mode, or because write accesses are actually commutative), use the
  63. @code{starpu_data_set_sequential_consistency_flag} function to disable implicit
  64. dependencies on that data.
  65. In the same vein, accumulation of results in the same data can become a
  66. bottleneck. The use of the @code{STARPU_REDUX} mode permits to optimize such
  67. accumulation (@pxref{Data reduction}).
  68. @node Task granularity
  69. @section Task granularity
  70. Like any other runtime, StarPU has some overhead to manage tasks. Since
  71. it does smart scheduling and data management, that overhead is not always
  72. neglectable. The order of magnitude of the overhead is typically a couple of
  73. microseconds. The amount of work that a task should do should thus be somewhat
  74. bigger, to make sure that the overhead becomes neglectible. The offline
  75. performance feedback can provide a measure of task length, which should thus be
  76. checked if bad performance are observed.
  77. @node Task submission
  78. @section Task submission
  79. To let StarPU make online optimizations, tasks should be submitted
  80. asynchronously as much as possible. Ideally, all the tasks should be
  81. submitted, and mere calls to @code{starpu_task_wait_for_all} or
  82. @code{starpu_data_unregister} be done to wait for
  83. termination. StarPU will then be able to rework the whole schedule, overlap
  84. computation with communication, manage accelerator local memory usage, etc.
  85. @node Task priorities
  86. @section Task priorities
  87. By default, StarPU will consider the tasks in the order they are submitted by
  88. the application. If the application programmer knows that some tasks should
  89. be performed in priority (for instance because their output is needed by many
  90. other tasks and may thus be a bottleneck if not executed early enough), the
  91. @code{priority} field of the task structure should be set to transmit the
  92. priority information to StarPU.
  93. @node Task scheduling policy
  94. @section Task scheduling policy
  95. By default, StarPU uses the @code{eager} simple greedy scheduler. This is
  96. because it provides correct load balance even if the application codelets do not
  97. have performance models. If your application codelets have performance models
  98. (@pxref{Performance model example} for examples showing how to do it),
  99. you should change the scheduler thanks to the @code{STARPU_SCHED} environment
  100. variable. For instance @code{export STARPU_SCHED=dmda} . Use @code{help} to get
  101. the list of available schedulers.
  102. The @b{eager} scheduler uses a central task queue, from which workers draw tasks
  103. to work on. This however does not permit to prefetch data since the scheduling
  104. decision is taken late. If a task has a non-0 priority, it is put at the front of the queue.
  105. The @b{prio} scheduler also uses a central task queue, but sorts tasks by
  106. priority (between -5 and 5).
  107. The @b{random} scheduler distributes tasks randomly according to assumed worker
  108. overall performance.
  109. The @b{ws} (work stealing) scheduler schedules tasks on the local worker by
  110. default. When a worker becomes idle, it steals a task from the most loaded
  111. worker.
  112. The @b{dm} (deque model) scheduler uses task execution performance models into account to
  113. perform an HEFT-similar scheduling strategy: it schedules tasks where their
  114. termination time will be minimal.
  115. The @b{dmda} (deque model data aware) scheduler is similar to dm, it also takes
  116. into account data transfer time.
  117. The @b{dmdar} (deque model data aware ready) scheduler is similar to dmda,
  118. it also sorts tasks on per-worker queues by number of already-available data
  119. buffers.
  120. The @b{dmdas} (deque model data aware sorted) scheduler is similar to dmda, it
  121. also supports arbitrary priority values.
  122. The @b{heft} (heterogeneous earliest finish time) scheduler is similar to dmda, it also supports task bundles.
  123. The @b{pheft} (parallel HEFT) scheduler is similar to heft, it also supports
  124. parallel tasks (still experimental).
  125. The @b{pgreedy} (parallel greedy) scheduler is similar to greedy, it also
  126. supports parallel tasks (still experimental).
  127. @node Task scheduling contexts
  128. @section Task scheduling contexts
  129. Task scheduling contexts represent abstracts sets of workers that allow the programmers to control the distribution of computational resources (i.e. CPUs and
  130. GPUs) to concurrent parallel kernels. The main goal is to minimize interferences between the execution of multiple parallel kernels, by partitioning the underlying pool of workers using contexts.
  131. By default, the application submits tasks to an initial context, which disposes of all the computation ressources available to StarPU (all the workers).
  132. If the application programmer plans to launch several parallel kernels simultaneusly, by default these kernels will be executed within this initial context, using a single scheduler policy(@pxref{Task scheduling policy}).
  133. Meanwhile, if the application programmer is aware of the demands of these kernels and of the specificity of the machine used to execute them, the workers can be divided between several contexts.
  134. These scheduling contexts will isolate the execution of each kernel and they will permit the use of a scheduling policy proper to each one of them.
  135. In order to create the contexts, you have to know the indentifiers of the workers running within StarPU.
  136. By passing a set of workers together with the scheduling policy to the function @code{starpu_create_sched_ctx}, you will get an identifier of the context created which you will use to indicate the context you want to submit the tasks to.
  137. @cartouche
  138. @smallexample
  139. /* @b{the list of ressources the context will manage} */
  140. int workerids[3] = @{1, 3, 10@};
  141. /* @b{indicate the scheduling policy to be used within the context, the list of
  142. workers assigned to it, the number of workers, the name of the context} */
  143. int id_ctx = starpu_create_sched_ctx("heft", workerids, 3, "my_ctx");
  144. /* @b{let StarPU know that the folowing tasks will be submitted to this context} */
  145. starpu_set_sched_ctx(id);
  146. /* @b{submit the task to StarPU} */
  147. starpu_task_submit(task);
  148. @end smallexample
  149. @end cartouche
  150. Note: Parallel greedy and parallel heft scheduling policies do not support the existence of several disjoint contexts on the machine.
  151. Combined workers are constructed depending on the entire topology of the machine, not only the one belonging to a context.
  152. @node Performance model calibration
  153. @section Performance model calibration
  154. Most schedulers are based on an estimation of codelet duration on each kind
  155. of processing unit. For this to be possible, the application programmer needs
  156. to configure a performance model for the codelets of the application (see
  157. @ref{Performance model example} for instance). History-based performance models
  158. use on-line calibration. StarPU will automatically calibrate codelets
  159. which have never been calibrated yet, and save the result in
  160. @code{~/.starpu/sampling/codelets}.
  161. 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
  162. @code{export STARPU_CALIBRATE=1} . This may be necessary if your application
  163. has not-so-stable performance. StarPU will force calibration (and thus ignore
  164. the current result) until 10 (_STARPU_CALIBRATION_MINIMUM) measurements have been
  165. made on each architecture, to avoid badly scheduling tasks just because the
  166. first measurements were not so good. Details on the current performance model status
  167. can be obtained from the @code{starpu_perfmodel_display} command: the @code{-l}
  168. option lists the available performance models, and the @code{-s} option permits
  169. to choose the performance model to be displayed. The result looks like:
  170. @example
  171. $ starpu_perfmodel_display -s starpu_dlu_lu_model_22
  172. performance model for cpu
  173. # hash size mean dev n
  174. 880805ba 98304 2.731309e+02 6.010210e+01 1240
  175. b50b6605 393216 1.469926e+03 1.088828e+02 1240
  176. 5c6c3401 1572864 1.125983e+04 3.265296e+03 1240
  177. @end example
  178. Which shows that for the LU 22 kernel with a 1.5MiB matrix, the average
  179. execution time on CPUs was about 11ms, with a 3ms standard deviation, over
  180. 1240 samples. It is a good idea to check this before doing actual performance
  181. measurements.
  182. A graph can be drawn by using the @code{starpu_perfmodel_plot}:
  183. @example
  184. $ starpu_perfmodel_plot -s starpu_dlu_lu_model_22
  185. 98304 393216 1572864
  186. $ gnuplot starpu_starpu_dlu_lu_model_22.gp
  187. $ gv starpu_starpu_dlu_lu_model_22.eps
  188. @end example
  189. If a kernel source code was modified (e.g. performance improvement), the
  190. calibration information is stale and should be dropped, to re-calibrate from
  191. start. This can be done by using @code{export STARPU_CALIBRATE=2}.
  192. Note: due to CUDA limitations, to be able to measure kernel duration,
  193. calibration mode needs to disable asynchronous data transfers. Calibration thus
  194. disables data transfer / computation overlapping, and should thus not be used
  195. for eventual benchmarks. Note 2: history-based performance models get calibrated
  196. only if a performance-model-based scheduler is chosen.
  197. @node Task distribution vs Data transfer
  198. @section Task distribution vs Data transfer
  199. Distributing tasks to balance the load induces data transfer penalty. StarPU
  200. thus needs to find a balance between both. The target function that the
  201. @code{dmda} scheduler of StarPU
  202. tries to minimize is @code{alpha * T_execution + beta * T_data_transfer}, where
  203. @code{T_execution} is the estimated execution time of the codelet (usually
  204. accurate), and @code{T_data_transfer} is the estimated data transfer time. The
  205. latter is estimated based on bus calibration before execution start,
  206. i.e. with an idle machine, thus without contention. You can force bus re-calibration by running
  207. @code{starpu_calibrate_bus}. The beta parameter defaults to 1, but it can be
  208. worth trying to tweak it by using @code{export STARPU_SCHED_BETA=2} for instance,
  209. since during real application execution, contention makes transfer times bigger.
  210. This is of course imprecise, but in practice, a rough estimation already gives
  211. the good results that a precise estimation would give.
  212. @node Data prefetch
  213. @section Data prefetch
  214. The @code{heft}, @code{dmda} and @code{pheft} scheduling policies perform data prefetch (see @ref{STARPU_PREFETCH}):
  215. as soon as a scheduling decision is taken for a task, requests are issued to
  216. transfer its required data to the target processing unit, if needeed, so that
  217. when the processing unit actually starts the task, its data will hopefully be
  218. already available and it will not have to wait for the transfer to finish.
  219. The application may want to perform some manual prefetching, for several reasons
  220. such as excluding initial data transfers from performance measurements, or
  221. setting up an initial statically-computed data distribution on the machine
  222. before submitting tasks, which will thus guide StarPU toward an initial task
  223. distribution (since StarPU will try to avoid further transfers).
  224. This can be achieved by giving the @code{starpu_data_prefetch_on_node} function
  225. the handle and the desired target memory node.
  226. @node Power-based scheduling
  227. @section Power-based scheduling
  228. If the application can provide some power performance model (through
  229. the @code{power_model} field of the codelet structure), StarPU will
  230. take it into account when distributing tasks. The target function that
  231. the @code{dmda} scheduler minimizes becomes @code{alpha * T_execution +
  232. beta * T_data_transfer + gamma * Consumption} , where @code{Consumption}
  233. is the estimated task consumption in Joules. To tune this parameter, use
  234. @code{export STARPU_SCHED_GAMMA=3000} for instance, to express that each Joule
  235. (i.e kW during 1000us) is worth 3000us execution time penalty. Setting
  236. @code{alpha} and @code{beta} to zero permits to only take into account power consumption.
  237. This is however not sufficient to correctly optimize power: the scheduler would
  238. simply tend to run all computations on the most energy-conservative processing
  239. unit. To account for the consumption of the whole machine (including idle
  240. processing units), the idle power of the machine should be given by setting
  241. @code{export STARPU_IDLE_POWER=200} for 200W, for instance. This value can often
  242. be obtained from the machine power supplier.
  243. The power actually consumed by the total execution can be displayed by setting
  244. @code{export STARPU_PROFILING=1 STARPU_WORKER_STATS=1} .
  245. @node Profiling
  246. @section Profiling
  247. A quick view of how many tasks each worker has executed can be obtained by setting
  248. @code{export STARPU_WORKER_STATS=1} This is a convenient way to check that
  249. execution did happen on accelerators without penalizing performance with
  250. the profiling overhead.
  251. A quick view of how much data transfers have been issued can be obtained by setting
  252. @code{export STARPU_BUS_STATS=1} .
  253. More detailed profiling information can be enabled by using @code{export STARPU_PROFILING=1} or by
  254. calling @code{starpu_profiling_status_set} from the source code.
  255. Statistics on the execution can then be obtained by using @code{export
  256. STARPU_BUS_STATS=1} and @code{export STARPU_WORKER_STATS=1} .
  257. More details on performance feedback are provided by the next chapter.
  258. @node CUDA-specific optimizations
  259. @section CUDA-specific optimizations
  260. Due to CUDA limitations, StarPU will have a hard time overlapping its own
  261. communications and the codelet computations if the application does not use a
  262. dedicated CUDA stream for its computations. StarPU provides one by the use of
  263. @code{starpu_cuda_get_local_stream()} which should be used by all CUDA codelet
  264. operations. For instance:
  265. @cartouche
  266. @smallexample
  267. func <<<grid,block,0,starpu_cuda_get_local_stream()>>> (foo, bar);
  268. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  269. @end smallexample
  270. @end cartouche
  271. StarPU already does appropriate calls for the CUBLAS library.
  272. Unfortunately, some CUDA libraries do not have stream variants of
  273. kernels. That will lower the potential for overlapping.
  274. @node Performance debugging
  275. @section Performance debugging
  276. To get an idea of what is happening, a lot of performance feedback is available,
  277. detailed in the next chapter. The various informations should be checked for.
  278. @itemize
  279. @item What does the Gantt diagram look like? (see @ref{Gantt diagram})
  280. @itemize
  281. @item If it's mostly green (tasks running in the initial context) or context specific
  282. color prevailing, then the machine is properly
  283. utilized, and perhaps the codelets are just slow. Check their performance, see
  284. @ref{Codelet performance}.
  285. @item If it's mostly purple (FetchingInput), tasks keep waiting for data
  286. transfers, do you perhaps have far more communication than computation? Did
  287. you properly use CUDA streams to make sure communication can be
  288. overlapped? Did you use data-locality aware schedulers to avoid transfers as
  289. much as possible?
  290. @item If it's mostly red (Blocked), tasks keep waiting for dependencies,
  291. do you have enough parallelism? It might be a good idea to check what the DAG
  292. looks like (see @ref{DAG}).
  293. @item If only some workers are completely red (Blocked), for some reason the
  294. scheduler didn't assign tasks to them. Perhaps the performance model is bogus,
  295. check it (see @ref{Codelet performance}). Do all your codelets have a
  296. performance model? When some of them don't, the schedulers switches to a
  297. greedy algorithm which thus performs badly.
  298. @end itemize
  299. @end itemize