Performance Tuning
This chapter describes various steps that can be taken to tune (and thereby hopefully improve) MSG’s computational performance.
Data Caching
The first and most obvious step to improving performance is to make sure that MSG’s data caching is appropriately configured. See the Data Caching chapter for full details.
Photgrid Files
As noted in the Data Files chapter, MSG supports loading a specgrid file and then convolving with a passband on-the-fly. There is a performance penalty to this approach: when photometric data are required and are not already in the cache, the corresponding spectroscopic data must be read from the specgrid file and then convolved. To avoid this penalty, consider instead creating a photgrid file using the specgrid_to_photgrid tool (described in the Grid Tools chapter), and working with this file directly.
Linear Interpolation
By default MSG uses cubic tensor product interpolation, which ensures that both interpolated functions and their first derivatives vary smoothly with respect to the photospheric parameters \(\vx\) (that is, the functions are \(C^{1}\) continuous). If smoothness in the first derivatives is not required, then consider using linear interpolation instead.
This can be achieved by setting the optional order argument to
1. For instance, a call to the function
pymsg.PhotGrid.flux() would look like this:
F = photgrid.flux(x, order=1)
The performance boost from replacing cubic interpolation with linear can be significant, \(\sim 4^N\) where \(N\) is the number of dimensions spanned by the grid.
Interface Language
The Python interface to MSG is by far the most user-friendly, but it has the disadvantage that Python is an interpreted language. While most of the intensive calculations performed by MSG are handled by compiled code, there is inevitably an overhead to accessing this code from Python. If good performance is critical, then consider switching the Fortran interface or the C interface.
Parallelization
In situations where multiple, independent calls to MSG routines are
required, consider using OpenMP
directives to run the calculations in parallel on multiple processor
cores[1]. First, ensure that MSG has been built with the
OMP environment variable set to yes (see the
Installation chapter for further details). Then, call MSG
within a loop with an appropriate OpenMP directive. In Fortran, the
code might look like this:
! Input: photospheric parameters stored in x_vec(:,:)
! Output: photometric flux stored in F(:)
!$OMP PARALLEL DO
do i = 1, SIZE(x_vec, 2)
call photgrid%interp_flux(x_vec(:,i), F(i))
end do
!$OMP END PARALLEL DO
A similar approach can be used in C.
Unfortunately Python doesn’t support this kind of parallelization, due to its global interpreter lock. However, it still benefits from OpenMP parallelization within MSG. Specifically, the routines for evaluating the spectroscopic irradiance \(\irrad\) and photometric irradiance \(\mirrad\) (see the Python Interface table) add the contributions from each visible photospheric element in parallel.
footnote