Search code examples
multithreadingmatlabparfor

Matlab nested parfor loops inside functions, will all workers be used if the outer loop is small?


If I understand the documentation correctly I can have a parfor loop inside another parfor loop as long as the inner loop is in a separate function call, which is true in my case.

My question is simply this:

In some instances my outer loop only uses 2 threads/workers. I have 6 workers in my parallel pool.

Will the inner parfor loop take advantage of the 4 unused workers?


Solution

  • From MATLAB's own documentation (http://www.mathworks.com/help/distcomp/nesting-and-flow-in-parfor-loops.html):

    The body of a parfor-loop cannot contain another parfor-loop. But it can call a function that contains another parfor-loop.

    However, because a worker cannot open a parallel pool, a worker cannot run the inner nested parfor-loop in parallel. This means that only one level of nested parfor-loops can run in parallel. If the outer loop runs in parallel on a parallel pool, the inner loop runs serially on each worker. If the outer loop runs serially in the client (e.g., parfor specifying zero workers), the function that contains the inner loop can run the inner loop in parallel on workers in a pool.

    As a worker cannot itself open a pool, the inner parfor loop will work in a serial manner.