there is aproblem in parfor
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Walter Roberson
2012-7-28
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 个评论
Walter Roberson
2012-7-29
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
2012-8-2
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!