Speeding up matrix multiplication and inversion

8 views (last 30 days)
I have a script which runs a loop, within which several matrix multiplications and inversions are computed (different matrices each iteration of the loop, sometimes different sizes). Each consecutive iteration relies on previous iterations, so parfor is not an option.
I've profiled my code and the slowest part (typically taking around 80% of the complete wall clock time) are those matrix multiplications and inversions, so I'd really like to speed that up if possible, but I'm aware Matlab operation on matrices/vectors are already very well optimised.
Is there any way at all to speed this up? Could MEX help at all? Could using a GPU and gpuArray help at all? (although this is a last resort).
Thanks a lot in advance, Olie
  2 Comments
Alfonso Nieto-Castanon
Alfonso Nieto-Castanon on 21 Jan 2015
Everything else been exhausted reworking the math is the most likely path to speed-ups in your code. Could you share some code showing the specific matrix operations that you are computing to see if something comes to mind?
Joss Knight
Joss Knight on 2 Feb 2015
Post some example MATLAB code, and I'll tell you what advantage you might get out of running on a GPU. For a large matrix multiplication or inversion you could well get a significant speedup.

Sign in to comment.

Answers (2)

Thorsten
Thorsten on 21 Jan 2015
Edited: Thorsten on 21 Jan 2015
If the matrices are sparse, coding them as such using
Msparse = sparse(M);
may help.
  1 Comment
Oliver Dellar
Oliver Dellar on 21 Jan 2015
Sorry should have mentioned, I've tried that, and typically get a tiny bit of speedup, but nothing at all significant. Thank you though.

Sign in to comment.


John D'Errico
John D'Errico on 21 Jan 2015
So you can't parallelize the computations. Large enough problems will already be multi-threaded when there is a gain anyway, but they need to be pretty large before that starts happening.
The best way to speed things up is to bump up your CPU speed. Replacing hard disks with solid state memory drives can also help some. GPU might be a gain, though I know nothing there.
Frequently, you can improve your code, even when you think you have wrung all you can from it. You might be surprised. Perhaps you can write it in a completely different way. For example, if you need to solve multiple problems with different size matrices, things can be done with block diagonal matrices.
As for Mex, unless you can write better code than those who provided optimized code based on the BLAS, I doubt you would gain much.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!