Proofs: Cache-oblivious matrix multiplication
These are the human proofs of Cache-oblivious matrix multiplication, at full length.
The recursion that halves the largest dimension computes AB, and its miss 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 on both sides. Commutativity of the multiplication and subtraction are not used. The call has of size , of size and of size , all given as index ranges into larger row-major arrays.
Correctness
Claim. For all and , the call ends with replaced by , and it changes no other entry of the arrays.
Proof by strong induction on . If , line L1 runs the triple loop, which adds to each of the range and touches nothing else.
Otherwise . The tests of lines L2 to L4 pick a dimension that is a largest one: line L2 when is a maximum, line L3 when is not a maximum and (then is a maximum), and line L4 when is the unique maximum. The picked dimension splits into halves of sizes and . So both calls have a smaller product of sides, and the induction hypothesis applies to them.
- Line L2. With and split at the same row, . The first call adds to and touches only ; the second adds to .
- Line L3. With and split at the same inner index, block multiplication gives . The first call adds to , the second adds , and since addition is associative and commutative the result is .
- Line L4. With and split at the same column, , and the two calls add the two parts to and .
In each case the factor from stays on the left.
Started with , the method returns . When , or is there is nothing to do; the implementation returns the zero matrix at once.
Summation order. Fix an entry . Lines L2 and L4 send it to exactly one of the two calls. Line L3 sends it to both, first with the lower half of the inner range, then with the upper half. By induction, the terms reach with increasing, in the same order as in the plain triple loop. So in floating point the two methods give the same result, provided the leaves use the same order.
Termination and depth
Each call either stops at line L1 or makes two calls with a smaller product , so the recursion is finite.
Along a path from the root, each split replaces one side by or , both at most . Since , a side that has been split times is at most , which is once . A side of size is never split, since only a largest side of size at least is split. So each path has at most splits, which bounds the depth. The stack therefore holds frames.
The miss bound for square matrices
Let be a power of two and , with . The cache is ideal: words, lines of words, fully associative, with the optimal offline replacement.
Shape of the recursion. For a cube of side , line L2 splits (all sides are equal), then line L3 splits (now ), then line L4 splits . After these three levels, there are cubes of side . So the subproblems at depth are cubes of side , and the method is the sequence of their calls, in some order.
Lemma 1 (footprint). A subproblem on a cube of side reads and writes words of three submatrices of row-major arrays. Each of the rows is a run of consecutive words, which touches at most lines. So the subproblem touches at most lines. If , this is at most lines, that is words.
Proof. For , . The count of lines for a run is shown on the blocked multiplication proofs page, Lemma 1.
Lemma 2 (a subproblem that fits). If a subproblem touches at most distinct lines, the ideal cache serves it with at most that many misses, whatever the cache holds at the start.
Proof. The replacement rule that never evicts a line of the subproblem during it, and evicts other lines when room is needed, misses at most once per line. The optimal rule misses no more often.
Lemma 3 (concatenation). For any sequences of accesses, the optimal number of misses of the concatenation, from any start state, is at most the sum of the optimal numbers for the pieces started from an empty cache.
Proof. Run, on each piece in turn, a copy of the optimal schedule for that piece started from an empty cache, and keep the invariant that every line held by the copy is also in the real cache. When the copy loads a line, the real cache loads it too unless it is already there, and if it must evict, it evicts a line that the copy does not hold. Such a line exists, because just before the load the copy holds fewer than lines and the real cache is full. So the real cache misses only when the copy misses, and the optimal schedule of the concatenation misses no more than this one.
Theorem. Let . If and , the method makes at most misses. If , it makes at most misses.
Proof. The first case is Lemma 1 and Lemma 2 for the whole problem.
In the second case let be the largest power of two with and . Since , we have , so and by the choice of . So , and since , also . The computation is the sequence of the subproblems at depth , which are cubes of side . By Lemma 1 each touches at most lines, so by Lemma 2 each costs at most misses from an empty cache. By Lemma 3 the total is at most
LRU. Sleator and Tarjan (1985) proved that LRU with lines misses at most times as often as the optimal rule with lines, plus . With and , LRU with words makes at most about twice the misses of the ideal cache with words. If , the theorem with in place of gives at most misses, up to the additive term, for LRU with words when .
General shapes. For all , , with a tall cache, Frigo, Leiserson, Prokop and Ramachandran prove the bound . Their proof follows the same plan, with the first subproblems that fit in the cache in place of the cubes of side ; it is not reproduced here.