Preconditioning and GMRES
Krylov methods converge fast when the matrix is well conditioned or has clustered eigenvalues, and slowly otherwise. A preconditioner , 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 , solve , which has the same solution. A good :
- makes close to the identity (small condition number or clustered eigenvalues);
- is cheap to apply: solving costs about as much as a matrix-vector product.
The extremes: (free but useless) and (perfect but as hard as the original problem). Common choices:
| Preconditioner | Idea |
|---|---|
| Jacobi (diagonal) | ; fixes bad scaling |
| incomplete LU / Cholesky | LU factorization that discards most fill-in |
| multigrid | one V-cycle as ; optimal for elliptic PDEs |
| domain decomposition | solve subproblems on pieces of the domain |
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 , the generalized minimal residual method builds an orthonormal basis of the Krylov subspace with the Arnoldi process (Gram-Schmidt applied to ) and picks the in it that minimizes , 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 . In practice GMRES is restarted every iterations (GMRES()).
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 relative) is a good approximation of , 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(), BiCGSTAB, (T)FQMR |
| least squares | LSQR, LSMR |
In software
scipy.sparse.linalg:cg,minres,gmres,lgmres,bicgstab,lsqr,lsmr,spilu; all accept a preconditionerMas a matrix orLinearOperator.- 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 for and the Jacobi preconditioner? Compare condition numbers.
Answer
; the symmetrically scaled shows the matrix is actually singular.
Solution
, so is singular whatever the scaling; diagonal scaling cannot fix genuine singularity, only bad units. With instead, scaling gives , condition number about , while .
Exercise 2: GMRES memory
GMRES on a system with unknowns stores one basis vector per iteration. How much memory do 100 iterations need?
Answer
About 8 GB.
Solution
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
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 is…
- equal to exactly
- close to in some sense and cheap to invert
- the identity
- always diagonal
GMRES minimizes…
- the error in the -norm
- the residual 2-norm over the Krylov subspace
- the number of iterations
- the condition number
Why is GMRES restarted?
- To improve accuracy
- Its storage and work grow with every iteration
- It diverges otherwise
- To make symmetric