Info

この質問は閉じられています。 編集または回答するには再度開いてください。

The variable newMap in a parfor cannot be classified.

1 回表示 (過去 30 日間)
mohit vadera
mohit vadera 2020 年 6 月 16 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
I am trying to store container map with fos value (returns from fun_foo function using x as key and fos as value) and if x already called before map will retuen correct value and I can reduce call of fun_foo function.
Goal is to make this program as fast as possible.
newMap= containers.Map();
parfor i=1:100
tf = isKey(newMap, mat2str(x(i,:)));
if tf == 1
fos(i,1) = newMap(mat2str(x(i,:)));
gg = gg+1;
else
fos(i,1) = fun_foo(x(i,:));
newMap(mat2str(x(i,:))) = fos(i,1);
end
end

回答 (1 件)

Edric Ellis
Edric Ellis 2020 年 6 月 18 日
This loop cannot run because the variable newMap is being read from and written to in arbitrary locations as the loop progresses. This is order-dependent behaviour, and parfor disallows that.
So far as I can tell, you're using newMap as a cache to avoid recomputing fun_foo for rows of x that you've already seen. It might therefore work to ensure that you run your parfor loop over only the unique rows of x. Something like this:
% Make some random data - this will probably
% have duplicated rows.
x = randi(5, 100, 3);
% Dummy function for testing
fun_foo = @sum;
% Extract the unique rows, keep the "mapping" vector
% ic for later
[xu, ~, ic] = unique(x, 'rows');
parfor i=1:size(xu, 1)
% We know that each row is unique, and hasn't
% been computed before, so just do:
fos_u(i,1) = fun_foo(xu(i,:));
end
% To get the original "fos", we need to undo the ordering
% from the call to 'unique'
fos = fos_u(ic);

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by