Using Parfor in Ga

10 ビュー (過去 30 日間)
Chi So
Chi So 2020 年 12 月 2 日
コメント済み: Raymond Norris 2020 年 12 月 5 日
I'm currently working on an optimization problem that uses the build in function ga. I take advantage of the parallel computing option with 'UseParallel',true so that it can speed the calculation. However, I've also implemented parfor in my objective function which processes some images. I'm wondering if it can causes any issue because everything seems to be working fine at this moment. However, I do realize the time required for each generation takes longer when I increase the 'MaxGenerations' from 10 to 400, which doesn't make sense to me. I'm afraid the parfor has something to do with it. Can anyone explain how Paralel computing works in ga and let me know if this isn't the right way to use parfor. Many thanks!

採用された回答

Raymond Norris
Raymond Norris 2020 年 12 月 2 日
Nested parfor loops will be treated as a parfor calling a for-loop. For example:
parfor idx = 1:N
myfcn(idx)
end
function myfcn(idx)
parfor jdx = 1:idx
end
end
The jdx parfor will be treated as a for-loop. The one exception is if the neste parfor is embedded within the outer.
parfor idx = 1:N
parfor jdx = 1:N
end
end
In this case, jdx will cause an error.
  3 件のコメント
Walter Roberson
Walter Roberson 2020 年 12 月 3 日
If I recall correctly, there is a small difference, which is that my understanding is that the parfor jidx would be treated as a for loop that ran the iterations "backwards", idx:-1:1
parfor starts with the last iteration because it assumes that the output variables have not necessarily be initialized to full size, and that by running backwards the first assignment will initialize to full size.
Raymond Norris
Raymond Norris 2020 年 12 月 5 日
I've gone back and forth about the need to preallocate slided outputs. Last I looked, it didn't appear to be impactfuly, but worth looking again. That said, if someone's preallocated a matrix, why comment it out just because your in a parfor loop.
Getting the your question about order, a parfor is "not executed in a guaranteed order", regardless of whether is nested or not. The nested loop runs in reverse, but ought not be counted on down the road.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by