MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply TodayTo resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Asked by 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

*No products are associated with this question.*

Answer by 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.

Show 1 older comment

Image Analyst on 28 Jul 2012

Try it again with something that takes some time. 8 milliseconds is less than a blink of an eye. The overhead took up the majority of time, not the loop. Use a loop that would take a few hours and compare.

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 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.

## 0 Comments