TPIE

v1.1rc1-6-g0c97303
progress_indicator_base.h
Go to the documentation of this file.
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 // Copyright 2008, The TPIE development team
4 //
5 // This file is part of TPIE.
6 //
7 // TPIE is free software: you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
19 
23 
24 #ifndef _TPIE_PROGRESS_INDICATOR_BASE_H
25 #define _TPIE_PROGRESS_INDICATOR_BASE_H
26 
27 #include <tpie/portability.h>
28 #include <algorithm>
29 #include <boost/thread.hpp>
30 #include <tpie/imported/cycle.h>
32 #include <tpie/tpie_log.h>
33 
34 namespace tpie {
35 
36 enum description_importance {
37  IMPORTANCE_NONE,
38  IMPORTANCE_LOG,
39  IMPORTANCE_MINOR,
40  IMPORTANCE_MAJOR
41 };
42 
62 
64 public:
69 
70  progress_indicator_base(stream_size_type range) :
71  m_range(range),
72  m_current(0),
73  //m_lastUpdate(getticks()),
74  m_next(0),
75  m_predictor(0) {
76  compute_threshold();
77  }
78 
83  // Do nothing.
84  };
85 
89  void step(stream_size_type step=1) {
90  m_current += step;
91 
92 #ifndef TPIE_NDEBUG
93  {
94  ticks currentTicks = getticks();
95  if (elapsed(currentTicks,m_lastCalled) > m_frequency * m_threshold * 5)
96  tpie::log_debug() << "Step was not called for an estimated "
97  << (elapsed(currentTicks,m_lastCalled) / (m_frequency * m_threshold))
98  << " seconds" << std::endl;;
99  m_lastCalled = currentTicks;
100  }
101 #endif
102  if (m_current > m_next) {
103  ticks currentTicks = getticks();
104  m_next = static_cast<stream_size_type>(
105  static_cast<double>(m_current) * (elapsed(currentTicks, m_start) + m_threshold)/
106  elapsed(currentTicks, m_start));
107  if (m_next > m_current *2) m_next=m_current*2; //For bad guestimation in the beginning
108  refresh();
109  }
110  }
111 
112  void raw_step(stream_size_type step) {
113  m_current += step;
114 #ifndef TPIE_NDEBUG
115  m_lastCalled = getticks();
116 #endif
117  refresh();
118  }
119 
124  virtual void init(stream_size_type range=0) {
125  if (range != 0) set_range(range);
126  m_current = 0;
127  refresh();
128 
129  m_start = getticks();
130  m_next = 1;
131 
132 #ifndef TPIE_NDEBUG
133  m_lastCalled = getticks();
134 #endif
135  }
136 
140  virtual void done() {}
141 
150  virtual void set_range(stream_size_type range) {
151  m_range = range;
152  }
153 
157  virtual void refresh() = 0;
158 
162  stream_size_type get_current() { return m_current; }
163 
167  stream_size_type get_range() { return m_range; }
168 
169  execution_time_predictor * get_time_predictor() {return m_predictor;}
170  void set_time_predictor(execution_time_predictor * p) {m_predictor = p;}
171 
172  std::string estimated_remaining_time() {
173  if (m_range == 0 || m_predictor == 0) return "";
174  return m_predictor->estimate_remaining_time( double(m_current) / double(m_range) );
175  }
176 
177  virtual void push_breadcrumb(const char *, description_importance) {}
178  virtual void pop_breadcrumb() {}
179 protected:
181  stream_size_type m_range;
182 
184  stream_size_type m_current;
185 
186 private:
188 #ifndef TPIE_NDEBUG
189  ticks m_lastCalled;
190 #endif
191 
192  stream_size_type m_next;
193  ticks m_start;
194 
196  static const unsigned int m_frequency;
197 
199  static double m_threshold;
200 
202  static bool m_thresholdComputed;
203 
204  execution_time_predictor * m_predictor;
208  static void compute_threshold();
209 
212 };
213 
214 } // tpie namespace
215 
216 #endif // _TPIE_PROGRESS_INDICATOR_BASE
The base class for indicating the progress of some task.
virtual void refresh()=0
Display the indicator.
stream_size_type get_range()
Get the maximum value of the current range.
Machine-dependent cycle counters code by Matteo Frigo at MIT.
virtual ~progress_indicator_base()
The destructor. Nothing is done.
Execution time predictor used by fractional progress.
virtual void done()
Advance the indicator to the end.
stream_size_type m_current
The current progress count [m_minRange...m_maxRange].
This file contains a few deprecated definitions for legacy code.
Logging functionality and log_level codes for different priorities of log messages.
stream_size_type m_range
The upper bound of the counting range.
virtual void set_range(stream_size_type range)
Set the upper bound of the counting range.
void step(stream_size_type step=1)
Record an increment to the indicator and advance the indicator.
progress_indicator_base(stream_size_type range)
Initializes the indicator.
stream_size_type get_current()
Get the current value of the step counter.
logstream & log_debug()
Return logstream for writing debug log messages.
Definition: tpie_log.h:124
virtual void init(stream_size_type range=0)
Initialize progress indicator.