我使用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。
发布于 2012-07-20 07:05:35
您可以将工作进程的数量作为参数传递给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
endhttps://stackoverflow.com/questions/11570004
复制相似问题