A1349 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 1012 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 n3=N6: hopeless past a few thousand unknowns. Treating the matrix as banded (bandwidth N) gives nN2=N4. Nested-dissection ordering, which eliminates the grid in a divide-and-conquer order, brings sparse LU to about N3.
  • 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?

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 12nnz+4n bytes (double values, 32-bit indices). A product Ax costs 2nnz operations. (CSC is the same by columns.)

Where sparse matrices come from

SourceNonzeros per row
nodal analysis of a circuitabout the number of parts per node, plus one
five-point Laplacian (2D grid)5
seven-point Laplacian (3D grid)7
finite elements10 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 nb2 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 n1.5 operations and nlog⁡n storage; in 3D, about n2.

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
  1. Nonzeros. nnz = 5×106.
  2. CSR storage. 12×5×106+4×106=64MB
  3. Dense storage. 8×(106)2=8×1012 bytes, 8 TB.
  4. Dense LU. It would take about 23×1018 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
  1. Hub first. Every pair of the 999 nodes becomes connected, so the remaining matrix is dense: about 106 entries and 23×109 operations to factor.
  2. 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 23×109 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
  1. Size and bandwidth. There are n=104 unknowns, and each couples to neighbors at most N = 100 positions away, so the bandwidth is b = 100.
  2. Banded LU. It fills in the band but no further, costing about 2nb2=2×108 operations.
  3. Dense LU. 23n3=6.7×1011 over 3000 times more.
  4. Better orderings. Smarter orderings (nested dissection) do better still on grids, about n3/2, which is how direct sparse solvers handle million-node circuits.

Answer about 2×108 operations banded, against 6.7×1011 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 nb2)?

Check

The Laplacian on a 2D grid has n = 1000000 unknowns. About how many operations does sparse LU with nested-dissection ordering take (n1.5)?

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, 2nb2 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°.

Explain why

Why does the order of elimination matter so much for a sparse matrix?

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.