我使用matlab2011进行多核并行计算,在我的代码中,它只是用块SPMD端实现的但是,有些时候,我想根据输入参数关闭程序中的spmd我尝试了下面的代码,但它不起作用

if (switchSpmdOn)
  spmd
end
% here I put my code for calculation in parallel or series
if (switchSpmdOn)
  end % this end is used to close the spmd block
end

我想知道在代码中是否有类似于marco的东西可以关闭spmd。

最佳答案

您可以将工人数量作为参数传递给spmd如果指定的工人数0,即。,

spmd(0)
   statement; %# block body
end

然后,MATLAB将在本地执行块体并创建复合对象,就像没有可用的池一样。
例子
switchSpmdOn = true;

%# set the number of workers
if (switchSpmdOn)
    workers = 3;
    matlabpool(workers);
else
    workers = 0;
end

%# application. if 'workers' is 0, MATLAB will execute this code locally
spmd (workers)
    %# build magic squares in parallel
    q = magic(labindex + 2);
end

for ii=1:length(q)
  % plot each magic square
  figure, imagesc(q{ii});
end

if (switchSpmdOn)
    matlabpool close
end

09-09 23:26
查看更多