## Issue of classification of variables in a parfor.

### Yonghun Lee (view profile)

on 10 Aug 2018
Latest activity Commented on by Jeff Miller

on 13 Aug 2018

### Jeff Miller (view profile)

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

### Jeff Miller (view profile)

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.

Show 1 older comment
Jeff Miller

### Jeff Miller (view profile)

on 13 Aug 2018
Move this
eval_val=zeros(length(gamma),length(xit));
inside the parfor loop so that each processor gets its own copy of this array. Then MATLAB will see that this is just a temporary working variable, not a variable that is shared by all processors.
Yonghun Lee

### Yonghun Lee (view profile)

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 (view profile)

on 13 Aug 2018
You are welcome. Please accept the answer if the problem is solved.