Is it possible to conditionally switch between parallel and non parallel for loops?

I would like to be able to set a flag as the input to a function that changes the code from executing a for loop to a parfor loop and vice versa.
It would be handy sometimes so I can debug the contents of the loop (in a standard for) without actually changing the function around it.

 Réponse acceptée

You can do this by setting the optional "number of workers" argument to 0. For example
runInSerial = <...>;
if runInSerial
parforArg = 0;
else
parforArg = Inf;
end
parfor (idx = 1:N, parforArg)
...
end

4 commentaires

Matt J
Matt J le 6 Nov 2014
Modifié(e) : Matt J le 6 Nov 2014
Edric, do MATLAB installations lacking the Parallel Computing Toolbox support this? I seem to remember older versions of parfor could run without the Toolbox, but much more slowly than a regular for. Can MATLAB installations without the PCT now set parforArg=0 and get the full normal performance of a for-loop?
Thanks, didn't know you could set that argument in a parfor. Just tested it and, it looks like Matlab won't actually let you put a breakpoint into a for loop, however the 'keyboard' function with parforArg=0 does effectively the same thing as a standard for loop with a breakpoint.
MATLAB without Parallel Computing Toolbox supports the "number of workers" argument (and ignores it). It doesn't change the performance - in all cases, even if "number of workers" is zero, the loop still runs as a PARFOR (in the sense that all the PARFOR constraints apply etc.) - it simply runs locally in the MATLAB process rather than using a pool.
Dear Edric Ellis, there was no clear answer before indication of the alternatives. Is the answer is no?

Connectez-vous pour commenter.

Plus de réponses (1)

I would just write two separate subfunctions one which uses parfor and one that doesn't. Parfor without any workers will be less efficient than a regular for-loop. If the parallel flag is on, call one, else, call the other.

4 commentaires

Sean, I wonder if this is still true in recent versions. In the test below (R2013b), I see basically equivalent execution times. On the other hand, I do have the PCT installed and I wonder whether this has an influence, even though I'm not using any workers. My question to Edric was about whether I would still see this if the PCT were not there.
function test
N=1e7;
tic;
for i=1:N
sin(N);
end
toc
tic;
parfor (i=1:N,0)
sin(N);
end
toc
Note that the difference in timing you're seeing there is not related to whether or not PCT is installed - and everything to do with running it outside the context of a function. PARFOR can perform much more accurate program analysis inside a function, and gets better performance.
Ah, good to know. And you're right, when I convert my code above to an ordinary script, the parfor version slows down greatly.
Thats actually not a good solution, since it leads to code duplication.

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by