From: Xiaolong Ma on
Is there a bug in matlab parfor:


%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% for loop
x=[];
for i=1:2
for j=1:3
x = [x, j];
end
end
x

x =

1 2 3 1 2 3


%%%%%%%%%%%%%%%
%%%% parfor loop

x=[];
parfor i=1:2
for j=1:3
x = [x, j];
end
end
x

x =

3 2 1 3 2 1
From: Edric M Ellis on
"Xiaolong Ma" <nguu(a)msn.com> writes:

> I think matlab anticipates internally that the parfor will be executed
> in reverse order. So when it assembles the output for the reduction
> variables, it tries to "correct" this by putting everything
> upside-down. With this simple approach, it runs into problems as soon
> as there's a nested loop, because the inner loop is never executed in
> reverse order (parfor can only nest with a for, not another
> parfor). So at the same time it corrects the order of the outer loop,
> it actually put the inner loop in the wrong order.

We're still looking into this, but it looks like you're right that the
code to handle reductions isn't dealing with this case correctly. Thanks
for reporting this. For now, you can work around the problem by running
the inner concatenation separately, like so:

x = [];
parfor ii=1:3
tmp = [];
for jj=1:3
tmp = [tmp, jj];
end
x = [x, tmp];
end


Cheers,

Edric.
From: Alan Weiss on
This is not a bug, it is documented behavior.
http://www.mathworks.com/access/helpdesk/help/toolbox/distcomp/parfor.html

Alan Weiss
MATLAB mathematical toolbox documentation

On 4/17/2010 12:48 AM, Xiaolong Ma wrote:
> Is there a bug in matlab parfor:
>
>
> %%%%%%%%%%%%%%%%%%%%%%
> %%%%%%%% for loop
> x=[];
> for i=1:2
> for j=1:3
> x = [x, j];
> end
> end
> x
>
> x =
>
> 1 2 3 1 2 3
>
>
> %%%%%%%%%%%%%%%
> %%%% parfor loop
>
> x=[];
> parfor i=1:2
> for j=1:3
> x = [x, j];
> end
> end
> x
>
> x =
>
> 3 2 1 3 2 1

From: Alan Weiss on
OOPS, strike that, I didn't read your post carefully enough. My answer
was not correct.

You do indeed point out a mysterious example, one that I do not
currently understand.

Alan Weiss
MATLAB mathematical toolbox documentation

On 4/19/2010 7:58 AM, Alan Weiss wrote:
> This is not a bug, it is documented behavior.
> http://www.mathworks.com/access/helpdesk/help/toolbox/distcomp/parfor.html
>
> Alan Weiss
> MATLAB mathematical toolbox documentation
>
> On 4/17/2010 12:48 AM, Xiaolong Ma wrote:
>> Is there a bug in matlab parfor:
>>
>>
>> %%%%%%%%%%%%%%%%%%%%%%
>> %%%%%%%% for loop
>> x=[];
>> for i=1:2
>> for j=1:3
>> x = [x, j];
>> end
>> end
>> x
>>
>> x =
>>
>> 1 2 3 1 2 3
>>
>>
>> %%%%%%%%%%%%%%%
>> %%%% parfor loop
>>
>> x=[];
>> parfor i=1:2
>> for j=1:3
>> x = [x, j];
>> end
>> end
>> x
>>
>> x =
>>
>> 3 2 1 3 2 1
>

From: Xiaolong Ma on
Thanks for the reply!

I think matlab anticipates internally that the parfor will be executed in reverse order. So when it assembles the output for the reduction variables, it tries to "correct" this by putting everything upside-down. With this simple approach, it runs into problems as soon as there's a nested loop, because the inner loop is never executed in reverse order (parfor can only nest with a for, not another parfor). So at the same time it corrects the order of the outer loop, it actually put the inner loop in the wrong order.


Alan Weiss <aweiss(a)mathworks.com> wrote in message <hqhigl$7r1$1(a)fred.mathworks.com>...
> OOPS, strike that, I didn't read your post carefully enough. My answer
> was not correct.
>
> You do indeed point out a mysterious example, one that I do not
> currently understand.
>
> Alan Weiss
> MATLAB mathematical toolbox documentation
>
> On 4/19/2010 7:58 AM, Alan Weiss wrote:
> > This is not a bug, it is documented behavior.
> > http://www.mathworks.com/access/helpdesk/help/toolbox/distcomp/parfor.html
> >
> > Alan Weiss
> > MATLAB mathematical toolbox documentation
> >
> > On 4/17/2010 12:48 AM, Xiaolong Ma wrote:
> >> Is there a bug in matlab parfor:
> >>
> >>
> >> %%%%%%%%%%%%%%%%%%%%%%
> >> %%%%%%%% for loop
> >> x=[];
> >> for i=1:2
> >> for j=1:3
> >> x = [x, j];
> >> end
> >> end
> >> x
> >>
> >> x =
> >>
> >> 1 2 3 1 2 3
> >>
> >>
> >> %%%%%%%%%%%%%%%
> >> %%%% parfor loop
> >>
> >> x=[];
> >> parfor i=1:2
> >> for j=1:3
> >> x = [x, j];
> >> end
> >> end
> >> x
> >>
> >> x =
> >>
> >> 3 2 1 3 2 1
> >