在 spmd 块中使用 parfor 时出错 - matlab

Error using parfor inside spmd block - matlab

据我所知,在 Matlab 中进行并行编程,我们可以准确地指定哪个 worker 做什么;使用 :

if labindex == x 
    %some computations
end

我们也可以 运行 for 并行循环;使用:

parfor i1 = x:y
    %some computations
end

我使用的集群有几个节点,每个节点有 8 个核心。
我想要 运行 2 个函数,每个函数都包含一个 parfor 循环,每个函数都由一个工作人员执行,我的代码是这样的:

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

我希望这 2 个函数由 2 个不同的节点同时执行。 但是 Matlab 抛回了这个错误:

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

为什么会这样? 有什么想法吗?

这包含在 parfor 文档中:

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.

spmd 语句也是如此:

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.

貌似其实可以嵌套spmd/parfor只要封装在函数里,但还是不会运行并行,所以没有意义.