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:
break a for-loop within a parfor-loop

Subject: break a for-loop within a parfor-loop

From: Oliver

Date: 14 Nov, 2013 19:51:06

Message: 1 of 4

Hi,

I have the following code:

parfor i= 1:iterations
for j = 1:N
%do stuff here
   if(something)
        %do something
    else
        %we wish to break the for j=1:N loop
        break;
    end

end
end

I get a warning that says "BREAK and RETURN statements cannot be used inside a PARFOR loop". why and how to solve this?

Subject: break a for-loop within a parfor-loop

From: someone

Date: 14 Nov, 2013 20:04:05

Message: 2 of 4

"Oliver" wrote in message <l639ja$ooi$1@newscl01ah.mathworks.com>...
> Hi,
>
> I have the following code:
>
> parfor i= 1:iterations
> for j = 1:N
> %do stuff here
> if(something)
> %do something
> else
> %we wish to break the for j=1:N loop
> break;
> end
>
> end
> end
>
> I get a warning that says "BREAK and RETURN statements cannot be used inside a PARFOR loop". why and how to solve this?

Can you change the inside for loop & if statement to a while loop instead?

Subject: break a for-loop within a parfor-loop

From: Oliver

Date: 14 Nov, 2013 21:33:24

Message: 3 of 4

No sorry, not possible.
"someone" wrote in message <l63abl$4sh$1@newscl01ah.mathworks.com>...
> "Oliver" wrote in message <l639ja$ooi$1@newscl01ah.mathworks.com>...
> > Hi,
> >
> > I have the following code:
> >
> > parfor i= 1:iterations
> > for j = 1:N
> > %do stuff here
> > if(something)
> > %do something
> > else
> > %we wish to break the for j=1:N loop
> > break;
> > end
> >
> > end
> > end
> >
> > I get a warning that says "BREAK and RETURN statements cannot be used inside a PARFOR loop". why and how to solve this?
>
> Can you change the inside for loop & if statement to a while loop instead?

Subject: break a for-loop within a parfor-loop

From: Edric M Ellis

Date: 15 Nov, 2013 10:20:58

Message: 4 of 4

"Oliver " <f_oliver5@hotmail.com> writes:

> I have the following code:
>
> parfor i= 1:iterations for j = 1:N
> %do stuff here
> if(something)
> %do something
> else
> %we wish to break the for j=1:N loop
> break; end
>
> end
> end
>
> I get a warning that says "BREAK and RETURN statements cannot be used
> inside a PARFOR loop". why and how to solve this?

You cannot use BREAK or RETURN within PARFOR because these effectively
introduce order-dependency. One approach you could try if you're using
R2013b is to use the new PARFEVAL construct, one of the aims of which
was to support parallel programs that need to finish early. You can see
an example of this here:

<http://www.mathworks.co.uk/help/distcomp/examples/parfeval-blackjack.html>

Cheers,

Edric.

Tags for 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