首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >利用matlab中的spmd (并行)对部分矢量应用函数

利用matlab中的spmd (并行)对部分矢量应用函数
EN

Stack Overflow用户
提问于 2015-10-02 23:22:25
回答 1查看 83关注 0票数 0

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

任务

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

因此,对于核心1: f1 =f(属于第1部分的v)

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

诸若此类。

然后我要收集结果,在此之后,我得到:F=“一个向量,包含f1的所有元素,以及f2的所有元素,等等。”在主核心(如果您愿意的话,MatLab可能会将其称为“客户端”,但我不确定)。

尝试

代码语言:javascript
复制
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并行编程。我将如何完成我的任务?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-05 07:56:23

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

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

https://stackoverflow.com/questions/32917205

复制
相关文章

相似问题

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