MATLAB: Using Parfor in Ga

gagenetic algorithmMATLABoptimizationparallel computingparfor

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!

Best Answer

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.