parfor in matlab
Show older comments
I am trying to do multiprocessor coding in MATLAB, and I tried the following:
tic;
parfor i = 1:800
A(i) = i;
end
toc;
The elapsed time is 0.006091 seconds. And I also tried
tic;
for i = 1:800
A(i) = i;
end
toc;
The elapsed time is 0.001258 seconds. I tried this several times, it seems "for" is always faster than "parfor". My laptop has two processors, so this does not make sense to me. Could anyone spot for me what is wrong? Thank you!
1 Comment
Daniel Shub
on 25 Sep 2011
Your difference in timing is about 5ms, with the slower one taking a total of 6 ms. It is difficult to time things with this precision. While it wouldn't surprise me if the parfor was in fact slower, you need a better way of timing it.
Answers (1)
Walter Roberson
on 25 Sep 2011
3 votes
Did you open a matlabpool for parfor? If you did not, then it would silently run the loop sequentially.
Did you initialize A to zeros(1,800) before hand? Did you clear or reinitialize A between runs?
Did you try the code within a function (not a script!) instead of from the command line? Until 2010b, command line and scripts are not JIT as functions are.
And finally, there is the fact that there is a lot of communications overhead to tell the parallel processors what to do. Because of this, parfor will be slower than plain for until the arrays get much larger than 800, unless the work being done at each step is quite significant.
3 Comments
Hui
on 26 Sep 2011
Hui
on 26 Sep 2011
Walter Roberson
on 26 Sep 2011
PCT is available for the student edition; see http://www.mathworks.com/academia/student_version/companion.html
It is an add-on product, not free.
Categories
Find more on Parallel for-Loops (parfor) 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!