Is it possible to use use `parfor` for parallel computing in Matlab in these codes?

2 次查看(过去 30 天)
I am using parfor for parallel computing in Matlab. I am not familiar with this command. If that is possible, please look at my code below and tell me if I can write it with parfor. These errors and warnings are appear in Matlab Editor:
The parfor loop cannot be run due to the way variable 'Dat' is used.
The entire array or structure 'Bound' is broadcast variable. This might result in unnecessary communication overhead.
parfor pj = 1:size(normXpj,1)
Dat.normXpj = normXpj(pj,:);
if size(Dat.InitialGuess)==0
X = (Bound(:,1)+(Bound(:,2)-Bound(:,1)).*rand(Nvar,1))';
else
X = Dat.InitialGuess;
end
[Xsqp, ~, FLAG,Options] = mopOPT(X,Dat);
FEVALS = Options.funcCount;
FES = FES+FEVALS;
PSet(pj,:) = Xsqp;
PFront(pj,:) = mop(Xsqp,Dat,0);
%disp(['ExitFlag: ' num2str(FLAG)]);
if FLAG==-2
disp('.......... Algo paso...');
else
F = PFront(pj,:);
if Nobj==2
plot(F(1,1),F(1,2),'*r'); grid on; hold on;
elseif Nobj==3
end
end
end

采纳的回答

Edric Ellis
Edric Ellis 2015-3-5
The problem here is that it we can see that you're not using `Dat` in a way that is order-dependent, but the static analysis machinery of `parfor` cannot deduce that because of the way you're assigning into it. I think you can work around this by instead creating a whole new `Dat` for each iteration of the loop, like so:
Dat = struct('normXpj', rand(10,1), 'InitialGuess', 3);
normXpj = rand(10);
parfor idx = 1:10
tmpDat = struct('normXpj', normXpj(:,idx), 'InitialGuess', Dat.InitialGuess);
% use 'tmpDat'
disp(tmpDat);
end
  1 个评论
Jamais avenir
Jamais avenir 2015-3-5
Thank you, it worked. The time is reduced and results are a little deviated(10^-4) except once case (20). Is there anyway to reduce this deviation? Also, is there a way to open all cores before running the main code? I am using `delete (gcp); parpool('local',2)`, but if I run it on other computers I must change it to 4 maybe. Also, after opening cores I use `pause(5)` to make the system stabilized, does it really help to run the main code faster/? Thanks.

请先登录,再进行评论。

更多回答(1 个)

Matt J
Matt J 2015-3-4
编辑:Matt J 2015-3-4
You should familiarize yourself with the material on classification of variables in parfor loops,
This line, in particular,
Dat.normXpj = normXpj(pj,:);
is not allowed. Since Dat is a broadcast variable, you cannot assign to it. It also doesn't make sense to try to do so. Assigning different parallel pj-dependent values to the same value would lead to an ill-defined value when the parfor loop ends.

类别

Help CenterFile Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by