Algorithm Search⌘K

Proofs: Cannon's algorithm

These are the human proofs of Cannon's algorithm, at full length.

Cannon's schedule computes AB, and its BSP cost full

This proof shows: correctness, termination.

Let RR be a semiring (addition associative and commutative with a zero, multiplication associative and distributive on both sides); commutativity of the multiplication and subtraction are not used. Let q≥1q \ge 1. The matrix AA is split into q×qq \times q blocks AikA_{ik} of size s1×s2s_1 \times s_2, and BB into blocks BkjB_{kj} of size s2×s3s_2 \times s_3, after padding with zeros so that qq divides each side. All indices of blocks and processors are taken modulo qq, in {0,…,q−1}\{0, \ldots, q-1\}.

Padding

Let A′A' and B′B' be AA and BB with zero rows and columns added at the bottom and right. Then the top-left block of A′B′A'B' of the size of ABAB equals ABAB, because each added row of A′A' or column of B′B' is zero, and each added column of A′A' meets an added row of B′B', so it contributes only products with a zero factor. So it is enough to prove that the schedule computes A′B′A'B'; from now on AA and BB denote the padded matrices.

The skew

Line L2 sends the AA block of (i,j)(i, j) to (i,j−i)(i, j - i). For fixed ii the map j↦j−ij \mapsto j - i is a bijection of Z/q\mathbb{Z}/q, so each processor (i,j′)(i, j') receives exactly one AA block, from (i,j′+i)(i, j' + i), namely Ai,i+j′A_{i, i + j'}. In the same way line L2 sends the BB block of (i,j)(i, j) to (i−j,j)(i - j, j), a bijection of the column for fixed jj, so (i′,j)(i', j) receives Bi′+j,jB_{i' + j, j}. So after line L2, processor (i,j)(i, j) holds Ai,kA_{i, k} and Bk,jB_{k, j} with k=i+jk = i + j.

The loop

Invariant. At the start of the pass with index tt of line L3, for 0≤t≤q−10 \le t \le q - 1, processor (i,j)(i, j) holds Ai,ktA_{i, k_t} and Bkt,jB_{k_t, j} with kt=i+j+t(modq)k_t = i + j + t \pmod q, and

Cij=∑u=0t−1Ai, i+j+u Bi+j+u, j.C_{ij} = \sum_{u = 0}^{t - 1} A_{i,\, i + j + u}\, B_{i + j + u,\, j}.

Initialization. For t=0t = 0 the sum is empty, which matches line L1, and the blocks are those of the skew.

Maintenance. Line L4 adds Ai,ktBkt,jA_{i, k_t} B_{k_t, j} to CijC_{ij}, which extends the sum to u=tu = t. If t<q−1t < q - 1, line L5 sends the AA block of every (i,j)(i, j) to (i,j−1)(i, j - 1) and the BB block to (i−1,j)(i - 1, j). These are bijections, so (i,j)(i, j) receives the AA block of (i,j+1)(i, j + 1), which is Ai, i+(j+1)+t=Ai,kt+1A_{i,\, i + (j + 1) + t} = A_{i, k_{t+1}}, and the BB block of (i+1,j)(i + 1, j), which is B(i+1)+j+t, j=Bkt+1,jB_{(i+1) + j + t,\, j} = B_{k_{t+1}, j}. So the invariant holds at the start of pass t+1t + 1.

End. After pass t=q−1t = q - 1, Cij=∑u=0q−1Ai, i+j+uBi+j+u, jC_{ij} = \sum_{u=0}^{q-1} A_{i,\, i+j+u} B_{i+j+u,\, j}. As uu runs over {0,…,q−1}\{0, \ldots, q - 1\}, i+j+u mod qi + j + u \bmod q runs over every residue exactly once, so, since addition is commutative and associative,

Cij=∑k=0q−1AikBkj,C_{ij} = \sum_{k=0}^{q-1} A_{ik} B_{kj},

which is block (i,j)(i, j) of ABAB by block multiplication. In each product the AA block is on the left. □\square

Termination

The loop of line L3 has qq passes, each local product at line L4 is a finite sequential computation, and each exchange moves a finite number of blocks. So the schedule ends after the skew and qq passes.

The BSP cost

Let qq divide the sides and s1=n/qs_1 = n/q, s2=m/qs_2 = m/q, s3=p/qs_3 = p/q.

  • Supersteps. The skew is one superstep, and each of the qq passes is one (the work of L4, then the exchange of L5 if t<q−1t < q - 1). So S=q+1S = q + 1.
  • Work. In each pass every processor does one product of an s1×s2s_1 \times s_2 and an s2×s3s_2 \times s_3 block, s1s2s3s_1 s_2 s_3 multiplications and as many additions. Over qq passes this is qs1s2s3=nmp/q2q s_1 s_2 s_3 = nmp/q^2 multiplications per processor, the same on every processor.
  • Communication. In the skew, each processor sends at most one AA block and one BB block and, by the bijections above, receives at most one of each. So the skew is an hh-relation with h≤s1s2+s2s3h \le s_1 s_2 + s_2 s_3, with equality when q≥2q \ge 2, because processor (1,1)(1, 1) then sends both of its blocks to other processors. Each exchange at line L5 with q≥2q \ge 2 moves every block to a different processor, so h=s1s2+s2s3h = s_1 s_2 + s_2 s_3. There are q−1q - 1 such exchanges. So
H=q(s1s2+s2s3)=nm+mpq,H = q(s_1 s_2 + s_2 s_3) = \frac{nm + mp}{q},

which is 2n2/q=2n2/P2n^2/q = 2n^2/\sqrt P for square matrices. For q=1q = 1 nothing moves and H=0H = 0.

The BSP cost is W+gH+LS=2n3/P+2g n2/P+L(P+1)W + gH + LS = 2n^3/P + 2g\,n^2/\sqrt P + L(\sqrt P + 1) for n×nn \times n matrices and P=q2P = q^2 processors, counting both multiplications and additions in WW.

Back to Cannon's algorithm