From: francios hoo on
hello,
i've been trying to use parfor in my program as i will use much for loops in it. however i realised that parfor loop consumes more time than typical for loop.
my parfor loop program:
a=1;
parfor i=1:100
for j=1:100
a=a+1;
end
end
time elapsed=0.288714 seconds

my for loop program:
a=1;
for i=1:100
for j=1:100
a=a+1;
end
end
time elapsed=0.000083 seconds.

i am wondering suppose the parallel programming should b faster isn't it?
any idea to fasten the parfor loop?
thanks in advanced^^

francios
From: neil on
"francios hoo" <francioshoo(a)yahoo.com> wrote in message <i252u8$298$1(a)fred.mathworks.com>...
> hello,
> i've been trying to use parfor in my program as i will use much for loops in it. however i realised that parfor loop consumes more time than typical for loop.
> my parfor loop program:
> a=1;
> parfor i=1:100
> for j=1:100
> a=a+1;
> end
> end
> time elapsed=0.288714 seconds
>
> my for loop program:
> a=1;
> for i=1:100
> for j=1:100
> a=a+1;
> end
> end
> time elapsed=0.000083 seconds.
>
> i am wondering suppose the parallel programming should b faster isn't it?
> any idea to fasten the parfor loop?
> thanks in advanced^^
>
> francios
The example you have is not good for demonstrating the improvement, since isn't really parallel. The value of a is required in all iterations of the loops. This means you have to communication across threads.

Try something with a matrix assignment, something like
b = zeros(1,100);
parfor i=1:100
a = 0;
for j=1:100
a=i+1;
end
b(i) = a;
end

You also might want to try doing something more complex within the parfor loop

Check out this blog post:
http://blogs.mathworks.com/loren/2009/10/02/using-parfor-loops-getting-up-and-running/

P.S. try stay away from i and j as variable as they are used for sqrt(-1)
From: Steven_Lord on


"francios hoo" <francioshoo(a)yahoo.com> wrote in message
news:i252u8$298$1(a)fred.mathworks.com...
> hello,
> i've been trying to use parfor in my program as i will use much for loops
> in it. however i realised that parfor loop consumes more time than typical
> for loop.
> my parfor loop program:
> a=1;
> parfor i=1:100
> for j=1:100
> a=a+1;
> end
> end
> time elapsed=0.288714 seconds
>
> my for loop program:
> a=1;
> for i=1:100
> for j=1:100
> a=a+1;
> end
> end
> time elapsed=0.000083 seconds.
>
> i am wondering suppose the parallel programming should b faster isn't it?

Parallel programming is NOT a "silver bullet". It's a useful tool, when
used well. That's all.

In the first case, you're timing not only the time required to perform your
"a = a+1" calculation but also the time required for the PARFOR to
communicate with the workers that are actually performing the "a = a+1"
calculation. That overhead is significant when the body of your loop is as
small and short as yours is.

> any idea to fasten the parfor loop?

Don't use a PARFOR loop for "small enough" problems, for some definition of
"small enough".

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com