我在Matlab中编写了一个有限元工具箱,对于大型网格来说,它变得相当慢,因此我决定并行化元素矩阵装配。
因此,在启动工作池之后,我遵循https://es.mathworks.com/matlabcentral/answers/203734-most-efficient-way-to-add-multiple-sparse-matrices-in-a-loop-in-matlab
中的有用建议,使用单元阵列来构建全局矩阵。% Ne is the number of elements in the mesh
% Mij: cell array to store mass matrix components Mij{k}
% Kij: cell array to store stiffness matrix components Kij{k}
% Fi: cell array to store RHS vector components Fi{k}
Mij = cell(Ne, 1);
Kij = cell(Ne, 1);
Fi = cell(Ne, 1);
% stcData is a large structure with the mesh data
% Temp is the temperature field (vector) at time t
parfor k = 1:Ne
% Mij{k} = [I, J, M] size DOF^2 x 3
% Kij{k} = [I, J, K] size DOF^2 x 3
% Fi{k} = [I, F] size DOF x 2
[Mij{k}, Kij{k}, Fi{k}] = ...
solv_AssemblElementMatrix(k, time, Temp, stcData);
end
Mmat = cell2mat(Mij);
Kmat = cell2mat(Kij);
Fmat = cell2mat(Fi);
% Global matrices assembly
M = sparse(Mmat(:, 1), Mmat(:, 2), Mmat(:, 3), Nx, Nx);
K = sparse(Kmat(:, 1), Kmat(:, 2), Kmat(:, 4), Nx, Nx);
F = sparse(Fmat(:, 1), ones(size(Fmat, 1), 1), Fmat(:, 2), Nx, 1);
我已经尝试了以前的序列号和具有2个工作程序的并行版本,几乎没有加速(大约1.1)。
希望您能帮助我找出问题所在。