cg.hh

Go to the documentation of this file.
1 
6 #ifndef cg_hh
7 #define cg_hh
8 
9 #include <string>
10 
11 #include "basics/typedefs.hh"
12 #include "basics/parallel.hh"
13 #include "compositions.hh"
14 
15 #ifdef HAS_MPI
16 #include <mpi.h>
17 #endif
18 
19 namespace concepts {
20 
21  // ******************************************************************* CG **
22 
38  template<class F>
39  class CG : public VecOperator<F> {
40  public:
49  CG(Operator<F>& A, Real maxeps, int maxit = 0, uint relres = false,
50  bool throwing = true )
51  : VecOperator<F>(A.dimY(), A.dimX()), W_(0), A_(A)
52  , maxeps_(maxeps*maxeps), maxit_(maxit), eps_(1.0), it_(0)
53  , relres_(relres), throwing_(throwing) {
54 #ifdef HAS_MPI
55  // Parallel compiled environment
56  if (isParallelRunning ())
57  {
58  // Parallel running environment
59  MPI_Comm_size(MPI_COMM_WORLD, &N_);
60  MPI_Comm_rank(MPI_COMM_WORLD, &rank_);
61  parallel_ = (N_>1);
63  }
64  else
65  {
66  // Sequential running environment
67  N_ = 1; rank_ = 0;
68  parallel_ = false;
69  }
70 #else
71  // Sequential compiled environment
72  N_ = 1; rank_ = 0;
73  parallel_=false;
74 #endif
75  }
76 
87  CG(Operator<F>& A, Operator<F>& Minv, Real maxeps, int maxit = 0,
88  bool relres = 0, bool throwing = true)
89  : VecOperator<F>(A.dimY(), A.dimX()), W_(&Minv), A_(A)
90  , maxeps_(maxeps*maxeps), maxit_(maxit), eps_(1.0), it_(0)
91  , relres_(relres), throwing_(throwing) {
92 #ifdef HAS_MPI
93  // Parallel compiled environment
94  if (isParallelRunning ())
95  {
96  // Parallel running environment
97  MPI_Comm_size(MPI_COMM_WORLD, &N_);
98  MPI_Comm_rank(MPI_COMM_WORLD, &rank_);
99  parallel_ = (N_>1);
101  }
102  else
103  {
104  // Sequential running environment
105  N_ = 1; rank_ = 0;
106  parallel_ = false;
107  }
108 #else
109  // Sequential compiled environment
110  N_ = 1; rank_ = 0;
111  parallel_=false;
112 #endif
113  }
114 
118  uint iterations() const { return it_; }
119 
123  Real epsilon() const { return eps_; }
124  protected:
125  std::ostream& info(std::ostream& os) const;
126 
127  private:
132 
136  uint maxit_;
140  uint it_;
142  bool relres_;
146  bool throwing_;
147 
149  int N_;
150 
152  int rank_;
153 
155  bool parallel_;
156 
159 
160  virtual void apply_(const Vector<F>& fncY, Vector<F>& fncX);
161  virtual void apply_();
162 
165 
166 #ifdef HAS_MPI
167  Real parallel_l2_2(const std::map<int,Vector<F>> &K_ij, const Vector<F>& r_i,std::map<int,Vector<F>> &map_tilde_r_ji);
169 #endif
170  };
171 
172 } // namespace concepts
173 
174 #endif // cg_hh
bool parallel_
flag for parallel version
Definition: cg.hh:155
Solves a symmetric system of linear equations with conjugate gradients (CG).
Definition: cg.hh:39
Operator< F > * W_
Optional preconditioner.
Definition: cg.hh:129
CG(Operator< F > &A, Operator< F > &Minv, Real maxeps, int maxit=0, bool relres=0, bool throwing=true)
Constructor.
Definition: cg.hh:87
Real epsilon() const
Returns the residual.
Definition: cg.hh:123
Vector< Real > J_
Vector containing indices of non-zero rows of A_.
Definition: cg.hh:158
Operator< F > & A_
Operator which is solved.
Definition: cg.hh:131
virtual const uint dimX() const
Returns the size of the image space of the operator (number of rows of the corresponding matrix)
Definition: compositions.hh:93
virtual void apply_()
Intrinsic application method without argument.
uint it_
Number of iterations.
Definition: cg.hh:140
bool throwing_
false: best solution is given, when non converging true: exception is thrown, when non converging
Definition: cg.hh:146
bool relres_
false: absolute residual, true: relative residual
Definition: cg.hh:142
bool isParallelRunning()
Tests if the instruction MPI::Init() was called.
virtual void apply_(const Vector< F > &fncY, Vector< F > &fncX)
Intrinsic application method, i.e.
int rank_
thread number
Definition: cg.hh:152
uint iterations() const
Returns the number of iterations.
Definition: cg.hh:118
Abstract class for an operator.
Definition: ARPACK.hh:16
uint maxit_
Maximal number of iterations until abortion.
Definition: cg.hh:136
Abstract class for an operator acting on vectors only, not arbitrary functions.
int N_
number of threads
Definition: cg.hh:149
Vector< Real > create_index_vector()
function to generate vector of indices of non-zero rows of A_
Real eps_
Current residual.
Definition: cg.hh:138
CG(Operator< F > &A, Real maxeps, int maxit=0, uint relres=false, bool throwing=true)
Constructor.
Definition: cg.hh:49
virtual const uint dimY() const
Returns the size of the source space of the operator (number of columns of the corresponding matrix)
Definition: compositions.hh:98
Real maxeps_
Convergence criterion.
Definition: cg.hh:134
double Real
Type normally used for a floating point number.
Definition: typedefs.hh:36
std::ostream & info(std::ostream &os) const
Basic namespace for Concepts-2.
Definition: pml_formula.h:16
Page URL: http://wiki.math.ethz.ch/bin/view/Concepts/WebHome
21 August 2020
© 2020 Eidgenössische Technische Hochschule Zürich