PIPS-NLP
sLinsysLeaf.h
Go to the documentation of this file.
1 /* PIPS
2  Authors: Cosmin Petra
3  See license and copyright information in the documentation */
4 
5 /* 2015. Modified by Nai-Yuan Chiang for NLP*/
6 
7 #ifndef STOCHLEAFLINSYS
8 #define STOCHLEAFLINSYS
9 
10 #include "sLinsys.h"
11 #include "sTree.h"
12 #include "sFactory.h"
13 #include "sData.h"
14 #include "SparseSymMatrix.h"
15 #include "SparseGenMatrix.h"
16 
17 
18 //#include "MtxSchurDecompSolver.h"
19 //#include "ReducedSpaceSolver.h"
20 
21 #ifdef WITH_UMFPACK
23 #endif
24 
25 //#include "PartitionAugSolver.h"
26 
27 
28 extern int gOuterSolve;
29 extern int separateHandDiag;
30 
31 extern int gBuildSchurComp;
32 extern int gUseReducedSpace;
33 extern int gNP_Alg;
34 
38 class sLinsysLeaf : public sLinsys
39 {
40  public:
41  //sLinsysLeaf(NlpGenStoch * factory_, sData * prob_);
42  template<class LINSOLVER>
44  sData* prob_,
45  OoqpVector* dd_, OoqpVector* dq_, OoqpVector* nomegaInv_,
46  OoqpVector* rhs_, OoqpVector* additiveDiag_, LINSOLVER *linsolver=NULL);
47 
48  virtual ~sLinsysLeaf();
49 
50  virtual int factor2( sData *prob, Variables *vars);
51  virtual void Lsolve ( sData *prob, OoqpVector& x );
52  virtual void Dsolve ( sData *prob, OoqpVector& x );
53  virtual void Ltsolve( sData *prob, OoqpVector& x );
54 
55  //virtual void Lsolve2 ( OoqpVector& x );
56  //virtual void Dsolve2 ( OoqpVector& x );
57  virtual void Ltsolve2( sData *prob, StochVector& x, SimpleVector& xp);
58 
59 
60  //virtual void solveCompressed( OoqpVector& rhs );
61  virtual void putXDiagonal( OoqpVector& xdiag_ );
62  virtual void putSDiagonal( OoqpVector& sdiag_ );
63  virtual void putYDualDiagonal( OoqpVector& ydiag_ );
64  virtual void putZDiagonal( OoqpVector& zdiag );
65 
66  virtual void setAdditiveDiagonal();
67 
68 
69  //void Ltsolve_internal( sData *prob, StochVector& x, SimpleVector& xp);
70  void sync();
71  virtual void deleteChildren();
72  virtual void UpdateMatrices( Data * prob_in,int const updateLevel=2);
73 
74 
75 
77 
78  std::map<int,int> LocQMap;
79  std::map<int,int> LocBMap;
80  std::map<int,int> LocDMap;
81 
82 
84 
85  std::map<int,int> xDiagIdxMap;
86  std::map<int,int> sDiagIdxMap;
87  std::map<int,int> yDiagIdxMap;
88  std::map<int,int> zDiagIdxMap;
89 
90  virtual void setXDiagonal( OoqpVector& xdiag );
91  virtual void setSDiagonal( OoqpVector& sdiag );
92  virtual void setYDiagonal( OoqpVector& ydiag );
93  virtual void setZDiagonal( OoqpVector& zdiag );
94 
95 
96 
97  protected:
99 
100  static void mySymAtPutSubmatrix(SymMatrix& kkt,
101  GenMatrix& B, GenMatrix& D,
102  int locnx, int locmy, int locmz);
103 };
104 
105 template<class LINSOLVER>
107  OoqpVector* dd_,
108  OoqpVector* dq_,
109  OoqpVector* nomegaInv_,
110  OoqpVector* rhs_,
111  OoqpVector* additiveDiag_,
112  LINSOLVER* thesolver)
113  : sLinsys(factory_, prob, dd_, dq_, nomegaInv_, rhs_, additiveDiag_)
114 {
115  //int rank; MPI_Comm_rank(MPI_COMM_WORLD,&rank);
116  //double t = MPI_Wtime();
117 
118  // create the KKT system matrix
119  // size = ? nnz = ?
120 
121 
122 #ifdef NY_TIMING
123  double time_Temp_1, time_Temp_2;
124  time_Temp_1=MPI_Wtime();
125  int mype_;
126  MPI_Comm_rank(MPI_COMM_WORLD,&mype_);
127 #endif
128 
129 
130  int nnzQ, nnzB, nnzD;
131  int n, nnzTotal;
132  int locns;
133 
134  firstQUpdate=true;
135  firstBUpdate=true;
136  firstDUpdate=true;
137  firstXDiagUpdate=true;
138  firstSDiagUpdate=true;
139  firstYDiagUpdate=true;
140  firstZDiagUpdate=true;
141 
142  prob->getLocalSizes(locnx, locmy, locmz);
143  locns = locmz;
144  prob->getLocalNnz(nnzQ, nnzB, nnzD);
145 
146  if(gOuterSolve >= 3 && separateHandDiag==0){
147  n = locnx+locns+locmy+locmz; // for x s y z
148  nnzTotal = n + nnzQ + nnzB + nnzD + locns;
149  }
150  else if(gOuterSolve >= 3 && separateHandDiag==1){
151  n = locnx+locns+locmy+locmz; // for x s y z
152  nnzTotal = nnzQ + nnzB + nnzD + locns;
153  }
154  else {
155  n = locnx+locmy+locmz; // only x y z, we need compress the linear system later
156  nnzTotal = n + nnzQ + nnzB + nnzD;
157  }
158 
159 
160  //alocate the matrix and copy the data into
161  SparseSymMatrix* kktsp = new SparseSymMatrix(n,nnzTotal);
162  kkt = kktsp;
163 
164  if(gOuterSolve >= 3){
165  if(separateHandDiag==0){
166  SimpleVectorHandle v( new SimpleVector(n) );
167  v->setToZero();
168  kkt->setToDiagonal(*v);
169  }
170 
171 #ifdef NY_TIMING
172  time_Temp_2=MPI_Wtime();
173  if(0==mype_){
174  cout << "\n before put matrix in sLinsysLeaf " << time_Temp_2- time_Temp_1<< "\n" << endl;
175  }
176  time_Temp_1=MPI_Wtime();
177 #endif
178 
179  kkt->symAtPutSubmatrix( 0, 0, prob->getLocalQ(), 0, 0, locnx, locnx);
180 
181 #ifdef NY_TIMING
182  time_Temp_2=MPI_Wtime();
183  if(0==mype_){
184  cout << "\n put Q in sLinsysLeaf " << time_Temp_2- time_Temp_1<< "\n" << endl;
185  }
186  time_Temp_1=MPI_Wtime();
187 #endif
188 
189 
190  kkt->symAtPutSubmatrix( locnx+locns, 0, prob->getLocalB(), 0, 0, locmy, locnx);
191 
192 #ifdef NY_TIMING
193  time_Temp_2=MPI_Wtime();
194  if(0==mype_){
195  cout << "\n put A in sLinsysLeaf " << time_Temp_2- time_Temp_1<< "\n" << endl;
196  }
197 #endif
198 
199 
200  // Fix the problem in shiftRows_CorrectMap in SparseStorage, then we can replace symAtPutSubmatrix
201  // by symAtSetSubmatrix e.g:
202  // kkt->symAtSetSubmatrix( 0, 0, prob->getLocalQ(), 0, 0, locnx, locnx,firstQUpdate, LocQMap);
203  // and others
204 
205  if (locns>0){
206 
207 #ifdef NY_TIMING
208  time_Temp_1=MPI_Wtime();
209 #endif
210  kkt->symAtPutSubmatrix( locnx+locns+locmy, 0, prob->getLocalD(), 0, 0, locmz, locnx);
211 #ifdef NY_TIMING
212  time_Temp_2=MPI_Wtime();
213  if(0==mype_){
214  cout << "\n put C in sLinsysLeaf " << time_Temp_2- time_Temp_1<< "\n" << endl;
215  }
216 #endif
217 
218 #ifdef NY_TIMING
219  time_Temp_1=MPI_Wtime();
220 #endif
221 
222 
223  SparseSymMatrixHandle tempDiagMat( new SparseSymMatrix( locns, locns ) );
224  int *tempDiagRowId = new int[locns];
225  int *tempDiagColId = new int[locns];
226  double *tempDiagEleId = new double[locns];
227  int info;
228 
229  for(int i=0;i<locns;i++){
230  tempDiagRowId[i]=i;
231  tempDiagColId[i]=i;
232  tempDiagEleId[i]=-1;
233  }
234  tempDiagMat->putSparseTriple( tempDiagRowId,locns, tempDiagColId, tempDiagEleId,info );
235  kkt->symAtPutSubmatrix( locnx + locns + locmy, locnx, *tempDiagMat, 0, 0, locns, locns);
236 
237 #ifdef NY_TIMING
238  time_Temp_2=MPI_Wtime();
239  if(0==mype_){
240  cout << "\n put Slack Diag Mat in sLinsysLeaf " << time_Temp_2- time_Temp_1<< "\n" << endl;
241  }
242 #endif
243 
244 #ifdef NY_TIMING
245  time_Temp_1=MPI_Wtime();
246 #endif
247  delete[] tempDiagRowId;
248  delete[] tempDiagColId;
249  delete[] tempDiagEleId;
250  }
251  }
252  else{
253  SimpleVectorHandle v( new SimpleVector(n) );
254  v->setToZero();
255  kkt->setToDiagonal(*v);
256 
257  kkt->symAtPutSubmatrix( 0, 0, prob->getLocalQ(), 0, 0, locnx, locnx);
258  if(locmz>0) {
259  kkt->symAtPutSubmatrix( locnx, 0, prob->getLocalB(), 0, 0, locmy, locnx);
260  kkt->symAtPutSubmatrix( locnx+locmy, 0, prob->getLocalD(), 0, 0, locmz, locnx);
261  } else{
262  mySymAtPutSubmatrix(*kkt, prob->getLocalB(), prob->getLocalD(), locnx, locmy, locmz);
263  }
264  }
265  // Fix the problem in shiftRows_CorrectMap in SparseStorage, then we can use the following codes.
266 // firstQUpdate = false;
267 // firstBUpdate = false;
268 // firstDUpdate = false;
269 
270  // create the solver for the linear system
271 
272 
273 #ifdef NY_TIMING
274  time_Temp_1=MPI_Wtime();
275 #endif
276 
277 
278  solver = NULL;
279  if(0==gUseReducedSpace && gNP_Alg==0){
280  if(1==gBuildSchurComp){
281  solver = new LINSOLVER( kktsp,locmy+locmz);
282  }else{
283  // build sc to solve problem, we only support dense schur comp
284  assert("Not Implemented" &&0);
285  }
286  }
287  else if (2==gUseReducedSpace && gNP_Alg==0 ){
288  // use reduced space schur solver, assume decition vars is inside kktsp
289  if(0==gBuildSchurComp){
290  assert("Not Implemented" &&0);
291  }else if(1==gBuildSchurComp){
292  //FIXME: prob->schursize should retunr local size
293  int decisionVarSize = prob->schurSize;
294  int *decisionVarID = prob->schurVarConID;
295 
296  int fullVarXSize = locnx;
297  int fullVarYSize = locmy;
298  int fullVarSSize = locmz;
299 
300 // solver = new ReducedSpaceSolver(kktsp,decisionVarSize,decisionVarID,fullVarXSize,fullVarYSize,fullVarSSize);
301  }
302 
303  }
304  else if(1==gUseReducedSpace && gNP_Alg==0){
305  // use reduced space schur solver, assume all the decition vars have already been moved
306 #ifdef WITH_UMFPACK
308 #endif
309  }
310  else if(gNP_Alg>0){
311 // solver = new PartitionAugSolver(kktsp,
312 // prob->var_Part_idx_in,
313 // prob->con_Part_idx_in,
314 // locmy+locmz, locnx+locmz, locmy+locmz, 0);
315 
316  }
317 
318 
319 #ifdef NY_TIMING
320  time_Temp_2=MPI_Wtime();
321  if(0==mype_){
322  cout << "\n creat solver in sLinsysLeaf " << time_Temp_2- time_Temp_1<< "\n" << endl;
323  }
324 #endif
325 
326 #ifdef NY_TIMING
327  time_Temp_1=MPI_Wtime();
328 #endif
329 
330 
331 // solver = new LINSOLVER(kktsp);
332  assert(solver != NULL);
333 
334  //t = MPI_Wtime() - t;
335  //if (rank == 0) printf("new sLinsysLeaf took %f sec\n",t);
336 
337  mpiComm = (dynamic_cast<StochVector*>(dd_))->mpiComm;
338 }
339 
340 
341 
342 #endif
Definition: sFactory.h:32
virtual void putZDiagonal(OoqpVector &zdiag)
Definition: sLinsysLeaf.C:60
Definition: ReducedSpaceSolverStateOnly.h:62
Definition: Data.h:16
int gNP_Alg
Definition: pipsOptions.C:42
std::map< int, int > zDiagIdxMap
Definition: sLinsysLeaf.h:88
SymMatrix * kkt
Definition: sLinsys.h:87
virtual void setZDiagonal(OoqpVector &zdiag)
Definition: sLinsysLeaf.C:101
Definition: DoubleMatrix.h:241
std::map< int, int > LocBMap
Definition: sLinsysLeaf.h:79
int locmz
Definition: sLinsys.h:79
virtual int factor2(sData *prob, Variables *vars)
Definition: sLinsysLeaf.C:24
virtual void deleteChildren()
Definition: sLinsysLeaf.C:175
bool firstDUpdate
Definition: sLinsysLeaf.h:76
static void mySymAtPutSubmatrix(SymMatrix &kkt, GenMatrix &B, GenMatrix &D, int locnx, int locmy, int locmz)
Definition: sLinsysLeaf.C:178
MPI_Comm mpiComm
Definition: sLinsys.h:138
int getLocalNnz(int &nnzQ, int &nnzB, int &nnzD)
Definition: sData.C:287
virtual void setYDiagonal(OoqpVector &ydiag)
Definition: sLinsysLeaf.C:87
bool firstQUpdate
Definition: sLinsysLeaf.h:76
virtual void symAtPutSubmatrix(int destRow, int destCol, DoubleMatrix &M, int srcRow, int srcCol, int rowExtent, int colExtent)=0
virtual void putXDiagonal(OoqpVector &xdiag_)
Definition: sLinsysLeaf.C:35
virtual void Ltsolve(sData *prob, OoqpVector &x)
Definition: sLinsysLeaf.C:142
bool firstSDiagUpdate
Definition: sLinsysLeaf.h:83
SparseSymMatrix & getLocalQ()
Definition: sData.C:307
std::map< int, int > yDiagIdxMap
Definition: sLinsysLeaf.h:87
std::map< int, int > xDiagIdxMap
Definition: sLinsysLeaf.h:85
bool firstYDiagUpdate
Definition: sLinsysLeaf.h:83
virtual void setToDiagonal(OoqpVector &vec)=0
std::map< int, int > LocQMap
Definition: sLinsysLeaf.h:78
virtual void setToZero()
Definition: SimpleVector.C:100
Definition: SparseSymMatrix.h:20
Definition: sLinsysLeaf.h:38
int separateHandDiag
Definition: pipsOptions.C:23
bool firstXDiagUpdate
Definition: sLinsysLeaf.h:83
virtual void Lsolve(sData *prob, OoqpVector &x)
Definition: sLinsysLeaf.C:122
virtual void Ltsolve2(sData *prob, StochVector &x, SimpleVector &xp)
Definition: sLinsysLeaf.C:151
virtual void Dsolve(sData *prob, OoqpVector &x)
Definition: sLinsysLeaf.C:133
virtual void putSparseTriple(int irow[], int len, int jcol[], double A[], int &info)
Definition: SparseSymMatrix.C:108
NlpGen * factory
Definition: NlpGenLinsys.h:61
SparseGenMatrix & getLocalD()
Definition: sData.C:357
virtual void setXDiagonal(OoqpVector &xdiag)
Definition: sLinsysLeaf.C:73
virtual void setAdditiveDiagonal()
Definition: sLinsysLeaf.C:115
virtual void UpdateMatrices(Data *prob_in, int const updateLevel=2)
Definition: sLinsysLeaf.C:242
bool firstZDiagUpdate
Definition: sLinsysLeaf.h:83
std::map< int, int > LocDMap
Definition: sLinsysLeaf.h:80
virtual void putYDualDiagonal(OoqpVector &ydiag_)
Definition: sLinsysLeaf.C:47
int schurSize
Definition: NlpGenData.h:115
virtual void setSDiagonal(OoqpVector &sdiag)
Definition: sLinsysLeaf.C:80
bool firstBUpdate
Definition: sLinsysLeaf.h:76
Definition: DoubleMatrix.h:188
int locmy
Definition: sLinsys.h:79
Definition: OoqpVector.h:34
int locnx
Definition: sLinsys.h:79
int gUseReducedSpace
Definition: pipsOptions.C:30
virtual ~sLinsysLeaf()
Definition: sLinsysLeaf.C:19
Definition: sData.h:28
Definition: sLinsys.h:26
virtual void putSDiagonal(OoqpVector &sdiag_)
Definition: sLinsysLeaf.C:41
Definition: StochVector.h:19
int gOuterSolve
Definition: Solver.C:31
DoubleLinearSolver * solver
Definition: sLinsys.h:83
Definition: Variables.h:23
SparseGenMatrix & getLocalB()
Definition: sData.C:333
std::map< int, int > sDiagIdxMap
Definition: sLinsysLeaf.h:86
void sync()
Definition: sLinsysLeaf.C:171
int getLocalSizes(int &nx, int &my, int &mz)
Definition: sData.C:245
Definition: SimpleVector.h:18
sLinsysLeaf()
Definition: sLinsysLeaf.h:98
int * schurVarConID
Definition: NlpGenData.h:114
int gBuildSchurComp
Definition: pipsOptions.C:28