Fork me on GitHub
 All Classes Files Functions Variables Groups Pages
monitor.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2014 NVIDIA Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
21 #pragma once
22 
23 #include <cusp/detail/config.h>
24 #include <cusp/blas/blas.h>
25 #include <cusp/complex.h>
26 
27 #include <limits>
28 #include <iostream>
29 #include <iomanip>
30 
31 namespace cusp
32 {
100 template <typename ValueType>
101 class monitor
102 {
103 public:
104  typedef typename cusp::norm_type<ValueType>::type Real;
105 
117  template <typename VectorType>
118  monitor(const VectorType& b,
119  const size_t iteration_limit = 500,
120  const Real relative_tolerance = 1e-5,
121  const Real absolute_tolerance = 0,
122  const bool verbose = false);
123 
127  void operator++(void);
128 
134  bool converged(void) const;
135 
141  Real residual_norm(void) const;
142 
148  size_t iteration_count(void) const;
149 
155  size_t iteration_limit(void) const;
156 
162  Real relative_tolerance(void) const;
163 
169  Real absolute_tolerance(void) const;
170 
176  Real tolerance(void) const;
177 
184  template <typename Vector>
185  bool finished(const Vector& r);
186 
193  void set_verbose(bool verbose_ = true);
194 
200  bool is_verbose(void);
201 
208  template <typename Vector>
209  void reset(const Vector& b);
210 
214  void print(void);
215 
221  Real immediate_rate(void);
222 
228  Real geometric_rate(void);
229 
235  Real average_rate(void);
236 
237  /*
238  * Array holding the residuals per iteration
239  */
241 
242 private:
243 
245  Real b_norm;
246  Real r_norm;
247  size_t iteration_limit_;
248  size_t iteration_count_;
249  Real relative_tolerance_;
250  Real absolute_tolerance_;
251  bool verbose;
253 };
254 
255 // TODO : Remove convergence, default, and verbose monitors in v0.6.0
256 template <typename ValueType>
257 class convergence_monitor : public monitor<ValueType>
258 {
259 private:
260 
261  typedef monitor<ValueType> Parent;
262 
263 public:
264 
265  typedef typename Parent::Real Real;
266 
281  template <typename VectorType>
282  CUSP_DEPRECATED
283  convergence_monitor(const VectorType& b,
284  const size_t iteration_limit = 500,
285  const Real relative_tolerance = 1e-5,
286  const Real absolute_tolerance = 0)
288 };
289 
290 template <typename ValueType>
291 class default_monitor : public monitor<ValueType>
292 {
293 private:
294 
295  typedef monitor<ValueType> Parent;
296 
297 public:
298 
299  typedef typename Parent::Real Real;
300 
315  template <typename VectorType>
316  CUSP_DEPRECATED
317  default_monitor(const VectorType& b,
318  const size_t iteration_limit = 500,
319  const Real relative_tolerance = 1e-5,
320  const Real absolute_tolerance = 0)
322 };
323 
324 template <typename ValueType>
325 class verbose_monitor : public monitor<ValueType>
326 {
327 private:
328 
329  typedef monitor<ValueType> Parent;
330 
331 public:
332 
333  typedef typename Parent::Real Real;
334 
349  template <typename VectorType>
350  CUSP_DEPRECATED
351  verbose_monitor(const VectorType& b,
352  const size_t iteration_limit = 500,
353  const Real relative_tolerance = 1e-5,
354  const Real absolute_tolerance = 0)
356 };
357 
358 } // end namespace cusp
359 
360 #include <cusp/detail/monitor.inl>
CUSP_DEPRECATED default_monitor(const VectorType &b, const size_t iteration_limit=500, const Real relative_tolerance=1e-5, const Real absolute_tolerance=0)
Constructs a default_monitor for a given right-hand-side b.
Definition: monitor.h:317
void set_verbose(bool verbose_=true)
Sets the verbosity level of the monitor.
Complex numbers.
BLAS-like functions.
Real absolute_tolerance(void) const
Returns the absolute tolerance.
size_t iteration_count(void) const
Returns the number of iterations that the monitor has executed.
Real geometric_rate(void)
Returns the geometric convergence rate.
Implements standard convergence criteria and reporting for iterative solvers.
Definition: monitor.h:101
CUSP_DEPRECATED verbose_monitor(const VectorType &b, const size_t iteration_limit=500, const Real relative_tolerance=1e-5, const Real absolute_tolerance=0)
Constructs a verbose_monitor for a given right-hand-side b.
Definition: monitor.h:351
size_t iteration_limit(void) const
Returns the maximum number of iterations.
bool finished(const Vector &r)
Applies convergence criteria to determine whether iteration is finished.
Real tolerance(void) const
Return the tolerance equal to absolute_tolerance() + relative_tolerance() * ||b||.
void reset(const Vector &b)
Resets the monitor using same convergence criteria.
Real immediate_rate(void)
Returns the immedidate convergence rate.
bool converged(void) const
Indicates whether the last tested residual satisfies the convergence tolerance.
void print(void)
Prints the number of iterations and convergence history information.
bool is_verbose(void)
Gets the verbosity level of the monitor.
CUSP_DEPRECATED convergence_monitor(const VectorType &b, const size_t iteration_limit=500, const Real relative_tolerance=1e-5, const Real absolute_tolerance=0)
Constructs a convergence_monitor for a given right-hand-side b.
Definition: monitor.h:283
Real relative_tolerance(void) const
Returns the relative tolerance.
void operator++(void)
Increments the iteration count.
monitor(const VectorType &b, const size_t iteration_limit=500, const Real relative_tolerance=1e-5, const Real absolute_tolerance=0, const bool verbose=false)
Constructs a monitor for a given right-hand-side b.
Real residual_norm(void) const
Euclidean norm of last residual.
Real average_rate(void)
Returns the average convergence rate.