SpikeGPU  1.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
monitor.h
Go to the documentation of this file.
1 #ifndef SPIKE_MONITOR_H
2 #define SPIKE_MONITOR_H
3 
4 #include <limits>
5 
6 #include <cusp/array1d.h>
7 #include <cusp/blas.h>
8 
9 
10 namespace spike {
11 
12 
13 // ----------------------------------------------------------------------------
14 // Monitor
15 //
16 // This class encapsulates...
17 // ----------------------------------------------------------------------------
18 template <typename SolverVector>
19 class Monitor
20 {
21 public:
22  typedef typename SolverVector::value_type SolverValueType;
23 
24  enum State {
28  };
29 
30  Monitor(const int maxIterations,
31  const SolverValueType tolerance);
32  ~Monitor() {}
33 
34  void init(const SolverVector& rhs);
35 
36  bool done(const SolverVector& r);
37 
38  void increment(float incr) {m_iterations += incr;}
39 
40  bool converged() const {return m_state == Converged;}
41  int getMaxIterations() const {return m_maxIterations;}
42  float getNumIterations() const {return m_iterations;}
43  SolverValueType getTolerance() const {return m_tolerance;}
44  SolverValueType getRHSNorm() const {return m_rhsNorm;}
45  SolverValueType getResidualNorm() const {return m_rNorm;}
46  SolverValueType getRelResidualNorm() const {return m_rNorm / m_rhsNorm;}
47 
48 private:
49  State m_state;
50  int m_maxIterations;
51  float m_iterations;
52 
53  SolverValueType m_tolerance;
54  SolverValueType m_rhsNorm;
55  SolverValueType m_rNorm;
56 };
57 
58 
59 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
61 template <typename SolverVector>
62 inline
63 Monitor<SolverVector>::Monitor(const int maxIterations,
64  const SolverValueType tolerance)
65 : m_rNorm(std::numeric_limits<SolverValueType>::max()),
66  m_maxIterations(maxIterations),
67  m_tolerance(tolerance),
68  m_iterations(0),
69  m_state(Continue)
70 {
71 }
72 
73 
74 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
76 template <typename SolverVector>
77 inline void
78 Monitor<SolverVector>::init(const SolverVector& rhs)
79 {
80  m_rhsNorm = cusp::blas::nrm2(rhs);
81  m_iterations = 0;
82 }
83 
84 
85 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
87 template <typename SolverVector>
88 inline bool
89 Monitor<SolverVector>::done(const SolverVector& r)
90 {
91  m_rNorm = cusp::blas::nrm2(r);
92 
93  if (m_rNorm <= m_tolerance * m_rhsNorm) m_state = Converged;
94  else if (isnan(m_rNorm)) m_state = Failed;
95  else if (m_iterations > m_maxIterations) m_state = Failed;
96  else m_state = Continue;
97 
98  return m_state != Continue;
99 }
100 
101 
102 } // namespace spike
103 
104 
105 #endif
Definition: monitor.h:27
void increment(float incr)
Definition: monitor.h:38
SolverValueType getRHSNorm() const
Definition: monitor.h:44
float getNumIterations() const
Definition: monitor.h:42
SolverValueType getRelResidualNorm() const
Definition: monitor.h:46
~Monitor()
Definition: monitor.h:32
SolverValueType getTolerance() const
Definition: monitor.h:43
bool done(const SolverVector &r)
Definition: monitor.h:89
Definition: monitor.h:25
SolverValueType getResidualNorm() const
Definition: monitor.h:45
SolverVector::value_type SolverValueType
Definition: monitor.h:22
bool converged() const
Definition: monitor.h:40
void init(const SolverVector &rhs)
Definition: monitor.h:78
int getMaxIterations() const
Definition: monitor.h:41
Definition: monitor.h:26
Definition: monitor.h:19
State
Definition: monitor.h:24
Monitor(const int maxIterations, const SolverValueType tolerance)
Definition: monitor.h:63