getting parfor to work!

조회 수: 1 (최근 30일)
tx213
tx213 2013년 12월 17일
답변: Walter Roberson 2013년 12월 18일
Hi guys,
I'm looking for some inspiration to get parfor working on my script here. I'm a bit stuck at the moment and can't seem to get it working properly.
Currently my script looks something like this:
[a b]=find(R>limit);
q=zeros(size(x,1),size(x,1), numel(a));
for n=1:numel(a)
q(a(n),b(n)) = field_ea( R(a(n),b(n)) );
end
Naively I thought I could just switch out for with "parfor" but I can't! Any comments will be really appreciated - thanks in advance!
this works just fine:
parfor n=1:numel(a)
q(n) = field_ea( R(a(n),b(n)) );
end
But that isn't what I want.
T

채택된 답변

Walter Roberson
Walter Roberson 2013년 12월 18일
"parfor" is not allowed in the first bit of code because "parfor" is not able to deduce that there will never be two different n, n1 and n2, such that [a(n1),b(n1)] == [a(n2),b(n2)] . Because if there were such a pair then two different workers could end up trying to write to that location in q() at the same time.
"parfor" is allowed in the second bit of code because "parfor" is able to deduce that all destination locations q(n) are unique and so no two workers will conflict in writing to a single location.
Note: if you were to switch your find() to only emit a single index, say "c",
c = find(...);
for n = 1 : numel(c)
q(c(n)) = ...
end
then you would still have difficulties because again parfor would not be able to deduce that no two c(*) were the same.
Depending on the work involved in field_ea and whether it is vectorizable, you should consider the loopless
q = zeros(size(R));
idx = R > limit;
q(idx) = field_ea( R(idx) );

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by