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 Exchange에서 Parallel 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