Reputation: 6518
How can I improve the efficiency of standard matrix multiplication algorithm?
The main operation involved in this approach is: C[i][j]+=A[i][p]*B[p][j]
What can be done to improve the efficiency of the algorithm?
Upvotes: 1
Views: 1897
Reputation: 6551
Well there's Strassen's Algorithm, which, depending on the size of your matrix, is ever so slightly faster than the standard algorithm that you listed. There are of course even faster algorithms, but they arent so simple to implement.
The standard algorithm is O(N^3), Strassen's algo is O(N^2.8), and Coppersmith-Winograd is O(N^2.3)
Upvotes: 0
Reputation: 7015
You might want to have a look at using a BLAS (Basic Linear Algebra Subroutine) library, specifically Intel offer their MKL here, AMD have their ACML here and there's also the (opensource) Goto BLAS here.
The (dense) matrix-matrix multiply kernel will be a ?GEMM
call, where the ?
indicates the floating point type. For example DGEMM
will call the double
routine.
Unless you're extremely confident you know what you're doing with low-level optimisations, these libraries will probably offer better performance than something you can code by hand.
If you do want to have a go at coding this yourself then you may want to consider the following:
SSE, SSE2..4
instructions are widely supported, some newer CPU
's will also support AVX
instructions.This reference might give you an idea of the current state of things:
High-performance implementation of the level-3 BLAS - K Goto.
Hope this helps.
Upvotes: 1
Reputation: 28322
If the question concerns multiple matrix multiplications - M1 x M2 x ... x Mn - then there is another optimization technique based on dynamic programming, which is sort of another ball-game. Note that this doesn't apply to improving the efficiency of multiplying two matrices; however, if you are multiplying three or more matrices in a pairwise fashion, then you can optimize at an even higher level. Just thought I'd throw this answer on the heap to round out the information.
Upvotes: 0
Reputation: 36476
Note that using these methods does not guarantee better performance. There is a lot of tuning required give a significant speedup. There is a lot of money going into figuring out how to multiply matrices quickly so there is no shortage of journal articles on the subject.
Upvotes: 1
Reputation: 500883
I would suggest reading Chapter 1 of Golub and Van Loan, which addresses this exact question.
Upvotes: 0