Search code examples
matlabfor-loopparallel-processingparfor

parfor/for depending on loopsize


Is there a simple way to write a for loop in matlab such that below a certain number (n) of iterations it is a normal for-loop, whereas above this threshold it is a parfor loop?

I want to reduce the overhead that is associated to creating a parallel pool. I tried to set the number of workers to 1 (for less than n-iterations) but still the overhead is non-negligible.


Solution

  • Option 1:

    Accelerating the startup of parpool (might not work on all systems and/or MATLAB versions):

    distcomp.feature( 'LocalUseMpiexec', false );
    

    (Source)

    Option 2:

    Set a really long timeout to the parpool, so that it surely exists whenever needed:

    % This will set the idle timeout to 2 hours 
    parpool('IdleTimeout', 120);
    ...
    functionThatPossiblyCallsParfor();
    

    % If pool is already running:
    p = parpool;
    p.IdleTimeout = 120;
    ...
    functionThatPossiblyCallsParfor();
    

    (Source)

    Option 3:

    if/else based on number of required iterations (this answers the question title):

    if n > 1E6 % or any other appropriate limit
      parfor (...)
        someFunction(slicedInput1,slicedInput2,...);
      end
    else
      for (...)
        someFunction(input1,input2,...);
      end
    end