TExcelTAD
Home › Numerical Methods › Preconditioning and GMRES

Preconditioning and GMRES

Krylov methods converge fast when the matrix is well conditioned or has clustered eigenvalues, and slowly otherwise. A preconditioner M≈AM \approx A, cheap to invert, transforms the system into one that is easier for the iteration. For nonsymmetric matrices, where CG does not apply, GMRES is the standard Krylov method. Together, preconditioning and Krylov methods solve the largest linear systems in science and engineering.

Preconditioning

Instead of Ax=bA\mathbf{x} = \mathbf{b}, solve M−1Ax=M−1bM^{-1}A\mathbf{x} = M^{-1}\mathbf{b}, which has the same solution. A good MM:

  • makes M−1AM^{-1}A close to the identity (small condition number or clustered eigenvalues);
  • is cheap to apply: solving Mz=rM\mathbf{z} = \mathbf{r} costs about as much as a matrix-vector product.

The extremes: M=IM = I (free but useless) and M=AM = A (perfect but as hard as the original problem). Common choices:

Preconditioner Idea
Jacobi (diagonal) M=diag⁡(A)M = \operatorname{diag}(A); fixes bad scaling
incomplete LU / Cholesky LU factorization that discards most fill-in
multigrid one V-cycle as M−1M^{-1}; optimal for elliptic PDEs
domain decomposition solve subproblems on pieces of the domain
python · try it yourself
import numpy as np
import scipy.sparse as sp
from scipy.sparse.linalg import cg, LinearOperator, spsolve_triangular

N = 60
T = sp.diags([-1, 2, -1], [-1, 0, 1], shape=(N, N))
A = (sp.kron(sp.identity(N), T) + sp.kron(T, sp.identity(N))).tocsr()
rng = np.random.default_rng(121)
scale = sp.diags(10.0 ** rng.uniform(-2, 2, N * N))       # badly scaled rows and columns
A = (scale @ A @ scale).tocsr()
b = A @ np.ones(N * N)

d = A.diagonal()
lower, upper = sp.tril(A, format="csr"), sp.triu(A, format="csr")
def ssor(r):                       # symmetric Gauss-Seidel: M = (D + L) D^-1 (D + U)
    z = spsolve_triangular(lower, r, lower=True)
    return spsolve_triangular(upper, d * z, lower=False)

preconditioners = {"none": None,
                   "Jacobi": LinearOperator(A.shape, matvec=lambda r: r / d),
                   "symmetric Gauss-Seidel": LinearOperator(A.shape, matvec=ssor)}
for name, M in preconditioners.items():
    count = [0]
    x, info = cg(A, b, rtol=1e-8, maxiter=20000, M=M, callback=lambda xk: count.__setitem__(0, count[0] + 1))
    print(f"{name:22s}: {count[0]:5d} iterations, converged {info == 0}, relative residual {np.linalg.norm(b - A @ x) / np.linalg.norm(b):.1e}")
none                  : 20000 iterations, converged False, relative residual 2.1e-07
Jacobi                :   160 iterations, converged True, relative residual 8.6e-09
symmetric Gauss-Seidel:    57 iterations, converged True, relative residual 8.2e-09

Without preconditioning CG does not converge within 20,000 iterations. Diagonal scaling removes the artificial ill-conditioning (160 iterations), and symmetric Gauss-Seidel, which also approximates the underlying Laplacian, cuts the count by roughly a factor of three more. CG needs a symmetric positive definite preconditioner, which both of these are; incomplete Cholesky and multigrid are the stronger standard choices.

GMRES for nonsymmetric systems

For nonsymmetric AA, the generalized minimal residual method builds an orthonormal basis q1,…,qk\mathbf{q}_1, \ldots, \mathbf{q}_k of the Krylov subspace with the Arnoldi process (Gram-Schmidt applied to AqjA\mathbf{q}_j) and picks the xk\mathbf{x}_k in it that minimizes ∥b−Axk∥2\lVert\mathbf{b} - A\mathbf{x}_k\rVert_2, a small least-squares problem. The residual never increases. The cost: all basis vectors must be stored and each new one orthogonalized against all the others, so work and memory grow with kk. In practice GMRES is restarted every mm iterations (GMRES(mm)).

python · try it yourself
import numpy as np
import scipy.sparse as sp
from scipy.sparse.linalg import gmres, spilu, LinearOperator

# 2D convection-diffusion: -Laplace(u) + 30 u_x = 1 on a 40 x 40 grid (nonsymmetric)
N = 40
h = 1 / (N + 1)
T = sp.diags([-1, 2, -1], [-1, 0, 1], shape=(N, N)) / h**2
C = sp.diags([-1, 1], [-1, 1], shape=(N, N)) / (2 * h)
I = sp.identity(N)
A = (sp.kron(I, T) + sp.kron(T, I) + 30 * sp.kron(I, C)).tocsc()
b = np.ones(N * N)

ilu = spilu(A, drop_tol=1e-3)
for name, M in (("GMRES(30)", None), ("GMRES(30) + ILU", LinearOperator(A.shape, matvec=ilu.solve))):
    count = [0]
    x, info = gmres(A, b, rtol=1e-10, restart=30, maxiter=500, M=M,
                    callback=lambda rk: count.__setitem__(0, count[0] + 1), callback_type="pr_norm")
    print(f"{name:16s}: inner iterations {count[0]:4d}, info {info}, relative residual {np.linalg.norm(b - A @ x) / np.linalg.norm(b):.1e}")
GMRES(30)       : inner iterations  242, info 0, relative residual 8.2e-11
GMRES(30) + ILU : inner iterations    6, info 0, relative residual 2.3e-12

The incomplete LU factorization (which drops entries below 10−310^{-3} relative) is a good approximation of AA, so preconditioned GMRES needs only a handful of iterations instead of hundreds.

Choosing a Krylov method

Matrix Method
symmetric positive definite CG (with incomplete Cholesky or multigrid)
symmetric indefinite MINRES
nonsymmetric GMRES(mm), BiCGSTAB, (T)FQMR
least squares LSQR, LSMR

In software

  • scipy.sparse.linalg: cg, minres, gmres, lgmres, bicgstab, lsqr, lsmr, spilu; all accept a preconditioner M as a matrix or LinearOperator.
  • PETSc (KSP/PC), Trilinos, hypre (BoomerAMG) and AmgX (GPU) provide industrial Krylov solvers with algebraic multigrid preconditioners.

Exercises

Exercise 1: Jacobi preconditioning by hand

What is M−1AM^{-1}A for A=(100110.01)A = \begin{pmatrix} 100 & 1 \\ 1 & 0.01\end{pmatrix} and the Jacobi preconditioner? Compare condition numbers.

Answer

(10.011001)\begin{pmatrix} 1 & 0.01 \\ 100 & 1\end{pmatrix}; the symmetrically scaled D−1/2AD−1/2=(1111)D^{-1/2}AD^{-1/2} = \begin{pmatrix} 1 & 1 \\ 1 & 1\end{pmatrix} shows the matrix is actually singular.

Solution

det⁡A=1−1=0\det A = 1 - 1 = 0, so AA is singular whatever the scaling; diagonal scaling cannot fix genuine singularity, only bad units. With a22=0.02a_{22} = 0.02 instead, scaling gives (10.7070.7071)\begin{pmatrix} 1 & 0.707 \\ 0.707 & 1\end{pmatrix}, condition number about 5.85.8, while κ(A)≈2×104\kappa(A) \approx 2 \times 10^4.

Exercise 2: GMRES memory

GMRES on a system with 10710^7 unknowns stores one basis vector per iteration. How much memory do 100 iterations need?

Answer

About 8 GB.

Solution

100×107×8100 \times 10^7 \times 8 bytes. That is why GMRES is restarted, e.g. every 30 iterations.

Exercise 3: BiCGSTAB (code)

Solve the lesson's 2D convection-diffusion system with bicgstab, with and without the ILU preconditioner, and report the relative residuals.

Answer

Both converge; the preconditioned run needs far fewer iterations.

Solution
python · try it yourself
import numpy as np
import scipy.sparse as sp
from scipy.sparse.linalg import bicgstab, spilu, LinearOperator
N = 40; h = 1 / (N + 1)
T = sp.diags([-1, 2, -1], [-1, 0, 1], shape=(N, N)) / h**2
C = sp.diags([-1, 1], [-1, 1], shape=(N, N)) / (2 * h)
I = sp.identity(N)
A = (sp.kron(I, T) + sp.kron(T, I) + 30 * sp.kron(I, C)).tocsc()
b = np.ones(N * N)
for name, M in (("none", None), ("ILU", LinearOperator(A.shape, matvec=spilu(A, drop_tol=1e-3).solve))):
    count = [0]
    x, info = bicgstab(A, b, rtol=1e-10, maxiter=5000, M=M, callback=lambda xk: count.__setitem__(0, count[0] + 1))
    print(f"{name:4s}: iterations {count[0]:4d}, relative residual {np.linalg.norm(b - A @ x) / np.linalg.norm(b):.1e}")
none: iterations   73, relative residual 2.7e-11
ILU : iterations    3, relative residual 1.6e-11

Test yourself

A good preconditioner MM is…

  1. equal to AA exactly
  2. close to AA in some sense and cheap to invert
  3. the identity
  4. always diagonal

GMRES minimizes…

  1. the error in the AA-norm
  2. the residual 2-norm over the Krylov subspace
  3. the number of iterations
  4. the condition number

Why is GMRES restarted?

  1. To improve accuracy
  2. Its storage and work grow with every iteration
  3. It diverges otherwise
  4. To make AA symmetric