execution time with or without parfor

2 views (last 30 days)
amir
amir on 3 Feb 2014
Commented: Matt J on 7 Feb 2014
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 Comments
Matt J
Matt J on 6 Feb 2014
You're not doing any of this over a network are you? This is all on a local CPU?
amir
amir on 6 Feb 2014
Edited: amir on 6 Feb 2014
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) ?

Sign in to comment.

Accepted Answer

Matt J
Matt J on 7 Feb 2014
Edited: Matt J on 7 Feb 2014
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 Comments
amir
amir on 7 Feb 2014
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 on 7 Feb 2014
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Parallel Computing Fundamentals in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!