parfor in matlab

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 件のコメント

Daniel Shub
Daniel Shub 2011 年 9 月 25 日
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.

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2011 年 9 月 25 日

3 投票

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 件のコメント

Hui
Hui 2011 年 9 月 26 日
It seems matlabpool is not defined on my portion of MATLAB. Is that because I need to install parallel computing toolbox?
Hui
Hui 2011 年 9 月 26 日
It seems the student version does not have the permission to download PCT, right?
Walter Roberson
Walter Roberson 2011 年 9 月 26 日
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.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeParallel for-Loops (parfor) についてさらに検索

質問済み:

Hui
2011 年 9 月 25 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by