execution time with or without parfor

1 回表示 (過去 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 Center および File ExchangeParallel for-Loops (parfor) についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by