# Fast matrix multiplication in MMA and GPU

Matlab multiplies two 3000by3000 matrix around 0.18s faster than MMA. While for just a few matrix multiplications the speed difference is not important, it becomes crucial when I want to do those matrix multiplications on the order of 1 million times.

I have three questions:

1- How much using GPU in MMA makes the computation faster relative to using CPU?

2- How much using GPU in Matlab makes the computation faster relative to using CPU?

3- Is there a way to make the built in matrix multiplication of MMA even faster without using GPU? If it's not possible, can I call Matlab or some other c library within MMA to do the calculations?

I am really reluctant to rewrite the program in Matlab because to me it is really hard to catch up with its too-much-concise syntax.

It would be good if someone could give some benchmark about multiplication of two 3000by3000 matrices by using GPU and mention the model of his/her GPU. It would be much better to compare the results with Matlab of course.

• – Artes Jan 3 '15 at 23:32
• Are you using packed arrays? – Mike Honeychurch Jan 3 '15 at 23:45
• @MikeHoneychurch. I didn't use DeveloperToPackedArray – MOON Jan 3 '15 at 23:46
• Arrays can also be automatically packed depending on how they are created. If you showed us your code you will get better responses – Mike Honeychurch Jan 3 '15 at 23:48
• @Szabolcs Wolfram always cited the speed comparison as the same or Mma marginally faster for version 7. Performance may well have deteriorated since then. Nevertheless we need to see what code is being written – Mike Honeychurch Jan 3 '15 at 23:54

This is too long for a comment, but here's a comparison between Matematica 10.0.2 and MATLAB R2014b on OS X, using MATLink. There is no appreciable difference between their performance. Mathematica 10 performs significantly better than Mathematica 9 due to updated MKL libraries. Both MATLAB and Mathematica rely on the MKL for matrix multiplications.

<< MATLink

OpenMATLAB[]

m = RandomReal[{-1, 1}, {3000, 3000}];

MSet["m", m]

Table[First@AbsoluteTiming[m.m;], {5}]
(* {0.630290, 0.622880, 0.619375, 0.637080, 0.630417} *)

Do[Print@MEvaluate["tic; a=m*m; toc"], {5}]

During evaluation of >> Elapsed time is 0.720687 seconds.

During evaluation of >> Elapsed time is 0.620147 seconds.

During evaluation of >> Elapsed time is 0.611716 seconds.

During evaluation of >> Elapsed time is 0.614447 seconds.

During evaluation of >> Elapsed time is 0.612863 seconds.

MGet["a"] == m.m
(* True *)


Given these timings, it would take more than 7 days to do the multiplication a million times on this computer.

• The size of the problem is indeed huge so I expected long run times. That's why I need to know if it is worthy to spend money on a GPU. As someone else mentioned in the comments by a link, it seems GPU makes the computation 4 times faster. But I want to know what brand or model of GPU gives the specific speed up so that I can decide on the budget spent of the GPU. I had used MMA 9 and got that difference between Matlab and MMA. – MOON Jan 4 '15 at 0:54
• @yashar GPU is not a panacea. Even though its memory bandwidth is much greater than that of main memory, bandwidth between main memory and GPU is much less. This makes the movement of data the most important problem and speed-up is difficult to get unless you do the calculation completely on the GPU (i.e., you cannot realistically use Mathematica). Additionally, for non-parallel workloads, GPU will be slow relative to CPU, so you can potentially run into Amdahl's law. The question is (or should be) a lot more complicated than "should I buy a GPU or not". – Oleksandr R. Jan 4 '15 at 3:29
• @yashar None of the comments said that the GPU would make it 4 times faster. I posted wrong information in a comment that I removed later, but I never mentioned GPUs. If you use the latest version of Mathematica, linear algebra speed is the same as in the latest version of MATLAB. – Szabolcs Jan 4 '15 at 3:38
• @Szabolcs I meant this link stackoverflow.com/questions/8638905/… which did some benchmark. – MOON Jan 4 '15 at 3:54
• @yashar You may find this comparison interesting. My own experience with GPU / CUDA has been that this is an art. Depending on how deeply you understand the GPU architecture, the problem at hand, the power of the particular GPU you have vs. the power of your CPU, and some other factors, your results can vary significantly. For matrix multiplication, it's probably safe to assume that you can get a speedup about 5x-10x with a modern GPU (compared to a modern CPU) without a huge effort. Perhaps, with more effort, you can get more. – Leonid Shifrin Jan 4 '15 at 12:03