2013-02-15 4 views
0

MATLAB에서 분산 형 핸들 클래스 객체 배열을 만들려고하는데이 벡터에서 특정 핸들을 추출하여 명령 행에서 사용하고 싶습니다. 아래 코드가 주어지면 다음을 실행하고 싶습니다. 나는 이것을 두 개의 실험실 (matlabpool 2)에서 실행하고있다. 내가 병렬로 배포하고자하는 클래스 다음을 getValue 기능은MATLAB 공동 분산 형 배열에서 값 추출

vec = buildArray; 
h = getValue(vec,6); 
h.id 

ClassA.m ... 난 덕분에, 도움이 필요한 것입니다.

classdef ClassA < handle 
    properties 
     id; 
    end 

    methods 
     function obj = ClassA(id) 
     obj.id = id; 
     end 
    end 
end 

buildArray.m : 를 ClassA의 로컬 인스턴스에서 codistributed 어레이를 구축하는 기능.

function vec = buildArray 
X(:,1) = 1:10;    % create ids 
gsize = size(X);   % the global size 
X = distributed(X);   % distribute the ids 

spmd  
    x = getLocalPart(X);  % extract the local ids 
    local = cell(length(x),1); % create local storage for handles 

    % Create the class instances 
    for i = 1:length(x);  
     local{i} = ClassA(x(i));  
    end 

    % Build the codistributed array of handles 
    codist = codistributor1d(codistributor1d.unsetDimension, ... 
    codistributor1d.unsetPartition, gsize); 
    vec = codistributed.build(local,codist); 
end 

는 getValue.m : 이 현재 그냥 어떤 실험실하면 지정된 ID를 가지는 클래스를 포함하는 표시, 내가 도움이 필요한 기능입니다. 명령 줄에서 사용할 수 있도록 핸들을 반환하고 싶습니다. 어떻게 이뤄지나요?

function h = getValue(vec, id) 
h = []; % just so it will not throw an error 
spmd 
local = getLocalPart(vec); 
for i = 1:length(local); 
    if local{i}.id == id; 
     % Export h here 
     disp(['ID ', num2str(id), ' is on lab ', num2str(labindex)]); 
     break; 
    end 
end 
end 

답변

0

getValue.m을 사용하여 내 문제를 해결할 수 있었습니까? 더 좋은 방법이 있습니까?

function h = getValue(vec, id) 

spmd 
local = getLocalPart(vec); 
idx = []; 
for i = 1:length(local); 
    if local{i}.id == id; 
     idx = i; 
     break; 
    end 
end 

codist = codistributor1d(codistributor1d.unsetDimension, ... 
    codistributor1d.unsetPartition, [numlabs,1]); 
IDX = codistributed.build(idx, codist); 
end 

c = gather(vec(IDX)); 
h = c{1};