// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- // vi: set et ts=4 sw=2 sts=2: #ifndef DUNE_AMG_AMG_HH #define DUNE_AMG_AMG_HH #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace Dune { namespace Amg { /** * @defgroup ISTL_PAAMG Parallel Algebraic Multigrid * @ingroup ISTL_Prec * @brief A Parallel Algebraic Multigrid based on Agglomeration. */ /** * @addtogroup ISTL_PAAMG * * @{ */ /** @file * @author Markus Blatt * @brief The AMG preconditioner. */ template class KAMG; template class KAmgTwoGrid; /** * @brief Parallel algebraic multigrid based on agglomeration. * * \tparam M The LinearOperator type which represents the matrix * \tparam X The vector type * \tparam S The smoother type * \tparam A An allocator for X * * \todo drop the smoother template parameter and replace with dynamic construction */ template > class AMG : public Preconditioner { template friend class KAMG; friend class KAmgTwoGrid; public: /** @brief The matrix operator type. */ typedef M Operator; /** * @brief The type of the parallel information. * Either OwnerOverlapCommunication or another type * describing the parallel data distribution and * providing communication methods. */ typedef PI ParallelInformation; /** @brief The operator hierarchy type. */ typedef MatrixHierarchy OperatorHierarchy; /** @brief The parallal data distribution hierarchy type. */ typedef typename OperatorHierarchy::ParallelInformationHierarchy ParallelInformationHierarchy; /** @brief The domain type. */ typedef X Domain; /** @brief The range type. */ typedef X Range; /** @brief the type of the coarse solver. */ typedef InverseOperator CoarseSolver; /** * @brief The type of the smoother. * * One of the preconditioners implementing the Preconditioner interface. * Note that the smoother has to fit the ParallelInformation.*/ typedef S Smoother; /** @brief The argument type for the construction of the smoother. */ typedef typename SmootherTraits::Arguments SmootherArgs; /** * @brief Construct a new amg with a specific coarse solver. * @param matrices The already set-up matrix hierarchy. * @param coarseSolver The set up solver to use on the coarse * grid, must match the coarse matrix in the matrix hierarchy. * @param smootherArgs The arguments needed for thesmoother to use * for pre and post smoothing. * @param parms The parameters for the AMG. */ AMG(OperatorHierarchy& matrices, CoarseSolver& coarseSolver, const SmootherArgs& smootherArgs, const Parameters& parms); /** * @brief Construct an AMG with an inexact coarse solver based on the smoother. * * As coarse solver a preconditioned CG method with the smoother as preconditioner * will be used. The matrix hierarchy is built automatically. * @param fineOperator The operator on the fine level. * @param criterion The criterion describing the coarsening strategy. E. g. SymmetricCriterion * or UnsymmetricCriterion, and providing the parameters. * @param smootherArgs The arguments for constructing the smoothers. * @param pinfo The information about the parallel distribution of the data. */ template AMG(const Operator& fineOperator, const C& criterion, const SmootherArgs& smootherArgs=SmootherArgs(), const ParallelInformation& pinfo=ParallelInformation()); /*! \brief Constructor an AMG via ParameterTree. \param fineOperator The operator on the fine level. \param configuration ParameterTree containing AMG parameters. \param pinfo Optionally, specify ParallelInformation ParameterTree Key | Meaning --------------------------|------------ smootherIterations | The number of iterations to perform. smootherRelaxation | The relaxation factor maxLevel | Maximum number of levels allowed in the hierarchy. coarsenTarget | Maximum number of unknowns on the coarsest level. minCoarseningRate | Coarsening will stop if the rate is below this threshold. accumulationMode | If and how data is agglomerated on coarser level to | fewer processors. ("atOnce": do agglomeration once and | to one process; "successive": Multiple agglomerations to | fewer processes until all data is on one process; | "none": Do no agglomeration at all and solve coarse level | iteratively). prolongationDampingFactor | Damping factor for the prolongation. alpha | Scaling value for marking connections as strong. beta | Threshold for marking nodes as isolated. additive | Whether to use additive multigrid. gamma | 1 for V-cycle, 2 for W-cycle. preSteps | Number of presmoothing steps. postSteps | Number of postsmoothing steps. verbosity | Output verbosity. default=2. criterionSymmetric | If true use SymmetricCriterion (default), else UnSymmetricCriterion strengthMeasure | What conversion to use to convert a matrix block | to a scalar when determining strength of connection: | diagonal (use a diagonal of row diagonalRowIndex, class Diagonal, default); | rowSum (rowSum norm), frobenius (Frobenius norm); | one (use always one and neglect the actual entries). diagonalRowIndex | The index to use for the diagonal strength (default 0) | if this is i and strengthMeasure is "diagonal", then | block[i][i] will be used when determining strength of | connection. defaultAggregationSizeMode | Whether to set default values depending on isotropy of | problem uses parameters "defaultAggregationDimension" and | "maxAggregateDistance" (isotropic: For and isotropic problem; | anisotropic: for an anisotropic problem). defaultAggregationDimension | Dimension of the problem (used for setting default aggregate size). maxAggregateDistance | Maximum distance in an aggregte (in term of minimum edges needed to travel. | one vertex to another within the aggregate). minAggregateSize | Minimum number of vertices an aggregate should consist of. maxAggregateSize | Maximum number of vertices an aggregate should consist of. See \ref ISTL_Factory for the ParameterTree layout and examples. */ AMG(std::shared_ptr fineOperator, const ParameterTree& configuration, const ParallelInformation& pinfo=ParallelInformation()); /** * @brief Copy constructor. */ AMG(const AMG& amg); /** \copydoc Preconditioner::pre */ void pre(Domain& x, Range& b); /** \copydoc Preconditioner::apply */ void apply(Domain& v, const Range& d); //! Category of the preconditioner (see SolverCategory::Category) virtual SolverCategory::Category category() const { return category_; } /** \copydoc Preconditioner::post */ void post(Domain& x); /** * @brief Get the aggregate number of each unknown on the coarsest level. * @param cont The random access container to store the numbers in. */ template void getCoarsestAggregateNumbers(std::vector& cont); std::size_t levels(); std::size_t maxlevels(); /** * @brief Recalculate the matrix hierarchy. * * It is assumed that the coarsening for the changed fine level * matrix would yield the same aggregates. In this case it suffices * to recalculate all the Galerkin products for the matrices of the * coarser levels. */ void recalculateHierarchy() { matrices_->recalculateGalerkin(NegateSet()); } /** * @brief Check whether the coarse solver used is a direct solver. * @return True if the coarse level solver is a direct solver. */ bool usesDirectCoarseLevelSolver() const; private: /* * @brief Helper function to create hierarchies with parameter tree. * * Will create the coarsen criterion with the norm and create the * Hierarchies * \tparam Norm Type of the norm to use. */ template void createCriterionAndHierarchies(std::shared_ptr matrixptr, const PI& pinfo, const Norm&, const ParameterTree& configuration, std::true_type compiles = std::true_type()); template void createCriterionAndHierarchies(std::shared_ptr matrixptr, const PI& pinfo, const Norm&, const ParameterTree& configuration, std::false_type); /** * @brief Helper function to create hierarchies with settings from parameter tree. * @param criterion Coarsen criterion to configure and use */ template void createHierarchies(C& criterion, std::shared_ptr matrixptr, const PI& pinfo, const ParameterTree& configuration); /** * @brief Create matrix and smoother hierarchies. * @param criterion The coarsening criterion. * @param matrix The fine level matrix operator. * @param pinfo The fine level parallel information. */ template void createHierarchies(C& criterion, const std::shared_ptr& matrixptr, const PI& pinfo); /** * @brief A struct that holds the context of the current level. * * These are the iterators to the smoother, matrix, parallel information, * and so on needed for the computations on the current level. */ struct LevelContext { typedef Smoother SmootherType; /** * @brief The iterator over the smoothers. */ typename Hierarchy::Iterator smoother; /** * @brief The iterator over the matrices. */ typename OperatorHierarchy::ParallelMatrixHierarchy::ConstIterator matrix; /** * @brief The iterator over the parallel information. */ typename ParallelInformationHierarchy::Iterator pinfo; /** * @brief The iterator over the redistribution information. */ typename OperatorHierarchy::RedistributeInfoList::const_iterator redist; /** * @brief The iterator over the aggregates maps. */ typename OperatorHierarchy::AggregatesMapList::const_iterator aggregates; /** * @brief The iterator over the left hand side. */ typename Hierarchy::Iterator lhs; /** * @brief The iterator over the updates. */ typename Hierarchy::Iterator update; /** * @brief The iterator over the right hand sided. */ typename Hierarchy::Iterator rhs; /** * @brief The level index. */ std::size_t level; }; /** * @brief Multigrid cycle on a level. * @param levelContext the iterators of the current level. */ void mgc(LevelContext& levelContext); void additiveMgc(); /** * @brief Move the iterators to the finer level * @param levelContext the iterators of the current level. * @param processedFineLevel Whether the process computed on * fine level or not. */ void moveToFineLevel(LevelContext& levelContext,bool processedFineLevel); /** * @brief Move the iterators to the coarser level. * @param levelContext the iterators of the current level */ bool moveToCoarseLevel(LevelContext& levelContext); /** * @brief Initialize iterators over levels with fine level. * @param levelContext the iterators of the current level */ void initIteratorsWithFineLevel(LevelContext& levelContext); /** @brief The matrix we solve. */ std::shared_ptr matrices_; /** @brief The arguments to construct the smoother */ SmootherArgs smootherArgs_; /** @brief The hierarchy of the smoothers. */ std::shared_ptr > smoothers_; /** @brief The solver of the coarsest level. */ std::shared_ptr solver_; /** @brief The right hand side of our problem. */ std::shared_ptr> rhs_; /** @brief The left approximate solution of our problem. */ std::shared_ptr> lhs_; /** @brief The total update for the outer solver. */ std::shared_ptr> update_; /** @brief The type of the scalar product for the coarse solver. */ using ScalarProduct = Dune::ScalarProduct; /** @brief Scalar product on the coarse level. */ std::shared_ptr scalarProduct_; /** @brief Gamma, 1 for V-cycle and 2 for W-cycle. */ std::size_t gamma_; /** @brief The number of pre and postsmoothing steps. */ std::size_t preSteps_; /** @brief The number of postsmoothing steps. */ std::size_t postSteps_; bool buildHierarchy_; bool additive; bool coarsesolverconverged; std::shared_ptr coarseSmoother_; /** @brief The solver category. */ SolverCategory::Category category_; /** @brief The verbosity level. */ std::size_t verbosity_; struct ToLower { std::string operator()(const std::string& str) { std::stringstream retval; std::ostream_iterator out(retval); std::transform(str.begin(), str.end(), out, [](char c){ return std::tolower(c, std::locale::classic()); }); return retval.str(); } }; }; template inline AMG::AMG(const AMG& amg) : matrices_(amg.matrices_), smootherArgs_(amg.smootherArgs_), smoothers_(amg.smoothers_), solver_(amg.solver_), rhs_(), lhs_(), update_(), scalarProduct_(amg.scalarProduct_), gamma_(amg.gamma_), preSteps_(amg.preSteps_), postSteps_(amg.postSteps_), buildHierarchy_(amg.buildHierarchy_), additive(amg.additive), coarsesolverconverged(amg.coarsesolverconverged), coarseSmoother_(amg.coarseSmoother_), category_(amg.category_), verbosity_(amg.verbosity_) {} template AMG::AMG(OperatorHierarchy& matrices, CoarseSolver& coarseSolver, const SmootherArgs& smootherArgs, const Parameters& parms) : matrices_(stackobject_to_shared_ptr(matrices)), smootherArgs_(smootherArgs), smoothers_(new Hierarchy), solver_(&coarseSolver), rhs_(), lhs_(), update_(), scalarProduct_(0), gamma_(parms.getGamma()), preSteps_(parms.getNoPreSmoothSteps()), postSteps_(parms.getNoPostSmoothSteps()), buildHierarchy_(false), additive(parms.getAdditive()), coarsesolverconverged(true), coarseSmoother_(), // #warning should category be retrieved from matrices? category_(SolverCategory::category(*smoothers_->coarsest())), verbosity_(parms.debugLevel()) { assert(matrices_->isBuilt()); // build the necessary smoother hierarchies matrices_->coarsenSmoother(*smoothers_, smootherArgs_); } template template AMG::AMG(const Operator& matrix, const C& criterion, const SmootherArgs& smootherArgs, const PI& pinfo) : smootherArgs_(smootherArgs), smoothers_(new Hierarchy), solver_(), rhs_(), lhs_(), update_(), scalarProduct_(), gamma_(criterion.getGamma()), preSteps_(criterion.getNoPreSmoothSteps()), postSteps_(criterion.getNoPostSmoothSteps()), buildHierarchy_(true), additive(criterion.getAdditive()), coarsesolverconverged(true), coarseSmoother_(), category_(SolverCategory::category(pinfo)), verbosity_(criterion.debugLevel()) { if(SolverCategory::category(matrix) != SolverCategory::category(pinfo)) DUNE_THROW(InvalidSolverCategory, "Matrix and Communication must have the same SolverCategory!"); // TODO: reestablish compile time checks. //static_assert(static_cast(PI::category)==static_cast(S::category), // "Matrix and Solver must match in terms of category!"); auto matrixptr = stackobject_to_shared_ptr(matrix); createHierarchies(criterion, matrixptr, pinfo); } template AMG::AMG(std::shared_ptr matrixptr, const ParameterTree& configuration, const ParallelInformation& pinfo) : smoothers_(new Hierarchy), solver_(), rhs_(), lhs_(), update_(), scalarProduct_(), buildHierarchy_(true), coarsesolverconverged(true), coarseSmoother_(), category_(SolverCategory::category(pinfo)) { if (configuration.hasKey ("smootherIterations")) smootherArgs_.iterations = configuration.get("smootherIterations"); if (configuration.hasKey ("smootherRelaxation")) smootherArgs_.relaxationFactor = configuration.get("smootherRelaxation"); auto normName = ToLower()(configuration.get("strengthMeasure", "diagonal")); auto index = configuration.get("diagonalRowIndex", 0); if ( normName == "diagonal") { using field_type = typename M::field_type; using real_type = typename FieldTraits::real_type; std::is_convertible compiles; switch (index) { case 0: createCriterionAndHierarchies(matrixptr, pinfo, Diagonal<0>(), configuration, compiles); break; case 1: createCriterionAndHierarchies(matrixptr, pinfo, Diagonal<1>(), configuration, compiles); break; case 2: createCriterionAndHierarchies(matrixptr, pinfo, Diagonal<2>(), configuration, compiles); break; case 3: createCriterionAndHierarchies(matrixptr, pinfo, Diagonal<3>(), configuration, compiles); break; case 4: createCriterionAndHierarchies(matrixptr, pinfo, Diagonal<4>(), configuration, compiles); break; default: DUNE_THROW(InvalidStateException, "Currently strengthIndex>4 is not supported."); } } else if (normName == "rowsum") createCriterionAndHierarchies(matrixptr, pinfo, RowSum(), configuration); else if (normName == "frobenius") createCriterionAndHierarchies(matrixptr, pinfo, FrobeniusNorm(), configuration); else if (normName == "one") createCriterionAndHierarchies(matrixptr, pinfo, AlwaysOneNorm(), configuration); else DUNE_THROW(Dune::NotImplemented, "Wrong config file: strengthMeasure "< template void AMG::createCriterionAndHierarchies(std::shared_ptr matrixptr, const PI& pinfo, const Norm&, const ParameterTree& configuration, std::false_type) { DUNE_THROW(InvalidStateException, "Strength of connection measure does not support this type (" << className() << ") as it is lacking a conversion to" << className::real_type>() << "."); } template template void AMG::createCriterionAndHierarchies(std::shared_ptr matrixptr, const PI& pinfo, const Norm&, const ParameterTree& configuration, std::true_type) { if (configuration.get("criterionSymmetric", true)) { using Criterion = Dune::Amg::CoarsenCriterion< Dune::Amg::SymmetricCriterion >; Criterion criterion; createHierarchies(criterion, matrixptr, pinfo, configuration); } else { using Criterion = Dune::Amg::CoarsenCriterion< Dune::Amg::UnSymmetricCriterion >; Criterion criterion; createHierarchies(criterion, matrixptr, pinfo, configuration); } } template template void AMG::createHierarchies(C& criterion, std::shared_ptr matrixptr, const PI& pinfo, const ParameterTree& configuration) { if (configuration.hasKey ("maxLevel")) criterion.setMaxLevel(configuration.get("maxLevel")); if (configuration.hasKey ("minCoarseningRate")) criterion.setMinCoarsenRate(configuration.get("minCoarseningRate")); if (configuration.hasKey ("coarsenTarget")) criterion.setCoarsenTarget (configuration.get("coarsenTarget")); if (configuration.hasKey ("accumulationMode")) { std::string mode = ToLower()(configuration.get("accumulationMode")); if ( mode == "none") criterion.setAccumulate(AccumulationMode::noAccu); else if ( mode == "atonce" ) criterion.setAccumulate(AccumulationMode::atOnceAccu); else if ( mode == "successive") criterion.setCoarsenTarget (AccumulationMode::successiveAccu); else DUNE_THROW(InvalidSolverFactoryConfiguration, "Parameter accumulationMode does not allow value " << mode <<"."); } if (configuration.hasKey ("prolongationDampingFactor")) criterion.setProlongationDampingFactor (configuration.get("prolongationDampingFactor")); if (configuration.hasKey("defaultAggregationSizeMode")) { auto mode = ToLower()(configuration.get("defaultAggregationSizeMode")); auto dim = configuration.get("defaultAggregationDimension"); std::size_t maxDistance = 2; if (configuration.hasKey("MaxAggregateDistance")) maxDistance = configuration.get("maxAggregateDistance"); if (mode == "isotropic") criterion.setDefaultValuesIsotropic(dim, maxDistance); else if(mode == "anisotropic") criterion.setDefaultValuesAnisotropic(dim, maxDistance); else DUNE_THROW(InvalidSolverFactoryConfiguration, "Parameter accumulationMode does not allow value " << mode <<"."); } if (configuration.hasKey("maxAggregateDistance")) criterion.setMaxDistance(configuration.get("maxAggregateDistance")); if (configuration.hasKey("minAggregateSize")) criterion.setMinAggregateSize(configuration.get("minAggregateSize")); if (configuration.hasKey("maxAggregateSize")) criterion.setMaxAggregateSize(configuration.get("maxAggregateSize")); if (configuration.hasKey("maxAggregateConnectivity")) criterion.setMaxConnectivity(configuration.get("maxAggregateConnectivity")); if (configuration.hasKey ("alpha")) criterion.setAlpha (configuration.get ("alpha")); if (configuration.hasKey ("beta")) criterion.setBeta (configuration.get ("beta")); if (configuration.hasKey ("gamma")) criterion.setGamma (configuration.get ("gamma")); gamma_ = criterion.getGamma(); if (configuration.hasKey ("additive")) criterion.setAdditive (configuration.get("additive")); additive = criterion.getAdditive(); if (configuration.hasKey ("preSteps")) criterion.setNoPreSmoothSteps (configuration.get ("preSteps")); preSteps_ = criterion.getNoPreSmoothSteps (); if (configuration.hasKey ("postSteps")) criterion.setNoPostSmoothSteps (configuration.get ("postSteps")); postSteps_ = criterion.getNoPostSmoothSteps (); verbosity_ = configuration.get("verbosity", 0); criterion.setDebugLevel (verbosity_); createHierarchies(criterion, matrixptr, pinfo); } template struct DirectSolverSelector { typedef typename Matrix :: field_type field_type; enum SolverType { umfpack, superlu, none }; static constexpr SolverType solver = #if DISABLE_AMG_DIRECTSOLVER none; #elif HAVE_SUITESPARSE_UMFPACK UMFPackMethodChooser< field_type > :: valid ? umfpack : none ; #elif HAVE_SUPERLU superlu ; #else none; #endif template struct Solver { typedef InverseOperator type; static type* create(const M& mat, bool verbose, bool reusevector ) { DUNE_THROW(NotImplemented,"DirectSolver not selected"); return nullptr; } static std::string name () { return "None"; } }; #if HAVE_SUITESPARSE_UMFPACK template struct Solver< M, umfpack > { typedef UMFPack< M > type; static type* create(const M& mat, bool verbose, bool reusevector ) { return new type(mat, verbose, reusevector ); } static std::string name () { return "UMFPack"; } }; #endif #if HAVE_SUPERLU template struct Solver< M, superlu > { typedef SuperLU< M > type; static type* create(const M& mat, bool verbose, bool reusevector ) { return new type(mat, verbose, reusevector ); } static std::string name () { return "SuperLU"; } }; #endif // define direct solver type to be used typedef Solver< Matrix, solver > SelectedSolver ; typedef typename SelectedSolver :: type DirectSolver; static constexpr bool isDirectSolver = solver != none; static std::string name() { return SelectedSolver :: name (); } static DirectSolver* create(const Matrix& mat, bool verbose, bool reusevector ) { return SelectedSolver :: create( mat, verbose, reusevector ); } }; template template void AMG::createHierarchies(C& criterion, const std::shared_ptr& matrixptr, const PI& pinfo) { Timer watch; matrices_ = std::make_shared( std::const_pointer_cast(matrixptr), stackobject_to_shared_ptr(const_cast(pinfo))); matrices_->template build >(criterion); // build the necessary smoother hierarchies matrices_->coarsenSmoother(*smoothers_, smootherArgs_); // test whether we should solve on the coarse level. That is the case if we // have that level and if there was a redistribution on this level then our // communicator has to be valid (size()>0) as the smoother might try to communicate // in the constructor. if(buildHierarchy_ && matrices_->levels()==matrices_->maxlevels() && ( ! matrices_->redistributeInformation().back().isSetup() || matrices_->parallelInformation().coarsest().getRedistributed().communicator().size() ) ) { // We have the carsest level. Create the coarse Solver SmootherArgs sargs(smootherArgs_); sargs.iterations = 1; typename ConstructionTraits::Arguments cargs; cargs.setArgs(sargs); if(matrices_->redistributeInformation().back().isSetup()) { // Solve on the redistributed partitioning cargs.setMatrix(matrices_->matrices().coarsest().getRedistributed().getmat()); cargs.setComm(matrices_->parallelInformation().coarsest().getRedistributed()); }else{ cargs.setMatrix(matrices_->matrices().coarsest()->getmat()); cargs.setComm(*matrices_->parallelInformation().coarsest()); } coarseSmoother_ = ConstructionTraits::construct(cargs); scalarProduct_ = createScalarProduct(cargs.getComm(),category()); typedef DirectSolverSelector< typename M::matrix_type, X > SolverSelector; // Use superlu if we are purely sequential or with only one processor on the coarsest level. if( SolverSelector::isDirectSolver && (std::is_same::value // sequential mode || matrices_->parallelInformation().coarsest()->communicator().size()==1 //parallel mode and only one processor || (matrices_->parallelInformation().coarsest().isRedistributed() && matrices_->parallelInformation().coarsest().getRedistributed().communicator().size()==1 && matrices_->parallelInformation().coarsest().getRedistributed().communicator().size()>0) ) ) { // redistribute and 1 proc if(matrices_->parallelInformation().coarsest().isRedistributed()) { if(matrices_->matrices().coarsest().getRedistributed().getmat().N()>0) { // We are still participating on this level solver_.reset(SolverSelector::create(matrices_->matrices().coarsest().getRedistributed().getmat(), false, false)); } else solver_.reset(); } else { solver_.reset(SolverSelector::create(matrices_->matrices().coarsest()->getmat(), false, false)); } if(verbosity_>0 && matrices_->parallelInformation().coarsest()->communicator().rank()==0) std::cout<< "Using a direct coarse solver (" << SolverSelector::name() << ")" << std::endl; } else { if(matrices_->parallelInformation().coarsest().isRedistributed()) { if(matrices_->matrices().coarsest().getRedistributed().getmat().N()>0) // We are still participating on this level // we have to allocate these types using the rebound allocator // in order to ensure that we fulfill the alignment requirements solver_.reset(new BiCGSTABSolver(const_cast(matrices_->matrices().coarsest().getRedistributed()), *scalarProduct_, *coarseSmoother_, 1E-2, 1000, 0)); else solver_.reset(); }else { solver_.reset(new BiCGSTABSolver(const_cast(*matrices_->matrices().coarsest()), *scalarProduct_, *coarseSmoother_, 1E-2, 1000, 0)); // // we have to allocate these types using the rebound allocator // // in order to ensure that we fulfill the alignment requirements // using Alloc = typename std::allocator_traits::template rebind_alloc>; // Alloc alloc; // auto p = alloc.allocate(1); // std::allocator_traits::construct(alloc, p, // const_cast(*matrices_->matrices().coarsest()), // *scalarProduct_, // *coarseSmoother_, 1E-2, 1000, 0); // solver_.reset(p,[](BiCGSTABSolver* p){ // Alloc alloc; // std::allocator_traits::destroy(alloc, p); // alloc.deallocate(p,1); // }); } } } if(verbosity_>0 && matrices_->parallelInformation().finest()->communicator().rank()==0) std::cout<<"Building hierarchy of "<maxlevels()<<" levels " <<"(including coarse solver) took "< void AMG::pre(Domain& x, Range& b) { // Detect Matrix rows where all offdiagonal entries are // zero and set x such that A_dd*x_d=b_d // Thus users can be more careless when setting up their linear // systems. typedef typename M::matrix_type Matrix; typedef typename Matrix::ConstRowIterator RowIter; typedef typename Matrix::ConstColIterator ColIter; typedef typename Matrix::block_type Block; Block zero; zero=typename Matrix::field_type(); const Matrix& mat=matrices_->matrices().finest()->getmat(); for(RowIter row=mat.begin(); row!=mat.end(); ++row) { bool isDirichlet = true; bool hasDiagonal = false; Block diagonal{}; for(ColIter col=row->begin(); col!=row->end(); ++col) { if(row.index()==col.index()) { diagonal = *col; hasDiagonal = true; }else{ if(*col!=zero) isDirichlet = false; } } if(isDirichlet && hasDiagonal) { auto&& xEntry = Impl::asVector(x[row.index()]); auto&& bEntry = Impl::asVector(b[row.index()]); Impl::asMatrix(diagonal).solve(xEntry, bEntry); } } if(smoothers_->levels()>0) smoothers_->finest()->pre(x,b); else // No smoother to make x consistent! Do it by hand matrices_->parallelInformation().coarsest()->copyOwnerToAll(x,x); rhs_ = std::make_shared>(std::make_shared(b)); lhs_ = std::make_shared>(std::make_shared(x)); update_ = std::make_shared>(std::make_shared(x)); matrices_->coarsenVector(*rhs_); matrices_->coarsenVector(*lhs_); matrices_->coarsenVector(*update_); // Preprocess all smoothers typedef typename Hierarchy::Iterator Iterator; typedef typename Hierarchy::Iterator RIterator; typedef typename Hierarchy::Iterator DIterator; Iterator coarsest = smoothers_->coarsest(); Iterator smoother = smoothers_->finest(); RIterator rhs = rhs_->finest(); DIterator lhs = lhs_->finest(); if(smoothers_->levels()>1) { assert(lhs_->levels()==rhs_->levels()); assert(smoothers_->levels()==lhs_->levels() || matrices_->levels()==matrices_->maxlevels()); assert(smoothers_->levels()+1==lhs_->levels() || matrices_->levels()maxlevels()); if(smoother!=coarsest) for(++smoother, ++lhs, ++rhs; smoother != coarsest; ++smoother, ++lhs, ++rhs) smoother->pre(*lhs,*rhs); smoother->pre(*lhs,*rhs); } // The preconditioner might change x and b. So we have to // copy the changes to the original vectors. x = *lhs_->finest(); b = *rhs_->finest(); } template std::size_t AMG::levels() { return matrices_->levels(); } template std::size_t AMG::maxlevels() { return matrices_->maxlevels(); } /** \copydoc Preconditioner::apply */ template void AMG::apply(Domain& v, const Range& d) { LevelContext levelContext; if(additive) { *(rhs_->finest())=d; additiveMgc(); v=*lhs_->finest(); }else{ // Init all iterators for the current level initIteratorsWithFineLevel(levelContext); *levelContext.lhs = v; *levelContext.rhs = d; *levelContext.update=0; levelContext.level=0; mgc(levelContext); if(postSteps_==0||matrices_->maxlevels()==1) levelContext.pinfo->copyOwnerToAll(*levelContext.update, *levelContext.update); v=*levelContext.update; } } template void AMG::initIteratorsWithFineLevel(LevelContext& levelContext) { levelContext.smoother = smoothers_->finest(); levelContext.matrix = matrices_->matrices().finest(); levelContext.pinfo = matrices_->parallelInformation().finest(); levelContext.redist = matrices_->redistributeInformation().begin(); levelContext.aggregates = matrices_->aggregatesMaps().begin(); levelContext.lhs = lhs_->finest(); levelContext.update = update_->finest(); levelContext.rhs = rhs_->finest(); } template bool AMG ::moveToCoarseLevel(LevelContext& levelContext) { bool processNextLevel=true; if(levelContext.redist->isSetup()) { levelContext.redist->redistribute(static_cast(*levelContext.rhs), levelContext.rhs.getRedistributed()); processNextLevel = levelContext.rhs.getRedistributed().size()>0; if(processNextLevel) { //restrict defect to coarse level right hand side. typename Hierarchy::Iterator fineRhs = levelContext.rhs++; ++levelContext.pinfo; Transfer ::restrictVector(*(*levelContext.aggregates), *levelContext.rhs, static_cast(fineRhs.getRedistributed()), *levelContext.pinfo); } }else{ //restrict defect to coarse level right hand side. typename Hierarchy::Iterator fineRhs = levelContext.rhs++; ++levelContext.pinfo; Transfer ::restrictVector(*(*levelContext.aggregates), *levelContext.rhs, static_cast(*fineRhs), *levelContext.pinfo); } if(processNextLevel) { // prepare coarse system ++levelContext.lhs; ++levelContext.update; ++levelContext.matrix; ++levelContext.level; ++levelContext.redist; if(levelContext.matrix != matrices_->matrices().coarsest() || matrices_->levels()maxlevels()) { // next level is not the globally coarsest one ++levelContext.smoother; ++levelContext.aggregates; } // prepare the update on the next level *levelContext.update=0; } return processNextLevel; } template void AMG ::moveToFineLevel(LevelContext& levelContext, bool processNextLevel) { if(processNextLevel) { if(levelContext.matrix != matrices_->matrices().coarsest() || matrices_->levels()maxlevels()) { // previous level is not the globally coarsest one --levelContext.smoother; --levelContext.aggregates; } --levelContext.redist; --levelContext.level; //prolongate and add the correction (update is in coarse left hand side) --levelContext.matrix; //typename Hierarchy::Iterator coarseLhs = lhs--; --levelContext.lhs; --levelContext.pinfo; } if(levelContext.redist->isSetup()) { // Need to redistribute during prolongateVector levelContext.lhs.getRedistributed()=0; Transfer ::prolongateVector(*(*levelContext.aggregates), *levelContext.update, *levelContext.lhs, levelContext.lhs.getRedistributed(), matrices_->getProlongationDampingFactor(), *levelContext.pinfo, *levelContext.redist); }else{ *levelContext.lhs=0; Transfer ::prolongateVector(*(*levelContext.aggregates), *levelContext.update, *levelContext.lhs, matrices_->getProlongationDampingFactor(), *levelContext.pinfo); } if(processNextLevel) { --levelContext.update; --levelContext.rhs; } *levelContext.update += *levelContext.lhs; } template bool AMG::usesDirectCoarseLevelSolver() const { return IsDirectSolver< CoarseSolver>::value; } template void AMG::mgc(LevelContext& levelContext){ if(levelContext.matrix == matrices_->matrices().coarsest() && levels()==maxlevels()) { // Solve directly InverseOperatorResult res; res.converged=true; // If we do not compute this flag will not get updated if(levelContext.redist->isSetup()) { levelContext.redist->redistribute(*levelContext.rhs, levelContext.rhs.getRedistributed()); if(levelContext.rhs.getRedistributed().size()>0) { // We are still participating in the computation levelContext.pinfo.getRedistributed().copyOwnerToAll(levelContext.rhs.getRedistributed(), levelContext.rhs.getRedistributed()); solver_->apply(levelContext.update.getRedistributed(), levelContext.rhs.getRedistributed(), res); } levelContext.redist->redistributeBackward(*levelContext.update, levelContext.update.getRedistributed()); levelContext.pinfo->copyOwnerToAll(*levelContext.update, *levelContext.update); }else{ levelContext.pinfo->copyOwnerToAll(*levelContext.rhs, *levelContext.rhs); solver_->apply(*levelContext.update, *levelContext.rhs, res); } if (!res.converged) coarsesolverconverged = false; }else{ // presmoothing presmooth(levelContext, preSteps_); #ifndef DUNE_AMG_NO_COARSEGRIDCORRECTION bool processNextLevel = moveToCoarseLevel(levelContext); if(processNextLevel) { // next level for(std::size_t i=0; imatrices().coarsest() && levels()==maxlevels()) break; if(i+1 < gamma_){ levelContext.matrix->applyscaleadd(-1., *levelContext.lhs, *levelContext.rhs); } } } moveToFineLevel(levelContext, processNextLevel); #else *lhs=0; #endif if(levelContext.matrix == matrices_->matrices().finest()) { coarsesolverconverged = matrices_->parallelInformation().finest()->communicator().prod(coarsesolverconverged); if(!coarsesolverconverged) DUNE_THROW(MathError, "Coarse solver did not converge"); } // postsmoothing postsmooth(levelContext, postSteps_); } } template void AMG::additiveMgc(){ // restrict residual to all levels typename ParallelInformationHierarchy::Iterator pinfo=matrices_->parallelInformation().finest(); typename Hierarchy::Iterator rhs=rhs_->finest(); typename Hierarchy::Iterator lhs = lhs_->finest(); typename OperatorHierarchy::AggregatesMapList::const_iterator aggregates=matrices_->aggregatesMaps().begin(); for(typename Hierarchy::Iterator fineRhs=rhs++; fineRhs != rhs_->coarsest(); fineRhs=rhs++, ++aggregates) { ++pinfo; Transfer ::restrictVector(*(*aggregates), *rhs, static_cast(*fineRhs), *pinfo); } // pinfo is invalid, set to coarsest level //pinfo = matrices_->parallelInformation().coarsest // calculate correction for all levels lhs = lhs_->finest(); typename Hierarchy::Iterator smoother = smoothers_->finest(); for(rhs=rhs_->finest(); rhs != rhs_->coarsest(); ++lhs, ++rhs, ++smoother) { // presmoothing *lhs=0; smoother->apply(*lhs, *rhs); } // Coarse level solve #ifndef DUNE_AMG_NO_COARSEGRIDCORRECTION InverseOperatorResult res; pinfo->copyOwnerToAll(*rhs, *rhs); solver_->apply(*lhs, *rhs, res); if(!res.converged) DUNE_THROW(MathError, "Coarse solver did not converge"); #else *lhs=0; #endif // Prologate and add up corrections from all levels --pinfo; --aggregates; for(typename Hierarchy::Iterator coarseLhs = lhs--; coarseLhs != lhs_->finest(); coarseLhs = lhs--, --aggregates, --pinfo) { Transfer ::prolongateVector(*(*aggregates), *coarseLhs, *lhs, 1.0, *pinfo); } } /** \copydoc Preconditioner::post */ template void AMG::post([[maybe_unused]] Domain& x) { // Postprocess all smoothers typedef typename Hierarchy::Iterator Iterator; typedef typename Hierarchy::Iterator DIterator; Iterator coarsest = smoothers_->coarsest(); Iterator smoother = smoothers_->finest(); DIterator lhs = lhs_->finest(); if(smoothers_->levels()>0) { if(smoother != coarsest || matrices_->levels()maxlevels()) smoother->post(*lhs); if(smoother!=coarsest) for(++smoother, ++lhs; smoother != coarsest; ++smoother, ++lhs) smoother->post(*lhs); smoother->post(*lhs); } lhs_ = nullptr; update_ = nullptr; rhs_ = nullptr; } template template void AMG::getCoarsestAggregateNumbers(std::vector& cont) { matrices_->getCoarsestAggregatesOnFinest(cont); } } // end namespace Amg struct AMGCreator{ template struct isValidMatrix : std::false_type{}; template struct isValidMatrix, A>> : std::true_type{}; template std::shared_ptr > makeAMG(const OP& op, const std::string& smoother, const Dune::ParameterTree& config) const { DUNE_THROW(Dune::Exception, "Operator type not supported by AMG"); } template std::shared_ptr > makeAMG(const std::shared_ptr>& op, const std::string& smoother, const Dune::ParameterTree& config) const { using OP = MatrixAdapter; if(smoother == "ssor") return std::make_shared>>(op, config); if(smoother == "sor") return std::make_shared>>(op, config); if(smoother == "jac") return std::make_shared>>(op, config); if(smoother == "gs") return std::make_shared>>(op, config); if(smoother == "ilu") return std::make_shared>>(op, config); else DUNE_THROW(Dune::Exception, "Unknown smoother for AMG"); } template std::shared_ptr > makeAMG(const std::shared_ptr>& op, const std::string& smoother, const Dune::ParameterTree& config) const { using OP = OverlappingSchwarzOperator; auto cop = std::static_pointer_cast(op); if(smoother == "ssor") return std::make_shared>,C>>(cop, config, op->getCommunication()); if(smoother == "sor") return std::make_shared>,C>>(cop, config, op->getCommunication()); if(smoother == "jac") return std::make_shared>,C>>(cop, config, op->getCommunication()); if(smoother == "gs") return std::make_shared>,C>>(cop, config, op->getCommunication()); if(smoother == "ilu") return std::make_shared>,C>>(cop, config, op->getCommunication()); else DUNE_THROW(Dune::Exception, "Unknown smoother for AMG"); } template std::shared_ptr > makeAMG(const std::shared_ptr>& op, const std::string& smoother, const Dune::ParameterTree& config) const { using OP = NonoverlappingSchwarzOperator; if(smoother == "ssor") return std::make_shared>,C>>(op, config, op->getCommunication()); if(smoother == "sor") return std::make_shared>,C>>(op, config, op->getCommunication()); if(smoother == "jac") return std::make_shared>,C>>(op, config, op->getCommunication()); if(smoother == "gs") return std::make_shared>,C>>(op, config, op->getCommunication()); if(smoother == "ilu") return std::make_shared>,C>>(op, config, op->getCommunication()); else DUNE_THROW(Dune::Exception, "Unknown smoother for AMG"); } template std::shared_ptr> operator() (OpTraits opTraits, const std::shared_ptr& op, const Dune::ParameterTree& config, std::enable_if_t::value,int> = 0) const { using field_type = typename OpTraits::matrix_type::field_type; using real_type = typename FieldTraits::real_type; if (!std::is_convertible()) DUNE_THROW(UnsupportedType, "AMG needs field_type(" << className() << ") to be convertible to its real_type (" << className() << ")."); std::string smoother = config.get("smoother", "ssor"); // we can irgnore the OpTraits here. As the AMG can only work // with actual matrices, the operator op must be of type // MatrixAdapter or *SchwarzOperator. In any case these // operators provide all necessary information about matrix, // domain and range type return makeAMG(op, smoother, config); } template std::shared_ptr> operator() (OpTraits opTraits, const std::shared_ptr& op, const Dune::ParameterTree& config, std::enable_if_t::value,int> = 0) const { DUNE_THROW(UnsupportedType, "AMG needs a FieldMatrix as Matrix block_type"); } }; DUNE_REGISTER_PRECONDITIONER("amg", AMGCreator()); } // end namespace Dune #endif