perf-optimization.texi 19 KB

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