A symmetric positive definite example#
The orderings guide works through an unsymmetric matrix with LU. Many of the matrices people actually factor are symmetric positive definite (SPD), provided they are nonsingular:
stiffness matrices, once enough boundary conditions remove the rigid-body modes;
graph Laplacians, once a node is grounded (a plain Laplacian is singular, since the constant vector is in its nullspace);
normal equations \(A^TA\), when \(A\) has full column rank;
covariance matrices, when the centred variables are linearly independent (so no variable is an affine function of the others, not even \(X\) and \(X + 1\)).
For SPD matrices, mtl5.sparse has two factorizations that exploit the
symmetry and need no pivoting. cholesky raises RuntimeError on a matrix
that turns out not to be positive definite. supernodal_ldlt also accepts a
symmetric indefinite matrix, and raises only on a zero pivot.
ms.cholesky(A), \(A = LL^T\), an up-looking (column-at-a-time, non-supernodal) factorization;ms.supernodal_ldlt(A), \(A = LDL^T\), which groups columns with the same pattern into supernodes and updates them with dense block operations.
This page factors the 2-D Poisson matrix with both, for every ordering, and
measures how the fill and the factor time grow with the problem size. The page
is executed when the documentation is built, and the assert statements check
what the prose claims.
import time
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import ListedColormap
import mtl5
import mtl5.sparse as ms
SURFACE = "#fcfcfb"
INK, INK_2 = "#0b0b0b", "#52514e"
SERIES = ["#2a78d6", "#eb6834", "#1baf7a", "#eda100"]
plt.rcParams.update(
{
"figure.facecolor": SURFACE,
"axes.facecolor": SURFACE,
"axes.edgecolor": "#d8d7d2",
"axes.labelcolor": INK_2,
"axes.titlecolor": INK,
"text.color": INK,
"xtick.color": INK_2,
"ytick.color": INK_2,
"font.size": 10,
"figure.dpi": 120,
}
)
SPY_DPI = 200 # about two pixels per matrix row in the spy figures
def spy(ax, M, title, color=SERIES[0]):
# One image cell per matrix entry, so two nonzeros never overlap.
S = ms.to_scipy(M).tocoo()
pattern = np.zeros(S.shape, dtype=bool)
pattern[S.row, S.col] = True
ax.imshow(pattern, cmap=ListedColormap([SURFACE, color]), interpolation="none")
ax.set_title(f"{title}\nnz = {S.nnz:,}", fontsize=9)
ax.set_xticks([])
ax.set_yticks([])
def best_ms(fn, repeats=3):
best, out = float("inf"), None
for _ in range(repeats):
t = time.perf_counter()
out = fn()
best = min(best, time.perf_counter() - t)
return best * 1e3, out
The matrix#
mtl5.generators.poisson2d(k, k) is the five-point Laplacian on a \(k \times k\)
grid with Dirichlet boundaries: \(n = k^2\) unknowns, numbered row by row, with
\(4/h^2\) on the diagonal and \(-1/h^2\) for each grid neighbour, where
\(h = 1/(k+1)\) is the grid spacing. It is SPD, and it is the standard model
problem for sparse Cholesky.
k = 24
A = mtl5.generators.poisson2d(k, k)
n = A.shape[0]
S = ms.to_scipy(A)
print(f"n = {n}, nnz(A) = {A.nnz}")
assert (S != S.T).nnz == 0 # symmetric
assert np.linalg.eigvalsh(S.toarray()).min() > 0 # positive definite
# Both factorizations solve it to rounding.
b = S @ np.ones(n)
for f in (ms.cholesky(A), ms.supernodal_ldlt(A)):
x = f.solve(b).to_numpy()
print(f"{type(f).__name__:>20}: max |x - 1| = {np.abs(x - 1).max():.1e}")
assert np.abs(x - 1).max() < 1e-12
n = 576, nnz(A) = 2784
SparseCholesky_f64: max |x - 1| = 5.2e-15
SupernodalLDLT_f64: max |x - 1| = 5.6e-15
What the ordering does to the factor#
A symmetric factorization must permute rows and columns together, \(PAP^T\), to
keep the symmetry, so it takes one permutation. amd is the default for both.
It orders the graph of \(A\) directly. colamd is offered too, but it orders
\(A^TA\), which for a symmetric \(A\) is the pattern of \(A^2\), a denser graph than
the one being factored. rcm is a heuristic that reduces the bandwidth,
without guaranteeing the minimum, and natural keeps the grid’s row-by-row
numbering.
ORDERINGS = ["natural", "rcm", "colamd", "amd", "nd"]
COLORS = SERIES + ["#8a5cc7"] # a fifth hue for nested dissection
chol = {o: ms.cholesky(A, ordering=o) for o in ORDERINGS}
fig, axes = plt.subplots(2, 3, figsize=(13, 9.4), dpi=SPY_DPI)
spy(axes[0, 0], A, "A", color=INK_2)
for ax, o, c in zip(axes.flat[1:], ORDERINGS, COLORS):
spy(ax, chol[o].L, f"L ({o})", color=c)
plt.tight_layout()
plt.show()
# The factors reconstruct the permuted matrix, to rounding.
for o, f in chol.items():
L = ms.to_scipy(f.L)
err = abs(S[f.perm][:, f.perm] - L @ L.T).max() / abs(S).max()
assert err < 1e-14, (o, err)
The natural ordering leaves \(A\) banded, with half-bandwidth \(k\). A Cholesky
factor never fills outside the envelope of \(A\), the region between each
row’s first nonzero and the diagonal, and for this matrix it fills that
envelope completely. So its fill can be counted without factoring: the first
grid row couples each unknown only to its left neighbour, giving \(2k - 1\)
entries, and each of the remaining \(n - k\) rows of \(L\) carries \(k + 1\).
rcm keeps a band of about the same width, but numbers the grid along its
diagonals, which shortens most rows of the envelope: it saves about a quarter
of the fill. amd does not try to keep a band at all. At each step it
eliminates an unknown of smallest approximate degree: few neighbours in the
graph that remains, estimated cheaply instead of counted exactly. Low degree is
a proxy for low fill, not a guarantee of it, but on this grid it leaves a
factor with well under half the entries. nd, nested dissection, splits the
grid at small separators and numbers each separator after the two parts it
divides. Its factor shows the recursion as nested blocks. At this size it
fills a little more than amd; the next section shows how that changes as
the grid grows.
envelope = (2 * k - 1) + (n - k) * (k + 1)
print(f"natural: nnz(L) = {chol['natural'].nnz:,}, envelope of A = {envelope:,}")
assert chol["natural"].nnz == envelope
for o in ORDERINGS:
print(f"{o:>8}: nnz(L) = {chol[o].nnz:7,} ({chol[o].nnz / A.nnz:4.1f}x nnz(A))")
assert chol["amd"].nnz < chol["colamd"].nnz < chol["rcm"].nnz <= chol["natural"].nnz
assert chol["amd"].nnz < chol["nd"].nnz < chol["colamd"].nnz
natural: nnz(L) = 13,847, envelope of A = 13,847
natural: nnz(L) = 13,847 ( 5.0x nnz(A))
rcm: nnz(L) = 10,052 ( 3.6x nnz(A))
colamd: nnz(L) = 8,700 ( 3.1x nnz(A))
amd: nnz(L) = 5,787 ( 2.1x nnz(A))
nd: nnz(L) = 6,759 ( 2.4x nnz(A))
How fill and time grow with the problem size#
Now repeat that for grids from \(16 \times 16\) to \(128 \times 128\), with both factorizations.
nnz counts what each object stores. cholesky stores \(L\) with its diagonal.
supernodal_ldlt stores \(L\) without its unit diagonal and keeps \(D\) apart, so
its count is smaller by exactly \(n\): the two factors have the same pattern.
SIZES = [16, 32, 64, 128]
results = {} # (ordering, k) -> (n, nnz(A), nnz(L), cholesky ms, supernodal ms)
for k_ in SIZES:
Ak = mtl5.generators.poisson2d(k_, k_)
nk = Ak.shape[0]
for o in ORDERINGS:
t_c, c = best_ms(lambda: ms.cholesky(Ak, ordering=o))
t_s, s = best_ms(lambda: ms.supernodal_ldlt(Ak, ordering=o))
assert s.nnz == c.nnz - nk # same pattern; LDL^T leaves out the unit diagonal
results[o, k_] = (nk, Ak.nnz, c.nnz, t_c, t_s)
print(f"{'grid':>9} {'n':>7} {'ordering':>8} {'nnz(L)':>11} {'fill':>6} {'cholesky':>9} {'supernodal':>11}")
for k_ in SIZES:
for o in ORDERINGS:
nk, nnz_a, nnz_l, t_c, t_s = results[o, k_]
print(f"{k_:>4}x{k_:<4} {nk:7,} {o:>8} {nnz_l:11,} {nnz_l / nnz_a:5.1f}x {t_c:7.1f}ms {t_s:9.1f}ms")
grid n ordering nnz(L) fill cholesky supernodal
16x16 256 natural 4,111 3.4x 0.2ms 0.3ms
16x16 256 rcm 3,096 2.5x 0.2ms 0.3ms
16x16 256 colamd 2,763 2.3x 0.4ms 0.4ms
16x16 256 amd 2,118 1.7x 0.3ms 0.3ms
16x16 256 nd 2,408 2.0x 0.3ms 0.4ms
32x32 1,024 natural 32,799 6.6x 1.9ms 2.2ms
32x32 1,024 rcm 23,344 4.7x 1.4ms 1.8ms
32x32 1,024 colamd 18,083 3.6x 2.1ms 1.8ms
32x32 1,024 amd 11,900 2.4x 1.4ms 1.3ms
32x32 1,024 nd 13,382 2.7x 1.8ms 1.8ms
64x64 4,096 natural 262,207 13.0x 26.6ms 24.9ms
64x64 4,096 rcm 180,832 8.9x 14.7ms 16.0ms
64x64 4,096 colamd 103,630 5.1x 12.8ms 8.1ms
64x64 4,096 amd 67,200 3.3x 7.7ms 5.8ms
64x64 4,096 nd 69,939 3.5x 9.6ms 8.0ms
128x128 16,384 natural 2,097,279 25.8x 376.3ms 356.8ms
128x128 16,384 rcm 1,422,528 17.5x 192.8ms 206.6ms
128x128 16,384 colamd 603,662 7.4x 107.9ms 53.5ms
128x128 16,384 amd 381,322 4.7x 59.4ms 33.9ms
128x128 16,384 nd 350,740 4.3x 58.3ms 40.2ms
On a log-log plot the slope is the growth exponent. The envelope argument gives
the natural ordering’s exactly: \(nnz(L) \approx n \cdot k = n^{3/2}\), and rcm,
a band of the same width, grows at nearly the same rate. The best possible
growth on a 2-D grid is \(O(n \log n)\), which nested dissection achieves
(George, 1973), and which shows up on this range of sizes as a slope of about
1.1. Minimum degree is a greedy heuristic with no such guarantee: here it lands
in between, well below \(3/2\) but above the nested dissection rate. mtl5’s
nd ordering (since MTL5 5.25.0) starts above amd on the small grids and
grows more slowly. Beyond about \(100 \times 100\) it produces the smaller
factor: 0.92 times amd’s at \(128 \times 128\), and 0.84 times at
\(256 \times 256\).
def slope(o):
# Least-squares slope of log nnz(L) against log n over the measured sizes.
ns = np.array([results[o, k_][0] for k_ in SIZES], dtype=float)
nz = np.array([results[o, k_][2] for k_ in SIZES], dtype=float)
return np.polyfit(np.log(ns), np.log(nz), 1)[0]
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 3.8))
for o, c in zip(ORDERINGS, COLORS):
ns = [results[o, k_][0] for k_ in SIZES]
ax1.loglog(ns, [results[o, k_][2] for k_ in SIZES], "o-", color=c, label=f"{o} (slope {slope(o):.2f})")
ax2.loglog(ns, [results[o, k_][3] for k_ in SIZES], "o-", color=c, label=f"{o}, cholesky")
ax2.loglog(ns, [results[o, k_][4] for k_ in SIZES], "s--", color=c, alpha=0.7, label=f"{o}, supernodal")
ax1.set(xlabel="n", ylabel="nnz(L)", title="fill")
ax2.set(xlabel="n", ylabel="factor time (ms)", title="time (best of 3)")
ax1.legend(fontsize=8, frameon=False)
ax2.legend(fontsize=7, frameon=False, ncol=2)
for ax in (ax1, ax2):
ax.grid(True, which="major", color="#ecebe7", linewidth=0.6)
plt.tight_layout()
plt.show()
for o in ORDERINGS:
print(f"{o:>8}: nnz(L) grows like n^{slope(o):.2f}")
assert abs(slope("natural") - 1.5) < 0.05
assert abs(slope("rcm") - 1.5) < 0.1
assert slope("amd") < 1.35
assert slope("nd") < slope("amd") # ND's fill grows more slowly
assert results["nd", SIZES[-1]][2] < results["amd", SIZES[-1]][2] # and wins at 128 x 128
# The O(n log n) rate, fitted over the same sizes, for comparison.
ns = np.array([k_ * k_ for k_ in SIZES], dtype=float)
nd_slope = np.polyfit(np.log(ns), np.log(ns * np.log(ns)), 1)[0]
print(f" n log n: grows like n^{nd_slope:.2f}")
assert nd_slope < slope("amd")
natural: nnz(L) grows like n^1.50
rcm: nnz(L) grows like n^1.47
colamd: nnz(L) grows like n^1.29
amd: nnz(L) grows like n^1.25
nd: nnz(L) grows like n^1.20
n log n: grows like n^1.13
Three things to take from the measurements:
The ordering decides the exponent, not just the constant. At \(128 \times 128\) the
amdfactor is several times smaller than thenaturalone, and the gap widens with every doubling of the grid. Work grows faster still: \(O(n^2)\) for a band of width \(\sqrt n\) against \(O(n^{3/2})\) for nested dissection orderings.colamdis the wrong tool for a symmetric matrix. It is the right default forsplu, but it orders \(A^TA\), so on this problem its factor is roughly half as large again asamd’s.Supernodes pay off once the factor is large enough. With
amd, the columns near the end of the elimination form dense blocks, andsupernodal_ldltupdates them with dense kernels instead of one column at a time. On small grids the two are about even, and the supernodal variant pulls ahead as the grid grows. The times above are measured on whatever machine built this page, so the exact ratio varies; the fill counts do not.
Symmetric indefinite matrices are outside cholesky’s reach, but
supernodal_ldlt and ldlt factor them as long as no pivot is zero. For those,
and for unsymmetric matrices, see
choosing a factorization.
API reference: mtl5.sparse.cholesky(), mtl5.sparse.supernodal_ldlt(), mtl5.sparse.ordering(), mtl5.sparse.orderings(), mtl5.sparse.to_scipy().