Parallel processing 有没有办法在matlab中为spmd命令添加一个开关?

Parallel processing 有没有办法在matlab中为spmd命令添加一个开关?,parallel-processing,matlab,Parallel Processing,Matlab,我正在使用matlab2011进行多核并行计算,在我的代码中,它只是用block SPMD END实现的。但是,有时我想根据输入参数关闭程序中的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

我正在使用matlab2011进行多核并行计算,在我的代码中,它只是用block SPMD END实现的。但是,有时我想根据输入参数关闭程序中的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
,即

然后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
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