1

我在计算时间方面遇到了下一个问题。顺序计算的时间比并行代码快。该代码计算一些矩阵表达式。例如,当我使用矩阵维度 3000*3000 计算此代码时,序列计算的时间约为 3.25 秒,但我使用超过 1 个实验室的计算时间为 5.3-5.7 秒。我有 4 个内核,内存 3 Gb。

sizeOfMatrix = 2000;

MatrixA = rand (sizeOfMatrix, sizeOfMatrix);
VectorB = rand (sizeOfMatrix,1);
StringVectorC = rand (1, sizeOfMatrix);

E = eye (sizeOfMatrix);
An = MatrixA(:, sizeOfMatrix);
An_ = MatrixA(1:sizeOfMatrix-1, sizeOfMatrix);

C_ = StringVectorC(1:sizeOfMatrix-1);
tic

P2 = VectorB * StringVectorC * VectorB * StringVectorC;
P1 = E - P2;

ax1 = P1 * MatrixA;
ax1 = ax1(1:sizeOfMatrix-1,1:sizeOfMatrix-1);

ax2 = An_*C_;
Ax = ax1 - ax2;
Hx = (P1 * MatrixA * VectorB) / (StringVectorC  * VectorB);
Hx(sizeOfMatrix,:) = [];
Asigma = (StringVectorC * MatrixA * VectorB) / (StringVectorC * VectorB);
hs1 = StringVectorC * MatrixA;
hs1(:,sizeOfMatrix) = [];
hs2= StringVectorC * An * C_;
Bsigma = StringVectorC * VectorB;
toc
%disp('Ax');disp(Ax);
%disp('Hx');disp(Hx)  ;
%disp('Hsigma');disp(Hsigma);
disp('Asigma');disp(Asigma);
disp('Bsigma');disp(Bsigma);

并行代码

tic
spmd
    sizeOfMatrix = 2000;
    matrixA = rand (sizeOfMatrix, sizeOfMatrix);
    vectorB = rand (sizeOfMatrix, 1);
    stringVectorC = rand (1, sizeOfMatrix);
    MatrixA = codistributed (matrixA);
    VectorB = codistributed (vectorB);
    StringVectorC = codistributed (stringVectorC);
    E = codistributed.eye (sizeOfMatrix);

    An = codistributed( matrixA(:, sizeOfMatrix));
    An_ = codistributed (matrixA(1:sizeOfMatrix-1, sizeOfMatrix));
    C_ = codistributed (StringVectorC(1:sizeOfMatrix-1));
    P2 = VectorB * StringVectorC * VectorB * StringVectorC;
    P1 = E - P2;
    ax1 = MatrixA*P1 ;
    AX1 = gather (ax1);
    Ax1 = codistributed (AX1(1:sizeOfMatrix-1, 1:sizeOfMatrix-1));

    Ax2 = An_ * C_;

    Ax = Ax1 - Ax2;

    Hx = (P1 * MatrixA * VectorB) / (StringVectorC  * VectorB);

    Asigma = (StringVectorC * MatrixA * VectorB) / (StringVectorC * VectorB);

    hs1 = StringVectorC * MatrixA;
    HS1 = gather (hs1);

    Hs1 = codistributed( HS1(1:sizeOfMatrix-1));

    hs2 = StringVectorC * An * C_;

    Hsigma = Hs1 - hs2 ;

    Bsigma = StringVectorC * VectorB;
end
toc
%AX = gather (Ax);
%disp('Ax');disp(AX);
%HX = gather (Hx);
%HX(sizeOfMatrix,:) = [];
%disp('HX');disp(HX);
%Hs = gather (Hsigma);
%disp('Hsigma');disp(Hs);
disp('Asigma');disp(Asigma);
disp('Bsigma');disp(Bsigma);
4

0 回答 0