Unable to classify the variable in the body of the parfor-loop.
3 views (last 30 days)
Show older comments
Commented: Nguyen Van Hieu on 1 Feb 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);
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!
Walter Roberson on 1 Feb 2021
Edited: Walter Roberson on 1 Feb 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.)
More Answers (0)
Find more on Parallel for-Loops (parfor) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!