matlabparallel-processingparforspmd

Error using parfor inside spmd block - matlab


As I know about parallel programming in Matlab, We can exactly specify what worker does what; using :

if labindex == x 
    %some computations
end

Also we can run for loops in parallel; using :

parfor i1 = x:y
    %some computations
end

I'm using a cluster with a few nodes and each node has 8 cores.
I want to run 2 functions which each one contains an parfor loop, and each function get executed by an worker, my code is something like this :

spmd
    if labindex == 1
        alpha  = forward( some parameters );
    end
    if labindex == 2
        beta  = backward( some parameters );
    end
end

I wanted these 2 functions get executed simultaneously by 2 different nodes. but Matlab throws back this error :

PARFOR or SPMD can not be used inside an SPMD block.

Why is that so? Any idea?


Solution

  • This is covered in parfor documentation:

    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.

    Same is true for spmd statements:

    The body of an spmd statement cannot directly contain another spmd. However, it can call a function that contains another spmd statement. The inner spmd statement does not run in parallel in another parallel pool, but runs serially in a single thread on the worker running its containing function.

    It appears that you can actually have nested spmd/parfor as long as they are encapsulated in functions, but they will still not run in parallel, so there's no point.