Why can this loop not be parallelized in Matlab Coder?
显示 更早的评论
I am using MEX code to speed up a part of my computations and I would expect parallel computations to help at that. Automatic Parallelization is enabled, but even though the code is quite simple Coder refuses to parallelize it. In the coder report I see "Array or variable access pattern inside the loop is not suitable for parallel execution.". Another issue in the report asks me to enable "OptimizeReductions" to parallelize the line where the inverse is computed.
In the code below, you can see that, essentially, the function performes some pagewise operations on a large 3D matrix M. This seems like an obvious case for sliced variables to me.
I do not understand why this cannot be parallelized. What am I missing?
function [dets, Minv] = getDets(tri, xVrtx)
% Compute determinants of all simplices in the triangulation.
% Return the inverse of the characteristic matrix.
% tri ... nSmplx x nDim+1 matrix of vertex indices
% xVrtx ... nVrtx x nDim matrix of vertex coordinates
% dets ... nSmplx x 1 vector of determinants
% Minv ... nDim+1 x nDim+1 x nSmplx inverse of characteristic matrix
tri = int32(tri);
nDim = size(xVrtx, 2);
nSmplx = size(tri,1);
% characteristic matrices of all simplices
% nDim+1 x nDim+1 x nSmplx
M = [reshape(xVrtx(tri',:)', [nDim nDim+1 nSmplx]); ones([1 nDim+1 nSmplx])];
% allocate memory
dets = zeros(nSmplx,1);
Minv = zeros(nDim+1, nDim+1, nSmplx);
for j = 1:nSmplx
M_ = M(:,:,j);
dets(j) = det(M_);
if det(j) > 0
Minv(:,:,j) = inv(M_);
end
end
end % function
9 个评论
Walter Roberson
2024-11-5
Experiment with
Zn = zeros(nDim+1, nDim+1);
for j = 1:nSmplx
M_ = M(:,:,j);
detj = det(M_);
if detj > 0
Minv_ = inv(M_);
else
Minv_ = Zn;
end
dets(j) = detj;
Minv(:,:,j) = Minv_;
end
Bruno Luong
2024-11-6
编辑:Bruno Luong
2024-11-6
Note that you could try this
M = rand(3,3,10)
pageinv(M)
% or pagemldivide(M, eye(3))
instead of mex the for loop. The pagexxx functions are internally multi thread coded, and it would be fast.
It looks like you want to compute barycentric coorinates of a mesh.
Felix
2024-11-6
Bruno Luong
2024-11-8
编辑:Bruno Luong
2024-11-8
Just for an experiment can you try this for loop
Minv = zeros(nDim+1, nDim+1, nSmplx);
for j = 1:size(Minv,3)
Minv(:,:,j) = M(:,:,j) \ eye(nDim+1);
end
Felix
2024-11-8
Bruno Luong
2024-11-9
编辑:Bruno Luong
2024-11-9
May be I'm wrong but it seems coder can only parallelize loop with simple arithmetic operations using omp clause. Calling function such as det, inv or mldivide is not supported at the pesence.
Note that your for loop can be transformed to parfor
Felix
2024-11-15
Divyam
2024-12-9
Hi @Felix Birkelbach, it wont be possible to parallelize your code here since the nested for loop contains iterations that are dependent on other iterations, i.e.
is dependent on
. Restructuring your code to remove this dependency should fix your problems with parallelization.
Felix
2024-12-15
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Performance and Memory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

