Unable to classify the variable in the body of the parfor-loop.
조회 수: 1 (최근 30일)
이전 댓글 표시
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!
댓글 수: 0
채택된 답변
Walter Roberson
2021년 2월 1일
편집: Walter Roberson
2021년 2월 1일
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.)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!