2014-09-25 63 views
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 

回答

1

如果你想在每一個單獨的工人產生4個陣列尺寸(300,1)的,最好是做像下面這樣。請注意,我的電腦/ 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)是你想要的。使用上面的代碼,將所有內容放在第二個spmd塊中會非常痛苦,因爲每個worker都不知道其他worker中的內容,並且每個變量在未使用它們的worker中都是未定義的。我不知道正確的術語很抱歉,但你可以閱讀的文檔,以獲得更好的解釋:)

編輯:

爲了回答您的評論,這裏是一個簡單的例子。希望這是你的意思:)

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 

和衛生組織的輸出是:

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  
+0

謝謝!我理解這個例子,但是如果我想讓每個核心運行不同的工作,例如運行一個函數來計算一些值,但是用一個不同的變量作爲每個核心的輸入? – holistic 2014-09-25 15:51:12

+0

好吧,我明白了。請看我編輯的答案。希望這是你想要的! – 2014-09-25 16:37:51

+0

這是你想要的嗎? – 2014-09-27 13:39:50