Algorithm Search⌘K

Proofs: Blocked matrix multiplication

These are the human proofs of Blocked matrix multiplication, at full length.

The blocked schedule computes AB and its I/O count full

This proof shows: correctness, termination.

Let RR be a semiring: addition is associative and commutative with a zero, multiplication is associative and distributes over addition. Commutativity of the multiplication is not used, and no subtraction is used. AA is n×mn \times m, BB is m×pm \times p, and b≥1b \ge 1 is the tile side.

Correctness

For t≥0t \ge 0 let It={tb,…,min⁡(tb+b,n)−1}I_t = \{tb, \ldots, \min(tb + b, n) - 1\}, and define KtK_t for mm and JtJ_t for pp in the same way. The nonempty sets ItI_t partition {0,…,n−1}\{0, \ldots, n-1\}, and similarly for KtK_t and JtJ_t. Line L1 runs over the ItI_t, line L2 over the JtJ_t and line L4 over the KtK_t.

Invariant of the loop at line L4. Fix the tile (I,J)(I, J) chosen at lines L1 and L2. Before the iteration of line L4 for KsK_s, the tile in fast memory satisfies

TC[i][j]=∑t<s∑k∈Ktaikbkjfor all i∈I,j∈J.T_C[i][j] = \sum_{t < s} \sum_{k \in K_t} a_{ik} b_{kj} \quad \text{for all } i \in I, j \in J.

Initialization. At line L3, TCT_C is zero (the output starts as zero), which is the empty sum for s=0s = 0.

Maintenance. Line L5 loads TA=(aik)i∈I,k∈KsT_A = (a_{ik})_{i \in I, k \in K_s} and TB=(bkj)k∈Ks,j∈JT_B = (b_{kj})_{k \in K_s, j \in J}. Line L6 adds ∑k∈Ksaikbkj\sum_{k \in K_s} a_{ik} b_{kj} to each entry (i,j)(i, j), by the correctness of the triple loop (each product keeps the aa factor on the left). So the invariant holds for s+1s + 1.

End. After the last KsK_s, TC[i][j]=∑k=0m−1aikbkj=(AB)ijT_C[i][j] = \sum_{k=0}^{m-1} a_{ik} b_{kj} = (AB)_{ij}, because the KtK_t partition {0,…,m−1}\{0, \ldots, m-1\}. Line L7 writes this into CC.

Every pair (i,j)(i, j) lies in exactly one tile (I,J)(I, J), and each tile is written only once, at line L7 of its own iteration. So at the end C=ABC = AB. □\square

Summation order. In the proof of maintenance, line L6 runs the triple loop with kk increasing over KsK_s, and line L4 takes the KsK_s in increasing order. So cijc_{ij} receives the terms ai0b0j,ai1b1j,…,ai,m−1bm−1,ja_{i0}b_{0j}, a_{i1}b_{1j}, \ldots, a_{i,m-1}b_{m-1,j} in this order, as in the plain loop. In floating point, each partial sum is rounded in the same order, so the two results agree bit for bit (when both use the same multiply and add instructions).

Termination

Each of the loops at lines L1, L2 and L4 runs over a finite range with a positive step bb, and line L6 runs a triple loop over finite ranges. So the method stops after ⌈n/b⌉⌈p/b⌉⌈m/b⌉\lceil n/b \rceil \lceil p/b \rceil \lceil m/b \rceil iterations of line L5.

The I/O count

Store each matrix in row-major order, starting at any word address. Blocks are the intervals [ℓB,ℓB+B)[\ell B, \ell B + B) of word addresses.

Lemma 1. A run of w≥1w \ge 1 consecutive words touches at most ⌈w/B⌉+1\lceil w/B \rceil + 1 blocks. If the run starts at a multiple of BB and BB divides ww, it touches exactly w/Bw/B blocks.

Proof. The run from address xx to x+w−1x + w - 1 touches the blocks ⌊x/B⌋\lfloor x/B \rfloor to ⌊(x+w−1)/B⌋\lfloor (x + w - 1)/B \rfloor, which is ⌊(x+w−1)/B⌋−⌊x/B⌋+1≤⌊(w−1)/B⌋+2≤⌈w/B⌉+1\lfloor (x+w-1)/B \rfloor - \lfloor x/B \rfloor + 1 \le \lfloor (w-1)/B \rfloor + 2 \le \lceil w/B \rceil + 1 blocks, since ⌊u+v⌋−⌊u⌋≤⌈v⌉\lfloor u + v \rfloor - \lfloor u \rfloor \le \lceil v \rceil for real uu and v≥0v \ge 0. The aligned case is direct. □\square

Lemma 2. A tile with at most bb rows and at most bb columns touches at most b(⌈b/B⌉+1)b(\lceil b/B \rceil + 1) blocks. If b≥Bb \ge B this is at most 3b2/B3b^2/B blocks, that is 3b23b^2 words.

Proof. Each row of the tile is a run of at most bb words in row-major storage, so Lemma 1 applies to each row. If b≥Bb \ge B then ⌈b/B⌉+1≤b/B+2≤3b/B\lceil b/B \rceil + 1 \le b/B + 2 \le 3b/B. □\square

Theorem. Let n=m=pn = m = p, and let T=⌈n/b⌉T = \lceil n/b \rceil.

  1. Aligned case. If bb divides nn, BB divides bb, and the matrices start at multiples of BB, the schedule makes exactly 2n3/(bB)+2n2/B2n^3/(bB) + 2n^2/B I/Os and needs 3b23b^2 words of fast memory.
  2. General case. If B≤b≤nB \le b \le n, the schedule makes at most 2(T3+T2)⋅3b2/B≤48n3/(bB)+24n2/B2(T^3 + T^2) \cdot 3b^2/B \le 48 n^3/(bB) + 24 n^2/B I/Os and needs at most 9b29b^2 words of fast memory. With b=⌊M/3⌋b = \lfloor \sqrt M / 3 \rfloor and M≥max⁡(9B2,36)M \ge \max(9B^2, 36), this is O(n3/(BM)+n2/B)O(n^3/(B\sqrt M) + n^2/B).

Proof. There are T2T^2 tiles of CC. Each is read once at line L3 and written once at line L7. For each of them, line L5 runs TT times and reads one tile of AA and one of BB. So there are 2T22T^2 tile transfers for CC and 2T32T^3 for AA and BB, 2(T3+T2)2(T^3 + T^2) in all. Fast memory holds three tiles at any time.

In the aligned case, every tile has bb rows of bb words, each row starts at a multiple of BB, so by Lemma 1 a tile is b⋅b/B=b2/Bb \cdot b/B = b^2/B blocks, and T=n/bT = n/b. The count is 2((n/b)3+(n/b)2)⋅b2/B=2n3/(bB)+2n2/B2((n/b)^3 + (n/b)^2) \cdot b^2/B = 2n^3/(bB) + 2n^2/B.

In the general case, Lemma 2 bounds each tile by 3b2/B3b^2/B blocks and 3b23b^2 words. Since b≤nb \le n, T≤n/b+1≤2n/bT \le n/b + 1 \le 2n/b, so 2(T3+T2)⋅3b2/B≤6(8n3/b3+4n2/b2)b2/B=48n3/(bB)+24n2/B2(T^3 + T^2) \cdot 3b^2/B \le 6(8n^3/b^3 + 4n^2/b^2) b^2/B = 48n^3/(bB) + 24n^2/B. Three tiles take at most 9b2≤M9b^2 \le M words when b=⌊M/3⌋b = \lfloor \sqrt M/3 \rfloor. If M≥36M \ge 36, then b≥M/3−1≥M/6b \ge \sqrt M/3 - 1 \ge \sqrt M/6, and if M≥9B2M \ge 9B^2, then b≥Bb \ge B (since M/3≥B\sqrt M / 3 \ge B and BB is an integer). So the count is at most 288 n3/(BM)+24n2/B288\, n^3/(B\sqrt M) + 24 n^2/B. (If b>nb > n, there is one tile of each matrix and the count is at most 4⋅3n2/B4 \cdot 3n^2/B.) □\square

Optimality. Hong and Kung (1981) proved that every schedule of the conventional computation graph of the n×nn \times n product, with SS words of fast memory, moves Ω(n3/S)\Omega(n^3/\sqrt S) words; the red-blue pebble game page proves n3/(42S)−Sn^3/(4\sqrt{2S}) - S for the one-shot game. Take S=MS = M. One I/O moves at most BB words, so every such schedule makes Ω(n3/(BM)−M/B)\Omega(n^3/(B\sqrt M) - M/B) I/Os. It also reads the 2n22n^2 input words, so it makes at least 2n2/B2n^2/B I/Os. Together these give Ω(n3/(BM)+n2/B)\Omega(n^3/(B\sqrt M) + n^2/B) when n3≥cM3/2n^3 \ge c M^{3/2} for a suitable constant cc, which is the case where the matrices do not fit in fast memory. So the theorem is optimal up to a constant factor.

Back to Blocked matrix multiplication