ug4
gauss_seidel.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2015: G-CSC, Goethe University Frankfurt
3  * Author: Martin Rupp
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef __H__UG__LIB_DISC__OPERATOR__LINEAR_OPERATOR__GAUSS_SEIDEL__
34 #define __H__UG__LIB_DISC__OPERATOR__LINEAR_OPERATOR__GAUSS_SEIDEL__
35 
39 #ifdef UG_PARALLEL
43 #endif
44 
47 
48 namespace ug{
49 
50 template<typename TAlgebra>
51 class GaussSeidelBase : public IPreconditioner<TAlgebra>
52 {
53  public:
54  // Algebra type
55  typedef TAlgebra algebra_type;
56 
57  // Vector type
58  typedef typename TAlgebra::vector_type vector_type;
59 
60  // Matrix type
61  typedef typename TAlgebra::matrix_type matrix_type;
62 
65 
68 
70  typedef std::vector<size_t> ordering_container_type;
72 
73  protected:
77 
78  public:
79  // Constructor
81  m_relax(1.0),
83  m_useOverlap(false) {};
84 
87  : base_type(parent),
89  m_useOverlap(parent.m_useOverlap),
91  {
92  set_sor_relax(parent.m_relax);
93  }
94 
96  void set_sor_relax(number relaxFactor){ m_relax = relaxFactor;}
97 
100 
101  void enable_overlap (bool enable) {m_useOverlap = enable;}
102 
105  m_spOrderingAlgo = ordering_algo;
106  }
107 
108  virtual const char* name() const = 0;
109  protected:
110 
111  virtual bool supports_parallel() const {return true;}
112 
113  // Preprocess routine
115  {
116  PROFILE_BEGIN_GROUP(GaussSeidel_preprocess, "algebra gaussseidel");
117  matrix_type *pA;
118 #ifdef UG_PARALLEL
119  if(pcl::NumProcs() > 1)
120  {
121  if(m_useOverlap){
122  m_A = *pOp;
124  m_oD.set_layouts(m_A.layouts());
125  m_oC.set_layouts(m_A.layouts());
126  m_oD.resize(m_A.num_rows(), false);
127  m_oC.resize(m_A.num_rows(), false);
128  }
129  else if (m_bConsistentInterfaces)
130  {
131  m_A = *pOp;
133  }
134  else
135  {
136  // copy original matrix
137  MakeConsistent(*pOp, m_A);
138  // set zero on slaves
139  std::vector<IndexLayout::Element> vIndex;
140  CollectUniqueElements(vIndex, m_A.layouts()->slave());
141  SetDirichletRow(m_A, vIndex);
142  }
143  pA = &m_A;
144  }
145  else
146  pA = &(*pOp);
147 #else
148  pA = &(*pOp);
149 #endif
150  THROW_IF_NOT_EQUAL(pA->num_rows(), pA->num_cols());
151 // UG_ASSERT(CheckDiagonalInvertible(A), "GS: A has noninvertible diagonal");
152  UG_COND_THROW(CheckDiagonalInvertible(*pA) == false, name() << ": A has noninvertible diagonal");
153 
154  return true;
155  }
156 
157  // Postprocess routine
158  virtual bool postprocess() {return true;}
159 
160  virtual void step(const matrix_type &A, vector_type &c, const vector_type &d, const number relax) = 0;
161 
162  // Stepping routine
164  {
165  PROFILE_BEGIN_GROUP(GaussSeidel_step, "algebra gaussseidel");
166 
167 #ifdef UG_PARALLEL
168  if(pcl::NumProcs() > 1)
169  {
170  if(m_useOverlap){
171  for(size_t i = 0; i < d.size(); ++i)
172  m_oD[i] = d[i];
173  for(size_t i = d.size(); i < m_oD.size(); ++i)
174  m_oD[i] = 0;
175  m_oD.set_storage_type(PST_ADDITIVE);
176  m_oD.change_storage_type(PST_CONSISTENT);
177 
178  step(m_A, m_oC, m_oD, m_relax);
179 
180  for(size_t i = 0; i < c.size(); ++i)
181  c[i] = m_oC[i];
182  SetLayoutValues(&c, c.layouts()->slave(), 0);
183  c.set_storage_type(PST_UNIQUE);
184  }
185  else if (m_bConsistentInterfaces)
186  {
187  // make defect consistent
188  SmartPtr<vector_type> spDtmp = d.clone();
189  spDtmp->change_storage_type(PST_CONSISTENT);
190 
191  THROW_IF_NOT_EQUAL_3(c.size(), spDtmp->size(), m_A.num_rows());
192  step(m_A, c, *spDtmp, m_relax);
193 
194  // declare c unique to enforce that only master correction is used
195  // when it is made consistent below
196  c.set_storage_type(PST_UNIQUE);
197 
198  //UG_COND_THROW(!d.has_storage_type(PST_ADDITIVE), "Additive or unique defect expected.");
199  //step(m_A, c, d, m_relax);
200  //c.set_storage_type(PST_ADDITIVE);
201  }
202  else
203  {
204  // todo: do not clone every time
205  // make defect unique
206  SmartPtr<vector_type> spDtmp = d.clone();
207  spDtmp->change_storage_type(PST_UNIQUE);
208 
209  THROW_IF_NOT_EQUAL_3(c.size(), spDtmp->size(), m_A.num_rows());
210  step(m_A, c, *spDtmp, m_relax);
211  c.set_storage_type(PST_UNIQUE);
212  }
213 
214  // make correction consistent
215  c.change_storage_type(PST_CONSISTENT);
216 
217  return true;
218  }
219  else
220 #endif
221  {
222  matrix_type &A = *pOp;
223  THROW_IF_NOT_EQUAL_4(c.size(), d.size(), A.num_rows(), A.num_cols());
224 
225  step(A, c, d, m_relax);
226 #ifdef UG_PARALLEL
227  c.set_storage_type(PST_CONSISTENT);
228 #endif
229  return true;
230  }
231  }
232 
233  protected:
234 #ifdef UG_PARALLEL
236 #endif
237  protected:
240 
244 
247 
248 
251 #ifdef NOT_YET
252  ordering_container_type m_ordering, m_old_ordering;
253  std::vector<size_t> m_newIndex, m_oldIndex;
254  bool m_bSortIsIdentity;
255 #endif
256 };
257 
259 
270 template <typename TAlgebra>
271 class GaussSeidel : public GaussSeidelBase<TAlgebra>
272 {
273  typedef TAlgebra algebra_type;
274  typedef typename TAlgebra::vector_type vector_type;
275  typedef typename TAlgebra::matrix_type matrix_type;
277 
278 public:
279  // Name of preconditioner
280  virtual const char* name() const {return "Gauss-Seidel";}
281 
284 
287  : base_type(parent)
288  { }
289 
292  {
293  return make_sp(new GaussSeidel<algebra_type>(*this));
294  }
295 
296  // Stepping routine
297  virtual void step(const matrix_type &A, vector_type &c, const vector_type &d, const number relax)
298  {
299  gs_step_LL(A, c, d, relax);
300  }
301 };
302 
304 
315 template <typename TAlgebra>
316 class BackwardGaussSeidel : public GaussSeidelBase<TAlgebra>
317 {
318  typedef TAlgebra algebra_type;
319  typedef typename TAlgebra::vector_type vector_type;
320  typedef typename TAlgebra::matrix_type matrix_type;
322 
323 public:
324  // Name of preconditioner
325  virtual const char* name() const {return "Backward Gauss-Seidel";}
326 
329 
332  : base_type(parent)
333  { }
334 
337  {
338  return make_sp(new BackwardGaussSeidel<algebra_type>(*this));
339  }
340 
341  // Stepping routine
342  virtual void step(const matrix_type &A, vector_type &c, const vector_type &d, const number relax)
343  {
344  gs_step_UR(A, c, d, relax);
345  }
346 };
347 
348 
349 template <typename TAlgebra>
350 class SymmetricGaussSeidel : public GaussSeidelBase<TAlgebra>
351 {
352  typedef TAlgebra algebra_type;
353  typedef typename TAlgebra::vector_type vector_type;
354  typedef typename TAlgebra::matrix_type matrix_type;
356 
357 public:
358  // Name of preconditioner
359  virtual const char* name() const {return "Symmetric Gauss-Seidel";}
360 
363 
366  : base_type(parent)
367  { }
368 
371  {
372  return make_sp(new SymmetricGaussSeidel<algebra_type>(*this));
373  }
374 
375  // Stepping routine
376  virtual void step(const matrix_type &A, vector_type &c, const vector_type &d, const number relax)
377  {
378  sgs_step(A, c, d, relax);
379  }
380 };
381 
382 } // end namespace ug
383 
384 #endif // __H__UG__LIB_DISC__OPERATOR__LINEAR_OPERATOR__GAUSS_SEIDEL__
Definition: smart_pointer.h:108
Gauss-Seidel preconditioner for the 'backward' ordering of the dofs.
Definition: gauss_seidel.h:317
virtual const char * name() const
returns the name of iterator
Definition: gauss_seidel.h:325
TAlgebra algebra_type
Definition: gauss_seidel.h:318
TAlgebra::matrix_type matrix_type
Definition: gauss_seidel.h:320
BackwardGaussSeidel()
constructor
Definition: gauss_seidel.h:328
GaussSeidelBase< TAlgebra > base_type
Definition: gauss_seidel.h:321
virtual SmartPtr< ILinearIterator< vector_type > > clone()
Clone.
Definition: gauss_seidel.h:336
BackwardGaussSeidel(const BackwardGaussSeidel< TAlgebra > &parent)
clone constructor
Definition: gauss_seidel.h:331
TAlgebra::vector_type vector_type
Definition: gauss_seidel.h:319
virtual void step(const matrix_type &A, vector_type &c, const vector_type &d, const number relax)
Definition: gauss_seidel.h:342
virtual void set_debug(SmartPtr< IDebugWriter< algebra_type > > spDebugWriter)
set debug writer
Definition: debug_writer.h:384
void write_debug(const matrix_type &mat, const char *filename)
write debug output for a matrix (if debug writer set)
Definition: debug_writer.h:399
TAlgebra::vector_type vector_type
type of vector
Definition: debug_writer.h:360
SmartPtr< IDebugWriter< algebra_type > > debug_writer()
returns the debug writer
Definition: debug_writer.h:391
TAlgebra::matrix_type matrix_type
type of matrix
Definition: debug_writer.h:363
Definition: gauss_seidel.h:52
IPreconditioner< TAlgebra > base_type
Base type.
Definition: gauss_seidel.h:67
void enable_overlap(bool enable)
Definition: gauss_seidel.h:101
void enable_consistent_interfaces(bool enable)
activates the new parallelization approach (disabled by default)
Definition: gauss_seidel.h:99
GaussSeidelBase(const GaussSeidelBase< TAlgebra > &parent)
clone constructor
Definition: gauss_seidel.h:86
void set_sor_relax(number relaxFactor)
set relaxation parameter to define a SOR-method
Definition: gauss_seidel.h:96
bool m_bConsistentInterfaces
Definition: gauss_seidel.h:245
number m_relax
relaxation parameter
Definition: gauss_seidel.h:239
bool m_useOverlap
Definition: gauss_seidel.h:246
TAlgebra::vector_type vector_type
Definition: gauss_seidel.h:58
void set_ordering_algorithm(SmartPtr< ordering_algo_type > ordering_algo)
sets an ordering algorithm
Definition: gauss_seidel.h:104
IPreconditioner< TAlgebra >::matrix_operator_type matrix_operator_type
Matrix Operator type.
Definition: gauss_seidel.h:64
virtual bool step(SmartPtr< MatrixOperator< matrix_type, vector_type > > pOp, vector_type &c, const vector_type &d)
computes a new correction c = B*d
Definition: gauss_seidel.h:163
GaussSeidelBase()
Definition: gauss_seidel.h:80
vector_type m_oD
for overlaps only
Definition: gauss_seidel.h:242
TAlgebra algebra_type
Definition: gauss_seidel.h:55
virtual const char * name() const =0
returns the name of iterator
SmartPtr< ordering_algo_type > m_spOrderingAlgo
for ordering algorithms
Definition: gauss_seidel.h:250
TAlgebra::matrix_type matrix_type
Definition: gauss_seidel.h:61
IOrderingAlgorithm< TAlgebra, ordering_container_type > ordering_algo_type
Definition: gauss_seidel.h:71
virtual bool postprocess()
cleans the operator
Definition: gauss_seidel.h:158
vector_type m_oC
Definition: gauss_seidel.h:243
virtual bool preprocess(SmartPtr< MatrixOperator< matrix_type, vector_type > > pOp)
initializes the preconditioner
Definition: gauss_seidel.h:114
matrix_type m_A
Definition: gauss_seidel.h:235
virtual void step(const matrix_type &A, vector_type &c, const vector_type &d, const number relax)=0
std::vector< size_t > ordering_container_type
Ordering type.
Definition: gauss_seidel.h:70
virtual bool supports_parallel() const
returns if parallel solving is supported
Definition: gauss_seidel.h:111
Gauss-Seidel preconditioner for the 'forward' ordering of the dofs.
Definition: gauss_seidel.h:272
GaussSeidel(const GaussSeidel< TAlgebra > &parent)
clone constructor
Definition: gauss_seidel.h:286
GaussSeidelBase< TAlgebra > base_type
Definition: gauss_seidel.h:276
TAlgebra::vector_type vector_type
Definition: gauss_seidel.h:274
virtual SmartPtr< ILinearIterator< vector_type > > clone()
Clone.
Definition: gauss_seidel.h:291
virtual const char * name() const
returns the name of iterator
Definition: gauss_seidel.h:280
TAlgebra algebra_type
Definition: gauss_seidel.h:273
GaussSeidel()
constructor
Definition: gauss_seidel.h:283
virtual void step(const matrix_type &A, vector_type &c, const vector_type &d, const number relax)
Definition: gauss_seidel.h:297
TAlgebra::matrix_type matrix_type
Definition: gauss_seidel.h:275
Definition: IOrderingAlgorithm.h:52
describes a linear iterator that is based on a matrix operator
Definition: preconditioner.h:103
Definition: matrix_operator.h:49
Definition: gauss_seidel.h:351
virtual SmartPtr< ILinearIterator< vector_type > > clone()
Clone.
Definition: gauss_seidel.h:370
SymmetricGaussSeidel(const SymmetricGaussSeidel< TAlgebra > &parent)
clone constructor
Definition: gauss_seidel.h:365
TAlgebra::matrix_type matrix_type
Definition: gauss_seidel.h:354
GaussSeidelBase< TAlgebra > base_type
Definition: gauss_seidel.h:355
SymmetricGaussSeidel()
constructor
Definition: gauss_seidel.h:362
virtual void step(const matrix_type &A, vector_type &c, const vector_type &d, const number relax)
Definition: gauss_seidel.h:376
virtual const char * name() const
returns the name of iterator
Definition: gauss_seidel.h:359
TAlgebra::vector_type vector_type
Definition: gauss_seidel.h:353
TAlgebra algebra_type
Definition: gauss_seidel.h:352
void SetLayoutValues(TVector *pVec, const IndexLayout &layout, typename TVector::value_type val)
sets the values of a vector to a given number only on the layout indices
Definition: parallelization_util.h:315
void MatMakeConsistentOverlap0(TMatrix &mat)
Generates a set of unique global algebra ids.
Definition: parallelization_util.h:126
@ PST_CONSISTENT
Definition: parallel_storage_type.h:68
@ PST_UNIQUE
Definition: parallel_storage_type.h:70
@ PST_ADDITIVE
Definition: parallel_storage_type.h:69
bool CheckDiagonalInvertible(const TSparseMatrix &A)
Definition: sparsematrix_util.h:1094
void gs_step_UR(const Matrix_type &A, Vector_type &c, const Vector_type &d, const number relaxFactor)
Performs a backward gauss-seidel-step, that is, solve on the upper right of A. Using gs_step_UR withi...
Definition: core_smoothers.h:150
void gs_step_LL(const Matrix_type &A, Vector_type &c, const Vector_type &d, const number relaxFactor)
Gauss-Seidel-Iterations.
Definition: core_smoothers.h:106
void SetDirichletRow(TSparseMatrix &A, size_t i, size_t alpha)
Definition: sparsematrix_util.h:796
void sgs_step(const Matrix_type &A, Vector_type &c, const Vector_type &d, const number relaxFactor)
Performs a symmetric gauss-seidel step. Using sgs_step within a preconditioner-scheme leads to the fa...
Definition: core_smoothers.h:189
int NumProcs()
returns the number of processes
Definition: pcl_base.cpp:91
void CollectUniqueElements(std::vector< typename TLayout::Element > &elemsOut, const TLayout &layout)
writes all elements in the interfaces into the resulting vector. avoids doubles.
Definition: pcl_layout_util.h:142
#define THROW_IF_NOT_EQUAL_4(s1, s2, s3, s4)
Definition: error.h:183
#define THROW_IF_NOT_EQUAL_3(s1, s2, s3)
Definition: error.h:182
#define THROW_IF_NOT_EQUAL(s1, s2)
Definition: error.h:181
#define UG_COND_THROW(cond, msg)
UG_COND_THROW(cond, msg) : performs a UG_THROW(msg) if cond == true.
Definition: error.h:61
double number
Definition: types.h:124
the ug namespace
bool MakeConsistent(const ParallelMatrix< matrix_type > &_mat, ParallelMatrix< matrix_type > &newMat)
Definition: parallel_matrix_overlap_impl.h:438
void CreateOverlap(TMatrix &matInOut)
Adds coefficients and connections to create an overlapping matrix.
Definition: matrix_overlap_impl.h:434
#define PROFILE_BEGIN_GROUP(name, groups)
Definition: profiler.h:255
SmartPtr< T, FreePolicy > make_sp(T *inst)
returns a SmartPtr for the passed raw pointer
Definition: smart_pointer.h:836