MATLAB Answers

0

Issue of classification of variables in a parfor.

Asked by Yonghun Lee on 10 Aug 2018 at 4:03
Latest activity Commented on by Jeff Miller on 13 Aug 2018 at 3:34

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

  0 Comments

Sign in to comment.

1 Answer

Answer by Jeff Miller on 10 Aug 2018 at 8:25
 Accepted Answer

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

Show 1 older comment

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.

I'm surprised of your insight! It was just solved! I'd like to be familiar with the tool as you! Thanks a lot!

You are welcome. Please accept the answer if the problem is solved.

Sign in to comment.