Info

This question is closed. Reopen it to edit or answer.

The variable newMap in a parfor cannot be classified.

1 view (last 30 days)
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

Answers (1)

Edric Ellis
Edric Ellis on 18 Jun 2020
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);

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!