Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Why can parfor only write to one dimension?

조회 수: 3 (최근 30일)
matt kennedy
matt kennedy 2015년 6월 13일
마감: MATLAB Answer Bot 2021년 8월 20일
Say I have some code like:
index = 1:100;
results = cell(2,length(index));
parfor index
results{1,index} = analysisFunction1( data{index} );
results{2,index} = analysisFunction2( data{index} );
end
I get a "parfor cannot run due to the way variable 'results' is used" error message, although as far as I understand this shouldn't interfere in any way with how parfor works. So why is this restriction in place?
Note that if I comment out the second line everything runs as expected, and it isn't dependent on the data I'm writing only that I mention more than one dimension of results.
Changing it to
parfor index
results(1:2,index) = { Fun1(...) ; Fun2(...) };
end
Gets me a "The variable results in a parfor cannot be classified."

답변 (2개)

Ken Atwell
Ken Atwell 2015년 6월 13일
From parfor's perspective, "result" is being accessed randomly. Try this instead:
parfor i=index
results1{i} = analysisFunction1( data{i} );
results2{i} = analysisFunction2( data{i} );
end
results = vertcat(results1, results2);
  댓글 수: 1
matt kennedy
matt kennedy 2015년 6월 13일
Your solution is what I ended up implementing, but not ideal in higher or even an arbitrary number of dimensions.
What do you mean by being accessed randomly? Yes a random index is being passed to each worker, but within a single iteration the index is constant. I'm able to write to results(2,index) leaving the first row empty, and I'm able to write to / access the same dimension more than once with no problems.
If I'm able to access the same variable more than once per iteration, they're both valid indices parfor is able to write to, and they're both completely independent of each other, it just seems like parfor is arbitrarily restricting itself.

Walter Roberson
Walter Roberson 2015년 6월 13일
http://www.mathworks.com/help/distcomp/sliced-variables.html and see the third example set, "The following example on the left does not slice A because the indexing of A is not the same in all places."
What is allowed is
parfor index
t = cell(2,1);
t{1} = analysisFunction1( data{index} );
t{2} = analysisFunction2( data{index} );
results(:,index) = t;
end
or probably
[results{:,index}] = t{:};

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by