0

I compared the following codes. Serial:

N = 500;
M = rand(500,500,N);
R = zeros(500,500,N);
tic
for k = 1:N
    R(:,:,k) = inv(M(:,:,k));
end
toc

Parallel:

N = 500;
M = rand(500,500,N);
R = zeros(500,500,N);
tic
parfor k = 1:N
    R(:,:,k) = inv(M(:,:,k));
end
toc

I get that the serial time is 3 times shorter than parallel time - though I have 4 available local cores that seem to be in use. Any thoughts on why is it happening?

ezfn
  • 173
  • 5
  • 4
    The overhead in distributing jobs to specific cores is fairly high, so you have to make sure that each iteration takes enough time to justify that extra time. Generally, you get better results the larger the problem is (both in terms of how long it takes for a single iteration, and how many iterations there are to do). – David Aug 18 '15 at 22:37
  • Your parallel code is also likely making copies of your large `M` and `R` arrays for each core. – horchler Aug 19 '15 at 00:36
  • Generally I agree there is overhead in distributing jobs, though in this example it seems pretty trivial distribution. I also tried it with a bit bigger matrices and more iterations (before I ran out of RAM) - but still serial worked better. Is there any way of preventing my code from producing copies of R and M - isn't matlab supposed to slice it across processes? Thanks. – ezfn Aug 19 '15 at 07:29

1 Answers1

1

Remember that many MATLAB operations (especially large linear algebra operations) are intrinsically multi-threaded. In this case, inv is multi-threaded, and is the dominant factor in your for loop. When you convert that to a parfor loop, if you only have the 'local' cluster type available, then you have no more computational cores available in parfor than you did in for. Therefore, the parfor loop simply must be slower than the for loop because it has to transmit data to the workers for them to operate on.

In general, if you have only 'local' workers available, then parfor can beat for only when MATLAB cannot multi-thread the body of the for loop.

Edric
  • 23,676
  • 2
  • 38
  • 40