Azzera filtri
Azzera filtri

getting parfor to work!

2 visualizzazioni (ultimi 30 giorni)
tx213
tx213 il 17 Dic 2013
Risposto: Walter Roberson il 18 Dic 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

Risposta accettata

Walter Roberson
Walter Roberson il 18 Dic 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) );

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by