首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >并行计算合成数据

并行计算合成数据
EN

Stack Overflow用户
提问于 2014-09-25 12:05:11
回答 1查看 172关注 0票数 0

我第一次使用并行计算(spmd)。

启动池并进行一些并行计算之后,我的工作区中只有复合变量,无法打开它们。或者当我双击打开它们时,它们是空的。我如何使用这些数据?

这是我的代码:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-25 13:25:33

如果您想为每个员工生成4个大小数组(300,1),最好像下面这样做。请注意,我的计算机/Matlab池上有4个核。

代码语言:javascript
复制
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块中是很痛苦的,因为每个工作人员都不知道其他工作人员中有什么,并且每个变量在不使用它们的工作人员中都没有定义。对不起,我不知道正确的术语,但您可以阅读文档以获得更好的解释:)

编辑:

为了回答你的评论,下面是一个简单的例子。希望这就是你的意思:)

代码语言:javascript
复制
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 

世界卫生组织的产出是:

代码语言:javascript
复制
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      
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26038216

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档