Issue of classification of variables in a parfor.

1 view (last 30 days)
I'd like to use parallel computing tool for massive calculations. I coded as below and tried to run it. and I got an error message like "The variable eval_val in a parfor cannot be classified". How do I solve this?
% code
clc, clear all
parpool
xis=0.001;
mu=0.01:0.01:0.2;
gamma=0.9:0.01:1.1;
xit=0.001:0.01:0.4;
eval_val=zeros(length(gamma),length(xit));
ng=length(gamma);
nxt=length(xit);
parfor mm=1:length(mu)
for gg=1:ng
for tt=1:nxt
M=[1+mu(mm), mu(mm);1,1];
C=[2*xis,0;0, 2*gamma(gg)*xit(tt)];
K=[1,0;0,gamma(gg)^2];
iM=inv(M);
A=[zeros(2),eye(2);-iM*K,-iM*C];
B=[zeros(2,1);1;0];
Q=lyap(A,4*xis*B*B');
eval_val(gg,tt)=Q(1,1);
end
end
[gamma_opt,xit_opt]=find(eval_val==min(min(eval_val)));
gamma_opt(mm)=gamma(gamma_opt);
xit_opt(mm)=xit(xit_opt);
end

Accepted Answer

Jeff Miller
Jeff Miller on 10 Aug 2018
I'm not sure I follow this, but I think there is a problem in these lines:
[gamma_opt,xit_opt]=find(eval_val==min(min(eval_val)));
gamma_opt(mm)=gamma(gamma_opt);
xit_opt(mm)=xit(xit_opt);
gamma_opt and xit_opt will be modified by all of the different processors in parallel, and that looks to MATLAB like the different processor outputs will be overwriting each other. Try using other variables as outputs of the find command, e.g.
[gamma_opt_idx,xit_opt_idx]=find(eval_val==min(min(eval_val)));
and change the next two lines accordingly.
Also, you should probably pre-allocate gamma_opt and xit_opt to hold length(mu) outputs.
  4 Comments
Yonghun Lee
Yonghun Lee on 13 Aug 2018
I'm surprised of your insight! It was just solved! I'd like to be familiar with the tool as you! Thanks a lot!
Jeff Miller
Jeff Miller on 13 Aug 2018
You are welcome. Please accept the answer if the problem is solved.

Sign in to comment.

More Answers (0)

Categories

Find more on Parallel Computing Fundamentals in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!