# Matrix multiplication algorithm

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Matrix_multiplication_algorithm
> Markdown URL: https://mediated.wiki/source/Matrix_multiplication_algorithm.md
> Source: https://en.wikipedia.org/wiki/Matrix_multiplication_algorithm
> Source revision: 1352365221
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

Algorithm to multiply matrices

Because [matrix multiplication](/source/Matrix_multiplication) is such a central operation in many [numerical algorithms](/source/Numerical_algorithm), 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](/source/Scientific_computing) and [pattern recognition](/source/Pattern_recognition) and in seemingly unrelated problems such as counting the paths through a [graph](/source/Graph_(graph_theory)).[1] Many different algorithms have been designed for multiplying matrices on different types of hardware, including [parallel](/source/Parallel_computing) and [distributed](/source/Distributed_computing) 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](/source/Analysis_of_algorithms) on the order of *n*3 [field](/source/Field_(mathematics)) operations to multiply two *n* × *n* matrices over that field (Θ(*n*3) in [big O notation](/source/Big_O_notation)). Better [asymptotic bounds](/source/Asymptotic_bound) on the time required to multiply matrices have been known since the [Strassen's algorithm](/source/Strassen_algorithm) in the 1960s, but the optimal time (that is, the [computational complexity of matrix multiplication](/source/Computational_complexity_of_matrix_multiplication)) remains unknown. As of September 2025[\[update\]](https://en.wikipedia.org/w/index.php?title=Matrix_multiplication_algorithm&action=edit), the best bound on the [asymptotic complexity](/source/Time_complexity) of a matrix multiplication algorithm is O(*n*2.371339) time, given by Alman, Duan, [Williams](/source/Virginia_Vassilevska_Williams), Xu, Xu, and Zhou.[2] However, this algorithm is a [galactic algorithm](/source/Galactic_algorithm) because of the large constants and cannot be realized practically.

## Iterative algorithm

The [definition of matrix multiplication](/source/Matrix_multiplication#Definition) 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 i j = ∑ k = 1 m a i k b k j . {\displaystyle 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 + *Aik* × *Bkj* - Set *Cij* ← sum

- Return C

This algorithm takes [time](/source/Time_complexity) Θ(*nmp*) (in [asymptotic notation](/source/Asymptotic_notation)).[1] A common simplification for the purpose of [algorithm analysis](/source/Analysis_of_algorithms) 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.[3]

### Cache behavior

Illustration of [row- and column-major order](/source/Row-_and_column-major_order)

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](/source/Locality_of_reference) and [cache](/source/CPU_cache) use of the algorithm;[1] which order is best also depends on whether the matrices are stored in [row-major order, column-major order](/source/Row-_and_column-major_order), or a mix of both.

In particular, in the idealized case of a [fully associative cache](/source/CPU_cache#Associativity) 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. As of 2010[\[update\]](https://en.wikipedia.org/w/index.php?title=Matrix_multiplication_algorithm&action=edit), 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.[4]

The optimal variant of the iterative algorithm for A and B in row-major layout is a *[tiled](/source/Loop_tiling)* version, where the matrix is implicitly divided into square tiles of size √*M* by √*M*:[4][5]

- Input: matrices A and B

- Let C be a new matrix of the appropriate size

- Pick a tile size *T* = Θ(√*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 + *Aik* × *Bkj* - Set *Cij* ← *Cij* + sum

- Return C

In the idealized cache model, this algorithm incurs only Θ(⁠*n*3/*b* √*M*⁠) cache misses; the divisor *b* √*M* amounts to several orders of magnitude on modern machines, so that the actual calculations dominate the running time, rather than the cache misses.[4]

## Divide-and-conquer algorithm

An alternative to the iterative algorithm is the [divide-and-conquer algorithm](/source/Divide-and-conquer_algorithm) for matrix multiplication. This relies on the [block partitioning](/source/Block_matrix)

- C = ( C 11 C 12 C 21 C 22 ) , A = ( A 11 A 12 A 21 A 22 ) , B = ( B 11 B 12 B 21 B 22 ) , {\displaystyle 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

- ( C 11 C 12 C 21 C 22 ) = ( A 11 A 12 A 21 A 22 ) ( B 11 B 12 B 21 B 22 ) = ( 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 ) {\displaystyle {\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](/source/Recursion), using the [scalar multiplication](/source/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[3]

- T ( 1 ) = Θ ( 1 ) ; {\displaystyle T(1)=\Theta (1);}

- T ( n ) = 8 T ( n / 2 ) + Θ ( n 2 ) , {\displaystyle 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](/source/Master_theorem_(analysis_of_algorithms)) shows this recursion to have the solution Θ(*n*3), the same as the iterative algorithm.[3]

### Non-square matrices

A variant of this algorithm that works for matrices of arbitrary shapes and is faster in practice[4] splits matrices in two instead of four submatrices, as follows.[6] 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](/source/Loop_unrolling) version of the iterative algorithm.

- Recursive cases:

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

- - C = ( A 1 A 2 ) B = ( A 1 B A 2 B ) {\displaystyle 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 ( B 1 B 2 ) = ( A B 1 A B 2 ) {\displaystyle C=A{\begin{pmatrix}B_{1}&B_{2}\end{pmatrix}}={\begin{pmatrix}AB_{1}&AB_{2}\end{pmatrix}}}

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

- - C = ( A 1 A 2 ) ( B 1 B 2 ) = A 1 B 1 + A 2 B 2 {\displaystyle 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](/source/Loop_tiling) iterative version, but unlike that algorithm, the recursive algorithm is [cache-oblivious](/source/Cache-oblivious_algorithm):[6] there is no tuning parameter required to get optimal cache performance, and it behaves well in a [multiprogramming](/source/Multiprogramming) environment where cache sizes are effectively dynamic due to other processes taking up cache space.[4] (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[6]: 13

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

## Sub-cubic algorithms

Further information: [Computational complexity of matrix multiplication](/source/Computational_complexity_of_matrix_multiplication)

Improvement of estimates of exponent ω over time for the computational complexity of matrix multiplication

        O
        (

          n

            ω

        )

    {\displaystyle O(n^{\omega })}

.

Algorithms exist that provide better running times than the straightforward ones. The first to be discovered was [Strassen's algorithm](/source/Strassen_algorithm), devised by [Volker Strassen](/source/Volker_Strassen) in 1969 and often referred to as "fast matrix multiplication". It is based on a way of multiplying two 2×2 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 ) ≈ O ( n 2.807 ) {\displaystyle O(n^{\log _{2}7})\approx O(n^{2.807})} . Strassen's algorithm is more complex, and the [numerical stability](/source/Numerical_stability) is reduced compared to the naïve algorithm,[7] but it is faster in cases where *n* > 100 or so[1] and appears in several libraries, such as [BLAS](/source/Basic_Linear_Algebra_Subprograms).[8] It is very useful for large matrices over exact domains such as [finite fields](/source/Finite_field), where numerical stability is not an issue.

Since Strassen's algorithm is actually used in practical numerical software and [computer algebra systems](/source/Computer_algebra_system), improving on the constants hidden in the [big-O notation](/source/Big_O_notation) has its merits. A table that compares key aspects of the improved version based on recursive multiplication of 2×2-block matrices via 7 block matrix multiplications follows. As usual, n {\displaystyle n} gives the dimensions of the matrix and M {\displaystyle M} designates the memory size.

Progress for Strassen-like recursive 2x2-block matrix multiplication Year Reference #matrix multiplications per step #matrix additions per step total arithmetic operations total I/O-complexity 1969 Strassen[9] 7 18 7 n log 2 ⁡ 7 − 6 n 2 {\displaystyle 7n^{\log _{2}7}-6n^{2}} 6 ( 3 n M ) log 2 ⁡ 7 ⋅ M − 18 n 2 + 3 M {\displaystyle 6\left({\frac {{\sqrt {3}}n}{\sqrt {M}}}\right)^{\log _{2}7}\cdot M-18n^{2}+3M} 1971 Winograd[10] 7 15 6 n log 2 ⁡ 7 − 5 n 2 {\displaystyle 6n^{\log _{2}7}-5n^{2}} 5 ( 3 n M ) log 2 ⁡ 7 ⋅ M − 15 n 2 + 3 M {\displaystyle 5\left({\frac {{\sqrt {3}}n}{\sqrt {M}}}\right)^{\log _{2}7}\cdot M-15n^{2}+3M} 2017 Karstadt, Schwartz[11] 7 12 5 n log 2 ⁡ 7 − 4 n 2 + 3 n 2 log 2 ⁡ n {\displaystyle 5n^{\log _{2}7}-4n^{2}+3n^{2}\log _{2}n} 4 ( 3 n M ) log 2 ⁡ 7 ⋅ M − 12 n 2 + 3 n 2 ⋅ log 2 ⁡ ( 2 n M ) + 5 M {\displaystyle 4\left({\frac {{\sqrt {3}}n}{\sqrt {M}}}\right)^{\log _{2}7}\cdot M-12n^{2}+3n^{2}\cdot \log _{2}\left({\frac {{\sqrt {2}}n}{\sqrt {M}}}\right)+5M} 2023 Schwartz, Vaknin[12] 7 12 5 n log 2 ⁡ 7 − 4 n 2 + 1.5 n 2 log 2 ⁡ n {\displaystyle 5n^{\log _{2}7}-4n^{2}+1.5n^{2}\log _{2}n} 4 ( 3 n M ) log 2 ⁡ 7 ⋅ M − 12 n 2 + 1.5 n 2 ⋅ log 2 ⁡ ( 2 n M ) + 5 M {\displaystyle 4\left({\frac {{\sqrt {3}}n}{\sqrt {M}}}\right)^{\log _{2}7}\cdot M-12n^{2}+1.5n^{2}\cdot \log _{2}\left({\frac {{\sqrt {2}}n}{\sqrt {M}}}\right)+5M}

It is known that a Strassen-like algorithm with a 2×2-block matrix step requires at least 7 block matrix multiplications. In 1976 Probert[13] showed that such an algorithm requires at least 15 additions (including subtractions); however, a hidden assumption was that the blocks and the 2×2-block matrix 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.[14] applied this base-change trick to more general decompositions than 2×2-block matrices and improved the leading constant for their run times.

It is an open question in [theoretical computer science](/source/Theoretical_computer_science) how well Strassen's algorithm can be improved in terms of [asymptotic complexity](/source/Time_complexity). The *matrix multiplication exponent*, usually denoted ω {\displaystyle \omega } , is the smallest [real number](/source/Real_number) for which any n × n {\displaystyle n\times n} matrix over a field can be multiplied together using n ω + o ( 1 ) {\displaystyle n^{\omega +o(1)}} field operations. The current best bound on ω {\displaystyle \omega } is ω < 2.371339 {\displaystyle \omega <2.371339} , by Alman, Duan, [Williams](/source/Virginia_Vassilevska_Williams), Xu, Xu, and Zhou.[2] 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](/source/Don_Coppersmith) and [Shmuel Winograd](/source/Shmuel_Winograd) in 1990.[15] The conceptual idea of these algorithms is similar to Strassen's algorithm: a way is devised for multiplying two *k* × *k*-matrices with fewer than *k*3 multiplications, and this technique is applied recursively. However, the constant coefficient hidden by the [big-O notation](/source/Big_O_notation) is so large that these algorithms are only worthwhile for matrices that are too large to handle on present-day computers.[16][17] Victor Pan proposed so-called feasible sub-cubic matrix multiplication algorithms with an exponent slightly above 2.77, but in return with a much smaller hidden constant coefficient.[18]

[Freivalds' algorithm](/source/Freivalds'_algorithm) is a simple [Monte Carlo algorithm](/source/Monte_Carlo_algorithm) that, given matrices A, B and C, verifies in Θ(*n*2) time if *AB* = *C*.

### AlphaTensor

In 2022, [DeepMind](/source/DeepMind) introduced AlphaTensor, a [neural network](/source/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.[19] Operations were restricted to the non-commutative ground field[*[clarification needed](https://en.wikipedia.org/wiki/Wikipedia:Please_clarify)*] (normal arithmetic) and [finite field Z / 2 Z {\displaystyle \mathbb {Z} /2\mathbb {Z} }](/source/GF(2)) (mod 2 arithmetic). The best "practical" (explicit low-rank decomposition of a matrix multiplication tensor) algorithm found ran in O(n2.778).[20] Finding low-rank decompositions of such tensors (and beyond) is NP-hard; optimal multiplication even for 3×3 matrices [remains unknown](/source/Computational_complexity_of_matrix_multiplication#Minimizing_number_of_multiplications), even in commutative field.[20] On 4×4 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 5×5 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 4×4 algorithm, and separately tweaked Deepmind's 96-step 5×5 algorithm down to 95 steps in mod 2 arithmetic and to 97[21] in normal arithmetic.[22] Some algorithms were completely new: for example, (4, 5, 5) was improved to 76 steps from a baseline of 80 in both normal and mod 2 arithmetic.

## Parallel and distributed algorithms

### Shared-memory parallelism

The [divide-and-conquer algorithm](#Divide-and-conquer_algorithm) sketched earlier can be [parallelized](/source/Parallel_algorithm) in two ways for [shared-memory multiprocessors](/source/Shared-memory_multiprocessor). These are based on the fact that the eight recursive matrix multiplications in

- ( 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 ) {\displaystyle {\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](/source/Fork%E2%80%93join_model) [pseudocode](/source/Pseudocode):[23]

**Procedure** multiply(*C*, *A*, *B*):

- Base case: if *n* = 1, set *c*11 ← *a*11 × *b*11 (or multiply a small block matrix).

- Otherwise, allocate space for a new matrix T of shape *n* × *n*, then: - Partition A into *A*11, *A*12, *A*21, *A*22. - Partition B into *B*11, *B*12, *B*21, *B*22. - Partition C into *C*11, *C*12, *C*21, *C*22. - Partition T into *T*11, *T*12, *T*21, *T*22. - Parallel execution: - *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). - *Fork* multiply(*T*21, *A*22, *B*21). - *Fork* multiply(*T*22, *A*22, *B*22). - *Join* (wait for parallel forks to complete). - add(*C*, *T*). - Deallocate T.

**Procedure** add(*C*, *T*) adds T into C, element-wise:

- Base case: if *n* = 1, set *c*11 ← *c*11 + *t*11 (or do a short loop, perhaps unrolled).

- Otherwise: - Partition C into *C*11, *C*12, *C*21, *C*22. - Partition T into *T*11, *T*12, *T*21, *T*22. - In parallel: - *Fork* add(*C*11, *T*11). - *Fork* add(*C*12, *T*12). - *Fork* add(*C*21, *T*21). - *Fork* add(*C*22, *T*22). - *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. partition achieves its goal by pointer manipulation only.

This algorithm has a [critical path length](/source/Critical_path_length) of Θ(log2 *n*) steps, meaning it takes that much time on an ideal machine with an infinite number of processors; therefore, it has a maximum possible [speedup](/source/Speedup) of Θ(*n*3/log2 *n*) on any real computer. The algorithm isn't practical due to the communication cost inherent in moving data to and from the temporary matrix T, but a more practical variant achieves Θ(*n*2) speedup, without using a temporary matrix.[23]

Block matrix multiplication. In the 2D algorithm, each processor is responsible for one submatrix of C. In the 3D algorithm, every pair of submatrices from A and B that is multiplied is assigned to one processor.

### 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](/source/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 Ω(*n*3) communication bandwidth.

[Cannon's algorithm](/source/Cannon's_algorithm), also known as the *2D algorithm*, is a [communication-avoiding algorithm](/source/Communication-avoiding_algorithm) that partitions each input matrix into a block matrix whose elements are submatrices of size √*M*/3 by √*M*/3, where *M* is the size of fast memory.[24] The naïve algorithm is then used over the block matrices, computing products of submatrices entirely in fast memory. This reduces communication bandwidth to *O*(*n*3/√*M*), which is asymptotically optimal (for algorithms performing Ω(*n*3) computation).[25][26]

In a distributed setting with p processors arranged in a √*p* by √*p* 2D mesh, one submatrix of the result can be assigned to each processor, and the product can be computed with each processor transmitting *O*(*n*2/√*p*) words, which is asymptotically optimal assuming that each node stores the minimum *O*(*n*2/*p*) elements.[26] 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.[27] This algorithm transmits *O*(*n*2/*p*2/3) words per processor, which is asymptotically optimal.[26] However, this requires replicating each input matrix element *p*1/3 times, and so requires a factor of *p*1/3 more memory than is needed to store the inputs. This algorithm can be combined with Strassen to further reduce runtime.[27] "2.5D" algorithms provide a continuous tradeoff between memory usage and communication bandwidth.[28] On modern distributed computing environments such as [MapReduce](/source/MapReduce), specialized multiplication algorithms have been developed.[29]

### Algorithms for meshes

Matrix multiplication completed in 2n-1 steps for two n×n matrices on a cross-wired  mesh.

There are a variety of algorithms for multiplication on [meshes](/source/Mesh_networking). For multiplication of two *n*×*n* on a standard two-dimensional mesh using the 2D [Cannon's algorithm](/source/Cannon's_algorithm), one can complete the multiplication in 3*n*-2 steps although this is reduced to half this number for repeated computations.[30] 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 2*n*-1 steps are needed.[31] The performance improves further for repeated computations leading to 100% efficiency.[32] The cross-wired mesh array may be seen as a special case of a non-planar (i.e. multilayered) processing structure.[33]

In a 3D mesh with *n*3 processing elements, two matrices can be multiplied in O ( log ⁡ n ) {\displaystyle {\mathcal {O}}(\log n)} using the DNS algorithm.[34]

## See also

- [Computational complexity of mathematical operations](/source/Computational_complexity_of_mathematical_operations)

- [Computational complexity of matrix multiplication](/source/Computational_complexity_of_matrix_multiplication)

- [CYK algorithm § Valiant's algorithm](/source/CYK_algorithm#Valiant's_algorithm)

- [Matrix chain multiplication](/source/Matrix_chain_multiplication)

- [Method of Four Russians](/source/Method_of_Four_Russians)

- [Multiplication algorithm](/source/Multiplication_algorithm)

- [Sparse matrix–vector multiplication](/source/Sparse_matrix%E2%80%93vector_multiplication)

## References

1. ^ [***a***](#cite_ref-skiena_1-0) [***b***](#cite_ref-skiena_1-1) [***c***](#cite_ref-skiena_1-2) [***d***](#cite_ref-skiena_1-3) [Skiena, Steven](/source/Steven_Skiena) (2012). "Sorting and Searching". [*The Algorithm Design Manual*](https://archive.org/details/algorithmdesignm00skie_772). Springer. pp. [45](https://archive.org/details/algorithmdesignm00skie_772/page/n56)–46, 401–3. [doi](/source/Doi_(identifier)):[10.1007/978-1-84800-070-4_4](https://doi.org/10.1007%2F978-1-84800-070-4_4). [ISBN](/source/ISBN_(identifier)) [978-1-84800-069-8](https://en.wikipedia.org/wiki/Special:BookSources/978-1-84800-069-8).

1. ^ [***a***](#cite_ref-alman2025_2-0) [***b***](#cite_ref-alman2025_2-1) Alman, Josh; Duan, Ran; Williams, Virginia Vassilevska; Xu, Yinzhan; Xu, Zixuan; Zhou, Renfei (2025). ["More Asymmetry Yields Faster Matrix Multiplication"](https://epubs.siam.org/doi/10.1137/1.9781611978322.63). *Proceedings of the 2025 Annual ACM-SIAM Symposium on Discrete Algorithms (SODA)*. SIAM. pp. 2005–2039. [doi](/source/Doi_(identifier)):[10.1137/1.9781611978322.63](https://doi.org/10.1137%2F1.9781611978322.63).

1. ^ [***a***](#cite_ref-clrs_3-0) [***b***](#cite_ref-clrs_3-1) [***c***](#cite_ref-clrs_3-2) [Cormen, Thomas H.](/source/Thomas_H._Cormen); [Leiserson, Charles E.](/source/Charles_E._Leiserson); [Rivest, Ronald L.](/source/Ron_Rivest); [Stein, Clifford](/source/Clifford_Stein) (2009) [1990]. [*Introduction to Algorithms*](/source/Introduction_to_Algorithms) (3rd ed.). MIT Press and McGraw-Hill. pp. 75–79. [ISBN](/source/ISBN_(identifier)) [0-262-03384-4](https://en.wikipedia.org/wiki/Special:BookSources/0-262-03384-4).

1. ^ [***a***](#cite_ref-ocw_4-0) [***b***](#cite_ref-ocw_4-1) [***c***](#cite_ref-ocw_4-2) [***d***](#cite_ref-ocw_4-3) [***e***](#cite_ref-ocw_4-4) Amarasinghe, Saman; [Leiserson, Charles](/source/Charles_E._Leiserson) (2010). ["6.172 Performance Engineering of Software Systems, Lecture 8"](https://web.archive.org/web/20191007160029/http://aka-ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-172-performance-engineering-of-software-systems-fall-2010/video-lectures/lecture-8-cache-efficient-algorithms/). *MIT OpenCourseWare*. Massachusetts Institute of Technology. Archived from [the original](http://aka-ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-172-performance-engineering-of-software-systems-fall-2010/video-lectures/lecture-8-cache-efficient-algorithms/) on 7 October 2019. Retrieved 27 January 2015.

1. **[^](#cite_ref-5)** [Lam, Monica S.](/source/Monica_S._Lam); Rothberg, Edward E.; Wolf, Michael E. (1991). *The Cache Performance and Optimizations of Blocked Algorithms*. ASPLOS91: 4th Int'l Conference on Architecture Support for Programming Languages & Operating Systems. [doi](/source/Doi_(identifier)):[10.1145/106972.106981](https://doi.org/10.1145%2F106972.106981). [ISBN](/source/ISBN_(identifier)) [978-0-89791-380-5](https://en.wikipedia.org/wiki/Special:BookSources/978-0-89791-380-5).

1. ^ [***a***](#cite_ref-prokop_6-0) [***b***](#cite_ref-prokop_6-1) [***c***](#cite_ref-prokop_6-2) [Prokop, Harald](/source/Harald_Prokop) (1999). [*Cache-Oblivious Algorithms*](https://web.archive.org/web/20231122003937/http://supertech.csail.mit.edu/papers/Prokop99.pdf) (PDF) (Master's). MIT. [hdl](/source/Hdl_(identifier)):[1721.1/80568](https://hdl.handle.net/1721.1%2F80568). Archived from [the original](http://supertech.csail.mit.edu/papers/Prokop99.pdf) (PDF) on 2023-11-22. Retrieved 2015-01-28.

1. **[^](#cite_ref-7)** [Miller, Webb](/source/Webb_Miller) (1975), "Computational complexity and numerical stability", *SIAM News*, **4** (2): 97–107, [CiteSeerX](/source/CiteSeerX_(identifier)) [10.1.1.148.9947](https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.148.9947), [doi](/source/Doi_(identifier)):[10.1137/0204009](https://doi.org/10.1137%2F0204009)

1. **[^](#cite_ref-8)** Press, William H.; Flannery, Brian P.; [Teukolsky, Saul A.](/source/Saul_Teukolsky); Vetterling, William T. (2007). [*Numerical Recipes: The Art of Scientific Computing*](/source/Numerical_Recipes) (3rd ed.). [Cambridge University Press](/source/Cambridge_University_Press). p. [108](https://archive.org/details/numericalrecipes00pres_033/page/n131). [ISBN](/source/ISBN_(identifier)) [978-0-521-88068-8](https://en.wikipedia.org/wiki/Special:BookSources/978-0-521-88068-8).

1. **[^](#cite_ref-9)** [Strassen, Volker](/source/Volker_Strassen) (1969). "Gaussian Elimination is not Optimal". *Numer. Math*. **13** (4): 354–356. [doi](/source/Doi_(identifier)):[10.1007/BF02165411](https://doi.org/10.1007%2FBF02165411). [S2CID](/source/S2CID_(identifier)) [121656251](https://api.semanticscholar.org/CorpusID:121656251).

1. **[^](#cite_ref-10)** Private communication with Probert, Robert L (1976). ["On the additive complexity of matrix multiplication"](https://doi.org/10.1137%2F0205016). *SIAM Journal on Computing*. **5** (2): 187–203. [doi](/source/Doi_(identifier)):[10.1137/0205016](https://doi.org/10.1137%2F0205016).

1. **[^](#cite_ref-11)** Karstadt, Elaye; Schwartz, Oded (July 2017). ["Matrix Multiplication, a Little Faster"](https://dl.acm.org/doi/10.1145/3087556.3087579). *Proceedings of the 29th ACM Symposium on Parallelism in Algorithms and Architectures*. SPAA '17. pp. 101–110. [doi](/source/Doi_(identifier)):[10.1145/3087556.3087579](https://doi.org/10.1145%2F3087556.3087579).

1. **[^](#cite_ref-12)** Schwartz, Oded; Vaknin, Noa (2023). ["Pebbling Game and Alternative Basis for High Performance Matrix Multiplication"](https://doi.org/10.1137/22M1502719). *SIAM Journal on Scientific Computing*. pp. C277–C303. [doi](/source/Doi_(identifier)):[10.1137/22M1502719](https://doi.org/10.1137%2F22M1502719).

1. **[^](#cite_ref-13)** Probert, Robert L. (1976). "On the additive complexity of matrix multiplication". *SIAM J. Comput*. **5** (2): 187–203. [doi](/source/Doi_(identifier)):[10.1137/0205016](https://doi.org/10.1137%2F0205016).

1. **[^](#cite_ref-14)** Beniamini, Gal; Cheng, Nathan; [Holtz, Olga](/source/Olga_Holtz); Karstadt, Elaye; Schwartz, Oded (2020). "Sparsifying the Operators of Fast Matrix Multiplication Algorithms". [arXiv](/source/ArXiv_(identifier)):[2008.03759](https://arxiv.org/abs/2008.03759) [[cs.DS](https://arxiv.org/archive/cs.DS)].

1. **[^](#cite_ref-coppersmith_15-0)** [Coppersmith, Don](/source/Don_Coppersmith); [Winograd, Shmuel](/source/Shmuel_Winograd) (1990), ["Matrix multiplication via arithmetic progressions"](http://www.cs.umd.edu/~gasarch/TOPICS/ramsey/matrixmult.pdf) (PDF), *Journal of Symbolic Computation*, **9** (3): 251, [doi](/source/Doi_(identifier)):[10.1016/S0747-7171(08)80013-2](https://doi.org/10.1016%2FS0747-7171%2808%2980013-2)

1. **[^](#cite_ref-16)** Iliopoulos, Costas S. (1989), ["Worst-case complexity bounds on algorithms for computing the canonical structure of finite abelian groups and the Hermite and Smith normal forms of an integer matrix"](https://web.archive.org/web/20140305182030/http://www.williamstein.org/home/wstein/www/home/pernet/Papers/Hermite/Iliopoulos88.pdf) (PDF), *SIAM Journal on Computing*, **18** (4): 658–669, [CiteSeerX](/source/CiteSeerX_(identifier)) [10.1.1.531.9309](https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.531.9309), [doi](/source/Doi_(identifier)):[10.1137/0218045](https://doi.org/10.1137%2F0218045), [MR](/source/MR_(identifier)) [1004789](https://mathscinet.ams.org/mathscinet-getitem?mr=1004789), archived from [the original](http://www.williamstein.org/home/wstein/www/home/pernet/Papers/Hermite/Iliopoulos88.pdf) (PDF) on 2014-03-05, retrieved 2015-01-16, The Coppersmith–Winograd algorithm is not practical, due to the very large hidden constant in the upper bound on the number of multiplications required.

1. **[^](#cite_ref-robinson_17-0)** Robinson, Sara (November 2005), ["Toward an Optimal Algorithm for Matrix Multiplication"](https://archive.siam.org/pdf/news/174.pdf) (PDF), *SIAM News*, **38** (9), Even if someone manages to prove one of the conjectures—thereby demonstrating that *ω* = 2—the wreath product approach is unlikely to be applicable to the large matrix problems that arise in practice. [...] the input matrices must be astronomically large for the difference in time to be apparent.

1. **[^](#cite_ref-18)** Laderman, Julian; Pan, Victor; Sha, Xuan-He (1992), "On practical algorithms for accelerated matrix multiplication", *Linear Algebra and Its Applications*, 162–164: 557–588, [doi](/source/Doi_(identifier)):[10.1016/0024-3795(92)90393-O](https://doi.org/10.1016%2F0024-3795%2892%2990393-O)

1. **[^](#cite_ref-19)** ["Discovering novel algorithms with AlphaTensor"](https://www.deepmind.com/blog/discovering-novel-algorithms-with-alphatensor). *www.deepmind.com*. 5 October 2022. Retrieved 2022-11-01.

1. ^ [***a***](#cite_ref-alphatensor_20-0) [***b***](#cite_ref-alphatensor_20-1) Fawzi, Alhussein; Balog, Matej; Huang, Aja; Hubert, Thomas; Romera-Paredes, Bernardino; Barekatain, Mohammadamin; Novikov, Alexander; R. Ruiz, Francisco J.; Schrittwieser, Julian; Swirszcz, Grzegorz; Silver, David; Hassabis, Demis; Kohli, Pushmeet (October 2022). ["Discovering faster matrix multiplication algorithms with reinforcement learning"](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC9534758). *Nature*. **610** (7930): 47–53. [Bibcode](/source/Bibcode_(identifier)):[2022Natur.610...47F](https://ui.adsabs.harvard.edu/abs/2022Natur.610...47F). [doi](/source/Doi_(identifier)):[10.1038/s41586-022-05172-4](https://doi.org/10.1038%2Fs41586-022-05172-4). [ISSN](/source/ISSN_(identifier)) [1476-4687](https://search.worldcat.org/issn/1476-4687). [PMC](/source/PMC_(identifier)) [9534758](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC9534758). [PMID](/source/PMID_(identifier)) [36198780](https://pubmed.ncbi.nlm.nih.gov/36198780).

1. **[^](#cite_ref-21)** Kauers, Manuel; Moosbauer, Jakob (2022-12-02). "Flip Graphs for Matrix Multiplication". [arXiv](/source/ArXiv_(identifier)):[2212.01175](https://arxiv.org/abs/2212.01175) [[cs.SC](https://arxiv.org/archive/cs.SC)].

1. **[^](#cite_ref-22)** Brubaker, Ben (23 November 2022). ["AI Reveals New Possibilities in Matrix Multiplication"](https://www.quantamagazine.org/ai-reveals-new-possibilities-in-matrix-multiplication-20221123/). *Quanta Magazine*. Retrieved 26 November 2022.

1. ^ [***a***](#cite_ref-cilk_23-0) [***b***](#cite_ref-cilk_23-1) Randall, Keith H. (1998). [*Cilk: Efficient Multithreaded Computing*](https://web.archive.org/web/20201106232735/http://supertech.csail.mit.edu/papers/randall-phdthesis.pdf) (PDF) (Ph.D.). Massachusetts Institute of Technology. pp. 54–57. [hdl](/source/Hdl_(identifier)):[1721.1/47519](https://hdl.handle.net/1721.1%2F47519). Archived from [the original](http://supertech.csail.mit.edu/papers/randall-phdthesis.pdf) (PDF) on 2020-11-06. Retrieved 2015-01-16.

1. **[^](#cite_ref-24)** Cannon, Lynn Elliot (14 July 1969). [*A cellular computer to implement the Kalman Filter Algorithm*](https://dl.acm.org/doi/abs/10.5555/905686) (Ph.D.). Montana State University.

1. **[^](#cite_ref-25)** Hong, J. W.; [Kung, H. T.](/source/H._T._Kung) (1981). ["I/O complexity: The red-blue pebble game"](https://apps.dtic.mil/dtic/tr/fulltext/u2/a104739.pdf) (PDF). *Proceedings of the thirteenth annual ACM symposium on Theory of computing - STOC '81*. pp. 326–333. [doi](/source/Doi_(identifier)):[10.1145/800076.802486](https://doi.org/10.1145%2F800076.802486). [S2CID](/source/S2CID_(identifier)) [8410593](https://api.semanticscholar.org/CorpusID:8410593). [Archived](https://web.archive.org/web/20191215182754/https://apps.dtic.mil/dtic/tr/fulltext/u2/a104739.pdf) (PDF) from the original on December 15, 2019.

1. ^ [***a***](#cite_ref-irony_26-0) [***b***](#cite_ref-irony_26-1) [***c***](#cite_ref-irony_26-2) Irony, Dror; Toledo, Sivan; Tiskin, Alexander (September 2004). "Communication lower bounds for distributed-memory matrix multiplication". *J. Parallel Distrib. Comput*. **64** (9): 1017–26. [CiteSeerX](/source/CiteSeerX_(identifier)) [10.1.1.20.7034](https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.20.7034). [doi](/source/Doi_(identifier)):[10.1016/j.jpdc.2004.03.021](https://doi.org/10.1016%2Fj.jpdc.2004.03.021).

1. ^ [***a***](#cite_ref-Agarwal_27-0) [***b***](#cite_ref-Agarwal_27-1) Agarwal, R.C.; Balle, S. M.; Gustavson, F. G.; Joshi, M.; Palkar, P. (September 1995). "A three-dimensional approach to parallel matrix multiplication". *IBM J. Res. Dev*. **39** (5): 575–582. [CiteSeerX](/source/CiteSeerX_(identifier)) [10.1.1.44.3404](https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.44.3404). [doi](/source/Doi_(identifier)):[10.1147/rd.395.0575](https://doi.org/10.1147%2Frd.395.0575).

1. **[^](#cite_ref-28)** Solomonik, Edgar; [Demmel, James](/source/James_Demmel) (2011). ["Communication-optimal parallel 2.5D matrix multiplication and LU factorization algorithms"](https://solomonik.cs.illinois.edu/talks/europar-sep-2011.pdf) (PDF). *Proceedings of the 17th International Conference on Parallel Processing*. Vol. Part II. pp. 90–109. [doi](/source/Doi_(identifier)):[10.1007/978-3-642-23397-5_10](https://doi.org/10.1007%2F978-3-642-23397-5_10). [ISBN](/source/ISBN_(identifier)) [978-3-642-23397-5](https://en.wikipedia.org/wiki/Special:BookSources/978-3-642-23397-5).

1. **[^](#cite_ref-29)** Bosagh Zadeh, Reza; Carlsson, Gunnar (2013). ["Dimension Independent Matrix Square Using MapReduce"](https://stanford.edu/~rezab/papers/dimsum.pdf) (PDF). [arXiv](/source/ArXiv_(identifier)):[1304.1467](https://arxiv.org/abs/1304.1467). [Bibcode](/source/Bibcode_(identifier)):[2013arXiv1304.1467B](https://ui.adsabs.harvard.edu/abs/2013arXiv1304.1467B). Retrieved 12 July 2014.

1. **[^](#cite_ref-30)** Bae, S.E.; Shinn, T.-W.; Takaoka, T. (2014). ["A faster parallel algorithm for matrix multiplication on a mesh array"](https://www.sciencedirect.com/science/article/pii/S1877050914003858/pdf). *Procedia Computer Science*. **29**: 2230–40. [doi](/source/Doi_(identifier)):[10.1016/j.procs.2014.05.208](https://doi.org/10.1016%2Fj.procs.2014.05.208).

1. **[^](#cite_ref-31)** Kak, S (1988). "A two-layered mesh array for matrix multiplication". *Parallel Computing*. **6** (3): 383–5. [CiteSeerX](/source/CiteSeerX_(identifier)) [10.1.1.88.8527](https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.88.8527). [doi](/source/Doi_(identifier)):[10.1016/0167-8191(88)90078-6](https://doi.org/10.1016%2F0167-8191%2888%2990078-6).

1. **[^](#cite_ref-32)** Kak, S. (2014). "Efficiency of matrix multiplication on the cross-wired mesh array". [arXiv](/source/ArXiv_(identifier)):[1411.3273](https://arxiv.org/abs/1411.3273) [[cs.DC](https://arxiv.org/archive/cs.DC)].

1. **[^](#cite_ref-33)** Kak, S (1988). "Multilayered array computing". *Information Sciences*. **45** (3): 347–365. [CiteSeerX](/source/CiteSeerX_(identifier)) [10.1.1.90.4753](https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.90.4753). [doi](/source/Doi_(identifier)):[10.1016/0020-0255(88)90010-2](https://doi.org/10.1016%2F0020-0255%2888%2990010-2).

1. **[^](#cite_ref-34)** Dekel, Eliezer; Nassimi, David; Sahni, Sartaj (1981). "Parallel Matrix and Graph Algorithms". *SIAM Journal on Computing*. **10** (4): 657–675. [doi](/source/Doi_(identifier)):[10.1137/0210049](https://doi.org/10.1137%2F0210049).

## Further reading

- Buttari, Alfredo; Langou, Julien; Kurzak, Jakub; [Dongarra, Jack](/source/Jack_Dongarra) (2009). "A class of parallel tiled linear algebra algorithms for multicore architectures". *Parallel Computing*. **35**: 38–53. [arXiv](/source/ArXiv_(identifier)):[0709.1272](https://arxiv.org/abs/0709.1272). [doi](/source/Doi_(identifier)):[10.1016/j.parco.2008.10.002](https://doi.org/10.1016%2Fj.parco.2008.10.002). [S2CID](/source/S2CID_(identifier)) [955](https://api.semanticscholar.org/CorpusID:955).

- Goto, Kazushige; van de Geijn, Robert A. (2008). "Anatomy of high-performance matrix multiplication". *ACM Transactions on Mathematical Software*. **34** (3): 1–25. [CiteSeerX](/source/CiteSeerX_(identifier)) [10.1.1.140.3583](https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.140.3583). [doi](/source/Doi_(identifier)):[10.1145/1356052.1356053](https://doi.org/10.1145%2F1356052.1356053). [S2CID](/source/S2CID_(identifier)) [9359223](https://api.semanticscholar.org/CorpusID:9359223).

- Van Zee, Field G.; van de Geijn, Robert A. (2015). "BLIS: A Framework for Rapidly Instantiating BLAS Functionality". *ACM Transactions on Mathematical Software*. **41** (3): 1–33. [doi](/source/Doi_(identifier)):[10.1145/2764454](https://doi.org/10.1145%2F2764454). [S2CID](/source/S2CID_(identifier)) [1242360](https://api.semanticscholar.org/CorpusID:1242360).

- [How To Optimize GEMM](https://github.com/flame/how-to-optimize-gemm)

v t e Numerical linear algebra Key concepts Floating point Numerical stability Problems System of linear equations Matrix decompositions Matrix multiplication (algorithms) Matrix splitting Sparse problems Hardware CPU cache TLB Cache-oblivious algorithm SIMD Multiprocessing Software ATLAS MATLAB Basic Linear Algebra Subprograms (BLAS) LAPACK Specialized libraries

---
Adapted from the Wikipedia article [Matrix multiplication algorithm](https://en.wikipedia.org/wiki/Matrix_multiplication_algorithm) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Matrix_multiplication_algorithm?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
