Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Matlab PARFOR loop be programmatically switched on/off?

Tags:

matlab

parfor

Have a simple question about parfor in MATLAB. I would like to set a flag in my program to change between parfor and regular for loops. Basically, I need this functionality so that some parts of my code can update graphics in a "debug" mode, then when the flag is turned off, use parfor with no graphics updates for speed.

So, I'm looking for something simple that has this functionality:

if (flag)
  for i = 1:n
else
  parfor i = 1:n
end

  % Do loop tasks.

  end

Any help would be greatly appreciated! Thanks!

like image 357
Kyle Lynch Avatar asked Apr 12 '12 23:04

Kyle Lynch


1 Answers

No, this is not possible. However, if you can wrap the loop body in a separate function, you can have either a parfor or a for loop call the body, i.e.

if (flag)
   parfor i=1:n
      out(i) = loopBody(i)
   end
else
   for i=1:n
      out(i) = loopBody(i)
   end
end

Alternatively, you can edit the code so that you have either parfor or for in front of your loop, which is what I often end up doing.

like image 185
Jonas Avatar answered Sep 25 '22 18:09

Jonas