我第一次使用并行计算(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发布于 2014-09-25 13:25:33
如果您想为每个员工生成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块中是很痛苦的,因为每个工作人员都不知道其他工作人员中有什么,并且每个变量在不使用它们的工作人员中都没有定义。对不起,我不知道正确的术语,但您可以阅读文档以获得更好的解释:)
编辑:
为了回答你的评论,下面是一个简单的例子。希望这就是你的意思:)
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 https://stackoverflow.com/questions/26038216
复制相似问题