Thread Subject: Problem with parfor

Subject: Problem with parfor

From: Pravin

Date: 23 Oct, 2009 07:22:19

Message: 1 of 3

Hi everyone,

Long-time reader, first time poster.

I am currently trying to improve the performance of a program I am working on by carrying out operations in parallel. The basic code that I am using for this operation is something like:

for i = 1:Mdivs
    parfor j = 1:Ndivs
                pic = fun1(img1((i-1)*step+1:min([factor+(i-1)*step,M]),(j-1)*step+1:min(factor+(j-1)*step,N),:));
        motion(i,j,:) = fun2(pic);
    end
end

fun1 takes an RGB image, processes it and returns a grayscale image. fun2 processes the grayscale image and returns a two-element vector (1x1x2 in size actually), which I store at the appropriate location in motion. step,factor,M and N are of class double. They are used to extract a subimage from the image img1.

The problem that I have is that the code behaves very erratically. Sometimes, the execution is perfect, but at other times I get the following error message:-

Error using ==> parallel_function at 594
Subscripted assignment dimension mismatch.

Error in ==> control2 at 22
    parfor j = 1:Ndivs
 
I have spent a long time trying to figure out any pattern in the instances which cause the error, but have come up with squat. Any suggestions at all would be very helpful.

Thanks and cheers!

Subject: Problem with parfor

From: Edric M Ellis

Date: 23 Oct, 2009 09:28:16

Message: 2 of 3

"Pravin " <pravinkakar@gmail.com> writes:

> I am currently trying to improve the performance of a program I am working on
> by carrying out operations in parallel. The basic code that I am using for
> this operation is something like:
>
> for i = 1:Mdivs
> parfor j = 1:Ndivs
> pic = fun1(img1((i-1)*step+1:min([factor+(i-1)*step,M]),(j-1)*step+1:min(factor+(j-1)*step,N),:));
> motion(i,j,:) = fun2(pic);
> end
> end

Unfortunately, the subscripted assignment error appears to be due to a bug in
PARFOR which incorrectly deals with some assignments - in this case, because
you're slicing the second dimension out of 3. One workaround for this problem is
to move the PARFOR loop to be the outer loop (generally a good idea in any
case), but to do this you'll also need to calculate a whole chunk of your
"motion" array, something like this:

parfor i = 1:Mdivs
  tmp = zeros( Ndivs, 2 );
  for j = 1:Ndivs
    tmp( j, : ) = fun2( fun1( ... ) );
  end
  motion( i, :, : ) = tmp;
end

Cheers,

Edric.

Subject: Problem with parfor

From: Pravin

Date: 24 Oct, 2009 03:48:04

Message: 3 of 3

Edric M Ellis <eellis@mathworks.com> wrote in message <ytw1vkuihn3.fsf@uk-eellis-deb5-64.mathworks.co.uk>...
>
> Unfortunately, the subscripted assignment error appears to be due to a bug in
> PARFOR which incorrectly deals with some assignments - in this case, because
> you're slicing the second dimension out of 3. One workaround for this problem is
> to move the PARFOR loop to be the outer loop (generally a good idea in any
> case), but to do this you'll also need to calculate a whole chunk of your
> "motion" array, something like this:
>
> parfor i = 1:Mdivs
> tmp = zeros( Ndivs, 2 );
> for j = 1:Ndivs
> tmp( j, : ) = fun2( fun1( ... ) );
> end
> motion( i, :, : ) = tmp;
> end
>
> Cheers,
>
> Edric.

Well, I'll be... This worked like a charm. I did try to move the parfor to the outer loop earlier but couldn't quite figure out how to make the assignment.

Thanks so much!

Cheers,
Pravin

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
bug Husam Aldahiyat 24 Oct, 2009 06:53:32
error Pravin 23 Oct, 2009 03:24:04
parfor Pravin 23 Oct, 2009 03:24:04
parallel comput... Pravin 23 Oct, 2009 03:24:04
rssFeed for this Thread

Contact us at files@mathworks.com