1.9. Optimize for speed¶
With the growth of our atomic database and the complexity of our models, SPEX does not always run as fast as it used to. We are doing our best to develop models that have a high accuracy and flexibility, but run within a reasonable time. This does not always work out, unfortunately. On this page, we provide some tips to make SPEX run as fast as possible.
1.9.1. Optimize multi-processing¶
SPEX uses a multi-processing library for both folding the model spectrum with the response matrix and matrix operations in various models. In our experience, these calculations are the fastest when using only 4 CPU cores. However, SPEX does not (yet) select 4 cores by default.
On machines with more cores, SPEX tries to use the full number of cores. In that case, the overhead of dividing the work over the cores becomes large and the calculation slows down. It is therefore best to set the number of cores to be used to a more optimal value. In most cases, this is 4.
You can set the number of used cores using the environment variable OMP_NUM_THREADS
.
Please set this parameter in the terminal before you use SPEX. In bash shells,
this is the command:
export OMP_NUM_THREADS=4
and in c-shell:
setenv OMP_NUM_THREADS 4
1.9.2. Processor architecture and compilers¶
In the last decade, SPEX binary distributions have been mostly compiled with the Intel Fortran compiler. This compiler is one of the best to get optimized code for Intel processors. Executables produced with this compiler are generally faster than code compiled with the open-source gfortran compiler.
Since Apple switched to using Apple Silicon processors, replacing the Intel chip, the Intel Fortran compiler is no longer the best to use on Mac. Intel compiled binaries usually run using Intel emulation, but this is not always stable. Therefore, we switched to using gfortran, which is slightly slower.
Depending on the hardware that you have access to, it may be more beneficial to run on Linux Intel machines. If you think SPEX runs slow on your Mac, then doing a test on a Linux machine may help determining which is the best to use.
1.9.2.1. Anaconda¶
We also started distributing SPEX as an Anaconda package. This is an easy way to install SPEX, because it automatically takes care of all the dependencies. This also means, however, that we need to compile with the gfortran compiler. The SPEX binaries in the Anaconda distribution are therefore not the fastest. If you really need speed and you are on an Intel machine, it may help to install the Intel compiled version (see How to install SPEX).
1.9.3. Faster model implementations¶
For some of our models, like cie
and neij
, we now have a quick calculation
mode. This mode calculates these models using pre-calculated data. The resulting
models are accurate at a level of a few percent. The quick calculation mode can be
activated with the command var calc qc
(See: Var: various settings for the plasma models).
The best way to use this mode is in the fitting process. When your parameters are
still far away from the optimum, the quick mode helps to find the optimum faster.
When you are close to the best fit, it is best to switch to var calc new
and
fit again. This should be your best result. For the error calculations, you can
switch back to var calc qc
. The errors derived using qc
should be
statistically the same as the errors in var calc new
.
In addition, it is also possible to ignore or set a quicker calculation for individual ions. See Optimizing model performance for details.
1.9.4. Python interface¶
Using our Python interface makes it much easier to script SPEX analysis, however, this also comes with a performance cost. For a number of Python functions, data is translated from the Fortran part of SPEX to Python, which takes CPU time.
To speed up, it can sometimes be beneficial to call the Fortran command directly
instead of the Python command. For example, s.calc()
calculates the model
and updates the model values in the Python part of SPEX. If you do not need this
data, you can also call s.command('calc')
, which only calculates the model
in the Fortran part.
Since the Python interface consists of a couple of layers on top of the Fortran code, it adds some complexity. This makes it a bit harder to analyse performance issues.
1.9.4.1. Multiple instances of PYSPEX¶
The method that we use to compile the Fortran to Python interface (F2PY) unfortunately does not allow one to have multiple instances of the SPEX program in one script. It is only possible to run SPEX in parallel when they are started as different processes. We provide a brief guide how to set this up: Multiprocessing with PYSPEX.
If you are experiencing performance issues that are not solved by the tips above, then please let us know through our issue tracker.