Sanjay Saxena
Sanjay Saxena

Reputation: 21

Parallel code is taking longer time than sequential code using Parallel computing toolbox in MATLAB. Why?

I am working with MATLAB. I am just new with parallel computing toolbox in MATLAB. I have core i3 processor, MATLAB R2011a, 2 GB of RAM, 320 Hard disk.

To calculate speed up, I just wrote following code and found that parallel code is taking longer time than a sequential code.

1st code is taking 0.039763 seconds

2nd code is taking 0.379056 seconds.


1st code:

tic
MM = magic(5);
MN = magic(5);
ML = magic(5);
MP = magic(5);
MK = magic(5);
MM
MN
ML
MP
MK
toc

2nd Code:

matlabpool open local 4

tic
spmd  % Uses all 3 workers
    MM = magic(5); % MM is a variable on each lab
end
MM{1}
MM{2}
MM{3}
MM{4}
toc
matlabpool close

I want to learn parallel computing toolbox.

Upvotes: 1

Views: 745

Answers (1)

ThijsW
ThijsW

Reputation: 2599

As mentioned by Dan in the comments, the problem is clearly too small for parallelization to be beneficial. Increasing for example the size of the magic matrices you create from 5 to 5000, already shows a clear improvement. That is, with the larger size the overhead of parallelization becomes (almost) negligible compared to the computation time for one matrix.

Upvotes: 1

Related Questions