From: Pravin on
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!
From: Edric M Ellis on
"Pravin " <pravinkakar(a)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.
From: Pravin on
Edric M Ellis <eellis(a)mathworks.com> wrote in message <ytw1vkuihn3.fsf(a)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