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:
Why does parfor fail here?

Subject: Why does parfor fail here?

From: Chuck37

Date: 21 Mar, 2013 20:04:05

Message: 1 of 6

This is gibberish but causes the failure I'm seeing:

parfor x = 1:2
    y = 1:x*4
    for z = y
        if z ~= y(1)
            b = a*5;
        end

        if z == y(1)
            a = 3;
        else
            a = a*4;
        end
     end
end

If parfor is replaced with for it works fine. With parfor in there, it appears to dump the variable "a" after each inner loop iteration, or something. It sets it, then complains that it doesn't exist at the "b=a*5" line.

Any ideas would be great.

Subject: Why does parfor fail here?

From: anyone

Date: 22 Mar, 2013 00:17:47

Message: 2 of 6

On Thu, 21 Mar 2013 20:04:05 +0000, Chuck37 wrote:

> This is gibberish but causes the failure I'm seeing:
>
> parfor x = 1:2
> y = 1:x*4 for z = y
> if z ~= y(1)
> b = a*5;
> end
>
> if z == y(1)
> a = 3;
> else
> a = a*4;
> end
> end
> end
>
> If parfor is replaced with for it works fine. With parfor in there, it
> appears to dump the variable "a" after each inner loop iteration, or
> something. It sets it, then complains that it doesn't exist at the
> "b=a*5" line.

"for" executes in-order -- the innermost loop, followed by the next outer, and so on. seems that will work here
because the first conditional, inner loop, evaluates to 'false' while the next conditional evaluates to 'true'. thereafter
everything executes to completion.

"parfor" does not have a guaranteed order-of-execution. if/when execution allows evaluation of the first conditional as
'true', you will attempt to assign a value to 'b' based on 'a', but 'a' is not yet defined.

> Any ideas would be great.

one great idea is to vote repugnicans out of office in 2014.

Subject: Why does parfor fail here?

From: Chuck37

Date: 22 Mar, 2013 15:12:05

Message: 3 of 6

Modify it just slightly, so it is (slightly) less like nonsense:

parfor x = 1:2
     y = 1:x*4
     for z = y
         if z ~= y(1)
             b(x) = a*5;
         end
 
         if z == y(1)
             a = 3;
         else
             a = a*4;
         end
      end
end

All I changed was "b" to "b(x)".

The intention is to have essentially this:

parfor x = 1:2
  b(x) = <function of x>
end

But for some reason the parfor doesn't see it that way and is getting confused. This is a run time problem though, I get the green light in the editor.

anyone <jaup@h/WSWLG{nR1³qHe> wrote in message <514ba32b$0$7624$c3e8da3$12bcf670@news.astraweb.com>...
> On Thu, 21 Mar 2013 20:04:05 +0000, Chuck37 wrote:
>
> > This is gibberish but causes the failure I'm seeing:
> >
> > parfor x = 1:2
> > y = 1:x*4 for z = y
> > if z ~= y(1)
> > b = a*5;
> > end
> >
> > if z == y(1)
> > a = 3;
> > else
> > a = a*4;
> > end
> > end
> > end
> >
> > If parfor is replaced with for it works fine. With parfor in there, it
> > appears to dump the variable "a" after each inner loop iteration, or
> > something. It sets it, then complains that it doesn't exist at the
> > "b=a*5" line.
>
> "for" executes in-order -- the innermost loop, followed by the next outer, and so on. seems that will work here
> because the first conditional, inner loop, evaluates to 'false' while the next conditional evaluates to 'true'. thereafter
> everything executes to completion.
>
> "parfor" does not have a guaranteed order-of-execution. if/when execution allows evaluation of the first conditional as
> 'true', you will attempt to assign a value to 'b' based on 'a', but 'a' is not yet defined.
>
> > Any ideas would be great.
>
> one great idea is to vote repugnicans out of office in 2014.

Subject: Why does parfor fail here?

From: anyone

Date: 22 Mar, 2013 16:32:11

Message: 4 of 6

On Fri, 22 Mar 2013 15:12:05 +0000, Chuck37 wrote:

> Modify it just slightly, so it is (slightly) less like nonsense:
[...]
> But for some reason the parfor doesn't see it that way and is getting
> confused. This is a run time problem though, I get the green light in
> the editor.

and did you read the earlier reply?

>
> anyone <jaup@h/WSWLG{nR1³qHe> wrote in message
> <514ba32b$0$7624$c3e8da3$12bcf670@news.astraweb.com>...
>> On Thu, 21 Mar 2013 20:04:05 +0000, Chuck37 wrote:
>>
>> > This is gibberish but causes the failure I'm seeing:
>> >
>> > parfor x = 1:2
>> > y = 1:x*4 for z = y
>> > if z ~= y(1)
>> > b = a*5;
>> > end
>> >
>> > if z == y(1)
>> > a = 3;
>> > else
>> > a = a*4;
>> > end
>> > end
>> > end
>> >
>> > If parfor is replaced with for it works fine. With parfor in there,
>> > it appears to dump the variable "a" after each inner loop iteration,
>> > or something. It sets it, then complains that it doesn't exist at
>> > the "b=a*5" line.
>>
>> "for" executes in-order -- the innermost loop, followed by the next
>> outer, and so on. seems that will work here because the first
>> conditional, inner loop, evaluates to 'false' while the next
>> conditional evaluates to 'true'. thereafter everything executes to
>> completion.
>>
>> "parfor" does not have a guaranteed order-of-execution. if/when
>> execution allows evaluation of the first conditional as 'true', you
>> will attempt to assign a value to 'b' based on 'a', but 'a' is not yet
>> defined.
>>
>> > Any ideas would be great.
>>
>> one great idea is to vote repugnicans out of office in 2014.

Subject: Why does parfor fail here?

From: Chuck37

Date: 22 Mar, 2013 21:29:04

Message: 5 of 6

I did and I don't think it answers my question, or else I don't understand what you are trying to say. The stuff inside the parfor is a function of x alone. There is no order dependence to it. I think I could grab everything inside the parfor out and form a function with x as the only argument and it would run fine, but inside the parfor it appears to throw away "a" after each execution of the inner loop.

anyone <jaup@h/WSWLG{nR1³qHe> wrote in message <514c878b$0$28750$c3e8da3$f017e9df@news.astraweb.com>...
> On Fri, 22 Mar 2013 15:12:05 +0000, Chuck37 wrote:
>
> > Modify it just slightly, so it is (slightly) less like nonsense:
> [...]
> > But for some reason the parfor doesn't see it that way and is getting
> > confused. This is a run time problem though, I get the green light in
> > the editor.
>
> and did you read the earlier reply?
>
> >
> > anyone <jaup@h/WSWLG{nR1³qHe> wrote in message
> > <514ba32b$0$7624$c3e8da3$12bcf670@news.astraweb.com>...
> >> On Thu, 21 Mar 2013 20:04:05 +0000, Chuck37 wrote:
> >>
> >> > This is gibberish but causes the failure I'm seeing:
> >> >
> >> > parfor x = 1:2
> >> > y = 1:x*4 for z = y
> >> > if z ~= y(1)
> >> > b = a*5;
> >> > end
> >> >
> >> > if z == y(1)
> >> > a = 3;
> >> > else
> >> > a = a*4;
> >> > end
> >> > end
> >> > end
> >> >
> >> > If parfor is replaced with for it works fine. With parfor in there,
> >> > it appears to dump the variable "a" after each inner loop iteration,
> >> > or something. It sets it, then complains that it doesn't exist at
> >> > the "b=a*5" line.
> >>
> >> "for" executes in-order -- the innermost loop, followed by the next
> >> outer, and so on. seems that will work here because the first
> >> conditional, inner loop, evaluates to 'false' while the next
> >> conditional evaluates to 'true'. thereafter everything executes to
> >> completion.
> >>
> >> "parfor" does not have a guaranteed order-of-execution. if/when
> >> execution allows evaluation of the first conditional as 'true', you
> >> will attempt to assign a value to 'b' based on 'a', but 'a' is not yet
> >> defined.
> >>
> >> > Any ideas would be great.
> >>
> >> one great idea is to vote repugnicans out of office in 2014.

Subject: Why does parfor fail here?

From: anyone

Date: 23 Mar, 2013 19:42:08

Message: 6 of 6

On Fri, 22 Mar 2013 21:29:04 +0000, Chuck37 wrote:

> I did and I don't think it answers my question, or else I don't
> understand what you are trying to say.

Review: earlier, you indicate changing the first line to 'for' from 'parfor' causes the script file to execute normally --
that is, it ends in an expected manner. You also wrote that use of 'parfor' causes the script to ab-end ("abnormally
end") after the first line, and an error message is displayed.

The primary difference between use of 'for' and use of 'parfor' is the former has a guaranteed execution order while the
latter does not. On reading your script file, my conclusion is the 'parfor' version fails because the script falls-through
on "x" actually equal to the first element, but "a" is not yet defined. A solution, then, is use 'for'. Another possible
solution is, use 'parfor' but find a more suitable way to define "a". Everything else depends on your choice.

> The stuff inside the parfor is a
> function of x alone. There is no order dependence to it. I think I
> could grab everything inside the parfor out and form a function with x
> as the only argument and it would run fine, but inside the parfor it
> appears to throw away "a" after each execution of the inner loop.
>
> anyone <jaup@h/WSWLG{nR1³qHe> wrote in message
> <514c878b$0$28750$c3e8da3$f017e9df@news.astraweb.com>...
>> On Fri, 22 Mar 2013 15:12:05 +0000, Chuck37 wrote:
>>
>> > Modify it just slightly, so it is (slightly) less like nonsense:
>> [...]
>> > But for some reason the parfor doesn't see it that way and is getting
>> > confused. This is a run time problem though, I get the green light
>> > in the editor.
>>
>> and did you read the earlier reply?
>>
>>
>> > anyone <jaup@h/WSWLG{nR1³qHe> wrote in message
>> > <514ba32b$0$7624$c3e8da3$12bcf670@news.astraweb.com>...
>> >> On Thu, 21 Mar 2013 20:04:05 +0000, Chuck37 wrote:
>> >>
>> >> > This is gibberish but causes the failure I'm seeing:
>> >> >
>> >> > parfor x = 1:2
>> >> > y = 1:x*4 for z = y
>> >> > if z ~= y(1)
>> >> > b = a*5;
>> >> > end
>> >> >
>> >> > if z == y(1)
>> >> > a = 3;
>> >> > else
>> >> > a = a*4;
>> >> > end
>> >> > end
>> >> > end
>> >> >
>> >> > If parfor is replaced with for it works fine. With parfor in
>> >> > there, it appears to dump the variable "a" after each inner loop
>> >> > iteration,
>> >> > or something. It sets it, then complains that it doesn't exist at
>> >> > the "b=a*5" line.
>> >>
>> >> "for" executes in-order -- the innermost loop, followed by the next
>> >> outer, and so on. seems that will work here because the first
>> >> conditional, inner loop, evaluates to 'false' while the next
>> >> conditional evaluates to 'true'. thereafter everything executes to
>> >> completion.
>> >>
>> >> "parfor" does not have a guaranteed order-of-execution. if/when
>> >> execution allows evaluation of the first conditional as 'true', you
>> >> will attempt to assign a value to 'b' based on 'a', but 'a' is not
>> >> yet defined.
>> >>
>> >> > Any ideas would be great.
>> >>
>> >> one great idea is to vote repugnicans out of office in 2014.

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