I'm trying to test parallel matrix multiplication with commands:

m1 = RandomReal[{-10, 10}, {2000, 2000}];
mykernel = First[Kernels[]];
startT1 = AbsoluteTime[];
ParallelEvaluate[a1 = m1.m1, mykernel];
runtime1 = AbsoluteTime[] - startT1;
mykernels = Kernels[];
startT2 = AbsoluteTime[];
ParallelEvaluate[a2 = m1.m1, mykernels];
runtime2 = AbsoluteTime[] - startT2;
Print[runtime1, " ", runtime2]

And output is:

{<<2 local kernels>>}
1.1577824 1.1675535

I expect that the first number is 2 times greater than the second, however I ran this script many times and these numbers are always about the same. Where is my mistake?

  • $\begingroup$ I would say that Dot gets parallelized quite well by the Intel MKL. So, there is no point in trying highlevel optimization, at least not with only 2 cores... $\endgroup$ – Henrik Schumacher Dec 3 '17 at 19:53
  • 3
    $\begingroup$ Read the documentation of ParallelEvaluate carefully. It evaluates the same thing on each kernel. It won't speed up anything. It isn't meant to. You are confusing it with Parallelize, but that won't speed up matrix multiplication either. Mathematica's parallel tools use the distributed memory paradigm and transferring the data would take longer than the multiplication. However, as Henrik said, Dot is already paralellized internally, so there's no need for you to try to do this. $\endgroup$ – Szabolcs Dec 3 '17 at 20:24
  • $\begingroup$ But how can I perform matrix multiplication forcibly on one core, then on two cores and compare its times? Or for procedure Inverse[]? $\endgroup$ – r1d1 Dec 3 '17 at 20:45
  • $\begingroup$ To restrict Mathematica to use only a single core, see mathematica.stackexchange.com/a/31401/12 $\endgroup$ – Szabolcs Dec 4 '17 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.