Timings and parallelization

This section summarizes the options DFTK offers to monitor and influence performance of the code.

Timing measurements

By default DFTK uses TimerOutputs.jl to record timings, memory allocations and the number of calls for selected routines inside the code. These numbers are accessible in the object DFTK.timer. Since the timings are automatically accumulated inside this datastructure, any timing measurement should first reset this timer before running the calculation of interest.

For example to measure the timing of an SCF:

DFTK.reset_timer!(DFTK.timer)
scfres = self_consistent_field(basis, tol=1e-8)

DFTK.timer
 ────────────────────────────────────────────────────────────────────────────────
                                        Time                    Allocations      
                               ───────────────────────   ────────────────────────
       Tot / % measured:            1.47s /  26.9%            214MiB /  73.7%    

 Section               ncalls     time    %tot     avg     alloc    %tot      avg
 ────────────────────────────────────────────────────────────────────────────────
 self_consistent_field      1    395ms   99.8%   395ms    157MiB   99.7%   157MiB
   compute_density          6    274ms   69.4%  45.7ms    124MiB   78.7%  20.7MiB
     symmetrize_ρ           6    263ms   66.6%  43.9ms    123MiB   78.2%  20.6MiB
   LOBPCG                  12   82.4ms   20.9%  6.87ms   12.6MiB    8.0%  1.05MiB
     DftHamiltonian...     42   57.9ms   14.6%  1.38ms   3.43MiB    2.2%  83.7KiB
       local+kinetic      282   53.2ms   13.4%   188μs    220KiB    0.1%     800B
       nonlocal            42   1.27ms    0.3%  30.2μs    831KiB    0.5%  19.8KiB
     ortho! X vs Y         48   10.5ms    2.7%   219μs   3.25MiB    2.1%  69.3KiB
       ortho!             100   4.26ms    1.1%  42.6μs    858KiB    0.5%  8.58KiB
     rayleigh_ritz         30   4.71ms    1.2%   157μs   1.05MiB    0.7%  35.7KiB
     preconditioning       42   1.13ms    0.3%  26.9μs    388KiB    0.2%  9.24KiB
     ortho!                12    701μs    0.2%  58.4μs   74.6KiB    0.0%  6.22KiB
   energy_hamiltonian      13   33.0ms    8.3%  2.54ms   16.6MiB   10.5%  1.28MiB
     ene_ops               13   25.8ms    6.5%  1.98ms   10.5MiB    6.7%   828KiB
       ene_ops: xc         13   18.9ms    4.8%  1.46ms   3.06MiB    1.9%   241KiB
       ene_ops: har...     13   3.78ms    1.0%   291μs   5.86MiB    3.7%   461KiB
       ene_ops: non...     13   1.06ms    0.3%  81.6μs    131KiB    0.1%  10.1KiB
       ene_ops: local      13    809μs    0.2%  62.2μs   1.23MiB    0.8%  96.8KiB
       ene_ops: kin...     13    566μs    0.1%  43.5μs    101KiB    0.1%  7.80KiB
   QR orthonormaliz...     12    223μs    0.1%  18.6μs    157KiB    0.1%  13.1KiB
   χ0Mixing                 6    112μs    0.0%  18.8μs   33.8KiB    0.0%  5.62KiB
 guess_density              1    678μs    0.2%   678μs    506KiB    0.3%   506KiB
 ────────────────────────────────────────────────────────────────────────────────

The output produced when printing or displaying the DFTK.timer now shows a nice table summarising total time and allocations as well as a breakdown over individual routines.

Timing measurements and stack traces

Timing measurements have the unfortunate disadvantage that they alter the way stack traces look making it sometimes harder to find errors when debugging. For this reason timing measurements can be disabled completely (i.e. not even compiled into the code) by setting the environment variable DFTK_TIMING to "0" or "false". For this to take effect recompiling all DFTK (including the precompile cache) is needed.

Options for parallelization

At the moment DFTK offers two ways to parallelize a calculation, firstly shared-memory parallelism using threading and secondly multiprocessing using MPI (via the MPI.jl Julia interface). MPI-based parallelism is currently only over $k$-points, such that it cannot be used for calculations with only a single $k$-point. Otherwise combining both forms of parallelism is possible as well.

The scaling of both forms of parallelism for a number of test cases is demonstrated in the following figure. These values were obtained using DFTK version 0.1.17 and Julia 1.6 and the precise scalings will likely be different depending on architecture, DFTK or Julia version. The rough trends should, however, be similar.

The MPI-based parallelization strategy clearly shows a superior scaling and should be preferred if available.

MPI-based parallelism

Currently DFTK uses MPI to distribute on $k$-points only. This implies that calculations with only a single $k$-point cannot use make use of this. For details on setting up and configuring MPI with Julia see the MPI.jl documentation.

  1. First disable all threading inside DFTK, by adding the following to your script running the DFTK calculation:

    using DFTK
    disable_threading()
  2. Run Julia in parallel using the mpiexecjl wrapper script from MPI.jl:

    mpiexecjl -np 16 julia myscript.jl

    In this -np 16 tells MPI to use 16 processes and -t 1 tells Julia to use one thread only. Notice that we use mpiexecjl to automatically select the mpiexec compatible with the MPI version used by MPI.jl.

As usual with MPI printing will be garbled. You can use

DFTK.mpi_master() || (redirect_stdout(); redirect_stderr())

at the top of your script to disable printing on all processes but one.

MPI-based parallelism not fully supported

While standard procedures (such as the SCF or band structure calculations) fully support MPI, not all routines of DFTK are compatible with MPI yet and will throw an error when being called in an MPI-parallel run. In most cases there is no intrinsic limitation it just has not yet been implemented. If you require MPI in one of our routines, where this is not yet supported, feel free to open an issue on github or otherwise get in touch.

Thread-based parallelism

Threading in DFTK currently happens on multiple layers distributing the workload over different $k$-points, bands or within an FFT or BLAS call between threads. At its current stage our scaling for thread-based parallelism is worse compared MPI-based and therefore the parallelism described here should only be used if no other option exists. To use thread-based parallelism proceed as follows:

  1. Ensure that threading is properly setup inside DFTK by adding to the script running the DFTK calculation:

    using DFTK
    setup_threading()

    This disables FFT threading and sets the number of BLAS threads to the number of Julia threads.

  2. Run Julia passing the desired number of threads using the flag -t:

    julia -t 8 myscript.jl

For some cases (e.g. a single $k$-point, fewish bands and a large FFT grid) it can be advantageous to add threading inside the FFTs as well. One example is the Caffeine calculation in the above scaling plot. In order to do so just call setup_threading(n_fft=2), which will select two FFT threads. More than two FFT threads is rarely useful.

Advanced threading tweaks

The default threading setup done by setup_threading is to select one FFT thread and the same number of BLAS and Julia threads. This section provides some info in case you want to change these defaults.

BLAS threads

All BLAS calls in Julia go through a parallelized OpenBlas or MKL (with MKL.jl. Generally threading in BLAS calls is far from optimal and the default settings can be pretty bad. For example for CPUs with hyper threading enabled, the default number of threads seems to equal the number of virtual cores. Still, BLAS calls typically take second place in terms of the share of runtime they make up (between 10% and 20%). Of note many of these do not take place on matrices of the size of the full FFT grid, but rather only in a subspace (e.g. orthogonalization, Rayleigh-Ritz, ...) such that parallelization is either anyway disabled by the BLAS library or not very effective. To set the number of BLAS threads use

using LinearAlgebra
BLAS.set_num_threads(N)

where N is the number of threads you desire. To check the number of BLAS threads currently used, you can use

Int(ccall((BLAS.@blasfunc(openblas_get_num_threads), BLAS.libblas), Cint, ()))

or (from Julia 1.6) simply BLAS.get_num_threads().

Julia threads

On top of BLAS threading DFTK uses Julia threads (Thread.@threads) in a couple of places to parallelize over $k$-points (density computation) or bands (Hamiltonian application). The number of threads used for these aspects is controlled by the flag -t passed to Julia or the environment variable JULIA_NUM_THREADS. To check the number of Julia threads use Threads.nthreads().

FFT threads

Since FFT threading is only used in DFTK inside the regions already parallelized by Julia threads, setting FFT threads to something larger than 1 is rarely useful if a sensible number of Julia threads has been chosen. Still, to explicitly set the FFT threads use

using FFTW
FFTW.set_num_threads(N)

where N is the number of threads you desire. By default no FFT threads are used, which is almost always the best choice.