getting parfor to work!

1 view (last 30 days)
tx213
tx213 on 17 Dec 2013
Answered: Walter Roberson on 18 Dec 2013
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

Accepted Answer

Walter Roberson
Walter Roberson on 18 Dec 2013
"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) );

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!