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 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. is , is , and is the tile side.
Correctness
For let , and define for and for in the same way. The nonempty sets partition , and similarly for and . Line L1 runs over the , line L2 over the and line L4 over the .
Invariant of the loop at line L4. Fix the tile chosen at lines L1 and L2. Before the iteration of line L4 for , the tile in fast memory satisfies
Initialization. At line L3, is zero (the output starts as zero), which is the empty sum for .
Maintenance. Line L5 loads and . Line L6 adds to each entry , by the correctness of the triple loop (each product keeps the factor on the left). So the invariant holds for .
End. After the last , , because the partition . Line L7 writes this into .
Every pair lies in exactly one tile , and each tile is written only once, at line L7 of its own iteration. So at the end .
Summation order. In the proof of maintenance, line L6 runs the triple loop with increasing over , and line L4 takes the in increasing order. So receives the terms 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 , and line L6 runs a triple loop over finite ranges. So the method stops after iterations of line L5.
The I/O count
Store each matrix in row-major order, starting at any word address. Blocks are the intervals of word addresses.
Lemma 1. A run of consecutive words touches at most blocks. If the run starts at a multiple of and divides , it touches exactly blocks.
Proof. The run from address to touches the blocks to , which is blocks, since for real and . The aligned case is direct.
Lemma 2. A tile with at most rows and at most columns touches at most blocks. If this is at most blocks, that is words.
Proof. Each row of the tile is a run of at most words in row-major storage, so Lemma 1 applies to each row. If then .
Theorem. Let , and let .
- Aligned case. If divides , divides , and the matrices start at multiples of , the schedule makes exactly I/Os and needs words of fast memory.
- General case. If , the schedule makes at most I/Os and needs at most words of fast memory. With and , this is .
Proof. There are tiles of . Each is read once at line L3 and written once at line L7. For each of them, line L5 runs times and reads one tile of and one of . So there are tile transfers for and for and , in all. Fast memory holds three tiles at any time.
In the aligned case, every tile has rows of words, each row starts at a multiple of , so by Lemma 1 a tile is blocks, and . The count is .
In the general case, Lemma 2 bounds each tile by blocks and words. Since , , so . Three tiles take at most words when . If , then , and if , then (since and is an integer). So the count is at most . (If , there is one tile of each matrix and the count is at most .)
Optimality. Hong and Kung (1981) proved that every schedule of the conventional computation graph of the product, with words of fast memory, moves words; the red-blue pebble game page proves for the one-shot game. Take . One I/O moves at most words, so every such schedule makes I/Os. It also reads the input words, so it makes at least I/Os. Together these give when for a suitable constant , which is the case where the matrices do not fit in fast memory. So the theorem is optimal up to a constant factor.