Matrix multiplication algorithm

Because matrix multiplication is such a central operation in many numerical algorithms, much work has been invested in making matrix multiplication algorithms efficient. Applications of matrix multiplication in computational problems are found in many fields including scientific computing and pattern recognition and in seemingly unrelated problems such as counting the paths through a graph. Many different algorithms have been designed for multiplying matrices on different types of hardware, including parallel and distributed systems, where the computational work is spread over multiple processors (perhaps over a network).

Directly applying the mathematical definition of matrix multiplication gives an algorithm that takes time on the order of $n^{3}$ field operations to multiply two $n × n$ matrices over that field ($Θ(n^{3})$ in big O notation). Better asymptotic bounds on the time required to multiply matrices have been known since the Strassen's algorithm in the 1960s, but the optimal time (that is, the computational complexity of matrix multiplication) remains unknown. , the best announced bound on the asymptotic complexity of a matrix multiplication algorithm is $O(n^{2.371552})$ time, given by Williams, Xu, Xu, and Zhou. This improves on the bound of $O(n^{2.3728596})$ time, given by Alman and Williams. However, this algorithm is a galactic algorithm because of the large constants and cannot be realized practically.

Iterative algorithm
The definition of matrix multiplication is that if $C = AB$ for an $n × m$ matrix $A$ and an $m × p$ matrix $B$, then $C$ is an $n × p$ matrix with entries


 * $$c_{ij} = \sum_{k=1}^m a_{ik} b_{kj}.$$

From this, a simple algorithm can be constructed which loops over the indices $i$ from 1 through $n$ and $j$ from 1 through $p$, computing the above using a nested loop:


 * Input: matrices $A$ and $B$
 * Let $C$ be a new matrix of the appropriate size
 * For $i$ from 1 to $n$:
 * For $j$ from 1 to $p$:
 * Let $sum = 0$
 * For $k$ from 1 to $m$:
 * Set $sum ← sum + A_{ik} × B_{kj}$
 * Set $C_{ij} ← sum$
 * Return $C$

This algorithm takes time $Θ(nmp)$ (in asymptotic notation). A common simplification for the purpose of algorithm analysis is to assume that the inputs are all square matrices of size $n × n$, in which case the running time is $Θ(n^{3})$, i.e., cubic in the size of the dimension.

Cache behavior
The three loops in iterative matrix multiplication can be arbitrarily swapped with each other without an effect on correctness or asymptotic running time. However, the order can have a considerable impact on practical performance due to the memory access patterns and cache use of the algorithm; which order is best also depends on whether the matrices are stored in row-major order, column-major order, or a mix of both.

In particular, in the idealized case of a fully associative cache consisting of $M$ bytes and $b$ bytes per cache line (i.e. $M⁄b$ cache lines), the above algorithm is sub-optimal for $A$ and $B$ stored in row-major order. When $n > M⁄b$, every iteration of the inner loop (a simultaneous sweep through a row of $A$ and a column of $B$) incurs a cache miss when accessing an element of $B$. This means that the algorithm incurs $Θ(n^{3})$ cache misses in the worst case. , the speed of memories compared to that of processors is such that the cache misses, rather than the actual calculations, dominate the running time for sizable matrices.

The optimal variant of the iterative algorithm for $A$ and $B$ in row-major layout is a tiled version, where the matrix is implicitly divided into square tiles of size $\sqrt{M}$ by $\sqrt{M}$:


 * Input: matrices $A$ and $B$
 * Let $C$ be a new matrix of the appropriate size
 * Pick a tile size $T = Θ(\sqrt{M})$
 * For $I$ from 1 to $n$ in steps of $T$:
 * For $J$ from 1 to $p$ in steps of $T$:
 * For $K$ from 1 to $m$ in steps of $T$:
 * Multiply $A_{I:I+T, K:K+T}$ and $B_{K:K+T, J:J+T}$ into $C_{I:I+T, J:J+T}$, that is:
 * For $i$ from $I$ to $min(I + T, n)$:
 * For $j$ from $J$ to $min(J + T, p)$:
 * Let $sum = 0$
 * For $k$ from $K$ to $min(K + T, m)$:
 * Set $sum ← sum + A_{ik} × B_{kj}$
 * Set $C_{ij} ← C_{ij} + sum$
 * Return $C$

In the idealized cache model, this algorithm incurs only $Θ(n^{3}⁄b \sqrt{M})$ cache misses; the divisor $b \sqrt{M}$ amounts to several orders of magnitude on modern machines, so that the actual calculations dominate the running time, rather than the cache misses.

Divide-and-conquer algorithm
An alternative to the iterative algorithm is the divide-and-conquer algorithm for matrix multiplication. This relies on the block partitioning


 * $$C = \begin{pmatrix}

C_{11} & C_{12} \\ C_{21} & C_{22} \\ \end{pmatrix},\, A = \begin{pmatrix} A_{11} & A_{12} \\ A_{21} & A_{22} \\ \end{pmatrix},\, B = \begin{pmatrix} B_{11} & B_{12} \\ B_{21} & B_{22} \\ \end{pmatrix},$$

which works for all square matrices whose dimensions are powers of two, i.e., the shapes are $2^{n} × 2^{n}$ for some $n$. The matrix product is now


 * $$\begin{pmatrix}

C_{11} & C_{12} \\ C_{21} & C_{22} \\ \end{pmatrix} = \begin{pmatrix} A_{11} & A_{12} \\ A_{21} & A_{22} \\ \end{pmatrix} \begin{pmatrix} B_{11} & B_{12} \\ B_{21} & B_{22} \\ \end{pmatrix} = \begin{pmatrix} A_{11} B_{11} + A_{12} B_{21} & A_{11} B_{12} + A_{12} B_{22}\\ A_{21} B_{11} + A_{22} B_{21} & A_{21} B_{12} + A_{22} B_{22}\\ \end{pmatrix} $$

which consists of eight multiplications of pairs of submatrices, followed by an addition step. The divide-and-conquer algorithm computes the smaller multiplications recursively, using the scalar multiplication $c_{11} = a_{11}b_{11}$ as its base case.

The complexity of this algorithm as a function of $n$ is given by the recurrence


 * $$T(1) = \Theta(1);$$
 * $$T(n) = 8T(n/2) + \Theta(n^2),$$

accounting for the eight recursive calls on matrices of size $n/2$ and $Θ(n^{2})$ to sum the four pairs of resulting matrices element-wise. Application of the master theorem for divide-and-conquer recurrences shows this recursion to have the solution $Θ(n^{3})$, the same as the iterative algorithm.

Non-square matrices
A variant of this algorithm that works for matrices of arbitrary shapes and is faster in practice splits matrices in two instead of four submatrices, as follows. Splitting a matrix now means dividing it into two parts of equal size, or as close to equal sizes as possible in the case of odd dimensions.


 * Inputs: matrices $A$ of size $n × m$, $B$ of size $m × p$.
 * Base case: if $max(n, m, p)$ is below some threshold, use an unrolled version of the iterative algorithm.
 * Recursive cases:


 * If $max(n, m, p) = n$, split $A$ horizontally:


 * $$C = \begin{pmatrix} A_1 \\ A_2 \end{pmatrix} {B}

= \begin{pmatrix} A_1 B \\ A_2 B \end{pmatrix}$$


 * Else, if $max(n, m, p) = p$, split $B$ vertically:


 * $$C = A \begin{pmatrix} B_1 & B_2 \end{pmatrix}

= \begin{pmatrix} A B_1 & A B_2 \end{pmatrix} $$


 * Otherwise, $max(n, m, p) = m$. Split $A$ vertically and $B$ horizontally:


 * $$C = \begin{pmatrix} A_1 & A_2 \end{pmatrix} \begin{pmatrix} B_1 \\ B_2 \end{pmatrix}

= A_1 B_1 + A_2 B_2$$

Cache behavior
The cache miss rate of recursive matrix multiplication is the same as that of a tiled iterative version, but unlike that algorithm, the recursive algorithm is cache-oblivious: there is no tuning parameter required to get optimal cache performance, and it behaves well in a multiprogramming environment where cache sizes are effectively dynamic due to other processes taking up cache space. (The simple iterative algorithm is cache-oblivious as well, but much slower in practice if the matrix layout is not adapted to the algorithm.)

The number of cache misses incurred by this algorithm, on a machine with $M$ lines of ideal cache, each of size $b$ bytes, is bounded by


 * $$\Theta \left(m + n + p + \frac{mn + np + mp}{b} + \frac{mnp}{b\sqrt{M}} \right)$$

Sub-cubic algorithms


Algorithms exist that provide better running times than the straightforward ones. The first to be discovered was Strassen's algorithm, devised by Volker Strassen in 1969 and often referred to as "fast matrix multiplication". It is based on a way of multiplying two $ω$-matrices which requires only 7 multiplications (instead of the usual 8), at the expense of several additional addition and subtraction operations. Applying this recursively gives an algorithm with a multiplicative cost of $$O( n^{\log_{2}7}) \approx O(n^{2.807})$$. Strassen's algorithm is more complex, and the numerical stability is reduced compared to the naïve algorithm, but it is faster in cases where $2 × 2$ or so and appears in several libraries, such as BLAS. It is very useful for large matrices over exact domains such as finite fields, where numerical stability is not an issue.

Since Strassen's algorithm is actually used in practical numerical software and computer algebra systems improving on the constants hidden in the big O notation has its merits. A table which compares key aspects of improved version based on recursive multiplication of 2x2-block matrices via 7 block matrix multiplications follows. As usual $$n$$ gives the dimensions of the matrix and $$M$$ designates the memory size.

It is known that a Strassen-like algorithm with a 2x2-block matrix step requires at least 7 block matrix multiplications. In 1976 Probert showed that such an algorithm requires at least 15 additions (including subtractions), however a hidden assumption was that the blocks and the 2x2-blockmatrix are represented in the same basis. Karstadt and Schwartz computed in different bases and traded 3 additions for less expensive basis transformations. They also proved that one cannot go below 12 additions per step using different bases. In subsequent work Beniamini et el. applied this base change trick to more general decompositions than 2x2-blockmatrices and improved leading constant for their run times.

It is an open question in theoretical computer science how well Strassen's algorithm can be improved in terms of asymptotic complexity. The matrix multiplication exponent, usually denoted $$\omega$$, is the smallest real number for which any $$n\times n$$ matrix over a field can be multiplied together using $$n^{\omega + o(1)}$$ field operations. The current best bound on $$\omega$$ is $$\omega < 2.371552$$, by Williams, Xu, Xu, and Zhou. This algorithm, like all other recent algorithms in this line of research, is a generalization of the Coppersmith–Winograd algorithm, which was given by Don Coppersmith and Shmuel Winograd in 1990. The conceptual idea of these algorithms are similar to Strassen's algorithm: a way is devised for multiplying two $n > 100$-matrices with fewer than $k × k$ multiplications, and this technique is applied recursively. However, the constant coefficient hidden by the Big O notation is so large that these algorithms are only worthwhile for matrices that are too large to handle on present-day computers.

Freivalds' algorithm is a simple Monte Carlo algorithm that, given matrices $A$, $B$ and $C$, verifies in $k^{3}$ time if $ω = 2$.

AlphaTensor
In 2022, DeepMind introduced AlphaTensor, a neural network that used a single-player game analogy to invent thousands of matrix multiplication algorithms, including some previously discovered by humans and some that were not. Operations were restricted to the non-commutative ground field (normal arithmetic) and finite field $\mathbb Z/2\mathbb Z$ (mod 2 arithmetic). The best "practical" (explicit low-rank decomposition of a matrix multiplication tensor) algorithm found ran in O(n2.778). Finding low-rank decompositions of such tensors (and beyond) is NP-hard; optimal multiplication even for 3x3 matrices remains unknown, even in commutative field. On 4x4 matrices, AlphaTensor unexpectedly discovered a solution with 47 multiplication steps, an improvement over the 49 required with Strassen’s algorithm of 1969, albeit restricted to mod 2 arithmetic. Similarly, AlphaTensor solved 5x5 matrices with 96 rather than Strassen's 98 steps. Based on the surprising discovery that such improvements exist, other researchers were quickly able to find a similar independent 4x4 algorithm, and separately tweaked Deepmind's 96-step 5x5 algorithm down to 95 steps in mod 2 arithmetic and to 97 in normal arithmetic. Some algorithms were never discovered before, e.g. (4, 5, 5) got improved to 76 from 80 in normal and mod 2 arithmetics.

Shared-memory parallelism
The divide-and-conquer algorithm sketched earlier can be parallelized in two ways for shared-memory multiprocessors. These are based on the fact that the eight recursive matrix multiplications in


 * $$\begin{pmatrix}

A_{11} B_{11} + A_{12} B_{21} & A_{11} B_{12} + A_{12} B_{22}\\ A_{21} B_{11} + A_{22} B_{21} & A_{21} B_{12} + A_{22} B_{22}\\ \end{pmatrix} $$

can be performed independently of each other, as can the four summations (although the algorithm needs to "join" the multiplications before doing the summations). Exploiting the full parallelism of the problem, one obtains an algorithm that can be expressed in fork–join style pseudocode:

Procedure $Θ(n^{2})$:


 * Base case: if $AB = C$, set $multiply(C, A, B)$ (or multiply a small block matrix).
 * Otherwise, allocate space for a new matrix $T$ of shape $n = 1$, then:
 * Partition $A$ into $c_{11} ← a_{11} × b_{11}$, $n × n$, $A_{11}$, $A_{12}$.
 * Partition $B$ into $A_{21}$, $A_{22}$, $B_{11}$, $B_{12}$.
 * Partition $C$ into $B_{21}$, $B_{22}$, $C_{11}$, $C_{12}$.
 * Partition $T$ into $C_{21}$, $C_{22}$, $T_{11}$, $T_{12}$.
 * Parallel execution:
 * Fork $T_{21}$.
 * Fork $T_{22}$.
 * Fork $multiply(C_{11}, A_{11}, B_{11})$.
 * Fork $multiply(C_{12}, A_{11}, B_{12})$.
 * Fork $multiply(C_{21}, A_{21}, B_{11})$.
 * Fork $multiply(C_{22}, A_{21}, B_{12})$.
 * Fork $multiply(T_{11}, A_{12}, B_{21})$.
 * Fork $multiply(T_{12}, A_{12}, B_{22})$.
 * Join (wait for parallel forks to complete).
 * Deallocate $T$.
 * Deallocate $T$.

Procedure $multiply(T_{21}, A_{22}, B_{21})$ adds $C$ into $C$, element-wise:


 * Base case: if $multiply(T_{22}, A_{22}, B_{22})$, set $add(C, T)$ (or do a short loop, perhaps unrolled).
 * Otherwise:
 * Partition $T$ into $add(C, T)$, $n = 1$, $c_{11} ← c_{11} + t_{11}$, $C_{11}$.
 * Partition $T$ into $C_{12}$, $C_{21}$, $C_{22}$, $T_{11}$.
 * In parallel:
 * Fork $T_{12}$.
 * Fork $T_{21}$.
 * Fork $T_{22}$.
 * Fork $add(C_{11}, T_{11})$.
 * Join.

Here, fork is a keyword that signal a computation may be run in parallel with the rest of the function call, while join waits for all previously "forked" computations to complete. $add(C_{12}, T_{12})$ achieves its goal by pointer manipulation only.

This algorithm has a critical path length of $add(C_{21}, T_{21})$ steps, meaning it takes that much time on an ideal machine with an infinite number of processors; therefore, it has a maximum possible speedup of $add(C_{22}, T_{22})$ on any real computer. The algorithm isn't practical due to the communication cost inherent in moving data to and from the temporary matrix $C$, but a more practical variant achieves $partition$ speedup, without using a temporary matrix.



Communication-avoiding and distributed algorithms
On modern architectures with hierarchical memory, the cost of loading and storing input matrix elements tends to dominate the cost of arithmetic. On a single machine this is the amount of data transferred between RAM and cache, while on a distributed memory multi-node machine it is the amount transferred between nodes; in either case it is called the communication bandwidth. The naïve algorithm using three nested loops uses $Θ(log^{2} n)$ communication bandwidth.

Cannon's algorithm, also known as the 2D algorithm, is a communication-avoiding algorithm that partitions each input matrix into a block matrix whose elements are submatrices of size $Θ(n^{3}/log^{2} n)$ by $Θ(n^{2})$, where $Ω(n^{3})$ is the size of fast memory. The naïve algorithm is then used over the block matrices, computing products of submatrices entirely in fast memory. This reduces communication bandwidth to $√M/3$, which is asymptotically optimal (for algorithms performing $√M/3$ computation).

In a distributed setting with $A$ processors arranged in a $M$ by $O(n^{3}/√M)$ 2D mesh, one submatrix of the result can be assigned to each processor, and the product can be computed with each processor transmitting $Ω(n^{3})$ words, which is asymptotically optimal assuming that each node stores the minimum $√p$ elements. This can be improved by the 3D algorithm, which arranges the processors in a 3D cube mesh, assigning every product of two input submatrices to a single processor. The result submatrices are then generated by performing a reduction over each row. This algorithm transmits $√p$ words per processor, which is asymptotically optimal. However, this requires replicating each input matrix element $O(n^{2}/√p)$ times, and so requires a factor of $O(n^{2}/p)$ more memory than is needed to store the inputs. This algorithm can be combined with Strassen to further reduce runtime. "2.5D" algorithms provide a continuous tradeoff between memory usage and communication bandwidth. On modern distributed computing environments such as MapReduce, specialized multiplication algorithms have been developed.

Algorithms for meshes


There are a variety of algorithms for multiplication on meshes. For multiplication of two n×n on a standard two-dimensional mesh using the 2D Cannon's algorithm, one can complete the multiplication in 3n-2 steps although this is reduced to half this number for repeated computations. The standard array is inefficient because the data from the two matrices does not arrive simultaneously and it must be padded with zeroes.

The result is even faster on a two-layered cross-wired mesh, where only 2n-1 steps are needed. The performance improves further for repeated computations leading to 100% efficiency. The cross-wired mesh array may be seen as a special case of a non-planar (i.e. multilayered) processing structure.