From: Aino on
Hi all,

I have basically this situation:

A=[1,2,3,4,5];
B=[5,6,7,8,9,10,11,12];

parfor i=1:5
n(i)=A(i);
for j=1:8
m=B(j);
end
end

Matlab tells me that B is not sliced. Is there any way around this problem? I haven't seen any(?) examples where there are two loops like this inside each other with parfor-loop as the outer loop. Can I only parfor the inner loop?

Thanks,
Aino
From: Walter Roberson on
Aino wrote:
> Hi all,
>
> I have basically this situation:
>
> A=[1,2,3,4,5];
> B=[5,6,7,8,9,10,11,12];
>
> parfor i=1:5
> n(i)=A(i);
> for j=1:8
> m=B(j);
> end
> end


m is not used after the j loop and is not used in the parfor loop. It is
thus pointless to compute it there and you might as well simply put

m = B(8);

after the parfor loop.

If you really want a loop over j then since it is independent of i, put
it before the parfor loop so that the work only needs to be done once
and the results would then be available for the body of the parfor.
From: Aino on
Thank you for your quick reply!

Oh no! What have I been doing.. The i and j-loops are not totally independent in my code, but the inner loop is still repeating the same big calculations over and over again. I got from 40s to 2s just by correcting that (taking the inner loop out, as you suggested)! I don't need the parfor now.

Lesson learned, check for any major flaws before going to parfor, mex-files such.

Thank you!

-Aino


Walter Roberson <roberson(a)hushmail.com> wrote in message <GKV8o.48957$F%7.4216(a)newsfe10.iad>...
> Aino wrote:
> > Hi all,
> >
> > I have basically this situation:
> >
> > A=[1,2,3,4,5];
> > B=[5,6,7,8,9,10,11,12];
> >
> > parfor i=1:5
> > n(i)=A(i);
> > for j=1:8
> > m=B(j);
> > end
> > end
>
>
> m is not used after the j loop and is not used in the parfor loop. It is
> thus pointless to compute it there and you might as well simply put
>
> m = B(8);
>
> after the parfor loop.
>
> If you really want a loop over j then since it is independent of i, put
> it before the parfor loop so that the work only needs to be done once
> and the results would then be available for the body of the parfor.