PIPS-NLP
sDriver.h
Go to the documentation of this file.
1 /* PIPS-IPM
2  * Author: Cosmin G. Petra
3  * (C) 2012 Argonne National Laboratory, see documentation for copyright
4  */
5 
6 /* 2015. Modified by Nai-Yuan Chiang for NLP*/
7 
8 #ifndef QPGENSTOCHDRIVER
9 #define QPGENSTOCHDRIVER
10 
11 #include <memory>
12 #include <cstring>
13 #include <iostream>
14 #include <fstream>
15 #include <cstdlib>
16 
17 #ifdef HAVE_GETRUSAGE
18 #include <sys/time.h>
19 #include <sys/resource.h>
20 #include <unistd.h>
21 #endif
22 
23 #include <stdexcept>
24 
25 #include "StochInputTree.h"
26 #include "sTree.h"
27 #include "MehrotraSolver.h"
28 
29 //#include "NlpGenStoch.h"
30 #include "StochMonitor.h"
31 #include "NlpGenVars.h"
32 #include "NlpGenResiduals.h"
33 #include "SimpleVector.h"
34 #include "Status.h"
35 #include "NlpGenData.h"
36 #include "OoqpVersion.h"
37 //#include "StochResourcesMonitor.h"
38 
40 {
41  int scale;
42  int printx;
44 };
45 
46 struct ScaParams
47 {
48  int nscaprocs;
49  int nb;
50 };
51 
53 {
54  StochRunParams* params = new StochRunParams();
55  params->scale=0; params->printx=0; params->printLevel=0;
56  return params;
57 }
58 
59 extern int gOoqpPrintLevel;
60 
65 template<class SOLVER, class FORMULATION>
66 int nlpstoch_solve( int argc, char *argv[],
67  StochInputTree* tree,
68  StochRunParams* params,
69  SOLVER* s,
70  FORMULATION* qp)
71 {
72  MPI_Init(&argc, &argv);
73 
74  int iRet = nlpstoch_solve(tree, params, s, qp);
75 
76  MPI_Finalize();
77 
78  return iRet;
79 }
80 
81 
87 template<class SOLVER, class FORMULATION>
89  StochRunParams* params,
90  SOLVER * ,
91  FORMULATION * )
92 {
93  try {
94 #ifdef HAVE_GETRUSAGE
95  rusage before_read;
96  getrusage( RUSAGE_SELF, &before_read );
97 #endif
98 
99  FORMULATION* qp = new FORMULATION(tree);
100  NlpGenData* prob = (NlpGenData * ) qp->makeData();
101 
102 
103 #ifdef HAVE_GETRUSAGE
104  rusage after_read;
105  getrusage( RUSAGE_SELF, &after_read );
106 #endif
107 
108  NlpGenVars * vars = (NlpGenVars * ) qp->makeVariables( prob );
109  Residuals * resid = qp->makeResiduals( prob );
110  SOLVER * s = new SOLVER( qp, prob );
111 
112  //s->monitorSelf();
113  s->addMonitor(new StochMonitor(qp));
114 
115 #ifdef STOCH_TESTING
116  //tree->displayProcessInfo();
117 #endif
118 
119 #ifdef HAVE_GETRUSAGE
120  rusage before_solve;
121  getrusage( RUSAGE_SELF, &before_solve );
122 #endif
123  // this monitor just monitors total time, not iterations
124  StochIterateResourcesMonitor execTmMonitor;
125  execTmMonitor.recIterateTm_start();
126 
127  int result = s->solve(prob, vars, resid);
128 
129  execTmMonitor.recIterateTm_stop();
130 
131  int rank=0, size; MPI_Comm_size(MPI_COMM_WORLD, &size);
132  if(size>1) MPI_Comm_rank(MPI_COMM_WORLD, &rank);
133 
134  if( 0 == result ) {
135  if( gOoqpPrintLevel > 0 ) {
136 #ifdef HAVE_GETRUSAGE
137  rusage after_solve;
138  getrusage( RUSAGE_SELF, &after_solve );
139  //double read_time =
140  // (after_read.ru_utime.tv_sec - before_read.ru_utime.tv_sec)
141  // + (after_read.ru_utime.tv_usec - before_read.ru_utime.tv_usec)
142  // / 1000000.0;
143  //double solve_time =
144  // (after_solve.ru_utime.tv_sec - before_solve.ru_utime.tv_sec)
145  // + (after_solve.ru_utime.tv_usec - before_solve.ru_utime.tv_usec)
146  // / 1000000.0;
147 
148 #endif
149 
150  // OUTPUT of the statistics
151 
152  double objective = prob->objectiveValue(vars);
153 
154  if(0==rank) {
155  sTree* stTree = qp->tree;
156  cout << stTree->N << " variables, "
157  << stTree->MY << " equality constraints, "
158  << stTree->MZ << " inequality constraints.\n";
159 
160  //cout << 11 << " variables, "
161  // << 12 << " equality constraints, "
162  // << 17 << " inequality constraints.\n";
163 #ifdef TIMING
164  printf("NITER %d\n", s->iter);
165  printf("SOL %f\n", objective);
166  printf("TOTTIME %f SEC\n", execTmMonitor.tmIterate);
167 #else
168  cout << "Iterates: " << s->iter
169  <<", Optimal Solution: " << objective << endl;
170 
171  cout << "Problem solved in " << execTmMonitor.tmIterate << " sec" << endl;
172 #endif
173  if(params->printx) {
174  cout << "The x-solution is:\n";
175  vars->x->writeToStream(cout);
176  }
177  }
178  }
179  } else {
180  if ( gOoqpPrintLevel > 0 && rank==0 ) {
181  cout << "Could not solve this QP.\n";
182  cout << "Terminated with code " << result;
183  if( result > 0 && result <= UNKNOWN ) {
184  cout << " : " << TerminationStrings[result];
185  }
186  cout << ".\n";
187  }
188  }
189  if (0==rank) {
190 #ifdef TIMING
191  cout << "MAXMEM " << StochIterateResourcesMonitor::getMaxMemUsage() <<
192  " KB" << endl;
193  //double objective = prob->objectiveValue(vars);
194  //cout << "OBJVAL " << objective << endl;
195 #else
196  cout << "Maximum memory usage: " <<
198 #endif
199  }
200  delete s;
201  delete vars;
202  delete resid;
203  delete prob;
204  delete qp;
205 
206  return result;
207  }
208  catch( logic_error &e ) {
209  cerr << "Elemental error: " << e.what() << endl;
210  return -1;
211  }
212  catch( ... ) {
213  cerr << "\nOops, out of memory\n";
214  return -1;
215  }
216 
217 }
218 
219 #endif
long long MZ
Definition: sTree.h:114
int printLevel
Definition: sDriver.h:43
int printx
Definition: sDriver.h:42
Definition: sDriver.h:39
Definition: sTree.h:17
virtual void recIterateTm_stop()
Definition: StochResourcesMonitor.C:222
long long N
Definition: sTree.h:114
int scale
Definition: sDriver.h:41
OoqpVectorHandle x
Definition: NlpGenVars.h:50
Definition: Residuals.h:22
Definition: StochResourcesMonitor.h:107
StochRunParams * defaultStochRunParams()
Definition: sDriver.h:52
int gOoqpPrintLevel
!
Definition: Solver.C:17
Definition: sDriver.h:46
Definition: NlpGenVars.h:27
Definition: Status.h:20
long long MY
Definition: sTree.h:114
virtual double objectiveValue(Variables *vars)
Definition: NlpGenData.C:659
virtual void recIterateTm_start()
Definition: StochResourcesMonitor.C:216
const char * TerminationStrings[]
Definition: Status.C:9
Definition: StochInputTree.h:30
int nb
Definition: sDriver.h:49
int nlpstoch_solve(int argc, char *argv[], StochInputTree *tree, StochRunParams *params, SOLVER *s, FORMULATION *qp)
Definition: sDriver.h:66
int nscaprocs
Definition: sDriver.h:48
virtual void writeToStream(std::ostream &out) const =0
Definition: StochMonitor.h:16
double tmIterate
Definition: StochResourcesMonitor.h:118
Definition: NlpGenData.h:32
static unsigned long getMaxMemUsage()
Definition: StochResourcesMonitor.C:230