1
votes

The body of a parfor-loop cannot contain a break statement.:

The parfor statement works by breaking up iterations of a loop and running these iterations on multiple MATLAB workers. Using break and return statements implies that later iterations of the loop should not run after either of these two statements execute. Therefore, the loop iterations must run in sequence. However, for the parfor loop to be valid, the result of running the successive loop iterations must not depend on the order in which they run.

E.g. the following won't work:

if matlabpool('size') == 0 % checking to see if my pool is already open
    matlabpool(2)
end

parfor i=1:10
    inv(rand(1000))
    break
end

Is there any usual way to imitate a break in a parfor?

2

2 Answers

2
votes

I'm afraid you can't get out of a parfor loop with something like a break statement since the order in which the loop is executed is totally arbitrary.

Would it be possible for you to use spmd blocks instead? With them you can let each worker know whether some condition is met/violated for example and thus better control the flow of the program. That might be more of a comment than an answer sorry; I though it was too long for a comment though.

0
votes

There is no way to break, but if you define a logical condition, you can use it to skip parfor counter and still use the advantages of parfor processing in exchange for a mild expense of skipping some counter numbers. for example

parfor i=1:10
    if (i>= 5)
       continue;
    end
    inv(rand(1000))
end

Also if possible, use simple for loop, it is better for small programs, I usually use parfor for heavy duty Monte Carlo simulation with multi-layer for loops and huge processing.

You can also operate parfor on little chunks and put it inside a while or another parfor loop. For example assume you want to run a program with "mounte_num = 10000" iterations. define "monte_temp = mounte_num / 100" and go like this

temp = 0
while (temp <= 100)
     parfor i=1:monte_temp 
           WHATEVER;
     end
     temp = temp + 1;
end