getting parfor to work!
1 回表示 (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!