YALMIP is not working with parfor loop

조회 수: 11 (최근 30일)
Adriana Kiszka
Adriana Kiszka 2021년 3월 3일
댓글: Adriana Kiszka 2021년 3월 9일
Hello,
I have the problem with decision variables and solving multiple problems using parfor loop.
I define some decision variables before parfor loop and when I run parfor loop I got the error: "Matrix dimensions must agree", because my decision variables are now empty vectors/matrices.
Here I attach a piece of my code for which I already get the error.
rep = 100;
n=2;
m=2;
T=4;
P_min=[0.0800; 0.0800];
P_max=[0.2; 0.2];
Pd = [1.0800 1.0800 1.0800 1.0800;
0.9700 0.9700 0.9700 0.9700];
Qd = [0.2200 0.2200 0.2200 0.2200;
0.2000 0.2000 0.2000 0.2000];
lambda_l = sdpvar(m,1);
lambda_u = sdpvar(m,1);
lambda = sdpvar(n,1);
h_bas = 0;
h_bas = h_bas + sum(lambda_l.*P_min.*(~isinf(P_min))-lambda_u.*P_max.*(~isinf(P_max)));
cons_bas = [];
cons_bas = [cons_bas, lambda_l>=0,lambda_u>=0];
for t=1:T
parfor k=1:rep
h = h_bas + sum(lambda.*Pd(:,t));
cons = [cons_bas];
options = sdpsettings('solver','mosek','verbose',0);
optimize(cons,-h,options);
end
end
Could you please help me to fix this problem?
Best,
Adriana
  댓글 수: 1
Raymond Norris
Raymond Norris 2021년 3월 3일
I would suggest posting some sample code.

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2021년 3월 3일
YALMIP permits you to define variables using calls such as
P = sdvpar(1)
and it expects that variable to be distinguished from
Q = sdvpar(1)
That can only happen if YALMIP is retaining state about which variables have been created and what their properties are.
In MATLAB, the methods of recording state like this are:
  • in graphics objects (not likely at all to be the case here)
  • in the base workspace
  • in persistent variables
  • in global variables
  • in handle objects
  • in class variables
However, graphics objects, base workspace, persistent variables, and global variables are not copied to parallel workers.
Objects (that are plainly referenced) are copied to parallel workers, but through a method equivalent of "save" and "load" --- a process that copies only serializable data, and loses dynamic properties, and effectively disconnects the copied clones from the original objects.
I suspect that the code was not designed with parallel use in mind.
  댓글 수: 7
Johan Löfberg
Johan Löfberg 2021년 3월 9일
Crashed? Weird, runs without issues on both 2018 and 2020 for me (windows)
Adriana Kiszka
Adriana Kiszka 2021년 3월 9일
As I wrote, I got the above error that "Matrix dimensions must agree" and it was using the MATLAB version 2020b. Unfortunately this problem appeared after some runs, when I increased the number of repetitions. That's why it is difficult for me to find what's wrong.
Have you run exactly the same code I attached or maybe you modified it to one-dimensional version?

댓글을 달려면 로그인하십시오.


Johan Löfberg
Johan Löfberg 2021년 3월 9일
편집: Johan Löfberg 2021년 3월 9일
EDIT: Works now in newer MATLAB version!
YALMIP does not work with parfor, and it cannot be fixed or circumvented.
For YALMIP specific questions, you are much better off posting quetions on the YALMIP google groups forum.
  댓글 수: 3
Johan Löfberg
Johan Löfberg 2021년 3월 9일
편집: Johan Löfberg 2021년 3월 9일
BTW, your code is going to run much faster using an optimizer construct, to the extent that the for parfor might be redundant, as most of the time in your version is spent in general overhead. I think this version survuves parfor in 2016 too, as an optimizer object is disconnected from YALMIPs global database and can be saved/loaded
c = sdpvar(m,1);
Solver = optimizer(cons_bas,h_bas + c'*lambda,options,c,lambda);
for t=1:T
parfor k=1:rep
Solver(Pd(:,t))
end
end
Adriana Kiszka
Adriana Kiszka 2021년 3월 9일
Thank you Johan for your suggestion, I will try to use optimizer.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Parallel Computing Fundamentals에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by