我正在使用并行计算工具箱在MatLab中工作。
任务
我有一个向量v,我有4个核。我希望在每个核心上拆分向量(因此每个核心处理向量的1/4,假设长度(V)可被4整除),并在每个部分上应用函数f()。
因此,对于核心1: f1 =f(属于第1部分的v)
对于核心2: f2 = f(v,属于第2部分)
诸若此类。
然后我要收集结果,在此之后,我得到:F=“一个向量,包含f1的所有元素,以及f2的所有元素,等等。”在主核心(如果您愿意的话,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并行编程。我将如何完成我的任务?
发布于 2015-10-05 07:56:23
我认为您的代码非常接近,但我认为您不需要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)));https://stackoverflow.com/questions/32917205
复制相似问题