execution time with or without parfor
조회 수: 2 (최근 30일)
이전 댓글 표시
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
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
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 Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!