OpenGM  2.3.x
Discrete Graphical Model Library
visitors/visitors.hxx
Go to the documentation of this file.
1 #ifndef OPENGM_VISITOR_HXX
2 #define OPENGM_VISITOR_HXX
3 
4 #include <iostream>
5 #include <map>
6 #include <cmath>
7 #include <opengm/opengm.hxx>
10 
11 
12 namespace opengm{
13 namespace visitors{
14 
16  const static size_t ContinueInf = 0;
17  const static size_t StopInfBoundReached = 1;
18  const static size_t StopInfTimeout = 2;
19 };
20 
21 
22 template<class INFERENCE>
24 public:
26  }
27  void begin(INFERENCE & inf){}
28  size_t operator()(INFERENCE & inf){
30  }
31  void end(INFERENCE & inf){
32  }
33 
34  void addLog(const std::string & logName){}
35  void log(const std::string & logName,const double logValue){}
36 };
37 
38 template<class INFERENCE>
40 public:
42  }
43  void begin(INFERENCE & inf, const typename INFERENCE::ValueType value, const typename INFERENCE::ValueType bound){}
44  size_t operator()(INFERENCE & inf, const typename INFERENCE::ValueType value, const typename INFERENCE::ValueType bound){
46  }
47  void end(INFERENCE & inf, const typename INFERENCE::ValueType value, const typename INFERENCE::ValueType bound){
48  }
49 };
50 
51 template<class INFERENCE>
53 public:
54  VerboseVisitor(const size_t visithNth=1,const bool multiline=false)
55  : iteration_(0),
56  visithNth_(visithNth),
57  multiline_(multiline){
58  }
59  void begin(INFERENCE & inf){
60  std::cout<<"begin: value "<<inf.value()<<" bound "<<inf.bound()<<"\n";
61  ++iteration_;
62  }
63  size_t operator()(INFERENCE & inf){
64  if((iteration_)%visithNth_==0){
65  std::cout<<"step: "<<iteration_<<" value "<<inf.value()<<" bound "<<inf.bound()<<"\n";
66  }
67  ++iteration_;
69  }
70  void end(INFERENCE & inf){
71  std::cout<<"value "<<inf.value()<<" bound "<<inf.bound()<<"\n";
72  }
73 
74  void addLog(const std::string & logName){}
75  void log(const std::string & logName,const double logValue){
76  if((iteration_)%visithNth_==0){
77  std::cout<<logName<<" "<<logValue<<"\n";
78  }
79  }
80 
81 
82 private:
83  size_t iteration_;
84  size_t visithNth_;
85  bool multiline_;
86 };
87 
88 template<class INFERENCE>
90 public:
91  ExplicitVerboseVisitor(const size_t visithNth=1,const bool multiline=false)
92  : iteration_(0),
93  visithNth_(visithNth),
94  multiline_(multiline){
95  }
96  void begin(INFERENCE & inf, const typename INFERENCE::ValueType value, const typename INFERENCE::ValueType bound){
97  std::cout<<"begin: value "<< value <<" bound "<< bound <<"\n";
98  ++iteration_;
99  }
100  size_t operator()(INFERENCE & inf, const typename INFERENCE::ValueType value, const typename INFERENCE::ValueType bound){
101  if((iteration_)%visithNth_==0){
102  std::cout<<"step: "<<iteration_<<" value "<< value <<" bound "<< bound <<"\n";
103  }
104  ++iteration_;
106  }
107  void end(INFERENCE & inf, const typename INFERENCE::ValueType value, const typename INFERENCE::ValueType bound){
108  std::cout<<"value "<< value <<" bound "<< bound <<"\n";
109  }
110 private:
111  size_t iteration_;
112  size_t visithNth_;
113  bool multiline_;
114 };
115 
116 
117 template<class INFERENCE>
119 public:
120  typedef typename INFERENCE::ValueType ValueType;
121 
123  const size_t visithNth=1,
124  const size_t reserve=0,
125  const bool verbose=true,
126  const bool multiline=true,
127  const double timeLimit=std::numeric_limits<double>::infinity(),
128  const double gapLimit=0.0,
129  const size_t memLogging=0
130  )
131  :
132  protocolMap_(),
133  times_(),
134  values_(),
135  bounds_(),
136  iterations_(),
137  timer_(),
138  iteration_(0),
139  visithNth_(visithNth),
140  verbose_(verbose),
141  multiline_(multiline),
142  memLogging_(memLogging),
143  timeLimit_(timeLimit),
144  gapLimit_(gapLimit),
145  totalTime_(0.0)
146  {
147  // allocate all protocolated items
148  ctime_ = & protocolMap_["ctime"] ;
149  times_ = & protocolMap_["times"] ;
150  values_ = & protocolMap_["values"] ;
151  bounds_ = & protocolMap_["bounds"] ;
152  iterations_ = & protocolMap_["iteration"];
153 
154  // reservations
155  if(reserve>0){
156  times_->reserve(reserve);
157  values_->reserve(reserve);
158  bounds_->reserve(reserve);
159  iterations_->reserve(reserve);
160  }
161 
162  // start timer to measure time from
163  // constructor call to "begin" call
164  timer_.tic();
165  }
166 
167  void begin(INFERENCE & inf){
168  // stop timer which measured time from
169  // constructor call to this "begin" call
170  timer_.toc();
171  // store values bound time and iteration number
172  const ValueType val=inf.value();
173  const ValueType bound=inf.bound();
174  ctime_->push_back(timer_.elapsedTime());
175  times_->push_back(0);
176  values_->push_back(val);
177  bounds_->push_back(bound);
178  iterations_->push_back(double(iteration_));
179 
180  if( memLogging_>0)
181  protocolMap_["mem"].push_back(sys::MemoryInfo::usedPhysicalMemMax()/1000.0);
182 
183  // print step
184  if(verbose_){
185  if( memLogging_>0)
186  std::cout<<"begin: value "<<val<<" bound "<<bound<<" mem "<< protocolMap_["mem"].back() << " MB\n";
187  else
188  std::cout<<"begin: value "<<val<<" bound "<<bound<<"\n";
189  }
190  // increment iteration
191  ++iteration_;
192  // restart timer
193  timer_.reset();
194  timer_.tic();
195  }
196 
197  size_t operator()(INFERENCE & inf){
198 
199  if(iteration_%visithNth_==0){
200  // stop timer
201  timer_.toc();
202 
203  // store values bound time and iteration number
204  const ValueType val =inf.value();
205  const ValueType bound =inf.bound();
206  const double t = timer_.elapsedTime();
207  totalTime_+=t;
208  times_->push_back(totalTime_);
209  values_->push_back(val);
210  bounds_->push_back(bound);
211  iterations_->push_back(double(iteration_));
212 
213  for(size_t el=0;el<extraLogs_.size();++el){
214  protocolMap_[extraLogs_[el]].push_back( std::numeric_limits<double>::quiet_NaN() );
215  }
216 
217  if( memLogging_==1)
218  protocolMap_["mem"].push_back(std::numeric_limits<double>::quiet_NaN());
219  if( memLogging_==2)
220  protocolMap_["mem"].push_back(sys::MemoryInfo::usedPhysicalMemMax()/1000.0);
221 
222  // increment total time
223  if(verbose_){
224  if( memLogging_==2)
225  std::cout<<"step: "<<iteration_<<" value "<<val<<" bound "<<bound<<" [ "<<totalTime_ << "]" <<" mem "<< protocolMap_["mem"].back() << " MB\n";
226  else
227  std::cout<<"step: "<<iteration_<<" value "<<val<<" bound "<<bound<<" [ "<<totalTime_ << "]" <<"\n";
228  }
229 
230  // check if gap limit reached
231  if(std::fabs(bound - val) <= gapLimit_){
232  if(verbose_)
233  std::cout<<"gap limit reached\n";
234  // restart timer
235  timer_.reset();
236  timer_.tic();
238  }
239 
240  // check if time limit reached
241  if(totalTime_ > timeLimit_) {
242  if(verbose_)
243  std::cout<<"timeout reached\n";
244  // restart timer
245  timer_.reset();
246  timer_.tic();
248  }
249  // restart timer
250  timer_.reset();
251  timer_.tic();
252  }
253  ++iteration_;
255  }
256 
257 
258  void end(INFERENCE & inf){
259  // stop timer
260  timer_.toc();
261  // store values bound time and iteration number
262  const ValueType val=inf.value();
263  const ValueType bound=inf.bound();
264  const double t = timer_.elapsedTime();
265  totalTime_+=t;
266  times_->push_back(totalTime_);
267  values_->push_back(val);
268  bounds_->push_back(bound);
269  iterations_->push_back(double(iteration_));
270 
271  if( memLogging_>0)
272  protocolMap_["mem"].push_back(sys::MemoryInfo::usedPhysicalMemMax()/1000.0);
273  if(verbose_){
274  if( memLogging_>0)
275  std::cout<<"end: value "<<val<<" bound "<<bound<<" [ "<<totalTime_ << "]" <<" mem "<< protocolMap_["mem"].back() << " MB\n";
276  else
277  std::cout<<"end: value "<<val<<" bound "<<bound<<" [ "<<totalTime_ << "]" <<"\n";
278  }
279  }
280 
281 
282  void addLog(const std::string & logName){
283  protocolMap_[logName]=std::vector<double>();
284  extraLogs_.push_back(logName);
285  }
286  void log(const std::string & logName,const double logValue){
287  if((iteration_)%visithNth_==0){
288  timer_.toc();
289  if(verbose_){
290  std::cout<<logName<<" "<<logValue<<"\n";
291  }
292  protocolMap_[logName].back()=logValue;
293  // start timer
294  timer_.tic();
295  }
296  }
297 
298  // timing visitor specific interface
299 
300  const std::map< std::string, std::vector<double > > & protocolMap()const{
301  return protocolMap_;
302  }
303 
304  const std::vector<double> & getConstructionTime()const{
305  return *ctime_;
306  }
307  const std::vector<double> & getTimes ()const{
308  return *times_;
309  }
310  const std::vector<double> & getValues ()const{
311  return *values_;
312  }
313  const std::vector<double> & getBounds ()const{
314  return *bounds_;
315  }
316  const std::vector<double> & getIterations ()const{
317  return *iterations_;
318  }
319 
320 
321 private:
322 
323  std::map< std::string, std::vector<double > > protocolMap_;
324  std::vector<std::string> extraLogs_;
325  std::vector<double > * ctime_;
326  std::vector<double > * times_;
327  std::vector<double > * values_;
328  std::vector<double > * bounds_;
329  std::vector<double > * iterations_;
330  opengm::Timer timer_;
331  opengm::Timer totalTimer_;
332  size_t iteration_;
333  size_t visithNth_;
334  bool verbose_;
335  bool multiline_;
336  size_t memLogging_; // 0=no, 1=only in the end, 2=each visit
337 
338  double timeLimit_;
339  double gapLimit_;
340  double totalTime_;
341 };
342 
343 template<class INFERENCE>
345 public:
346  typedef typename INFERENCE::ValueType ValueType;
347 
349  const size_t visithNth=1,
350  const size_t reserve=0,
351  const bool verbose=true,
352  const bool multiline=true,
353  const double timeLimit=std::numeric_limits<double>::infinity(),
354  const double gapLimit=0.0,
355  const size_t memLogging=1
356  )
357  :
358  protocolMap_(),
359  times_(),
360  values_(),
361  bounds_(),
362  iterations_(),
363  timer_(),
364  iteration_(0),
365  visithNth_(visithNth),
366  verbose_(verbose),
367  multiline_(multiline),
368  memLogging_(memLogging),
369  timeLimit_(timeLimit),
370  gapLimit_(gapLimit),
371  totalTime_(0.0)
372  {
373  // allocate all protocolated items
374  ctime_ = & protocolMap_["ctime"] ;
375  times_ = & protocolMap_["times"] ;
376  values_ = & protocolMap_["values"] ;
377  bounds_ = & protocolMap_["bounds"] ;
378  iterations_ = & protocolMap_["iteration"];
379 
380  // reservations
381  if(reserve>0){
382  times_->reserve(reserve);
383  values_->reserve(reserve);
384  bounds_->reserve(reserve);
385  iterations_->reserve(reserve);
386  }
387 
388  // start timer to measure time from
389  // constructor call to "begin" call
390  timer_.tic();
391  }
392 
393  void begin(INFERENCE & inf, const typename INFERENCE::ValueType value, const typename INFERENCE::ValueType bound){
394  // stop timer which measured time from
395  // constructor call to this "begin" call
396  timer_.toc();
397  // store values bound time and iteration number
398  ctime_->push_back(timer_.elapsedTime());
399  times_->push_back(0);
400  values_->push_back(value);
401  bounds_->push_back(bound);
402  iterations_->push_back(double(iteration_));
403  if( memLogging_>0)
404  protocolMap_["mem"].push_back(sys::MemoryInfo::usedPhysicalMemMax()/1000.0);
405 
406  // print step
407  if(verbose_){
408  if( memLogging_>0)
409  std::cout<<"begin: value "<<value<<" bound "<<bound<<" mem "<< protocolMap_["mem"].back() << " MB\n";
410  else
411  std::cout<<"begin: value "<<value<<" bound "<<bound<<"\n";
412  }
413  // increment iteration
414  ++iteration_;
415  // restart timer
416  timer_.reset();
417  timer_.tic();
418  }
419 
420  size_t operator()(INFERENCE & inf, const typename INFERENCE::ValueType value, const typename INFERENCE::ValueType bound){
421 
422  if(iteration_%visithNth_==0){
423  // stop timer
424  timer_.toc();
425 
426  // store values bound time and iteration number
427  const double t = timer_.elapsedTime();
428  totalTime_+=t;
429  times_->push_back(totalTime_);
430  values_->push_back(value);
431  bounds_->push_back(bound);
432  iterations_->push_back(double(iteration_));
433 
434  if( memLogging_==1)
435  protocolMap_["mem"].push_back(std::numeric_limits<double>::quiet_NaN());
436  if( memLogging_==2)
437  protocolMap_["mem"].push_back(sys::MemoryInfo::usedPhysicalMemMax()/1000.0);
438 
439  // increment total time
440  if(verbose_){
441  if( memLogging_==2)
442  std::cout<<"step: "<<iteration_<<" value "<<value<<" bound "<<bound<<" [ "<<totalTime_ << "]" <<" mem"<< protocolMap_["mem"].back() << " MB\n";
443  else
444  std::cout<<"step: "<<iteration_<<" value "<<value<<" bound "<<bound<<" [ "<<totalTime_ << "]" <<"\n";
445  }
446 
447  // check if gap limit reached
448  if(std::fabs(bound - value) <= gapLimit_){
449  if(verbose_)
450  std::cout<<"gap limit reached\n";
451  // restart timer
452  timer_.reset();
453  timer_.tic();
455  }
456  // check if time limit reached
457  if(totalTime_ > timeLimit_) {
458  if(verbose_)
459  std::cout<<"timeout reached\n";
460  // restart timer
461  timer_.reset();
462  timer_.tic();
464  }
465  // restart timer
466  timer_.reset();
467  timer_.tic();
468  }
469  ++iteration_;
471  }
472 
473 
474  void end(INFERENCE & inf, const typename INFERENCE::ValueType value, const typename INFERENCE::ValueType bound){
475  // stop timer
476  timer_.toc();
477  const double t = timer_.elapsedTime();
478  totalTime_+=t;
479  // store values bound time and iteration number
480  times_->push_back(totalTime_);
481  values_->push_back(value);
482  bounds_->push_back(bound);
483  iterations_->push_back(double(iteration_));
484  if( memLogging_>0)
485  protocolMap_["mem"].push_back(sys::MemoryInfo::usedPhysicalMemMax()/1000.0);
486 
487  if(verbose_){
488  if( memLogging_>0)
489  std::cout<<"end: value "<<value<<" bound "<<bound<<" [ "<<totalTime_ << "]" <<" mem "<< protocolMap_["mem"].back() << " MB\n";
490  else
491  std::cout<<"end: value "<<value<<" bound "<<bound<<" [ "<<totalTime_ << "]" <<"\n";
492  }
493  }
494 
495 
496  // timing visitor specific interface
497 
498  const std::map< std::string, std::vector<double > > & protocolMap()const{
499  return protocolMap_;
500  }
501 
502  const std::vector<double> & getConstructionTime()const{
503  return *ctime_;
504  }
505  const std::vector<double> & getTimes ()const{
506  return *times_;
507  }
508  const std::vector<double> & getValues ()const{
509  return *values_;
510  }
511  const std::vector<double> & getBounds ()const{
512  return *bounds_;
513  }
514  const std::vector<double> & getIterations ()const{
515  return *iterations_;
516  }
517 
518 
519 private:
520 
521  std::map< std::string, std::vector<double > > protocolMap_;
522 
523  std::vector<double > * ctime_;
524  std::vector<double > * times_;
525  std::vector<double > * values_;
526  std::vector<double > * bounds_;
527  std::vector<double > * iterations_;
528  opengm::Timer timer_;
529  opengm::Timer totalTimer_;
530  size_t iteration_;
531  size_t visithNth_;
532  bool verbose_;
533  bool multiline_;
534  size_t memLogging_;
535  double timeLimit_;
536  double gapLimit_;
537  double totalTime_;
538 };
539 
540 template<class VISITOR, class INFERENCE_TYPE>
542 {
543 public:
544  typedef VISITOR VisitorType;
545  typedef INFERENCE_TYPE InferenceType;
546  typedef typename InferenceType::ValueType ValueType;
547 
548  ExplicitVisitorWrapper(VISITOR* pvisitor,INFERENCE_TYPE* pinference)
549  :_pvisitor(pvisitor),
550  _pinference(pinference){};
551  void begin(ValueType value,ValueType bound){_pvisitor->begin(*_pinference,value,bound);}
552  void end(ValueType value,ValueType bound){_pvisitor->end(*_pinference,value,bound);}
553  size_t operator() (ValueType value,ValueType bound){return (*_pvisitor)(*_pinference,value,bound);}
554  size_t operator() (){return (*_pvisitor)(*_pinference);}
555 private:
556  VISITOR* _pvisitor;
557  INFERENCE_TYPE* _pinference;
558 };
559 
560 template<class VISITOR, class INFERENCE_TYPE>
562 {
563 public:
564  typedef VISITOR VisitorType;
565  typedef INFERENCE_TYPE InferenceType;
566  typedef typename InferenceType::ValueType ValueType;
567 
568  VisitorWrapper(VISITOR* pvisitor,INFERENCE_TYPE* pinference)
569  :_pvisitor(pvisitor),
570  _pinference(pinference){};
571  void begin(){_pvisitor->begin(*_pinference);}
572  void end(){_pvisitor->end(*_pinference);}
573  size_t operator() (){return (*_pvisitor)(*_pinference);}
574  void addLog(const std::string& logName){_pvisitor->addLog(logName);}
575  void log(const std::string& logName, double value){_pvisitor->log(logName,value);}
576 private:
577  VISITOR* _pvisitor;
578  INFERENCE_TYPE* _pinference;
579 };
580 
581 
582 }
583 }
584 
585 #endif //OPENGM_VISITOR_HXX
void begin(INFERENCE &inf, const typename INFERENCE::ValueType value, const typename INFERENCE::ValueType bound)
The OpenGM namespace.
Definition: config.hxx:43
const std::vector< double > & getValues() const
void reset()
Definition: timer.hxx:124
void begin(ValueType value, ValueType bound)
const std::map< std::string, std::vector< double > > & protocolMap() const
const std::map< std::string, std::vector< double > > & protocolMap() const
TimingVisitor(const size_t visithNth=1, const size_t reserve=0, const bool verbose=true, const bool multiline=true, const double timeLimit=std::numeric_limits< double >::infinity(), const double gapLimit=0.0, const size_t memLogging=0)
void toc()
Definition: timer.hxx:109
void end(INFERENCE &inf, const typename INFERENCE::ValueType value, const typename INFERENCE::ValueType bound)
void log(const std::string &logName, double value)
Platform-independent runtime measurements.
Definition: timer.hxx:29
void addLog(const std::string &logName)
void addLog(const std::string &logName)
const std::vector< double > & getIterations() const
size_t operator()(INFERENCE &inf, const typename INFERENCE::ValueType value, const typename INFERENCE::ValueType bound)
void log(const std::string &logName, const double logValue)
static double usedPhysicalMemMax()
Definition: meminfo.hxx:121
ExplicitVerboseVisitor(const size_t visithNth=1, const bool multiline=false)
size_t operator()(INFERENCE &inf, const typename INFERENCE::ValueType value, const typename INFERENCE::ValueType bound)
const std::vector< double > & getTimes() const
ExplicitVisitorWrapper(VISITOR *pvisitor, INFERENCE_TYPE *pinference)
const std::vector< double > & getConstructionTime() const
void begin(INFERENCE &inf, const typename INFERENCE::ValueType value, const typename INFERENCE::ValueType bound)
size_t operator()(INFERENCE &inf)
void begin(INFERENCE &inf, const typename INFERENCE::ValueType value, const typename INFERENCE::ValueType bound)
InferenceType::ValueType ValueType
const std::vector< double > & getValues() const
VisitorWrapper(VISITOR *pvisitor, INFERENCE_TYPE *pinference)
size_t operator()(INFERENCE &inf, const typename INFERENCE::ValueType value, const typename INFERENCE::ValueType bound)
double elapsedTime() const
Definition: timer.hxx:130
void tic()
Definition: timer.hxx:96
VerboseVisitor(const size_t visithNth=1, const bool multiline=false)
const std::vector< double > & getBounds() const
void log(const std::string &logName, const double logValue)
void log(const std::string &logName, const double logValue)
void addLog(const std::string &logName)
const std::vector< double > & getConstructionTime() const
const std::vector< double > & getTimes() const
const std::vector< double > & getIterations() const
ExplicitTimingVisitor(const size_t visithNth=1, const size_t reserve=0, const bool verbose=true, const bool multiline=true, const double timeLimit=std::numeric_limits< double >::infinity(), const double gapLimit=0.0, const size_t memLogging=1)
const std::vector< double > & getBounds() const
void end(INFERENCE &inf, const typename INFERENCE::ValueType value, const typename INFERENCE::ValueType bound)
void end(ValueType value, ValueType bound)
void end(INFERENCE &inf, const typename INFERENCE::ValueType value, const typename INFERENCE::ValueType bound)
void addLog(const std::string &logName)