FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
amavanka.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#include <kernel/lafem/null_matrix.hpp>
9#include <kernel/lafem/sparse_matrix_bcsr.hpp>
10#include <kernel/lafem/sparse_matrix_csr.hpp>
11#include <kernel/lafem/saddle_point_matrix.hpp>
12#include <kernel/lafem/tuple_matrix.hpp>
13#include <kernel/global/matrix.hpp>
14#include <kernel/solver/base.hpp>
15#include <kernel/solver/amavanka_base.hpp>
16#include <kernel/util/stop_watch.hpp>
17
18namespace FEAT
19{
20 namespace Solver
21 {
62 template<typename Matrix_, typename Filter_>
63 class AmaVanka :
64 public Solver::SolverBase<typename Matrix_::VectorTypeL>
65 {
66 public:
69
71 typedef typename Matrix_::DataType DataType;
73 typedef typename Matrix_::IndexType IndexType;
75 typedef typename Matrix_::VectorTypeL VectorType;
77 typedef typename Intern::AmaVankaMatrixHelper<Matrix_>::VankaMatrix VankaMatrixType;
78
79 protected:
81 const Matrix_& _matrix;
83 const Filter_& _filter;
91 std::vector<Adjacency::Graph> _macro_dofs, _dof_macros;
93 std::vector<int> _macro_mask;
100
101 // stop watch for symbolic factorization
102 StopWatch watch_init_symbolic;
103 // stop watch for numeric factorization
104 StopWatch watch_init_numeric;
105 // stop watch for apply time
106 StopWatch watch_apply;
107
110
111 public:
127 explicit AmaVanka(const Matrix_& matrix, const Filter_& filter,
128 const DataType omega = DataType(1), const Index num_steps = Index(1)) :
129 _matrix(matrix),
130 _filter(filter),
131 _vanka(),
132 _auto_macros(true),
133 _skip_singular(false),
134 _num_steps(num_steps),
135 _omega(omega)
136 {
137 }
138
139 VankaMatrixType& get_vanka_matrix()
140 {
141 return _vanka;
142 }
143
144 const VankaMatrixType& get_vanka_matrix() const
145 {
146 return _vanka;
147 }
148
153 {
154 this->_macro_dofs.clear();
155 _auto_macros = true;
156 }
157
165 {
166 _auto_macros = false;
167
168 // make sure that we do not push more graphs than we have blocks
169 if(int(_macro_dofs.size()) >= Intern::AmaVankaMatrixHelper<Matrix_>::num_blocks)
170 XABORTM("all macro-dofs graphs have already been added");
171
172 // make sure that the number of macros matches our previous graph
173 if(!_macro_dofs.empty() && (_macro_dofs.back().get_num_nodes_domain() != dofs.get_num_nodes_domain()))
174 XABORTM("macro count mismatch");
175
176 // push graph into the list
177 _macro_dofs.emplace_back(std::forward<Adjacency::Graph>(dofs));
178
179 // sort the dof indices
180 _macro_dofs.back().sort_indices();
181 }
182
189 void set_num_steps(Index num_steps)
190 {
191 XASSERT(num_steps > Index(0));
192 this->_num_steps = num_steps;
193 }
194
201 void set_omega(DataType omega)
202 {
203 XASSERT(omega > DataType(0));
204 this->_omega = omega;
205 }
206
213 void set_skip_singular(bool skip_sing)
214 {
215 this->_skip_singular = skip_sing;
216 }
217
221 std::size_t bytes() const
222 {
223 std::size_t s = _vanka.bytes();
224 for(const auto& g : _macro_dofs)
225 s += sizeof(Index) * std::size_t(g.get_num_nodes_domain() + g.get_num_indices());
226 for(const auto& g : _dof_macros)
227 s += sizeof(Index) * std::size_t(g.get_num_nodes_domain() + g.get_num_indices());
228 s += _vec_c.bytes();
229 s += _vec_d.bytes();
230 return s;
231 }
232
239 std::size_t data_size() const
240 {
241 return std::size_t(_vanka.num_nzes_raw());
242 }
243
248 {
249 watch_init_symbolic.reset();
250 watch_init_numeric.reset();
251 watch_apply.reset();
252 }
253
257 double time_init_symbolic() const
258 {
259 return watch_init_symbolic.elapsed();
260 }
261
265 double time_init_numeric() const
266 {
267 return watch_init_numeric.elapsed();
268 }
269
273 double time_apply() const
274 {
275 return watch_apply.elapsed();
276 }
277
279 virtual String name() const override
280 {
281 return "AmaVanka";
282 }
283
285 virtual void init_symbolic() override
286 {
287 watch_init_symbolic.start();
288
290
291 // we also need two vectors if we have to perform multiple steps
292 if(this->_num_steps > Index(1))
293 {
294 this->_vec_c = this->_matrix.create_vector_l();
295 this->_vec_d = this->_matrix.create_vector_l();
296 }
297
298 // automatically deduct macros?
299 if(this->_auto_macros)
300 {
301 // try to deduct macros by pressure matrices in SaddlePointMatrix
302 if(!Intern::AmaVankaCore::deduct_macro_dofs(this->_matrix, this->_macro_dofs))
303 XABORTM("Cannot auto-deduct macros for this matrix type");
304 }
305
306 // make sure we have one macro-dof graph for each matrix block
307 XASSERTM(int(_macro_dofs.size()) == Intern::AmaVankaMatrixHelper<Matrix_>::num_blocks,
308 "invalid number of macro-dof graphs; did you push all of them?");
309
310 // compute dof-macro graphs by transposing
311 this->_dof_macros.resize(this->_macro_dofs.size());
312 for(std::size_t i(0); i < this->_macro_dofs.size(); ++i)
313 {
314 // ensure that we have the same number of macros in all graphs
315 XASSERT(this->_macro_dofs.at(i).get_num_nodes_domain() == this->_macro_dofs.front().get_num_nodes_domain());
316
317 // transpose macro-dofs graph
318 this->_dof_macros.at(i) = Adjacency::Graph(Adjacency::RenderType::transpose, this->_macro_dofs.at(i));
319 }
320
321 // allocate macro skip mask?
322 if(this->_skip_singular)
323 this->_macro_mask.resize(this->_macro_dofs.front().get_num_nodes_domain(), 0);
324
325 Solver::Intern::AmaVankaCore::alloc(this->_vanka, this->_dof_macros, this->_macro_dofs, Index(0), Index(0));
326 this->get_vanka_matrix().init_workspace();
327
328 watch_init_symbolic.stop();
329 }
330
332 virtual void done_symbolic() override
333 {
334 this->get_vanka_matrix().reset_workspace();
335 this->_vanka.clear();
336 this->_macro_mask.clear();
337 this->_dof_macros.clear();
338 if(this->_auto_macros)
339 this->_macro_dofs.clear();
340 if(this->_num_steps > Index(1))
341 {
342 this->_vec_d.clear();
343 this->_vec_c.clear();
344 }
346 }
347
349 virtual void init_numeric() override
350 {
351 const DataType eps = Math::eps<DataType>();
352
353 watch_init_numeric.start();
355
356 this->_vanka.format();
357
358 // get maximum macro size
359 const Index num_macros = Index(this->_macro_dofs.front().get_num_nodes_domain());
360 const Index stride = Intern::AmaVankaCore::calc_stride(this->_vanka, this->_macro_dofs);
361
362 FEAT_PRAGMA_OMP(parallel)
363 {
364 // allocate arrays for local matrix
365 std::vector<DataType> vec_local(stride*stride, DataType(0)), vec_local_t(stride*stride, DataType(0));
366 std::vector<Index> vec_pivot(stride);
367 DataType* local = vec_local.data();
368 DataType* local_t = vec_local_t.data();
369 Index* pivot = vec_pivot.data();
370
371 // loop over all macros
372 FEAT_PRAGMA_OMP(for)
373 for(Index imacro = 0; imacro < num_macros; ++imacro)
374 {
375 // gather local matrix
376 const std::pair<Index,Index> nrc = Intern::AmaVankaCore::gather(this->_matrix,
377 local, stride, imacro, this->_macro_dofs, Index(0), Index(0), Index(0), Index(0));
378
379 // make sure we have gathered a square matrix
380 XASSERTM(nrc.first == nrc.second, "local matrix is not square");
381
382 // assume a non-singular matrix
383 bool singular = false;
384
385 // do we check for singular macros?
386 if(this->_skip_singular)
387 {
388 // the approach used for checking the regularity of the local matrix is to check whether
389 //
390 // || I - A*A^{-1} ||_F^2 < eps
391 //
392 // we could try to analyse the pivots returned by invert_matrix function instead, but
393 // unfortunately this approach sometimes leads to false positives
394
395 // make a backup if checking for singularity
396 for(Index i(0); i < nrc.first; ++i)
397 for(Index j(0); j < nrc.second; ++j)
398 local_t[i*stride+j] = local[i*stride+j];
399
400 // invert local matrix
401 Math::invert_matrix(nrc.first, stride, local, pivot);
402
403 // compute (squared) Frobenius norm of (I - A*A^{-1})
404 DataType norm = DataType(0);
405 for(Index i(0); i < nrc.first; ++i)
406 {
407 for(Index j(0); j < nrc.first; ++j)
408 {
409 DataType xij = DataType(i == j ? 1 : 0);
410 for(Index k(0); k < nrc.first; ++k)
411 xij -= local_t[i*stride+k] * local[k*stride+j]; // A_ik * (A^{-1})_kj
412 norm += xij * xij;
413 }
414 }
415
416 // is the matrix block singular?
417 // Note: we check for !(norm < eps) instead of (norm >= eps),
418 // because the latter one evaluates to false if norm is NaN,
419 // which would result in a false negative
420 singular = !(norm < eps);
421
422 // set macro regularity mask
423 this->_macro_mask[imacro] = (singular ? 0 : 1);
424 }
425 else // no singularity check
426 {
427 // invert local matrix
428 Math::invert_matrix(nrc.first, stride, local, pivot);
429 }
430
431 // scatter local matrix
432 if(!singular)
433 {
434 FEAT_PRAGMA_OMP(critical)
435 {
436 Intern::AmaVankaCore::scatter_add(this->_vanka, local, stride, imacro, this->_macro_dofs,
437 Index(0), Index(0), Index(0), Index(0));
438 }
439 }
440
441 // reformat local matrix
442 for(Index i(0); i < nrc.first; ++i)
443 for(Index j(0); j < nrc.second; ++j)
444 local[i*stride+j] = DataType(0);
445 } // #pragma omp for
446 } // #pragma omp parallel
447
448 // scale rows of Vanka matrix
449 Solver::Intern::AmaVankaCore::scale_rows(this->_vanka, this->_omega, this->_dof_macros, this->_macro_mask, Index(0), Index(0));
450
451 watch_init_numeric.stop();
452 }
453
454 virtual void done_numeric() override
455 {
456 }
457
459 virtual Status apply(VectorType& vec_x, const VectorType& vec_b) override
460 {
461 watch_apply.start();
462
463 // first step
464 this->_vanka.apply(vec_x, vec_b);
465 this->_filter.filter_cor(vec_x);
466
467 // steps 2, ..., n (if any)
468 for(Index step(1); step < _num_steps; ++step)
469 {
470 // compute defect
471 this->_matrix.apply(this->_vec_d, vec_x, vec_b, -DataType(1));
472 // filter defect
473 this->_filter.filter_def(this->_vec_d);
474 // apply Vanka matrix
475 this->_vanka.apply(this->_vec_c, this->_vec_d);
476 // filter correct
477 this->_filter.filter_cor(this->_vec_c);
478 // update solution
479 vec_x.axpy(this->_vec_c);
480 }
481
482 watch_apply.stop();
483
484 return Status::success;
485 }
486
487 /*bool compare(const AmaVanka* other) const
488 {
489 return Intern::AmaVankaMatrixHelper<VankaMatrixType>::compare(this->_vanka, other->_vanka);
490 }*/
491
492 }; // class AmaVanka
493
512 template<typename Matrix_, typename Filter_>
513 std::shared_ptr<AmaVanka<Matrix_, Filter_>> new_amavanka(const Matrix_& matrix, const Filter_& filter,
514 typename Matrix_::DataType omega = typename Matrix_::DataType(1), Index num_steps = Index(1))
515 {
516 return std::make_shared<AmaVanka<Matrix_, Filter_>>(matrix, filter, omega, num_steps);
517 }
518 } // namespace Solver
519} // namespace FEAT
#define XABORTM(msg)
Abortion macro definition with custom message.
Definition: assertion.hpp:192
#define XASSERT(expr)
Assertion macro definition.
Definition: assertion.hpp:262
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
Adjacency Graph implementation.
Definition: graph.hpp:34
Additive Macro-wise Matrix-based Vanka preconditioner/smoother.
Definition: amavanka.hpp:65
std::size_t bytes() const
Returns the total number of bytes currently allocated in this object.
Definition: amavanka.hpp:221
virtual String name() const override
Returns the name of the solver.
Definition: amavanka.hpp:279
const Matrix_ & _matrix
the system matrix
Definition: amavanka.hpp:81
std::vector< int > _macro_mask
the macro mask
Definition: amavanka.hpp:93
Matrix_::DataType DataType
our data type
Definition: amavanka.hpp:71
virtual Status apply(VectorType &vec_x, const VectorType &vec_b) override
applies the preconditioner
Definition: amavanka.hpp:459
double time_apply() const
Returns the total accumulated time for the solver application.
Definition: amavanka.hpp:273
Index _num_steps
number of steps
Definition: amavanka.hpp:95
void set_omega(DataType omega)
Sets the damping parameter omega.
Definition: amavanka.hpp:201
VectorType _vec_c
temporary vectors
Definition: amavanka.hpp:99
Matrix_::IndexType IndexType
our index type
Definition: amavanka.hpp:73
bool _auto_macros
deduct macro dofs automatically?
Definition: amavanka.hpp:87
DataType _omega
damping parameter
Definition: amavanka.hpp:97
bool _skip_singular
skip singular macros?
Definition: amavanka.hpp:89
int _num_threads
number of threads for numeric factorization
Definition: amavanka.hpp:109
virtual void init_numeric() override
Performs numeric factorization.
Definition: amavanka.hpp:349
void reset_timings()
Resets the internal stop watches for time measurement.
Definition: amavanka.hpp:247
virtual void init_symbolic() override
Performs symbolic factorization.
Definition: amavanka.hpp:285
double time_init_symbolic() const
Returns the total accumulated time for symbolic initialization.
Definition: amavanka.hpp:257
std::size_t data_size() const
Returns the total data size used by the AmaVanka smoother.
Definition: amavanka.hpp:239
void set_skip_singular(bool skip_sing)
Sets whether singular macros are to be skipped.
Definition: amavanka.hpp:213
const Filter_ & _filter
the system filter
Definition: amavanka.hpp:83
Matrix_::VectorTypeL VectorType
our vector type
Definition: amavanka.hpp:75
AmaVanka(const Matrix_ &matrix, const Filter_ &filter, const DataType omega=DataType(1), const Index num_steps=Index(1))
Constructor.
Definition: amavanka.hpp:127
double time_init_numeric() const
Returns the total accumulated time for numeric initialization.
Definition: amavanka.hpp:265
VankaMatrixType _vanka
the Vanka preconditioner matrix
Definition: amavanka.hpp:85
virtual void done_numeric() override
Numeric finalization method.
Definition: amavanka.hpp:454
virtual void done_symbolic() override
Releases the symbolic factorization data.
Definition: amavanka.hpp:332
void set_num_steps(Index num_steps)
Sets the number of smoothing steps.
Definition: amavanka.hpp:189
Solver::SolverBase< typename Matrix_::VectorTypeL > BaseClass
our base-class
Definition: amavanka.hpp:68
void push_macro_dofs(Adjacency::Graph &&dofs)
Pushes the dofs-at-macro graph of the next block.
Definition: amavanka.hpp:164
void clear_macro_dofs()
Clears the macro dofs graphs.
Definition: amavanka.hpp:152
Intern::AmaVankaMatrixHelper< Matrix_ >::VankaMatrix VankaMatrixType
the type of our Vanka matrix
Definition: amavanka.hpp:77
std::vector< Adjacency::Graph > _macro_dofs
the DOF-macro graphs
Definition: amavanka.hpp:91
Polymorphic solver interface.
Definition: base.hpp:183
virtual void init_symbolic()
Symbolic initialization method.
Definition: base.hpp:227
virtual void init_numeric()
Numeric initialization method.
Definition: base.hpp:237
virtual void done_symbolic()
Symbolic finalization method.
Definition: base.hpp:255
Stop-Watch class.
Definition: stop_watch.hpp:21
double elapsed() const
Returns the total elapsed time in seconds.
Definition: stop_watch.hpp:70
void start()
Starts the stop-watch.
Definition: stop_watch.hpp:43
void reset()
Resets the elapsed time.
Definition: stop_watch.hpp:36
void stop()
Stops the stop-watch and increments elapsed time.
Definition: stop_watch.hpp:51
String class implementation.
Definition: string.hpp:47
@ transpose
Render-Transpose mode.
DT_ invert_matrix(const IT_ n, const IT_ stride, DT_ a[], IT_ p[])
Inverts a matrix and returns its determinant.
Definition: math.hpp:1292
std::shared_ptr< AmaVanka< Matrix_, Filter_ > > new_amavanka(const Matrix_ &matrix, const Filter_ &filter, typename Matrix_::DataType omega=typename Matrix_::DataType(1), Index num_steps=Index(1))
Creates a new AmaVanka smoother object.
Definition: amavanka.hpp:513
Status
Solver status return codes enumeration.
Definition: base.hpp:47
@ success
solving successful (convergence criterion fulfilled)
FEAT namespace.
Definition: adjactor.hpp:12
std::uint64_t Index
Index data type.