Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
The variable newMap in a parfor cannot be classified.
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
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);
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!