parfor unable to classify, why?

13 ビュー (過去 30 日間)
Bruno Luong
Bruno Luong 2023 年 1 月 4 日
回答済み: Edric Ellis 2025 年 1 月 30 日 11:59
Can someone explain why the ind2sub first statement triggers the error, but the second works fine?
m = 2;
n = 3;
mycell = cell(m,n);
parfor k = 1:numel(mycell)
[i,j] = ind2sub(size(mycell), k); % MATLAB complains cannot classify mycell because of this
%[i,j] = ind2sub([m n], k); % this iis however accepted
mycell{k} = 1;
end
Error: Unable to classify the variable 'mycell' in the body of the parfor-loop. For more information, see Parallel for Loops in MATLAB, "Solve Variable Classification Issues in parfor-Loops".

採用された回答

Matt J
Matt J 2023 年 1 月 4 日
編集済み: Matt J 2023 年 1 月 4 日
I suspect it is because mycell is intended to be a sliced variable, but the code violates the fixed indexing rule,
Fixed Index Listing. Within the first-level indexing of a sliced variable, the list of indices is the same for all occurrences of a given variable.
since in one place you index mycell with {k} and elsewhere you do not index it at all. Notice that this also doesn't work:
m = 2;
n = 3;
mycell = cell(m,n);
parfor k = 1:numel(mycell)
mycell;
mycell{k} = 1;
end
I also vaguely wonder if it makes sense for a parpool worker to try to determine the size() of a sliced variable when it only receives a piece of it.
  5 件のコメント
Matt J
Matt J 2023 年 1 月 5 日
編集済み: Matt J 2023 年 1 月 5 日
@Edric Ellis If mycell were sliced, is it even possible for a parpool worker to determine its original size? Putting it another way, does a sliced variable somehow carry the metadata of the original, unsliced variable?
Edric Ellis
Edric Ellis 2023 年 1 月 5 日
@Matt J no, the sliced portion on the worker doesn't directly know the metadata of the original array (but the code running on the worker does know enough to make sure it writes the correct piece of the sliced portion that it is working with) - this is just one reason why it's not valid to attempt to access the "whole" variable in any way in the body of the loop.

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

その他の回答 (1 件)

Edric Ellis
Edric Ellis 2025 年 1 月 30 日 11:59
By the way, just wanted to mention that we attempted to improve the message for this particular error case. Hope it makes sense.
m = 2;
n = 3;
mycell = cell(m,n);
parfor k = 1:numel(mycell)
[i,j] = ind2sub(size(mycell), k); % MATLAB complains cannot classify mycell because of this
%[i,j] = ind2sub([m n], k); % this iis however accepted
mycell{k} = 1;
end
Error: The parfor-loop variable 'mycell' is accessed with an invalid combination of sliced indexing expressions and non-indexed reads. It is not valid to access the whole value of a sliced output variable. For more information, see Parallel for Loops in MATLAB, "Solve Variable Classification Issues in parfor-Loops".

カテゴリ

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

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by