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

1 回表示 (過去 30 日間)
Nguyen Van Hieu
Nguyen Van Hieu 2021 年 2 月 1 日
コメント済み: Nguyen Van Hieu 2021 年 2 月 1 日
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!

採用された回答

Walter Roberson
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.)
  1 件のコメント
Nguyen Van Hieu
Nguyen Van Hieu 2021 年 2 月 1 日
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;

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeParallel for-Loops (parfor) についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by