there is aproblem in parfor

1 view (last 30 days)
huda nawaf
huda nawaf on 28 Jul 2012
hi, currently ,I'm working on super computer with 8 cores. to see how I can change my old serial code, I tried those codes. what I got the time of parallel code is more than serial code. where is the problem?
tic;
matlabpool open local 2
parfor i=1:1024
a(i)=sin(i*2*pi/1024);
end
plot(a)
parfor i=1:1024
a(i)=cos(i*2*pi/1024);
end
plot(a)
matlabpool close
toc;
Elapsed time is 8.493090 seconds.
when I tried the serial code
tic;
for i=1:1024
a(i)=sin(i*2*pi/1024);
end
plot(a)
for i=1:1024
a(i)=cos(i*2*pi/1024);
end
plot(a)
toc;
Elapsed time is 0.008288 seconds.
thanks in advance

Answers (1)

Walter Roberson
Walter Roberson on 28 Jul 2012
How long does it take to create a worker? How long does it take to tell the worker what to do? Can the creation be done in parallel or does it have to be done serially? Can telling each worker what to do be done in parallel or does it have to be done serially? How long does it take to get results back from a worker and put the results into the correct place in the output vector? Can that be done in parallel or does it have to be done serially?
You have mistaken expectations that parallel processing has no more overhead than serial processing.
  4 Comments
Walter Roberson
Walter Roberson on 29 Jul 2012
Suppose that the serial overhead per worker is H. Suppose the number of workers is n. Suppose the total work to be done is T, and thus that the work to be done per worker is T/n. Then the elapsed time for execution is n*H (the serial part) + T/n (done in parallel). If this time is to be less than T, the time it would take to do the same work in serial, then
n*H + T/n < T
which is a simple quadratic to solve. If you let n = 2, and solve for H, you will find that with two workers, the overhead, H, needs to be less than T/4 in order for there to be a speed-up.
The overhead, H, is more or less fixed (to within the simplification of using only one overhead parameter), so by looking at the equation, you can see that the more work you have to do per worker, the less impact the overhead H has on the total time.
nah
nah on 2 Aug 2012
Walter, thanks for the analysis that provides a quantitative for understanding the effect of overheads. But, there is also a great difference between similar jobs, say, a matlabpooljob of n workers that is launched interactively from a client or as a batch job (which requires n+1 workers). This is not addressed in this analysis.

Sign in to comment.

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!