0

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?

  • Added relevant pointers in Matlab documentation. It doesn't seem like your code should throw an error (not according to the latest Matlab documentation, at least), so you should provide [MCVE](http://stackoverflow.com/help/mcve) if you want to get further help here. – nirvana-msu Jan 05 '17 at 21:55

1 Answers1

0

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.

nirvana-msu
  • 3,877
  • 2
  • 19
  • 28
  • Your answer is clear, but consider that each worker (node of the cluster that I'm using) has a cpu with 8 cores. and in `SPMD` block I'm assigning each worker a function that contains a `PARFOR`. which means the worker can open a parallel pool itself. so what will happen in this situation? does the `PARFOR` loops get executed in parallel? –  Jan 06 '17 at 18:16
  • Doesn't matter how many cores the workers have since they cannot open parallel pool themselves.This is a limitation of Matlab which is clearly described in the documentation I cited. You need to either refactor your code to avoid nested `parfor`/`spmd`, or else span multiple matlab instances yourself instead of relying on `spmd`to do that for you. – nirvana-msu Jan 07 '17 at 14:34