2012-07-19 3 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 

코드에서 spmd를 끄기 위해 marco와 같은 것이 있으면 궁금합니다.

답변

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 
관련 문제