2014-09-25 67 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