# Asymptotically faster matrix-multiplication algorithms

**URL:** https://discourse.julialang.org/t/asymptotically-faster-matrix-multiplication-algorithms/96710
**Category:** Offtopic
**Tags:** linearalgebra
**Created:** [March 27, 2023, 2:57pm UTC](https://discourse.julialang.org/t/asymptotically-faster-matrix-multiplication-algorithms/96710 "2023-03-27T14:57:11Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [March 27, 2023, 2:57pm UTC](https://discourse.julialang.org/t/asymptotically-faster-matrix-multiplication-algorithms/96710/1 "2023-03-27T14:57:11Z")

</div>

> [@Inverting a symmetric matrix is not faster than inverting a random one](https://discourse.julialang.org/t/inverting-a-symmetric-matrix-is-not-faster-than-inverting-a-random-one/96654/10):
>
> I’ve never heard of Coppersmith-Winograd actually being used for real matrix computations.

Does there even exist an actual implementation?

---

<div class="post-metadata">

### Author: ![mstewart](https://avatars.discourse-cdn.com/v4/letter/m/b5a626/32.png) [@mstewart](https://discourse.julialang.org/u/mstewart)
#### Post date: [March 27, 2023, 3:10pm UTC](https://discourse.julialang.org/t/asymptotically-faster-matrix-multiplication-algorithms/96710/2 "2023-03-27T15:10:29Z")

</div>

> Does there even exist an actual implementation?

I wouldn’t have known, but google did turn up [Matrix-Multiplication](https://github.com/YYYYYW/Matrix-Multiplication). From the descriptions I’ve heard, I always assumed that the break-even point would be for some matrix that is so large that I wouldn’t have a computer with that much RAM.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [March 27, 2023, 3:29pm UTC](https://discourse.julialang.org/t/asymptotically-faster-matrix-multiplication-algorithms/96710/3 "2023-03-27T15:29:32Z")

</div>

> [@mstewart](#):
>
> I wouldn’t have known, but google did turn up [Matrix-Multiplication](https://github.com/YYYYYW/Matrix-Multiplication). From the descriptions I’ve heard, I always assumed that the break-even point would be for some matrix that is so large that I wouldn’t have a computer with that much RAM.

Your search karma seems better than mine. I gave up after finding a reference to the [original paper](http://www.cs.umd.edu/~gasarch/TOPICS/ramsey/matrixmult.pdf) where it was noted that the authors state (start of section 5):

> Previous authors in this field have exhibited their algorithms directly, but we will have to rely on hashing and counting arguments to show the existence of a suitable algorithm.

---

<div class="post-metadata">

### Author: ![Jean\_Michel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jean_michel/32/8282_2.png) [@Jean\_Michel](https://discourse.julialang.org/u/Jean_Michel)
#### Post date: [March 28, 2023, 7:55am UTC](https://discourse.julialang.org/t/asymptotically-faster-matrix-multiplication-algorithms/96710/4 "2023-03-28T07:55:11Z")

</div>

Coppersmith-Winograd is certainly a bad idea to implement, but what about Strassen? Would it beat  
the plain method for sizes a few hundred?

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [March 28, 2023, 12:04pm UTC](https://discourse.julialang.org/t/asymptotically-faster-matrix-multiplication-algorithms/96710/5 "2023-03-28T12:04:01Z")

</div>

the strassen reloaded papers put the cutoff closer to 1000 and they were benchmarking vs worse regular implementions. I would be interested in someone trying strassen on Octavian

---

<div class="post-metadata">

### Author: ![mstewart](https://avatars.discourse-cdn.com/v4/letter/m/b5a626/32.png) [@mstewart](https://discourse.julialang.org/u/mstewart)
#### Post date: [March 28, 2023, 12:21pm UTC](https://discourse.julialang.org/t/asymptotically-faster-matrix-multiplication-algorithms/96710/6 "2023-03-28T12:21:39Z")

</div>

Yes. It’s dependent on the hardware and the details of the implementation, but Strassen’s algorithm (and the Winograd variant of Strassen’s algorithm that trims away some additions) can have break-even points in that range. Doing a step or two of Strassen’s algorithm before using a conventional BLAS call can speed things up on large matrices. People who really want to multiply large dense matrices do that sometimes.

But I don’t think it has ever made it into a commonly used BLAS library, mostly for practical reasons: You need extra storage. It is less effective for products involving tall and wide matrices. It has weaker stability guarantees.

Speculating a little bit, it also doesn’t seem entirely clear to me that it would be easy to get big benefits in other dense matrix computations by using a Strassen BLAS. LAPACK uses its own block sizes that are typically less than the sizes that would be strong candidates for Strassen. (On my machine, unless I messed something up, an attempted `ccall` of LAPACK’s `ilaenv` returns 64 for the block size LU factorization with OpenBLAS). The calculation of optimal block sizes would presumably change if Strassen’s algorithm were available. But there could be some places where that increases overall work in some secondary computation that was previously negligible for modest block sizes. (computation of triangular factors associated with block Householder transformations using `xLARTF` seems like a candidate for something that becomes more costly with increasing block size.) That could make it harder to break even or do better than standard BLAS. I’m speculating and could be wrong, but using Strassen for such things doesn’t look trivial to me.

The report [Implementing Strassen’s Algorithm with BLIS](https://www.cs.utexas.edu/users/flame/pubs/FLAWN79.pdf) makes the case that some of these concerns can be handled. They are getting good results for matrix multiplication of large matrices. I’d still be skeptical about broader use in dense matrix computations, however.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [March 28, 2023, 12:23pm UTC](https://discourse.julialang.org/t/asymptotically-faster-matrix-multiplication-algorithms/96710/7 "2023-03-28T12:23:27Z")

</div>

> [@Oscar\_Smith](#):
>
> the strassen reloaded papers put the cutoff closer to 1000 and they were benchmarking vs worse regular implementions. I would be interested in someone trying strassen on Octavian

To have more than marginal (\> 10%) speedup they need a few thousand, and to have \> 20% speedup they need sizes \gtrsim 10^4. This seems pretty consistent with other experiments I’ve seen over the years, e.g. with [ATLAS BLAS in 2006](https://link.springer.com/chapter/10.1007/978-3-540-77704-5_12).

See also this discussion: [Matrix multiply breakthrough, AlphaTensor (could also do for other algorithms): "AlphaTensor discovered algorithms that are more efficient than the state of the art for many matrix sizes." - #7 by stevengj](https://discourse.julialang.org/t/matrix-multiply-breakthrough-alphatensor-could-also-do-for-other-algorithms-alphatensor-discovered-algorithms-that-are-more-efficient-than-the-state-of-the-art-for-many-matrix-sizes/88455/7)

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [March 28, 2023, 1:17pm UTC](https://discourse.julialang.org/t/asymptotically-faster-matrix-multiplication-algorithms/96710/8 "2023-03-28T13:17:54Z")

</div>

IIRC, some of the issues with the algorithms are decreased numerical stability though, and that’s something that also needs to be factored into the discussion. Faster without accuracy isn’t necessarily faster. But it would be interesting to have a good Julia implementation to play around with and benchmark in real applications.

---

<div class="post-metadata">

### Author: ![mstewart](https://avatars.discourse-cdn.com/v4/letter/m/b5a626/32.png) [@mstewart](https://discourse.julialang.org/u/mstewart)
#### Post date: [March 28, 2023, 1:53pm UTC](https://discourse.julialang.org/t/asymptotically-faster-matrix-multiplication-algorithms/96710/9 "2023-03-28T13:53:45Z")

</div>

Specifically, for Strassen you lose componentwise bounds. For C=AB, ordinary matrix multiplication gives computed \hat{C} with

|C-\hat C| \leq nu |A| |B| + O(u^2).

For the original Strassen algorithm, the bounds are normwise and of the form

\|C-\hat C\| \leq f\_n u \|A\| \|B\|+O(u^2)

for some f\_n that has a larger constant factor but is not really dramatically more quickly growing than a normwise bound would be for conventional multiplication, especially if recursion isn’t going too deep before you apply conventional multiplication. The Winograd variant with fewer additions doesn’t necessarily satisfy the normwise bound unless you introduce some scaling. Unless you are specifically worried about componentwise errors, this is probably still fine for many uses. There is a very nice chapter in N. Higham’s “Accuracy and Stability of Numerical Algorithms” that covers the stability of these algorithms, which is where I pulled this from.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [March 28, 2023, 1:56pm UTC](https://discourse.julialang.org/t/asymptotically-faster-matrix-multiplication-algorithms/96710/10 "2023-03-28T13:56:59Z")

</div>

> [@mstewart](#):
>
> There is a very nice chapter in N. Higham’s “Accuracy and Stability of Numerical Algorithms” that covers the stability of these algorithms, which is where I pulled this from.

See also [“Fast matrix multiplication is stable” (Demmel et al, 2006)](https://arxiv.org/abs/math/0603207).

---

<div class="post-metadata">

### Author: ![fph](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fph/32/17159_2.png) [@fph](https://discourse.julialang.org/u/fph)
#### Post date: [March 28, 2023, 9:47pm UTC](https://discourse.julialang.org/t/asymptotically-faster-matrix-multiplication-algorithms/96710/11 "2023-03-28T21:47:15Z")

</div>

> [@mstewart](#):
>
> > Does there even exist an actual implementation [of Coppersmith-Winograd]?
> 
> I wouldn’t have known, but google did turn up [Matrix-Multiplication](https://github.com/YYYYYW/Matrix-Multiplication).

Despite the name, at a quick look that implementation seems to be [the Winograd form of Strassen’s method](https://en.wikipedia.org/wiki/Strassen_algorithm#Winograd_form), not the [Coppersmith-Winograd O(n^{2.3755}) algorithm from 1990](https://en.wikipedia.org/wiki/Computational_complexity_of_matrix_multiplication#Matrix_multiplication_exponent), which is significantly more complex.

---

<div class="post-metadata">

### Author: ![mstewart](https://avatars.discourse-cdn.com/v4/letter/m/b5a626/32.png) [@mstewart](https://discourse.julialang.org/u/mstewart)
#### Post date: [March 28, 2023, 11:05pm UTC](https://discourse.julialang.org/t/asymptotically-faster-matrix-multiplication-algorithms/96710/12 "2023-03-28T23:05:28Z")

</div>

Good point. So I guess it was mislabeled and I fell for it. I did look briefly at the implementation and was surprised it didn’t look more complicated. I was also pondering the point made by @GunnarFarneback that the original paper looks more like an existence proof than a full specification. So now I’m circling back to wondering if the algorithm has even been fully described anywhere. I suppose that if you go much past Strassen’s algorithm an existence proof is as useful as a fully optimized implementation.

---

<div class="post-metadata">

### Author: ![fph](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fph/32/17159_2.png) [@fph](https://discourse.julialang.org/u/fph)
#### Post date: [March 29, 2023, 6:20am UTC](https://discourse.julialang.org/t/asymptotically-faster-matrix-multiplication-algorithms/96710/13 "2023-03-29T06:20:55Z")

</div>

I also do not know all the details (and those I know are very rusty), but from what I understand it is very hard to turn the mathematics into an actual implementation of CW or any later algorithm. The proof is indeed an existence proof, proving with probabilistic methods the existence of a sequence of algorithms (for increasing sizes n) that attain the desired complexity.

---

<div class="post-metadata">

### Author: ![pitsianis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pitsianis/32/26588_2.png) [@pitsianis](https://discourse.julialang.org/u/pitsianis)
#### Post date: [March 29, 2023, 3:48pm UTC](https://discourse.julialang.org/t/asymptotically-faster-matrix-multiplication-algorithms/96710/14 "2023-03-29T15:48:10Z")

</div>

I wanted to add it here for completeness.

```plaintext
“Galactic” algorithms, a term coined by Regan, are algorithms 
with good-looking asymptotic running times but concrete costs 
so high as to prevent their practical use on scales smaller than 
the universe.

```

from [Lipton, R.J., Regan, K.W. (2013). David Johnson: Galactic Algorithms. In: People, Problems, and Proofs. Springer, Berlin, Heidelberg](https://doi.org/10.1007/978-3-642-41422-0_20)
