【问题标题】:use spmd in matlab (parallel) to apply a function to parts of vector在matlab(并行)中使用spmd将函数应用于向量的一部分
【发布时间】:2015-10-02 23:22:25
【问题描述】:

我正在使用并行计算工具箱在 MatLab 中工作。

任务

我有一个向量 v。我有 4 个核心。 我想在每个核心上拆分向量(因此每个核心处理向量的 1/4,假设 length(v) 可被 4 整除)并在每个部分上应用函数 f()。

所以对于核心 1: f1 = f(v 属于第 1 部分)

对于核心 2: f2 = f(v 属于第 2 部分)

等等。

然后我想收集结果,以便在此之后: f = "一个包含 f1 的所有元素和 f2 的所有元素等的向量" 在主核心上(如果您愿意,可以使用 root,也许 MatLab 将其称为“客户端”,但我不确定)。

尝试

spmd

    v_dist    = codistributed( v ); %split v onto cores
    lpv       = getLocalPart( v_dist ); %this core's part ("my part")

    f1        = f( lpv ); %apply f to my part of v

    %I want to piece back together the outputs?
    f_tmp     = codistributed( zeros(length(f1) * 4, 1) );

    %get my part of the container where I want to put the output
    f_tmp_lp  = getLocalPart( f_tmp );
    %now actually put my part of the output here:
    f_tmp_lp  = f1;

    %and then finally piece back together my part into 
    f_tmp     = codistributed.build( f_tmp_lp, getCodistributor( f_tmp ) );

end

%we should gather the output on the client?
f = gather( f_tmp );

还有?

这没有按预期工作。我确实得到了正确的 f 大小,但不知何故,似乎发生的是“lpv”只是赋予每个核心的同一块。但我不确定这是否是问题所在。

帮助?

我没有做过很多 MatLab 并行编程。我将如何完成我的任务?

【问题讨论】:

    标签: matlab parallel-processing spmd


    【解决方案1】:

    我认为您的代码非常接近,但我认为您不需要f_tmp。这是一个例子:

    v = 1:10;
    spmd
        v_dist = codistributed(v);
        lpv = getLocalPart(v_dist);
        f1 = sqrt(lpv);
        v2 = codistributed.build(f1, getCodistributor(v_dist));
    end
    assert(isequal(gather(v2), sqrt(v)));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-25
      • 1970-01-01
      • 1970-01-01
      • 2013-02-23
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多