I wrote a finite element toolbox in Matlab which turned to be rather slow for large meshes, so I decided to parallelize the element matrices assembly.
Hence, after initiating the workers pools I use cell arrays in order to build the global matrices, following the helpful advice in 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);
I have tried the previous serial code and this parallelized version with 2 workers with hardly no speedup (around 1.1).
I hope you can help me out to locate the problem.