| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 | @c -*-texinfo-*-@c This file is part of the StarPU Handbook.@c Copyright (C) 2009--2011  Universit@'e de Bordeaux 1@c Copyright (C) 2010, 2011  Centre National de la Recherche Scientifique@c Copyright (C) 2011 Institut National de Recherche en Informatique et Automatique@c See the file starpu.texi for copying conditions.@node Performance optimization@chapter How to optimize performance with StarPUTODO: improve!@menu* Data management::* Task submission::* Task priorities::* Task scheduling policy::* Performance model calibration::* Task distribution vs Data transfer::* Data prefetch::* Power-based scheduling::* Profiling::* CUDA-specific optimizations::@end menuSimply encapsulating application kernels into tasks already permits toseamlessly support CPU and GPUs at the same time. To achieve good performance, afew additional changes are needed.@node Data management@section Data managementWhen the application allocates data, whenever possible it should use the@code{starpu_malloc} function, which will ask CUDA orOpenCL to make the allocation itself and pin the corresponding allocatedmemory. This is needed to permit asynchronous data transfer, i.e. permit datatransfer to overlap with computations. Otherwise, the trace will show that the@code{DriverCopyAsync} state takes a lot of time, this is because CUDA or OpenCLthen reverts to synchronous transfers.By default, StarPU leaves replicates of data wherever they were used, in case theywill be re-used by other tasks, thus saving the data transfer time. When sometask modifies some data, all the other replicates are invalidated, and only theprocessing unit which ran that task will have a valid replicate of the data. If the application knowsthat this data will not be re-used by further tasks, it should advise StarPU toimmediately replicate it to a desired list of memory nodes (given through abitmask). This can be understood like the write-through mode of CPU caches.@examplestarpu_data_set_wt_mask(img_handle, 1<<0);@end examplewill for instance request to always automatically transfer a replicate into themain memory (node 0), as bit 0 of the write-through bitmask is being set.@examplestarpu_data_set_wt_mask(img_handle, ~0U);@end examplewill request to always automatically broadcast the updated data to all memorynodes.@node Task submission@section Task submissionTo let StarPU make online optimizations, tasks should be submittedasynchronously as much as possible. Ideally, all the tasks should besubmitted, and mere calls to @code{starpu_task_wait_for_all} or@code{starpu_data_unregister} be done to wait fortermination. StarPU will then be able to rework the whole schedule, overlapcomputation with communication, manage accelerator local memory usage, etc.@node Task priorities@section Task prioritiesBy default, StarPU will consider the tasks in the order they are submitted bythe application. If the application programmer knows that some tasks shouldbe performed in priority (for instance because their output is needed by manyother tasks and may thus be a bottleneck if not executed early enough), the@code{priority} field of the task structure should be set to transmit thepriority information to StarPU.@node Task scheduling policy@section Task scheduling policyBy default, StarPU uses the @code{eager} simple greedy scheduler. This isbecause it provides correct load balance even if the application codelets do nothave performance models. If your application codelets have performance models(@pxref{Performance model example} for examples showing how to do it),you should change the scheduler thanks to the @code{STARPU_SCHED} environmentvariable. For instance @code{export STARPU_SCHED=dmda} . Use @code{help} to getthe list of available schedulers.The @b{eager} scheduler uses a central task queue, from which workers draw tasksto work on. This however does not permit to prefetch data since the schedulingdecision is taken late. If a task has a non-0 priority, it is put at the front of the queue.The @b{prio} scheduler also uses a central task queue, but sorts tasks bypriority (between -5 and 5).The @b{random} scheduler distributes tasks randomly according to assumed workeroverall performance.The @b{ws} (work stealing) scheduler schedules tasks on the local worker bydefault. When a worker becomes idle, it steals a task from the most loadedworker.The @b{dm} (deque model) scheduler uses task execution performance models into account toperform an HEFT-similar scheduling strategy: it schedules tasks where theirtermination time will be minimal.The @b{dmda} (deque model data aware) scheduler is similar to dm, it also takesinto account data transfer time.The @b{dmdar} (deque model data aware ready) scheduler is similar to dmda,it also sorts tasks on per-worker queues by number of already-available databuffers.The @b{dmdas} (deque model data aware sorted) scheduler is similar to dmda, italso supports arbitrary priority values.The @b{heft} (HEFT) scheduler is similar to dmda, it also supports task bundles.The @b{pheft} (parallel HEFT) scheduler is similar to heft, it also supportsparallel tasks (still experimental).The @b{pgreedy} (parallel greedy) scheduler is similar to greedy, it alsosupports parallel tasks (still experimental).@node Performance model calibration@section Performance model calibrationMost schedulers are based on an estimation of codelet duration on each kindof processing unit. For this to be possible, the application programmer needsto configure a performance model for the codelets of the application (see@ref{Performance model example} for instance). History-based performance modelsuse on-line calibration.  StarPU will automatically calibrate codeletswhich have never been calibrated yet, and save the result in@code{~/.starpu/sampling/codelets}.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@code{export STARPU_CALIBRATE=1} . This may be necessary if your applicationhas not-so-stable performance. StarPU will force calibration (and thus ignorethe current result) until 10 (_STARPU_CALIBRATION_MINIMUM) measurements have beenmade on each architecture, to avoid badly scheduling tasks just because thefirst measurements were not so good. Details on the current performance model statuscan be obtained from the @code{starpu_perfmodel_display} command: the @code{-l}option lists the available performance models, and the @code{-s} option permitsto choose the performance model to be displayed. The result looks like:@example$ starpu_perfmodel_display -s starpu_dlu_lu_model_22performance model for cpu# hash    size     mean          dev           n880805ba  98304    2.731309e+02  6.010210e+01  1240b50b6605  393216   1.469926e+03  1.088828e+02  12405c6c3401  1572864  1.125983e+04  3.265296e+03  1240@end exampleWhich shows that for the LU 22 kernel with a 1.5MiB matrix, the averageexecution time on CPUs was about 12ms, with a 2ms standard deviation, over1240 samples. It is a good idea to check this before doing actual performancemeasurements.A graph can be drawn by using the @code{starpu_perfmodel_plot}:@example$ starpu_perfmodel_plot -s starpu_dlu_lu_model_2298304 393216 1572864 $ gnuplot starpu_starpu_dlu_lu_model_22.gp$ gv starpu_starpu_dlu_lu_model_22.eps@end exampleIf a kernel source code was modified (e.g. performance improvement), thecalibration information is stale and should be dropped, to re-calibrate fromstart. This can be done by using @code{export STARPU_CALIBRATE=2}.Note: due to CUDA limitations, to be able to measure kernel duration,calibration mode needs to disable asynchronous data transfers. Calibration thusdisables data transfer / computation overlapping, and should thus not be usedfor eventual benchmarks. Note 2: history-based performance models get calibratedonly if a performance-model-based scheduler is chosen.@node Task distribution vs Data transfer@section Task distribution vs Data transferDistributing tasks to balance the load induces data transfer penalty. StarPUthus needs to find a balance between both. The target function that the@code{dmda} scheduler of StarPUtries to minimize is @code{alpha * T_execution + beta * T_data_transfer}, where@code{T_execution} is the estimated execution time of the codelet (usuallyaccurate), and @code{T_data_transfer} is the estimated data transfer time. Thelatter is estimated based on bus calibration before execution start,i.e. with an idle machine, thus without contention. You can force bus re-calibration by running@code{starpu_calibrate_bus}. The beta parameter defaults to 1, but it can beworth trying to tweak it by using @code{export STARPU_BETA=2} for instance,since during real application execution, contention makes transfer times bigger.This is of course imprecise, but in practice, a rough estimation already givesthe good results that a precise estimation would give.@node Data prefetch@section Data prefetchThe @code{heft}, @code{dmda} and @code{pheft} scheduling policies perform data prefetch (see @ref{STARPU_PREFETCH}):as soon as a scheduling decision is taken for a task, requests are issued totransfer its required data to the target processing unit, if needeed, so thatwhen the processing unit actually starts the task, its data will hopefully bealready available and it will not have to wait for the transfer to finish.The application may want to perform some manual prefetching, for several reasonssuch as excluding initial data transfers from performance measurements, orsetting up an initial statically-computed data distribution on the machinebefore submitting tasks, which will thus guide StarPU toward an initial taskdistribution (since StarPU will try to avoid further transfers).This can be achieved by giving the @code{starpu_data_prefetch_on_node} functionthe handle and the desired target memory node.@node Power-based scheduling@section Power-based schedulingIf the application can provide some power performance model (throughthe @code{power_model} field of the codelet structure), StarPU willtake it into account when distributing tasks. The target function thatthe @code{dmda} scheduler minimizes becomes @code{alpha * T_execution +beta * T_data_transfer + gamma * Consumption} , where @code{Consumption}is the estimated task consumption in Joules. To tune this parameter, use@code{export STARPU_GAMMA=3000} for instance, to express that each Joule(i.e kW during 1000us) is worth 3000us execution time penalty. Setting@code{alpha} and @code{beta} to zero permits to only take into account power consumption.This is however not sufficient to correctly optimize power: the scheduler wouldsimply tend to run all computations on the most energy-conservative processingunit. To account for the consumption of the whole machine (including idleprocessing units), the idle power of the machine should be given by setting@code{export STARPU_IDLE_POWER=200} for 200W, for instance. This value can oftenbe obtained from the machine power supplier.The power actually consumed by the total execution can be displayed by setting@code{export STARPU_PROFILING=1 STARPU_WORKER_STATS=1} .@node Profiling@section ProfilingA quick view of how many tasks each worker has executed can be obtained by setting @code{export STARPU_WORKER_STATS=1} This is a convenient way to check thatexecution did happen on accelerators without penalizing performance withthe profiling overhead.A quick view of how much data transfers have been issued can be obtained by setting @code{export STARPU_BUS_STATS=1} .More detailed profiling information can be enabled by using @code{export STARPU_PROFILING=1} or bycalling @code{starpu_profiling_status_set} from the source code.Statistics on the execution can then be obtained by using @code{exportSTARPU_BUS_STATS=1} and @code{export STARPU_WORKER_STATS=1} . More details on performance feedback are provided by the next chapter.@node CUDA-specific optimizations@section CUDA-specific optimizationsDue to CUDA limitations, StarPU will have a hard time overlapping its owncommunications and the codelet computations if the application does not use adedicated CUDA stream for its computations. StarPU provides one by the use of@code{starpu_cuda_get_local_stream()} which should be used by all CUDA codeletoperations. For instance:@examplefunc <<<grid,block,0,starpu_cuda_get_local_stream()>>> (foo, bar);cudaStreamSynchronize(starpu_cuda_get_local_stream());@end exampleStarPU already does appropriate calls for the CUBLAS library.Unfortunately, some CUDA libraries do not have stream variants ofkernels. That will lower the potential for overlapping.
 |