This section summarizes the options DFTK offers to monitor and influence performance of the code.
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: 735ms / 18.5% 109MiB / 34.8% Section ncalls time %tot avg alloc %tot avg ────────────────────────────────────────────────────────────────────────────── self_consistent_field 1 135ms 99.5% 135ms 37.6MiB 99.4% 37.6MiB LOBPCG 14 83.0ms 61.1% 5.93ms 11.2MiB 29.7% 822KiB Hamiltonian mu... 40 59.5ms 43.8% 1.49ms 2.90MiB 7.66% 74.2KiB kinetic+local 40 56.1ms 41.3% 1.40ms 269KiB 0.70% 6.73KiB nonlocal 40 1.97ms 1.45% 49.3μs 802KiB 2.07% 20.0KiB ortho 92 4.82ms 3.55% 52.4μs 771KiB 1.99% 8.38KiB rayleigh_ritz 26 4.08ms 3.00% 157μs 871KiB 2.25% 33.5KiB block multipli... 99 2.12ms 1.56% 21.4μs 1.45MiB 3.83% 15.0KiB compute_density 7 23.3ms 17.1% 3.32ms 5.42MiB 14.3% 793KiB energy_hamiltonian 15 21.5ms 15.9% 1.44ms 13.4MiB 35.3% 912KiB ene_ops 15 17.9ms 13.2% 1.19ms 9.19MiB 24.3% 627KiB ene_ops: xc 15 9.26ms 6.82% 617μs 2.86MiB 7.56% 195KiB ene_ops: har... 15 4.18ms 3.08% 279μs 3.97MiB 10.5% 271KiB ene_ops: local 15 1.81ms 1.33% 121μs 1.62MiB 4.27% 110KiB ene_ops: non... 15 1.05ms 0.77% 70.1μs 160KiB 0.41% 10.7KiB ene_ops: kin... 15 915μs 0.67% 61.0μs 563KiB 1.45% 37.5KiB QR orthonormaliz... 14 323μs 0.24% 23.1μs 159KiB 0.41% 11.4KiB SimpleMixing 7 167μs 0.12% 23.8μs 958KiB 2.48% 137KiB guess_density 1 688μs 0.51% 688μs 232KiB 0.60% 232KiB guess_spin_density 1 300ns 0.00% 300ns 0.00B 0.00% 0.00B ──────────────────────────────────────────────────────────────────────────────
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 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
"false". For this to take effect recompiling all DFTK (including the precompile cache) is needed.
Unfortunately measuring timings in
TimerOutputs is not yet thread-safe. Therefore taking timings of threaded parts of the code will be disabled unless you set
"all". In this case you must not use Julia threading (see section below) or otherwise undefined behaviour results.
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.
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.
First disable all threading inside DFTK, by adding the following to your script running the DFTK calculation:
using DFTK disable_threading()
Run Julia in parallel using the
mpiexecjlwrapper script from MPI.jl:
mpiexecjl -np 16 julia myscript.jl
-np 16tells MPI to use 16 processes and
-t 1tells Julia to use one thread only. Notice that we use
mpiexecjlto automatically select the
mpiexeccompatible 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.
Even though MPI-based parallelism shows the better scaling it is still experimental and some routines (e.g. band structure and direct minimization) are not compatible with it yet.
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:
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.
Run Julia passing the desired number of threads using the flag
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.
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.
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)
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
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
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)
N is the number of threads you desire. By default no FFT threads are used, which is almost always the best choice.