A13 · Lesson 49 · Sparse matrices
Storing and solving systems that are almost all zeros
A circuit with a million nodes has a nodal matrix with entries, 8 TB as a dense array. But each node connects to only a few others, so all but about five million of those entries are zero. Storing only the nonzeros brings it down to tens of megabytes, and solvers that never touch the zeros make it solvable in seconds. The catch is fill-in: elimination can turn zeros into nonzeros, and the order in which you eliminate decides whether a sparse problem stays sparse. Every circuit simulator, finite-element program and field solver lives or dies by this.
- Operation counts for solving the five-point Laplacian on an N × N grid (n = N² unknowns) four ways, on log scales.
- Dense LU grows as : hopeless past a few thousand unknowns. Treating the matrix as banded (bandwidth N) gives . Nested-dissection ordering, which eliminates the grid in a divide-and-conquer order, brings sparse LU to about .
- Conjugate gradients with a good preconditioner (or multigrid) costs about a constant times the nonzeros per iteration; the readouts compare them at the marked size.
Predict
A 1000 × 1000 grid has a million unknowns and about 5 million nonzeros. Roughly how much memory does compressed sparse row storage need?
About 64 MB: 8 bytes for each nonzero value, 4 for its column index, and 4 per row for the row pointers. The dense matrix would be 8 TB.
Name it
Storage
Compressed sparse row (CSR) keeps three arrays: the nonzero values row by row, the column of each, and where each row starts. With nnz nonzeros, that's about bytes (double values, 32-bit indices). A product Ax costs operations. (CSC is the same by columns.)
Where sparse matrices come from
| Source | Nonzeros per row |
|---|---|
| nodal analysis of a circuit | about the number of parts per node, plus one |
| five-point Laplacian (2D grid) | 5 |
| seven-point Laplacian (3D grid) | 7 |
| finite elements | 10 to 30 |
Direct solvers and fill-in
Eliminating a variable connects all of its neighbors to each other, so zeros can become nonzeros. The order matters enormously:
- An arrow matrix (one node tied to all the others, like a ground or supply node) fills in completely if that node is eliminated first, and not at all if it's eliminated last.
- Banded matrices (bandwidth b) factor in about operations with no fill outside the band.
- Reordering heuristics (minimum degree, Markowitz pivoting in SPICE, nested dissection for grids) choose an order with little fill. For a 2D grid nested dissection gives about operations and storage; in 3D, about .
Iterative solvers
CG, GMRES and multigrid need only products Ax, so their cost per iteration is O(nnz) and they create no fill. With a good preconditioner they're the method of choice for large 3D problems, where direct solvers' fill becomes too large.
In Python
scipy.sparse provides CSR and CSC matrices; scipy.sparse.linalg.spsolve does sparse LU with reordering, and cg and gmres the iterative methods.
Worked example
Store the nodal matrix of a million-node circuit with an average of 5 nonzeros per row.
Show the solution
- Nonzeros. nnz = .
- CSR storage.
- Dense storage. bytes, 8 TB.
- Dense LU. It would take about operations.
Answer 64 MB in CSR, against 8 TB stored dense.
Worked example
An arrow matrix has a hub connected to all 999 other nodes, which connect only to the hub. Compare eliminating the hub first and last.
Show the solution
- Hub first. Every pair of the 999 nodes becomes connected, so the remaining matrix is dense: about entries and operations to factor.
- Hub last. Each of the 999 nodes eliminates with one operation on the hub's diagonal, with no fill at all: about 3000 operations in all.
Answer hub first costs about operations on a dense matrix; hub last costs about 3000, with no fill.
Worked example
A 100 × 100 grid problem is numbered row by row. How much work does banded LU take, compared with dense LU?
Show the solution
- Size and bandwidth. There are unknowns, and each couples to neighbors at most N = 100 positions away, so the bandwidth is b = 100.
- Banded LU. It fills in the band but no further, costing about operations.
- Dense LU. over 3000 times more.
- Better orderings. Smarter orderings (nested dissection) do better still on grids, about , which is how direct sparse solvers handle million-node circuits.
Answer about operations banded, against dense.
Check
A sparse matrix has 100000 rows and 5 nonzeros per row. About how much memory does CSR take (8-byte values, 4-byte indices)?
Check
How many operations does one sparse matrix-vector product take, with 100000 rows of 5 nonzeros?
Check
About how many operations does LU of a banded matrix take, with n = 10000 and bandwidth 10 (use )?
Check
The Laplacian on a 2D grid has n = 1000000 unknowns. About how many operations does sparse LU with nested-dissection ordering take ()?
Check
An arrow matrix of size 1000 has one hub node tied to all the others, which connect only to it. If the hub is eliminated first, about how many nonzeros does the remaining matrix have?
Check
In SPICE, why is the ground node's row and column removed rather than kept and eliminated first?
Check
A 2D grid of 100 × 100 unknowns is numbered row by row.
(a)
About how many operations does banded LU take, with b = N?
(b)
How many times more would dense LU take?
Check
A sparse matrix in CSR form has n rows and k nonzeros per row, with 8-byte values and 4-byte indices.
Step 1
How many bytes does it take?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
How many operations does one matrix–vector product take?
Explain why
Why does the order of elimination matter so much for a sparse matrix?
Eliminating a variable replaces it, in every equation that contained it, with a combination of its neighbors, so after elimination all of its neighbors are linked to each other. A variable with few neighbors adds little or nothing; one with many neighbors turns a big block of zeros into nonzeros, each of which costs memory and further work. Eliminating the highly connected variables last, after their neighbors have mostly been removed, keeps the fill small. Choosing the order is a graph problem (minimum degree, nested dissection), and a good order can make the difference between megabytes and terabytes.
Practice
Keep practicing
9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.