Unable to classify the variable in the body of the parfor-loop.

1 vue (au cours des 30 derniers jours)
Nguyen Van Hieu
Nguyen Van Hieu le 1 Fév 2021
Commenté : Nguyen Van Hieu le 1 Fév 2021
I am trying to set up a parfor nested loop in MatLab R2020a as below
function [stiffness, mass] = assemble(stiffness, mass, k, m, index)
edof = length(index);
parfor i = 1:edof
ii = index(i);
for j = 1:edof
jj = index(j);
stiffness(ii, jj) = stiffness(ii, jj) + k(i, j);
mass(ii, jj) = mass(ii, jj) + m(i, j);
end
end
The code analyzer returns this message: "Valid indices for 'stiffness' are restricted in parfor loops".
and the command window said: "Unable to classify the variable 'stiffness' in the body of the parfor-loop".
I am really appreciate if somone could explain the problem to me. Thank you very much!

Réponse acceptée

Walter Roberson
Walter Roberson le 1 Fév 2021
Modifié(e) : Walter Roberson le 1 Fév 2021
That cannot be done with parfor. parfor cannot know that the index numbers are unique, so it must assume that there could be two different iterations both trying to assign into the same stiffness(index(i),:) vector.
What you can potentially do is select a subset of stiffness and mass ordered according to unique(index), work with that subset, and write it back into stiffness and mass; you would have to reorder by rows and reorder by columns because of your dual use of index() for subscripts.
However, your k and m are not to be re-ordered -- but they do need to have subsets extracted according to the number of unique index values that are present.
I should not say that your k and m are not to be re-ordered: rather they should be de-ordered like
rs = stiffness(unique_index, unique_index); %re-ordered
rm = mass(unique_index, unique_index); %re-ordered
dk(unique_index,unique_index) = k; %de-ordered
dm(unique_index,unique_index) = m; %de-ordered
Once that is done, the parfor loop simplifies (all of it!) to
rs = rs + dk;
rm = rm + dm;
After which you de-order rs back into stiffness and de-order rm back into mass. No parfor needed provided the index values are unique (and no parfor possible if index values are not unique.)
  1 commentaire
Nguyen Van Hieu
Nguyen Van Hieu le 1 Fév 2021
Thank you so much for helping me. I fixed my problem by the simply way.
stiffness(index, index) = stiffness(index, index) + k;
mass(index, index) = mass(index, index) + m;

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Parallel for-Loops (parfor) dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by