并行代码计算速度比Matlab中的sequantial代码慢

时间:2013-10-29 07:51:39

标签: parallel-processing matlab

我接下来的计算时间问题。序列计算的时间比并行代码快。 代码计算一些矩阵表达式。例如,当我使用矩阵维度3000 * 3000计算此代码时,sequantial计算的时间约为3.25秒,但我使用的实验室数超过1个,计算时间为5.3-5.7秒。 我有4个核心,ram 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);

0 个答案:

没有答案