0

初めて並列計算 (spmd) を使用しています。

プールを開始していくつかの並列計算を行った後、ワークスペースに複合変数しかなく、それらを開くことができません。または、ダブルクリックして開くと、空です。データはどのように使用できますか?

これが私のコードです:

matlabpool open local 4

spmd
    if labindex==1
    a = randn(300,1);
    end
    if labindex==2
     b = randn(300,1);
    end
    if labindex==3
    c = randn(300,1);
    end
    if labindex==4
     d = randn(300,1);
    end
 end

matlabpool close
4

1 に答える 1

1

個々のワーカーでそれぞれサイズ (300,1) の 4 つの配列を生成する場合は、次のようにすることをお勧めします。私のコンピューター/Matlab プールには 4 つのコアがあることに注意してください。

clc
clear

spmd    
    RandomArray = rand(300,1); % Matlab automatically creates a (300,1) array in each worker.
end

FinalArray = [RandomArray{:}]; % Concatenate everything outside of the spmd block.

whos % Check the results


Name               Size            Bytes  Class        Attributes

  FinalArray       300x4              9600  double                 
  RandomArray        1x4              1145  Composite              

ご覧のとおり、FinalArray のサイズは (300,4) です。上記のコードでは、2 番目の spmd ブロックにすべてをまとめるのは非常に面倒です。各ワーカーは他のワーカーの内容を認識しておらず、変数を使用していないワーカーでは各変数が定義されていないためです。申し訳ありませんが、正しい用語はわかりませんが、ドキュメントを読んでより良い説明を得ることができます:)

編集:

あなたのコメントに答えるために、ここに簡単な例があります。うまくいけば、これはあなたが意図したものです:)

clc
clear

% Define different variables.
w = ones(1,10); 
x = 1:10;
y = x/2;
z = rand(1,10);

% Use different functions in each worker. Of course you could use the same function with different inputs.
spmd
    if labindex==1        
        a = w;        
    end
    if labindex==2
        b = sin(x);
    end
    if labindex==3
        c = y.^2;
    end
    if labindex==4
        d = 4*z;
    end
end

% This is the important part
FinalArray = [a{1} ;b{2}; c{3} ;d{4}];
whos 

whos の出力は次のとおりです。

Name            Size            Bytes  Class        Attributes

  FinalArray      4x10              320  double                 
  a               1x4               497  Composite              
  b               1x4               497  Composite              
  c               1x4               497  Composite              
  d               1x4               497  Composite              
  w               1x10               80  double                 
  x               1x10               80  double                 
  y               1x10               80  double                 
  z               1x10               80  double      
于 2014-09-25T13:25:33.660 に答える