Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
performance of parfor

Subject: performance of parfor

From: kinor

Date: 4 Apr, 2013 17:13:06

Message: 1 of 5

Hello,

I used parfor a while ago and it worked quite well (2.5 time faster with 3 labs).
Now I tried to use it on win 7 with 2012a and an example from http://blogs.mathworks.com/loren/2007/10/03/parfor-the-course/ with the following results:

matlabpool local 3

n = 600;
tic
ranks = zeros(1,n);
parfor (ind = 1:n)
    ranks(ind) = rank(magic(ind)); % last index could be ind,not n-ind+1
end
toc

tic
ranks = zeros(1,n);
for (ind = 1:n)
    ranks(ind) = rank(magic(ind)); % last index could be ind,not n-ind+1
end
toc

Starting matlabpool using the 'local' profile ... connected to 3 labs.
Elapsed time is 10.773548 seconds.
Elapsed time is 13.993966 seconds.

Which is surprisingly slow for 3 labs. I would be really grateful for any hint to get the 3 labs working.

Best
kinor

Subject: performance of parfor

From: Edric M Ellis

Date: 5 Apr, 2013 12:50:00

Message: 2 of 5

"kinor " <kinor.removethiswithdot@gmx.de> writes:

> I used parfor a while ago and it worked quite well (2.5 time faster
> with 3 labs). Now I tried to use it on win 7 with 2012a and an
> example from
> http://blogs.mathworks.com/loren/2007/10/03/parfor-the-course/ with
> the following results:
>
> matlabpool local 3
>
> n = 600;
> tic
> ranks = zeros(1,n);
> parfor (ind = 1:n)
> ranks(ind) = rank(magic(ind)); % last index could be ind,not n-ind+1
> end
> toc
>
> tic
> ranks = zeros(1,n);
> for (ind = 1:n)
> ranks(ind) = rank(magic(ind)); % last index could be ind,not n-ind+1
> end
> toc
>
> Starting matlabpool using the 'local' profile ... connected to 3 labs.
> Elapsed time is 10.773548 seconds.
> Elapsed time is 13.993966 seconds.
>
> Which is surprisingly slow for 3 labs. I would be really grateful for
> any hint to get the 3 labs working.

The MATLAB function 'RANK' is instrinsically multithreaded. On a single
machine, it's always going to be more efficient to use intrinsic
multithreading than to use explicit parallelism using a MATLABPOOL and
PARFOR because of the overheads of distributing the body of the loop to
the workers. MATLABPOOL workers run in "single computational thread"
mode, so to make a fair comparison, you should start MATLAB using
"-singleCompThread".

Cheers,

Edric.

Subject: performance of parfor

From: kinor

Date: 5 Apr, 2013 14:49:18

Message: 3 of 5

Edric M Ellis <eellis@mathworks.com> wrote in message <ytw6201w39j.fsf@uk-eellis0l.dhcp.mathworks.com>...
> "kinor " <kinor.removethiswithdot@gmx.de> writes:
>
> > I used parfor a while ago and it worked quite well (2.5 time faster
> > with 3 labs). Now I tried to use it on win 7 with 2012a and an
> > example from
> > http://blogs.mathworks.com/loren/2007/10/03/parfor-the-course/ with
> > the following results:
> >
> > matlabpool local 3
> >
> > n = 600;
> > tic
> > ranks = zeros(1,n);
> > parfor (ind = 1:n)
> > ranks(ind) = rank(magic(ind)); % last index could be ind,not n-ind+1
> > end
> > toc
> >
> > tic
> > ranks = zeros(1,n);
> > for (ind = 1:n)
> > ranks(ind) = rank(magic(ind)); % last index could be ind,not n-ind+1
> > end
> > toc
> >
> > Starting matlabpool using the 'local' profile ... connected to 3 labs.
> > Elapsed time is 10.773548 seconds.
> > Elapsed time is 13.993966 seconds.
> >
> > Which is surprisingly slow for 3 labs. I would be really grateful for
> > any hint to get the 3 labs working.
>
> The MATLAB function 'RANK' is instrinsically multithreaded. On a single
> machine, it's always going to be more efficient to use intrinsic
> multithreading than to use explicit parallelism using a MATLABPOOL and
> PARFOR because of the overheads of distributing the body of the loop to
> the workers. MATLABPOOL workers run in "single computational thread"
> mode, so to make a fair comparison, you should start MATLAB using
> "-singleCompThread".
>
> Cheers,
>
> Edric.

Hi Edric,

thanks a lot for this clear explanation. I should have read the example more carefully, now there is a certain gain in speed by parfor: double speed with 3 labs.

I looked in the help of rank, its multithreading is not mentioned there, where did you know that?

As a simple example I tried to generate an image with the following function:

function speedtest
maxNumCompThreads(1);
matlabpool(3)
n = 6000;
k = ones(n, n, 3);
fh = {@r @g @b};

tic
for c = 1:3
    k(:,:,c) = fh{c}(ones(n));
end
toc

tic
parfor c = 1:3
    k(:,:,c) = fh{c}(ones(n));
end
toc

matlabpool close


function rV = r(k)
for y = size(k,1):-1:1
    for x = size(k,2):-1:1
        rV(y,x) = sin(y*0.1)*cos(x*0.2);
    end
end

function rV = g(k)
for y = size(k,1):-1:1
    for x = size(k,2):-1:1
        rV(y,x) = sin(y*0.015)*cos(x*0.12);
    end
end

function rV = b(k)
for y = size(k,1):-1:1
    for x = size(k,2):-1:1
        rV(y,x) = sin(y*0.1)*cos(x*0.2);
    end
end

in 2012a i got:
Starting matlabpool using the 'local' profile ... connected to 3 labs.
Elapsed time is 8.331768 seconds.
Elapsed time is 11.959129 seconds.
Sending a stop signal to all the labs ... stopped.

which means a loss of speed applying parfor to long executing functions.
I am curious if there is any explanation for this.


Cheers
kinor

Subject: performance of parfor

From: Steven_Lord

Date: 5 Apr, 2013 17:45:45

Message: 4 of 5



"kinor " <kinor.removethiswithdot@gmx.de> wrote in message
news:kjmo9e$pl6$1@newscl01ah.mathworks.com...
> Edric M Ellis <eellis@mathworks.com> wrote in message
> <ytw6201w39j.fsf@uk-eellis0l.dhcp.mathworks.com>...
>> "kinor " <kinor.removethiswithdot@gmx.de> writes:

*snip*

> I looked in the help of rank, its multithreading is not mentioned there,
> where did you know that?

Technically the RANK function itself is not multithreaded, as it is a MATLAB
function file and it doesn't use any of the parallel constructs like PARFOR
or SPMD from Parallel Computing Toolbox.

However, most of the time required to compute RANK is used by computation of
the singular values of the input matrix using SVD, and SVD _is_
multithreaded (under certain conditions, notably when the data is large
enough.)

http://www.mathworks.com/support/solutions/en/data/1-4PG4AN/index.html?solution=1-4PG4AN

I'd say that's close enough to count Edric's statement as correct.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: performance of parfor

From: kinor

Date: 8 Apr, 2013 07:28:10

Message: 5 of 5

"Steven_Lord" <slord@mathworks.com> wrote in message <kjn2k9$1p7$1@newscl01ah.mathworks.com>...
>
>
> "kinor " <kinor.removethiswithdot@gmx.de> wrote in message
> news:kjmo9e$pl6$1@newscl01ah.mathworks.com...
> > Edric M Ellis <eellis@mathworks.com> wrote in message
> > <ytw6201w39j.fsf@uk-eellis0l.dhcp.mathworks.com>...
> >> "kinor " <kinor.removethiswithdot@gmx.de> writes:
>
> *snip*
>
> > I looked in the help of rank, its multithreading is not mentioned there,
> > where did you know that?
>
> Technically the RANK function itself is not multithreaded, as it is a MATLAB
> function file and it doesn't use any of the parallel constructs like PARFOR
> or SPMD from Parallel Computing Toolbox.
>
> However, most of the time required to compute RANK is used by computation of
> the singular values of the input matrix using SVD, and SVD _is_
> multithreaded (under certain conditions, notably when the data is large
> enough.)
>
> http://www.mathworks.com/support/solutions/en/data/1-4PG4AN/index.html?solution=1-4PG4AN
>
> I'd say that's close enough to count Edric's statement as correct.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com


Hi Steve

thanks a lot for the enlighting comment, I did not doubt Erics comment. I was just surprised.
thanks again Eric.

best regards
kinor

@ all

any idea why the last example i posted is not faster with parfor?

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us