In Eigen, there are several methods available to solve linear systems when the coefficient matrix is sparse. Because of the special representation of this class of matrices, special care should be taken in order to get a good performance. See Sparse matrix manipulations for a detailed introduction about sparse matrices in Eigen. This page lists the sparse solvers available in Eigen. The main steps that are common to all these linear solvers are introduced as well. Depending on the properties of the matrix, the desired accuracy, the end-user is able to tune those steps in order to improve the performance of its code. Note that it is not required to know deeply what's hiding behind these steps: the last section presents a benchmark routine that can be easily used to get an insight on the performance of all the available solvers.
Eigen currently provides a wide set of built-in solvers, as well as wrappers to external solver libraries. They are summarized in the following tables:
|Class||Solver kind||Matrix kind||Features related to performance||License||
|SimplicialLLT ||Direct LLt factorization||SPD||Fill-in reducing||LGPL|
SimplicialLDLT is often preferable
|SimplicialLDLT ||Direct LDLt factorization||SPD||Fill-in reducing||LGPL|
Recommended for very sparse and not too large problems (e.g., 2D Poisson eq.)
|SparseLU ||LU factorization||Square||Fill-in reducing, Leverage fast dense algebra||MPL2|
optimized for small and large problems with irregular patterns
|SparseQR ||QR factorization||Any, rectangular||Fill-in reducing||MPL2||recommended for least-square problems, has a basic rank-revealing feature|
|Class||Solver kind||Matrix kind||Supported preconditioners, [default]||License||
|ConjugateGradient ||Classic iterative CG||SPD||IdentityPreconditioner, [DiagonalPreconditioner], IncompleteCholesky||MPL2|
Recommended for large symmetric problems (e.g., 3D Poisson eq.)
|LeastSquaresConjugateGradient ||CG for rectangular least-square problem||Rectangular||IdentityPreconditioner, [LeastSquareDiagonalPreconditioner]||MPL2|
Solve for min |A'Ax-b|^2 without forming A'A
|BiCGSTAB ||Iterative stabilized bi-conjugate gradient||Square||IdentityPreconditioner, [DiagonalPreconditioner], IncompleteLUT||MPL2||To speedup the convergence, try it with the IncompleteLUT preconditioner.|
|Class||Module||Solver kind||Matrix kind||Features related to performance||Dependencies,License||
|PaStiXSupport||Direct LLt, LDLt, LU factorizations||SPD |
|Fill-in reducing, Leverage fast dense algebra, Multithreading||Requires the PaStiX package, CeCILL-C||optimized for tough problems and symmetric patterns|
|CholmodSupernodalLLT||CholmodSupport||Direct LLt factorization||SPD||Fill-in reducing, Leverage fast dense algebra||Requires the SuiteSparse package, GPL|
|UmfPackLU||UmfPackSupport||Direct LU factorization||Square||Fill-in reducing, Leverage fast dense algebra||Requires the SuiteSparse package, GPL|
|SuperLU||SuperLUSupport||Direct LU factorization||Square||Fill-in reducing, Leverage fast dense algebra||Requires the SuperLU library, (BSD-like)|
|SPQR||SPQRSupport||QR factorization||Any, rectangular||fill-in reducing, multithreaded, fast dense algebra||requires the SuiteSparse package, GPL||recommended for linear least-squares problems, has a rank-revealing feature|
|PardisoSupport||Direct LLt, LDLt, LU factorizations||SPD |
|Fill-in reducing, Leverage fast dense algebra, Multithreading||Requires the Intel MKL package, Proprietary||optimized for tough problems patterns, see also using MKL with Eigen|
SPD means symmetric positive definite.
All these solvers follow the same general concept. Here is a typical and general example:
SPD solvers, a second optional template argument allows to specify which triangular part have to be used, e.g.:
In the above example, only the upper triangular part of the input matrix A is considered for solving. The opposite triangle might either be empty or contain arbitrary values.
In the case where multiple problems with the same sparsity pattern have to be solved, then the "compute" step can be decomposed as follow:
The compute() method is equivalent to calling both analyzePattern() and factorize().
Each solver provides some specific features, such as determinant, access to the factors, controls of the iterations, and so on. More details are available in the documentations of the respective classes.
Finally, most of the iterative solvers, can also be used in a matrix-free context, see the following example .
In the compute() function, the matrix is generally factorized: LLT for self-adjoint matrices, LDLT for general hermitian matrices, LU for non hermitian matrices and QR for rectangular matrices. These are the results of using direct solvers. For this class of solvers precisely, the compute step is further subdivided into analyzePattern() and factorize().
The goal of analyzePattern() is to reorder the nonzero elements of the matrix, such that the factorization step creates less fill-in. This step exploits only the structure of the matrix. Hence, the results of this step can be used for other linear systems where the matrix has the same structure. Note however that sometimes, some external solvers (like SuperLU) require that the values of the matrix are set in this step, for instance to equilibrate the rows and columns of the matrix. In this situation, the results of this step should not be used with other matrices.
Eigen provides a limited set of methods to reorder the matrix in this step, either built-in (COLAMD, AMD) or external (METIS). These methods are set in template parameter list of the solver :
See the OrderingMethods module for the list of available methods and the associated options.
In factorize(), the factors of the coefficient matrix are computed. This step should be called each time the values of the matrix change. However, the structural pattern of the matrix should not change between multiple calls.
For iterative solvers, the compute step is used to eventually setup a preconditioner. For instance, with the ILUT preconditioner, the incomplete factors L and U are computed in this step. Remember that, basically, the goal of the preconditioner is to speedup the convergence of an iterative method by solving a modified linear system where the coefficient matrix has more clustered eigenvalues. For real problems, an iterative solver should always be used with a preconditioner. In Eigen, a preconditioner is selected by simply adding it as a template parameter to the iterative solver object.
The member function preconditioner() returns a read-write reference to the preconditioner to directly interact with it. See the Iterative solvers module and the documentation of each class for the list of available methods.
The solve() function computes the solution of the linear systems with one or many right hand sides.
Here, B can be a vector or a matrix where the columns form the different right hand sides. The solve() function can be called several times as well, for instance when all the right hand sides are not available at once.
For direct methods, the solution are computed at the machine precision. Sometimes, the solution need not be too accurate. In this case, the iterative methods are more suitable and the desired accuracy can be set before the solve step using setTolerance(). For all the available functions, please, refer to the documentation of the Iterative solvers module .
Most of the time, all you need is to know how much time it will take to solve your system, and hopefully, what is the most suitable solver. In Eigen, we provide a benchmark routine that can be used for this purpose. It is very easy to use. In the build directory, navigate to bench/spbench and compile the routine by typing make spbenchsolver. Run it with –help option to get the list of all available options. Basically, the matrices to test should be in MatrixMarket Coordinate format, and the routine returns the statistics from all available solvers in Eigen.
To export your matrices and right-hand-side vectors in the matrix-market format, you can the the unsupported SparseExtra module:
The following table gives an example of XML statistics from several Eigen built-in and external solvers.
|Matrix||N||NNZ||UMFPACK||SUPERLU||PASTIX LU||BiCGSTAB||BiCGSTAB+ILUT||GMRES+ILUT||LDLT||CHOLMOD LDLT||PASTIX LDLT||LLT||CHOLMOD SP LLT||CHOLMOD LLT||PASTIX LLT||CG|
|Error(Iter)||1.299e-16||2.04207e-16||4.83393e-15||3.94856e-11 (80)||1.03861e-12 (3)||5.81088e-14 (6)||1.97578e-16||1.83927e-16||4.24115e-15|
|Error(Iter)||4.67146e-16||1.068e-15||1.3397e-15||6.29233e-11 (201)||3.68527e-11 (6)||3.3168e-15 (16)||1.86376e-15||1.31518e-16||1.42593e-15||3.45361e-15||3.14575e-16||2.21723e-15||7.21058e-16||9.06435e-12 (261)|
|Error(Iter)||1.83099e-15||8.19351e-15||2.625e-14||1.3678e+69 (1080)||4.1911e-12 (7)||5.0299e-13 (12)|
|Error(Iter)||1.03474e-16||2.23046e-16||2.01273e-16||4.87455e-07 (48)||1.03553e-16 (2)||3.55965e-16 (2)||2.48189e-16||1.88808e-16||1.97976e-16||2.37248e-16||1.82701e-16||2.71474e-16||2.11322e-16||3.547e-09 (48)|
|Error(Iter)||1.16839e-16||2.25968e-16||2.59116e-16||3.76779e-11 (248)||4.13343e-11 (4)||2.22347e-14 (10)||2.05861e-16||1.83555e-16||1.02917e-15|
|Error(Iter)||1.27029e-16||2.81321e-16||5.0492e-15||8.0507e-11 (706)||3.00447e-12 (8)||1.46532e-12 (16)|
|Error(Iter)||1.32953e-16||3.08646e-16||6.734e-16||8.83132e-11 (40)||1.51153e-16 (1)||6.08556e-16 (8)||1.89264e-16||1.97477e-16||6.68126e-09|
|Error(Iter)||1.76348e-16||4.58395e-16||1.67982e-14||8.64144e-11 (61)||8.5996e-12 (2)|