如何在spmd块中的worker之间分配向量

问题描述 投票:0回答:1

我在for循环中有一个spmd块。我需要一个名为Aa的2 * 9向量分布在三个或更多工人之间,进行一些计算,结果产生的矩阵和向量很少。然后将每个工人和所有工人的矩阵连接在一起。将得到的矩阵和向量转换为double,并启动新的spmd块。运行代码时显示错误“Subscripted assignment dimension mismatch”。我可以增加工人数量吗?以下是代码的一部分:

先感谢您

   Aa=[0 0; 0 1; 0 2; 1 0; 1 1; 1 2; 2 0; 2 1; 2 2 ];

   for n=3:TGponts1

 % &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
 %        In this part, some calculation is done on Good_path_1 and R2_1
 %        and a new Good_path1_1 and R_1 and s1_1 are defined

 % &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

q_1=0; q_2=0;
nn1=n-1; nn2=2*n-2;

spmd (3)
    for ss=1:3  %   To assign three pairs of Aa to any worker
        if (ss == 1)
            a=Aa(labindex,1);
            b=Aa(labindex,2);
        elseif (ss == 2)
            a=Aa(labindex+3,1);
            b=Aa(labindex+3,2);
        else
            a=Aa(labindex+6,1);
            b=Aa(labindex+6,2);
        end
        Sum1=single(0);

        %             Here, some calculation is done.

        if p>thr_Wght
            q_1=q_1+1;

            if n<=n_c1

                Good_path1_1(q_1,1:2*n)=
             [a,Good_path_1(k_1,1:nn1),b,Good_path_1(k_1,n:nn2)];
            else
                Good_path1_1(q_1,1:L)=
             [a,Good_path_1(k_1,1:n_c),b,Good_path_1(k_1,n_c1:n_c2)];
            end

            R_1(q_1)=R3;

            Sum1=Sum1+R3;
        end

        %             Each worker performs these commands three times.
        Good_path1_1(q_1+1:s1_1,:)=[];
        R_1(1,q_1+1:s1_1)=[];
        %             Is it true to write like this to avoid that the third 
        %             (second) Good_path1_1 and R_1 are not replaced with 
                       the second (first) one?
        Good_path1_1ew(:,:,ss)=Good_path1_1;
        R_1ew(:,ss)= R_1;
        % &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    end
end

% Here all the Good_path1_1 of each worker and all workers are concatenated
Good_path1_1 = gcat(Good_path1_1ew, 1);
R_1=gcat(R_1ew, 1);
% &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

Good_path1_1=str2double(Good_path1_1);
R_1=str2double(R_1);

% Here Good_path1_1 and R_1 are substituted into Good_path_1 and R2_1
Good_path_1=Good_path1_1;
R2_1=R_1;    
end
matlab spmd
1个回答
0
投票

以下代码适用于此目的。

    Aa=[0 0; 0 1; 0 2; 1 0; 1 1; 1 2; 2 0; 2 1; 2 2 ]; 
    spmd (3)
        for ss=1:3  %   To assign three pairs of Aa to any worker

            if (labindex==1)
                a=Aa(ss,1);
                b=Aa(ss,2);
            elseif (labindex==2)
                a=Aa(3+ss,1);
                b=Aa(3+ss,2);
            else
                a=Aa(6+ss,1);
                b=Aa(6+ss,2);
            end
           % the remaining commands with some modifications

    end
© www.soinside.com 2019 - 2024. All rights reserved.