2012-07-19 120 views
3

我使用matlab2011用於與多個核,這在我的代碼簡單地與塊SPMD END實現並行計算。但是,有時候,我想根據輸入參數關閉程序中的spmd。我嘗試下面的代碼,但它不起作用任何方式在MATLAB中添加開關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。

回答

3

您可以通過工人的數量作爲參數傳遞給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