【问题标题】:composite date from parallel computing来自并行计算的复合日期
【发布时间】:2014-09-25 12:05:11
【问题描述】:

我是第一次使用并行计算 (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

【问题讨论】:

    标签: matlab parallel-processing spmd


    【解决方案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 块中会很痛苦,因为每个工作人员都不知道其他工作人员中的内容,并且每个变量在没有使用它们的工作人员中都是未定义的。抱歉,我不知道正确的术语,但您可以阅读文档以获得更好的解释 :)

    编辑:

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

    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      
    

    【讨论】:

    • 谢谢!我理解这个例子,但如果我希望每个核心运行不同的工作,例如运行一个函数来计算一些值,但每个核心都使用不同的变量作为输入?
    • 好的,我明白了。请参阅我编辑的答案。希望这是你想要的!
    • 这就是你想要的吗?
    • 哦,抱歉,没看到。是的,太完美了,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2016-11-12
    • 1970-01-01
    • 2018-04-06
    • 2016-10-20
    • 1970-01-01
    • 2012-03-04
    • 2019-08-28
    • 2019-01-16
    相关资源
    最近更新 更多