9

Strassen's algorithm for matrix multiplication just gives a marginal improvement over the conventional O(N^3) algorithm. It has higher constant factors and is much harder to implement. Given these shortcomings, is strassens algorithm actually useful and is it implemented in any library for matrix multiplication? Moreover, how is matrix multiplication implemented in libraries?

Nikunj Banka
  • 11,117
  • 16
  • 74
  • 112
  • 2
    This question isn't really appropriate here as it stands, it's more about algorithms and programming philosophy than it is about actual code. Are you asking what matrix multiplication algorithm does specifically *Matlab* use? – Dan Apr 02 '14 at 09:50
  • No, I just want to know how is matrix multiplication implemented in standard libraries and if strassen's algorithm is employed or not. – Nikunj Banka Apr 02 '14 at 09:54
  • 1
    Then you should not have tagged it Matlab. – Dan Apr 02 '14 at 09:58
  • From my homebrew experiments, Strassen's algorithm starts to outperform the by-definition method at sizes on the order of several hundred. Don't know about the libraries. – Gassa Apr 02 '14 at 10:12
  • 1
    From my personal experience as well as superficial research on literature, Strassen's algorithm outperforms the naive method at order of less than 100; however thies greatly depends on memory layout and degree of parallelization. The fact that Strassen's algorithm is actually suitable for practice seems to be a result that is constantly re-discovered. A different discussion can be found here: http://tawqer.com/tag/strassen%20algorithm#.UzvndFdTtas – Codor Apr 02 '14 at 10:30

4 Answers4

9

Generally Strassen’s Method is not preferred for practical applications for following reasons.

  1. The constants used in Strassen’s method are high and for a typical application Naive method works better.
  2. For Sparse matrices, there are better methods especially designed for them.
  3. The submatrices in recursion take extra space.
  4. Because of the limited precision of computer arithmetic on noninteger values, larger errors accumulate in Strassen’s algorithm than in Naive Method.
  • @roottraveller https://en.wikipedia.org/wiki/Strassen_algorithm#Asymptotic_complexity – Dan Jul 08 '19 at 13:28
5

So the idea of strassen's algorithm is that it's faster (asymptotically speaking). This could make a big difference if you are dealing with either huge matrices or else a very large number of matrix multiplications. However, just because it's faster asymptotically doesn't make it the most efficient algorithm practically. There are all sorts of implementation considerations such as caching and architecture specific quirks. Also there is also parallelism to consider.

I think your best bet would be to look at the common libraries and see what they are doing. Have a look at BLAS for example. And I think that Matlab uses MAGMA.


If your contention is that you don't think O(n^2.8) is that much faster than O(n^3) this chart shows you that n doesn't need to be very large before that difference becomes significant.

enter image description here

Dan
  • 45,079
  • 17
  • 88
  • 157
2

It's very important to stop at the right moment.

With 1,000 x 1,000 matrices, you can multiply them by doing seven 500 x 500 products plus a few additions. That's probably useful. With 500 x 500, maybe. With 10 x 10 matrices, most likely not. You'd just have to do some experiments first at what point to stop.

But Strassen's algorithm only saves a factor 2 (at best) when the number of rows grows by a factor 32, the number of coefficients grows by 1,024, and the total time grows by a factor 16,807 instead of 32,768. In practice, that's a "constant factor". I'd say you gain more by transposing the second matrix first so you can multiply rows by rows, then look carefully at cache sizes, vectorise as much as possible, and distribute over multiple cores that don't step on each others' feet.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
1
  • Marginal improvement: True, but growing as the matrix sizes grow.
  • Higher constant factors: Practical implementations of Strassen's algorithm use conventional n^3 for blocks below a particular size, so this doesn't really matter.
  • Harder to implement: whatever.

As for what's used in practice: First, you have to understand that multiplying two ginormous dense matrices is unusual. Much more often, one or both of them is sparse, or symmetric, or upper triangular, or some other pattern, which means that there are quite a few specialized tools which are essential to the efficient large matrix multiplication toolbox. With that said, for giant dense matrices, Strassen's is The Solution.

Sneftel
  • 40,271
  • 12
  • 71
  • 104