execution time with or without parfor

조회 수: 2 (최근 30일)
amir
amir 2014년 2월 3일
댓글: Matt J 2014년 2월 7일
I have a simple code for testing parfor in my local profile (with 4 cores)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%code 1
matlabpool open 4 % 2 or 1
tic;
parfor i = 1:30
res = 0;
for n = 1 : 3000000
res = res + sin(n) + cos(n);
end
A(i) = res;
end
toc;
matlabpool close
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%code 2
tic;
for i = 1:30
res = 0;
for n = 1 : 3000000
res = res + sin(n) + cos(n);
end
A(i) = res;
end
toc;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
I have executed code 1 using 4 labs or 2 labs or 1 lab and executed code 2. the results is here:
code-1 - 8 labs(4 core with 4 hypthread) --> 15 sec
code-1 - 4 labs --> 22 sec
code-1 - 2 labs --> 35 sec
code-1 - 1 labs --> 65 sec
code-2 - --> 18 sec
regards the results, it is better to use code-2 and releasing all other cores (you may also consider the time needed to run 'matlabpool open' and 'matlabpool close'). I have read this : http://www.mathworks.co.uk/matlabcentral/answers/44734-there-is-aproblem-in-parfor
but it seems in this case execution time is much longer than setup time of parallel mechanism.
if there is not any thing wrong with my results, main question is when its better to use parfor.
  댓글 수: 17
Matt J
Matt J 2014년 2월 6일
You're not doing any of this over a network are you? This is all on a local CPU?
amir
amir 2014년 2월 6일
편집: amir 2014년 2월 6일
I think I have found the reason:
If I run the code as a function, I will get your results and If I run it as a script (without deceleration of function and name) I get bad results. and my new results (poolnum = 4) :
19.4899
20.7605
11.2675
7.8502
6.4180
Please tell me how did you run this code (as a function in a function file or as a script) ?

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2014년 2월 7일
편집: Matt J 2014년 2월 7일
If I run the code as a function, I will get your results and If I run it as a script (without deceleration of function and name) I get bad results. and my new results (poolnum = 4) :
I think you're on to something! I was indeed running inside a function. When I repeated the test, but running it as a script instead, I get bad behavior similar to what you were reporting.
Times =
20.7096
67.3600
33.4894
23.1408
18.0744
13.8923
11.6439
11.3326
9.2532
9.3852
7.0565
7.0984
7.1319
As can be seen here, PARFOR eventually does outperform a plain for-loop, but it takes a very large worker pool, and with very marginal benefits.
I'm just wondering now whether this is known/documented behavior, or a bug...
  댓글 수: 2
amir
amir 2014년 2월 7일
maybe if I had 12 pools, using 'parfor' was better than for. I think using 'parfor' in script makes some overhead. you can see it in your results too (20.7096 -->'for' 67.3600-->'parfor').
I think its not a known behavior because at least it must shows some warning or something.
Thanks for your help.
Matt J
Matt J 2014년 2월 7일
Interestingly, I seem to be encountering the opposite behavior here
There, I have an example where putting the code inside a function is slower than inside a script.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by