FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
idrs.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
8// includes, FEAT
9#include <kernel/solver/iterative.hpp>
10#include <vector>
11#include <kernel/global/vector.hpp>
12#include <kernel/lafem/dense_matrix.hpp>
13#include <kernel/lafem/dense_vector.hpp>
14#include <kernel/util/random.hpp>
15#include <kernel/util/dist.hpp>
16namespace FEAT
17{
18 namespace Solver
19 {
20
38 template<
39 typename Matrix_,
40 typename Filter_>
41 class IDRS :
42 public PreconditionedIterativeSolver<typename Matrix_::VectorTypeR>
43 {
44 public:
45 typedef Matrix_ MatrixType;
46 typedef Filter_ FilterType;
47 typedef typename MatrixType::VectorTypeR VectorType;
48 typedef typename MatrixType::DataType DataType;
49 typedef typename MatrixType::IndexType IndexType;
51
55
56 protected:
58 const MatrixType& _system_matrix;
60 const FilterType& _system_filter;
64 VectorType _vec_q, _vec_r, _vec_v, _vec_t;
66 VectorType _vec_res;
68 std::vector<VectorType> _vec_P, _vec_dR, _vec_dX;
70 DMatrix _dmat_M, _dmat_Minv;
72 DVector _dvec_m, _dvec_dm, _dvec_c;
73 bool _shadow_space_setup, _random;
74
75 public:
91 explicit IDRS(const MatrixType& matrix, const FilterType& filter, Index krylov_dim,
92 std::shared_ptr<PrecondType> precond = nullptr) :
93 BaseClass("IDRS(" + stringify(krylov_dim) + ")", precond),
94 _system_matrix(matrix),
95 _system_filter(filter),
96 _krylov_dim(krylov_dim),
97 _shadow_space_setup(false),
98 _random(true)
99 {
100 // set communicator by system matrix
101 this->_set_comm_by_matrix(matrix);
102 }
103
104 explicit IDRS(const String& section_name, const PropertyMap* section,
105 const MatrixType& matrix, const FilterType& filter, std::shared_ptr<PrecondType> precond = nullptr) :
106 BaseClass("IDRS", section_name, section, precond),
107 _system_matrix(matrix),
108 _system_filter(filter),
109 _shadow_space_setup(false),
110 _random(true)
111 {
112 // set communicator by system matrix
113 this->_set_comm_by_matrix(matrix);
114
115 // Check if we have set _krylov_vim
116 auto krylov_dim_p = section->query("krylov_dim");
117 if(!krylov_dim_p.second)
118 throw ParseError("IDRS config section is missing the mandatory krylov_dim!");
119
120 if(!krylov_dim_p.first.parse(this->_krylov_dim) || (this->_krylov_dim <= Index(0)))
121 throw ParseError(section_name + ".krylov_dim", krylov_dim_p.first, "a positive integer");
122
123 this->set_plot_name("IDRS("+stringify(_krylov_dim)+")");
124 }
125
129 virtual ~IDRS()
130 {
131 }
132
134 virtual String name() const override
135 {
136 return "IDRS";
137 }
138
139 virtual void init_symbolic() override
140 {
142
145 _dvec_dm = DVector(_krylov_dim);
146 _dvec_c = DVector(_krylov_dim);
147 _dmat_M.format();
148
149 _vec_q = this->_system_matrix.create_vector_r();
150 _vec_r = _vec_q.clone(LAFEM::CloneMode::Layout);
152 _vec_t = _vec_q.clone(LAFEM::CloneMode::Layout);
153 _vec_v = _vec_q.clone(LAFEM::CloneMode::Layout);
154 _vec_dX.reserve(_krylov_dim);
155 _vec_dR.reserve(_krylov_dim);
156 _vec_P.reserve(_krylov_dim);
157
158 for(Index i(0); i < _krylov_dim; ++i)
159 {
160 _vec_dX.push_back(this->_vec_q.clone(LAFEM::CloneMode::Layout));
161 _vec_dR.push_back(this->_vec_q.clone(LAFEM::CloneMode::Layout));
162 _vec_P.push_back( this->_vec_q.clone(LAFEM::CloneMode::Layout));
163 }
164 }
165
166 virtual void done_symbolic() override
167 {
168 _vec_q.clear();
169 _vec_r.clear();
170 _vec_res.clear();
171 _vec_t.clear();
172 _vec_v.clear();
173
174 _vec_dX.clear();
175 _vec_dR.clear();
176 _vec_P.clear();
177
179 }
180
188 virtual void set_krylov_dim(Index krylov_dim)
189 {
190 XASSERT(krylov_dim > Index(0));
191 _krylov_dim = krylov_dim;
192 }
193
195 virtual Status apply(VectorType& vec_sol, const VectorType& vec_rhs) override
196 {
197 // save input rhs vector as initial defect
198 this->_vec_res.copy(vec_rhs);
199
200 // clear solution vector
201 vec_sol.format();
202
203 // apply solver
204 this->_status = _apply_intern(vec_sol, vec_rhs);
205
206 // plot summary
207 this->plot_summary();
208
209 // return status
210 return this->_status;
211 }
212
214 virtual Status correct(VectorType& vec_sol, const VectorType& vec_rhs) override
215 {
216 // compute initial defect
217 this->_system_matrix.apply(this->_vec_res, vec_sol, vec_rhs, -DataType(1));
218 this->_system_filter.filter_def(this->_vec_res);
219
220 // apply solver
221 this->_status = _apply_intern(vec_sol, vec_rhs);
222
223 // plot summary
224 this->plot_summary();
225
226 // return status
227 return this->_status;
228 }
229
236 void reset_shadow_space( bool random = true)
237 {
238 _random = random;
239 _shadow_space_setup = false;
240 }
241
242
243
244 protected:
245
246 void _set_shadow_space()
247 {
248 // select set of random vectors
249 Random rng;
250
251 // use world comm rank as seed unless a truly random seed is desired
252 if(!_random)
253 rng = Random(Random::SeedType(Dist::Comm::world().rank()+1));
254
255 for (Index l(0); l<_krylov_dim; ++l)
256 _vec_P.at(l).format(rng, DataType(0), DataType(1));
257
258 // orthonormalize vector set P (modified Gram-Schmidt)
259 for (Index j(0); j<_krylov_dim; ++j)
260 {
261 for (Index i(0); i<j; ++i)
262 {
263 _vec_P.at(j).axpy(_vec_P.at(i), -_vec_P.at(j).dot(_vec_P.at(i)));
264 }
265 _vec_P.at(j).scale(_vec_P.at(j), DataType(1)/_vec_P.at(j).norm2());
266 }
267 _shadow_space_setup = true;
268 }
269
270 virtual Status _apply_intern(VectorType& vec_sol, const VectorType& DOXY(vec_rhs))
271 {
272 IterationStats pre_iter(*this);
273 Statistics::add_solver_expression(std::make_shared<ExpressionStartSolve>(this->name()));
274 const MatrixType& matrix(this->_system_matrix);
275 const FilterType& filter(this->_system_filter);
276
277 // compute initial defect
278 Status status = this->_set_initial_defect(this->_vec_res, vec_sol);
279 _vec_t.copy(_vec_res);
280 if(!this->_apply_precond(this->_vec_r, this->_vec_t, filter))
281 {
282 pre_iter.destroy();
283 Statistics::add_solver_expression(std::make_shared<ExpressionEndSolve>(this->name(), Status::aborted, this->get_num_iter()));
284 return Status::aborted;
285 }
286 //select random vector set (shadow space)
287 if (!_shadow_space_setup)
288 _set_shadow_space();
289
290 pre_iter.destroy();
291
292 //produce start vectors
293 IterationStats first_iter(*this);
294 DataType om(0);
295 for (Index k(0); k < _krylov_dim; ++k)
296 {
297 matrix.apply(_vec_v, _vec_r);
298 filter.filter_def(_vec_v);
299 //save unpreconditioned residual update
300 _vec_t.copy(_vec_v);
301 if(!this->_apply_precond(this->_vec_v, this->_vec_t, filter))
302 {
303 first_iter.destroy();
304 Statistics::add_solver_expression(std::make_shared<ExpressionEndSolve>(this->name(), Status::aborted, this->get_num_iter()));
305 return Status::aborted;
306 }
307 om = _vec_v.dot(_vec_r) / _vec_v.dot(_vec_v);
308 _vec_dX.at(k).scale(_vec_r, om);
309 _vec_dR.at(k).scale(_vec_v, -om);
310 vec_sol.axpy(_vec_dX.at(k));
311 _vec_r.axpy(_vec_dR.at(k));
312 _vec_res.axpy(_vec_t, -om);
313
314 // push our new defect
315 status = this->_set_new_defect(_vec_res, vec_sol);
316
317 //check for early convergence
318 if (status != Status::progress)
319 {
320 Statistics::add_solver_expression(std::make_shared<ExpressionEndSolve>(this->name(), status, this->get_num_iter()));
321 return status;
322 }
323
324 // update k-th column of M
325 {
326 Memory::TypedView<DataType> dM = _dmat_M.elements_view_rw();
327 for (Index l(0); l < _krylov_dim; ++l)
328 dM[l*_krylov_dim+k] = _vec_P.at(l).dot(_vec_dR.at(k));
329 }
330
331 }
332
333 Index oldest(0);
334 //calculate m
335 {
336 Memory::TypedView<DataType> dm = _dvec_m.elements_view_w();
337 for (Index l(0); l< _krylov_dim; ++l)
338 dm[l] = _vec_P.at(l).dot(_vec_r);
339 }
340
341 first_iter.destroy();
342 // outer loop
343 while(status == Status::progress)
344 {
345 IterationStats stat(*this);
346
347 //inner loop
348 for (Index k(0); k <= _krylov_dim; ++k)
349 {
350 //solve c=M\m
351 _dmat_Minv = _dmat_M.inverse();
352 _dmat_Minv.apply(_dvec_c, _dvec_m);
353
354 {
355 const Memory::TypedView<DataType> dc = _dvec_c.elements_view_r();
356 _vec_q.scale(_vec_dR.at(0), -dc(0));
357 for (Index l(1); l < _krylov_dim; ++l)
358 _vec_q.axpy(_vec_dR.at(l), -dc(l));
359 }
360
361 _vec_v.copy(_vec_q);
362 _vec_v.axpy(_vec_r);
363
364 if (k == 0)
365 {
366 matrix.apply(_vec_dR.at(oldest), _vec_v);
367 filter.filter_def(_vec_dR.at(oldest));
368 if(!this->_apply_precond(this->_vec_t, this->_vec_dR.at(oldest), filter))
369 {
370 stat.destroy();
371 Statistics::add_solver_expression(std::make_shared<ExpressionEndSolve>(this->name(), Status::aborted, this->get_num_iter()));
372 return Status::aborted;
373 }
374 om = _vec_v.dot(_vec_t) / _vec_t.dot(_vec_t);
375 _vec_dR.at(oldest).copy(_vec_q);
376 _vec_dR.at(oldest).axpy(_vec_t, -om);
377
378 //dX(oldest)=-dX*c+om*v
379 {
380 const Memory::TypedView<DataType> dc = _dvec_c.elements_view_r();
381 _vec_dX.at(oldest).scale(_vec_dX.at(oldest), -dc(oldest));
382 for (Index l(0); l < oldest; ++l)
383 _vec_dX.at(oldest).axpy(_vec_dX.at(l), -dc(l));
384 for (Index l(oldest+1); l < _krylov_dim; ++l)
385 _vec_dX.at(oldest).axpy(_vec_dX.at(l), -dc(l));
386 _vec_dX.at(oldest).axpy(_vec_v, om);
387 }
388
389 matrix.apply(_vec_t, _vec_dX.at(oldest));
390 filter.filter_def(_vec_t);
391 }
392 else
393 {
394 //dX(oldest)=-dX*c+om*v
395 {
396 const Memory::TypedView<DataType> dc = _dvec_c.elements_view_r();
397 _vec_dX.at(oldest).scale(_vec_dX.at(oldest), -dc(oldest));
398 for (Index l(0); l < oldest; ++l)
399 _vec_dX.at(oldest).axpy(_vec_dX.at(l), -dc(l));
400 for (Index l(oldest+1); l < _krylov_dim; ++l)
401 _vec_dX.at(oldest).axpy(_vec_dX.at(l), -dc(l));
402 _vec_dX.at(oldest).axpy(_vec_v, om);
403 }
404
405 matrix.apply( _vec_dR.at(oldest), _vec_dX.at(oldest));
406 filter.filter_def(_vec_dR.at(oldest));
407 _vec_t.copy(_vec_dR.at(oldest));
408 if(!this->_apply_precond(this->_vec_dR.at(oldest), this->_vec_t, filter))
409 {
410 stat.destroy();
411 Statistics::add_solver_expression(std::make_shared<ExpressionEndSolve>(this->name(), Status::aborted, this->get_num_iter()));
412 return Status::aborted;
413 }
414 _vec_dR.at(oldest).scale(_vec_dR.at(oldest), DataType(-1));
415 }
416
417
418 _vec_r.axpy(_vec_dR.at(oldest));
419 vec_sol.axpy(_vec_dX.at(oldest));
420 _vec_res.axpy(_vec_t, -1);
421
422 status = this->_set_new_defect(_vec_res, vec_sol);
423 if (status != Status::progress)
424 break;
425
426 {
427 Memory::TypedView<DataType> dM = _dmat_M.elements_view_rw();
428 Memory::TypedView<DataType> dm = _dvec_dm.elements_view_w();
429 for (Index l(0); l < _krylov_dim; ++l)
430 {
431 dM[l*_krylov_dim+oldest] = dm[l] = _vec_P.at(l).dot(_vec_dR.at(oldest));
432 }
433 }
434 _dvec_m.axpy(_dvec_dm);
435 oldest = (oldest+1) % _krylov_dim;
436 } //end inner loop
437
438 } //end outer loop
439 // finished
440 Statistics::add_solver_expression(std::make_shared<ExpressionEndSolve>(this->name(), status, this->get_num_iter()));
441 return status;
442 } //_apply_intern(...)
443 }; // class IDRS<...>
444
463 template<typename Matrix_, typename Filter_>
464 inline std::shared_ptr<IDRS<Matrix_, Filter_>> new_idrs(
465 const Matrix_& matrix, const Filter_& filter, Index krylov_dim,
466 std::shared_ptr<SolverBase<typename Matrix_::VectorTypeL>> precond = nullptr)
467 {
468 return std::make_shared<IDRS<Matrix_, Filter_>>(matrix, filter, krylov_dim, precond);
469 }
470
492 template<typename Matrix_, typename Filter_>
493 inline std::shared_ptr<IDRS<Matrix_, Filter_>> new_idrs(
494 const String& section_name, const PropertyMap* section,
495 const Matrix_& matrix, const Filter_& filter,
496 std::shared_ptr<SolverBase<typename Matrix_::VectorTypeL>> precond = nullptr)
497 {
498 return std::make_shared<IDRS<Matrix_, Filter_>>(section_name, section, matrix, filter, precond);
499 }
500 } // namespace Solver
501} // namespace FEAT
#define XASSERT(expr)
Assertion macro definition.
Definition: assertion.hpp:262
static Comm world()
Returns a copy of the world communicator.
Definition: dist.cpp:432
void format()
Reset all elements of the container to a given value or zero if missing.
Definition: container.hpp:831
Dense data matrix class template.
void apply(DenseVector< DT_, IT_ > &r, const DenseVector< DT_, IT_ > &x) const
Calculate .
DenseMatrix inverse() const
Create an inverse of the current matrix.
Dense data vector class template.
void axpy(const DenseVector &x, const DT_ alpha=DT_(1))
Calculate .
Class for parser related errors.
Definition: exception.hpp:132
A class organizing a tree of key-value pairs.
std::pair< String, bool > query(String key_path) const
Queries a value by its key path.
Pseudo-Random Number Generator.
Definition: random.hpp:54
std::uint64_t SeedType
seed type
Definition: random.hpp:57
IDR(s) solver implementation.
Definition: idrs.hpp:43
virtual ~IDRS()
Empty virtual destructor.
Definition: idrs.hpp:129
virtual Status apply(VectorType &vec_sol, const VectorType &vec_rhs) override
Solver application method.
Definition: idrs.hpp:195
DVector _dvec_m
local ("small") dense vectors
Definition: idrs.hpp:72
void reset_shadow_space(bool random=true)
Reset the shadow space before the next system will be solved.
Definition: idrs.hpp:236
virtual String name() const override
Returns a descriptive string.
Definition: idrs.hpp:134
std::vector< VectorType > _vec_P
n x s "matrices"
Definition: idrs.hpp:68
DMatrix _dmat_M
dense s x s - matrices
Definition: idrs.hpp:70
IDRS(const MatrixType &matrix, const FilterType &filter, Index krylov_dim, std::shared_ptr< PrecondType > precond=nullptr)
Constructor.
Definition: idrs.hpp:91
VectorType _vec_q
auxillary vectors from the algorithm
Definition: idrs.hpp:64
virtual void done_symbolic() override
Symbolic finalization method.
Definition: idrs.hpp:166
virtual Status correct(VectorType &vec_sol, const VectorType &vec_rhs) override
Definition: idrs.hpp:214
const FilterType & _system_filter
the filter for the solver
Definition: idrs.hpp:60
VectorType _vec_res
real (unpreconditioned) residual
Definition: idrs.hpp:66
virtual void set_krylov_dim(Index krylov_dim)
Sets the inner Krylov space dimension.
Definition: idrs.hpp:188
virtual void init_symbolic() override
Symbolic initialization method.
Definition: idrs.hpp:139
const MatrixType & _system_matrix
the matrix for the solver
Definition: idrs.hpp:58
Index _krylov_dim
krylov dimension (s)
Definition: idrs.hpp:62
void set_plot_name(const String &plot_name)
Sets the plot name of the solver.
Definition: iterative.hpp:517
Index get_num_iter() const
Returns number of performed iterations.
Definition: iterative.hpp:462
Status _status
current status of the solver
Definition: iterative.hpp:213
void _set_comm_by_matrix(const Matrix_ &matrix)
Sets the communicator for the solver from a matrix.
Definition: iterative.hpp:680
virtual Status _set_new_defect(const VectorType &vec_def, const VectorType &vec_sol)
Internal function: sets the new (next) defect vector.
Definition: iterative.hpp:924
virtual void plot_summary() const
Plot a summary of the last solver run.
Definition: iterative.hpp:627
virtual Status _set_initial_defect(const VectorType &vec_def, const VectorType &vec_sol)
Internal function: sets the initial defect vector.
Definition: iterative.hpp:802
Abstract base-class for preconditioned iterative solvers.
Definition: iterative.hpp:1027
virtual void init_symbolic() override
Symbolic initialization method.
Definition: iterative.hpp:1086
bool _apply_precond(VectorType &vec_cor, const VectorType &vec_def, const Filter_ &filter)
Applies the preconditioner onto a defect vector.
Definition: iterative.hpp:1140
virtual void done_symbolic() override
Symbolic finalization method.
Definition: iterative.hpp:1110
Polymorphic solver interface.
Definition: base.hpp:183
String class implementation.
Definition: string.hpp:47
Status
Solver status return codes enumeration.
Definition: base.hpp:47
@ progress
continue iteration (internal use only)
@ aborted
premature abort (solver aborted due to internal errors or preconditioner failure)
std::shared_ptr< IDRS< Matrix_, Filter_ > > new_idrs(const Matrix_ &matrix, const Filter_ &filter, Index krylov_dim, std::shared_ptr< SolverBase< typename Matrix_::VectorTypeL > > precond=nullptr)
Creates a new IDR(s) solver object.
Definition: idrs.hpp:464
FEAT namespace.
Definition: adjactor.hpp:12
String stringify(const T_ &item)
Converts an item into a String.
Definition: string.hpp:993
std::uint64_t Index
Index data type.