Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Parallel processing Matlab中并行代码的计算速度比串行代码慢_Parallel Processing_Matlab - Fatal编程技术网

Parallel processing Matlab中并行代码的计算速度比串行代码慢

Parallel processing Matlab中并行代码的计算速度比串行代码慢,parallel-processing,matlab,Parallel Processing,Matlab,下一个问题是计算时间。sequntial计算的时间比并行代码快。 代码计算一些矩阵表达式。例如,当我用矩阵维数3000*3000计算这段代码时,顺序计算的时间约为3.25秒,但我使用了超过1个实验室,计算时间为5.3-5.7秒。 我有4个内核,内存3 Gb sizeOfMatrix = 2000; MatrixA = rand (sizeOfMatrix, sizeOfMatrix); VectorB = rand (sizeOfMatrix,1); StringVectorC = rand

下一个问题是计算时间。sequntial计算的时间比并行代码快。 代码计算一些矩阵表达式。例如,当我用矩阵维数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);

您正在对每个工作进程执行几乎完整的代码,这应该如何提高性能?我看到您使用了许多矩阵操作,如
*
。我相信Matlab通常有这些的多线程实现。但是如果你已经在并行你的代码,那么我认为它默认回到单线程版本。你有可能会像那样表现不佳。但更可能的是,您只是失去了管理多个工作人员所需的开销。我是matlab和并行计算工具箱的初学者。你能告诉我应该如何为4个工作人员并行我的代码吗。@丹:你是对的@neruad:我目前看不到在不同的工作人员上并行你的代码的可能性。在花费大量时间之前:检查您的CPU负载(所有内核的总和)。由于mc实现,这应该已经很高了,因此使用并行计算无法获得任何性能。