KASKADE 7 development version
Loading...
Searching...
No Matches
multiplicativeMultigrid.hh
Go to the documentation of this file.
1
2/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
3/* */
4/* This file is part of the library KASKADE 7 */
5/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
6/* */
7/* Copyright (C) 2015-2020 Zuse Institute Berlin */
8/* */
9/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
10/* see $KASKADE/academic.txt */
11/* */
12/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
13
14#ifndef MG_MULTIPLICATIVEMULTIGRID_HH
15#define MG_MULTIPLICATIVEMULTIGRID_HH
16
17#include <type_traits>
18
19#include <boost/timer/timer.hpp>
20
21#include <dune/istl/preconditioner.hh>
22
23#include "fem/spaces.hh"
24#include "linalg/conjugation.hh"
25#include "linalg/direct.hh"
29#include "mg/prolongation.hh"
30#include "utilities/memory.hh"
31#include "utilities/timing.hh"
32
33namespace Kaskade
34{
35
36
37 // ----------------------------------------------------------------------------------------------------------------------
38
75 template <class Entry, class Index, class Smoother, class Prolongation>
77 : public SymmetricPreconditioner<typename Smoother::domain_type,typename Smoother::range_type>
78 {
79 public:
80 using field_type = typename Smoother::field_type;
81 using domain_type = typename Smoother::domain_type;
82 using range_type = typename Smoother::range_type;
83 using CoarsePreconditioner = Dune::Preconditioner<domain_type,range_type>; // symmetric would be better? but elaborate implementation for direct solvers
84
89
99 template <class MakeSmoother>
100 MultiplicativeMultiGrid(MultiGridStack<Prolongation,Entry,Index>&& mgStack_, MakeSmoother const& makeSmoother,
101 std::unique_ptr<CoarsePreconditioner>&& coarsePreconditioner_, int nPre_=3, int nPost_=3)
102 : mgStack(std::move(mgStack_)), nPre(nPre_), nPost(nPost_), coarsePreconditioner(std::move(coarsePreconditioner_)),
103 linesearch(false), smootherStepSize(1.0)
104 {
105 // Create the smoothers for all levels.
106 Timings& timer = Timings::instance();
107 timer.start("smoother construction");
108 for (int l=1; l<mgStack.levels(); ++l)
109 smoothers.push_back(makeSmoother(mgStack.a(l)));
110 timer.stop("smoother construction");
111 }
112
114
116
117
123 virtual void apply(domain_type& x, range_type const& r)
124 {
125 assert(x.two_norm()==0);
126 range_type b = r;
127 runMG(x,b,mgStack.levels()-1);
128 }
129
131 {
132 apply(x,r);
133 return x*r;
134 }
135
136 virtual bool requiresInitializedInput() const
137 {
138 return true;
139 }
140
149 void setSmoothings(int nPre_, int nPost_)
150 {
151 nPre = nPre_;
152 nPost = nPost_;
153 assert(nPre>=0 && nPost>=0 && nPre+nPost>0);
154 }
155
161 void setSmootherStepSize(double w)
162 {
163 assert(w>0);
164 smootherStepSize = w;
165 }
166
167 double getSmootherStepSize() const { return smootherStepSize; }
168
181 void setLinesearch(bool ls)
182 {
183 linesearch = ls;
184 }
185
190 {
191 return *coarsePreconditioner;
192 }
193
197 std::pair<int,int> getSmoothings()
198 {
199 return std::make_pair(nPre,nPost);
200 }
201
202 private:
203 // precondition: x==0
204 void runMG(domain_type& x, range_type& r, int level)
205 {
206 Timings& timer = Timings::instance();
207
208 if (level==0)
209 {
210 timer.start("coarse grid solver");
211 coarsePreconditioner->pre(x,r);
212 coarsePreconditioner->apply(x,r);
213 coarsePreconditioner->post(x);
214 timer.stop("coarse grid solver");
215 }
216 else
217 {
218 auto& s = smoothers[level-1]; // Smoother S
219 auto const& a = mgStack.a(level); // Galerkin matrix A
220 double w = smootherStepSize;
221 domain_type dx(x); // temporary vector
222 range_type adx(r);
223
224 // pre-smoothing
225 bool const sRequiresZeroInput = s.requiresInitializedInput();
226 s.pre(x,r);
227 for (int i=0; i<nPre; ++i)
228 {
229 if (sRequiresZeroInput) dx = 0;
230 timer.start("smoother application");
231 s.apply(dx,r); // dx = B^{-1} r
232 timer.stop("smoother application");
233
234 timer.start("matrix-vector");
235 if (linesearch)
236 {
237 double dxadx = a.mv(dx,adx); // adx = A*dx residual update direction
238 w = dx*r / (dxadx); // w = dx*r / dx*A*dx "optimal" step length
239 r.axpy(-w,adx); // r = r - w*adx update residual
240 }
241 else
242 a.usmv(-w,dx,r); // r = r - w*A*dx update residual (without intermediate adx access)
243 timer.stop("matrix-vector");
244
245 x.axpy(w,dx); // x = x + w*dx update iterate
246 }
247 s.post(x);
248
249 // coarse grid correction
250 auto const& p = mgStack.p(level-1);
251 timer.start("restriction");
252 range_type cr; cr.resize(p.M());
253 p.mtv(r,cr); // cr = P^T r
254 timer.stop("restriction");
255 domain_type cx; cx.resize(p.M()); cx = 0;
256 runMG(cx,cr,level-1); // cx ~ (P^T A P)^{-1} cr
257 timer.start("prolongation");
258 p.mv(cx,dx); // dx = P*cx
259 timer.stop("prolongation");
260 x += dx; // x = x+dx
261 a.usmv(-1,dx,r); // r = r-A*dx
262
263
264 // post-smoothing
265 s.pre(x,r);
266 for (int i=0; i<nPost; ++i)
267 {
268 if (sRequiresZeroInput) dx = 0;
269 timer.start("smoother application");
270 s.apply(dx,r); // dx = B^{-1} r
271 timer.stop("smoother application");
272
273 if (i+1<nPost || linesearch) // update residual only if needed lateron
274 {
275 timer.start("matrix-vector");
276 if (linesearch)
277 {
278 double dxadx = a.mv(dx,adx); // adx = A*dx
279 w = dx*r / (dxadx); // w = dx*r / dx*A*dx
280 r.axpy(-w,adx); // r = r - w*adx
281 }
282 else
283 a.usmv(-w,dx,r); // r = r - w*A*dx
284 timer.stop("matrix-vector");
285 }
286 x.axpy(w,dx); // x = x + w*dx
287 }
288 s.post(x);
289
290 // optional line search
291 if (linesearch)
292 {
293 a.mv(x,dx); // dx = Ax
294 double omega = (x*r) / (dx*x); // omega = r^T x / x^T A x
295 x *= 1+omega; // x = x + omega * x
296 }
297 }
298 }
299
300 MultiGridStack<Prolongation,Entry,Index> mgStack;
301 std::vector<Smoother> smoothers;
302 int nPre, nPost;
303 std::unique_ptr<CoarsePreconditioner> coarsePreconditioner;
304 bool linesearch;
305 double smootherStepSize;
306 };
307
308 // ----------------------------------------------------------------------------------------------------------------------
309
323 template <class Entry, class Index, class Prolongation, class MakeSmoother, class CoarsePreconditioner>
325 MakeSmoother const& makeSmoother,
326 std::unique_ptr<CoarsePreconditioner>&& coarsePreconditioner,
327 int nPre=3, int nPost=3)
328 {
329 using Smoother = std::result_of_t<MakeSmoother(NumaBCRSMatrix<Entry,Index>)>;
331 std::move(mgStack),makeSmoother,std::move(coarsePreconditioner),nPre,nPost);
332 }
333
334 // ----------------------------------------------------------------------------------------------------------------------
335 // ----------------------------------------------------------------------------------------------------------------------
336
342 {
343 public:
344 template <typename Matrix>
345 auto operator()(Matrix const& a) const
346 {
347 return JacobiPreconditioner(a);
348 }
349 };
350
356 template <typename Space, typename Scalar=float>
358 {
359 public:
366 MakeAdditiveSchwarzSmoother(Space const& space_, double targetCondition_=-1.0)
367 : space(space_), targetCondition(targetCondition_)
368 {}
369
370 template <typename Entry, typename Index>
376
377 private:
378 Space const& space;
379 double targetCondition;
380 };
381
386 template <typename Matrix>
391
395 template <class Domain, class Range, class SparseIndex>
396 class DirectPreconditionerFloatWrapper : public Dune::Preconditioner<Domain,Range>
397 {
398 public:
399 using FloatDomain = Domain;
400 using FloatRange = Range;
401 static constexpr int domainBlockSize = Domain::block_type::dimension;
402 static constexpr int rangeBlockSize = Range::block_type::dimension;
407
408 DirectPreconditionerFloatWrapper(FloatMatrix const& matA, DirectType directType) : directSolver(DoubleMatrix(matA), directType) {}
409
410 virtual void pre (FloatDomain &, FloatRange &) {}
411
412 virtual void apply (FloatDomain & x, FloatRange const& b)
413 {
414 DoubleDomain xd(x.size());
415 DoubleRange bd(b.size());
416 auto endBd = vectorToSequence(b, &(bd[0][0]));
417 if(endBd != &(bd[0][0]) + bd.dim()) {
418 throw DirectSolverException("Copying float to double block-vector failed. End Iterators differ.",__FILE__,__LINE__);
419 }
420 directSolver.apply(xd, bd);
421 auto endX = vectorToSequence(xd, &(x[0][0]));
422 if(endX != &(x[0][0]) + x.dim()) {
423 throw DirectSolverException("Copying float to double block-vector failed. End Iterators differ.",__FILE__,__LINE__);
424 }
425 }
426
427 virtual void post (FloatDomain &) {}
428
429 private:
431 };
432
439 template <int domainBlockSize, int rangeBlockSize, class Index>
445
446 // ----------------------------------------------------------------------------------------------------------------------
447 // ----------------------------------------------------------------------------------------------------------------------
448
463 template <class Entry, class Index, class GridMan>
464 auto makeJacobiMultiGrid(NumaBCRSMatrix<Entry,Index> const& A, GridMan const& gridman,
465 int nPre=3, int nPost=3, bool onlyLowerTriangle=false,
466 DirectType directType=DirectType::MUMPS)
467 {
468 Timings& timer = Timings::instance();
469
470 timer.start("MG stack creation");
471 auto mgStack = makeGeometricMultiGridStack(gridman,duplicate(A),onlyLowerTriangle);
472 timer.stop("MG stack creation");
473
474 timer.start("direct solver creation");
475 auto coarsePreconditioner = makeDirectPreconditioner(std::move(mgStack.coarseGridMatrix()),directType);
476 timer.stop("direct solver creation");
477
478 timer.start("MG creation");
479 auto mg = makeMultiplicativeMultiGrid(std::move(mgStack),MakeJacobiSmoother(),
480 moveUnique(std::move(coarsePreconditioner)),nPre,nPost);
481 timer.stop("MG creation");
482 mg.setSmootherStepSize(0.5);
483
484 return mg;
485 }
486
501 template <class Entry, class Index, class Space, class MakeSmoother>
503 MakeSmoother const& makeSmoother, int nPre=3, int nPost=3, bool onlyLowerTriangle=false, DirectType directType=DirectType::MUMPS)
504 {
505 Timings& timer = Timings::instance();
506 timer.start("create P multigrid stack");
507 auto mgStack = makePMultiGridStack(space,std::move(A),onlyLowerTriangle);
508 timer.stop("create P multigrid stack");
509
510 timer.start("create MG as coarse solver");
511 auto coarsePreconditioner = makeJacobiMultiGrid(std::move(mgStack.coarseGridMatrix()),space.gridManager(),nPre,nPost,onlyLowerTriangle,directType);
512 coarsePreconditioner.setSmootherStepSize(0.6);
513 timer.stop("create MG as coarse solver");
514
515 timer.start("create multigrid");
516 auto mg = makeMultiplicativeMultiGrid(std::move(mgStack),makeSmoother,moveUnique(std::move(coarsePreconditioner)),nPre,nPost);
517 timer.stop("create multigrid");
518
519 return mg;
520 }
521
542 template <class Entry, class Index, class Space, class CoarseSpace>
544 NumaBCRSMatrix<Entry,Index> coarseA, CoarseSpace const& coarseSpace,
545 int nPre=3, int nPost=3, DirectType directType=DirectType::MUMPS)
546 {
547 Timings& timer = Timings::instance();
548 timer.start("create P multigrid stack");
549 auto mgStack = makePMultiGridStack(space,std::move(A),coarseSpace,std::move(coarseA));
550 timer.stop("create P multigrid stack");
551
552 timer.start("create MG as coarse solver");
553 auto coarsePreconditioner = makeJacobiMultiGrid(std::move(mgStack.coarseGridMatrix()),space.gridManager(),nPre,nPost,false,directType);
554 coarsePreconditioner.setSmootherStepSize(0.6);
555 timer.stop("create MG as coarse solver");
556
557 timer.start("create multigrid");
558 auto mg = makeMultiplicativeMultiGrid(std::move(mgStack),MakeAdditiveSchwarzSmoother<Space>(space),
559 moveUnique(std::move(coarsePreconditioner)),nPre,nPost);
560 mg.setSmootherStepSize(Space::Grid::dimension==3 ? 0.4 : 0.5);
561 timer.stop("create multigrid");
562
563 return mg;
564 }
565
586 template <class Entry, class Index, class Space>
588 int nPre=3, int nPost=3, bool onlyLowerTriangle=false, DirectType directType=DirectType::MUMPS)
589 {
590 auto mg = makeMultiplicativePMultiGrid(std::move(A),space,MakeAdditiveSchwarzSmoother<Space>(space),
591 nPre,nPost,onlyLowerTriangle,directType);
592 mg.setSmootherStepSize(Space::Grid::dimension==3 ? 0.4 : 0.5);
593 return mg;
594 }
595
608 template <class Entry, class Index, class FineSpace>
609 auto makeJacobiPMultiGrid(NumaBCRSMatrix<Entry,Index> A, FineSpace const& space,
610 int nPre=3, int nPost=3, bool onlyLowerTriangle=false, DirectType directType=DirectType::MUMPS)
611 {
612 auto mg = makeMultiplicativePMultiGrid(std::move(A),space,MakeJacobiSmoother(),nPre,nPost,onlyLowerTriangle,directType);
613 mg.setSmootherStepSize(0.5);
614 return mg;
615 }
616
617
618
619
620 // ---------------------------------------------------------------------------------------------------------
621
622
623}
624
625#endif
virtual void pre(FloatDomain &, FloatRange &)
NumaBCRSMatrix< Dune::FieldMatrix< float, rangeBlockSize, domainBlockSize >, SparseIndex > FloatMatrix
DirectPreconditionerFloatWrapper(FloatMatrix const &matA, DirectType directType)
virtual void apply(FloatDomain &x, FloatRange const &b)
NumaBCRSMatrix< Dune::FieldMatrix< double, rangeBlockSize, domainBlockSize >, SparseIndex > DoubleMatrix
typename MatrixTraits< DoubleMatrix >::NaturalRange DoubleRange
typename MatrixTraits< DoubleMatrix >::NaturalDomain DoubleDomain
To be raised if a direct solver fails.
Functor for creating overlapping Schwarz smoothers.
MakeAdditiveSchwarzSmoother(Space const &space_, double targetCondition_=-1.0)
Constructor.
auto operator()(NumaBCRSMatrix< Entry, Index > const &a) const
Functor for creating Jacobi smoothers.
auto operator()(Matrix const &a) const
Class for multigrid stacks.
CoarsePreconditioner & getCoarsePreconditioner()
Provides access to the coarse grid preconditioner.
void setLinesearch(bool ls)
Enable or disable the line search option.
typename Smoother::domain_type domain_type
typename Smoother::field_type field_type
MultiplicativeMultiGrid(MultiplicativeMultiGrid &&other)=default
MultiplicativeMultiGrid & operator=(MultiplicativeMultiGrid &&other)=default
void setSmoothings(int nPre_, int nPost_)
Sets the number of pre- and post-smoothing iterations to perform.
virtual field_type applyDp(domain_type &x, range_type const &r)
Computes and returns .
Dune::Preconditioner< domain_type, range_type > CoarsePreconditioner
virtual void apply(domain_type &x, range_type const &r)
Application of preconditioner.
typename Smoother::range_type range_type
MultiplicativeMultiGrid()=default
Default constructor.
void setSmootherStepSize(double w)
Define the smoother step size.
virtual bool requiresInitializedInput() const
Returns true if the target vector x has to be initialized to zero before calling apply or applyDp.
std::pair< int, int > getSmoothings()
Returns a pair of number of pre- and post-smoothing iterations to perform.
MultiplicativeMultiGrid(MultiGridStack< Prolongation, Entry, Index > &&mgStack_, MakeSmoother const &makeSmoother, std::unique_ptr< CoarsePreconditioner > &&coarsePreconditioner_, int nPre_=3, int nPost_=3)
Constructor.
A NUMA-aware compressed row storage matrix adhering mostly to the Dune ISTL interface (to complete....
An additive overlapping domain decomposition type preconditioner for higher order finite elements app...
Interface for symmetric preconditioners.
Supports gathering and reporting execution times information for nested program parts.
Definition timing.hh:64
static Timings & instance()
Returns a reference to a single default instance.
void stop(std::string const &name)
Stops the timing of given section.
struct Times const * start(std::string const &name)
Starts or continues the timing of given section.
DirectType
Available direct solvers for linear equation systems.
Definition enums.hh:35
@ MUMPS
MUMPS sparse solver.
Definition enums.hh:52
auto makeBlockJacobiPMultiGrid(NumaBCRSMatrix< Entry, Index > A, Space const &space, NumaBCRSMatrix< Entry, Index > coarseA, CoarseSpace const &coarseSpace, int nPre=3, int nPost=3, DirectType directType=DirectType::MUMPS)
Convenience function creating multiplicative multigrid preconditioner of V-cycle type for higher orde...
auto makeDirectPreconditioner(Matrix &&A, DirectType directType=DirectType::MUMPS)
Creates a direct solver for the given matrix.
auto makeMultiplicativeMultiGrid(MultiGridStack< Prolongation, Entry, Index > &&mgStack, MakeSmoother const &makeSmoother, std::unique_ptr< CoarsePreconditioner > &&coarsePreconditioner, int nPre=3, int nPost=3)
Convenience function creating multiplicative multigrid preconditioner of V-cycle type for P1 elements...
auto makeJacobiMultiGrid(NumaBCRSMatrix< Entry, Index > const &A, GridMan const &gridman, int nPre=3, int nPost=3, bool onlyLowerTriangle=false, DirectType directType=DirectType::MUMPS)
Convenience function creating multiplicative Jacobi multigrid for P1 elements.
auto makeMultiplicativePMultiGrid(NumaBCRSMatrix< Entry, Index > &&A, Space const &space, MakeSmoother const &makeSmoother, int nPre=3, int nPost=3, bool onlyLowerTriangle=false, DirectType directType=DirectType::MUMPS)
Convenience function creating multiplicative multigrid preconditioner of V-cycle type for higher orde...
auto makePMultiGridStack(FineSpace const &space, Matrix &&A, bool onlyLowerTriangle)
convenience routine for creating multigrid stacks based on coarsening by reducing the ansatz order fr...
auto makeBlockJacobiPMultiGrid(NumaBCRSMatrix< Entry, Index > A, Space const &space, int nPre=3, int nPost=3, bool onlyLowerTriangle=false, DirectType directType=DirectType::MUMPS)
Convenience function creating multiplicative multigrid preconditioner of V-cycle type for higher orde...
MultiGridStack< MGProlongation, Entry, Index > makeGeometricMultiGridStack(GridMan const &gridManager, NumaBCRSMatrix< Entry, Index > &&A, size_t minNodes=10000, bool onlyLowerTriangle=false)
convenience routine for creating multigrid stacks based on geometric coarsening for P1 elements
auto makeJacobiPMultiGrid(NumaBCRSMatrix< Entry, Index > A, FineSpace const &space, int nPre=3, int nPost=3, bool onlyLowerTriangle=false, DirectType directType=DirectType::MUMPS)
Convenience function creating multiplicative multigrid preconditioner of V-cycle type with Jacobi smo...
auto moveUnique(A &&a)
Creates a dynamically allocated object from an r-value reference.
Definition memory.hh:33
A duplicate(A const &a)
Creates a copy of a given object.
Definition memory.hh:48
OutIter vectorToSequence(double x, OutIter iter)
Definition crsutil.hh:30
Convenience typedefs and creation functions for common function spaces.
void NaturalDomain
The natural domain type.
void NaturalRange
The natural range type.