| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// MueLu: A package for multigrid based preconditioning |
| 4 |
// |
| 5 |
// Copyright 2012 NTESS and the MueLu contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
#ifndef MUELU_HIERARCHY_DECL_HPP |
| 11 |
#define MUELU_HIERARCHY_DECL_HPP |
| 12 |
|
| 13 |
#include <Teuchos_ParameterList.hpp> |
| 14 |
#include <Teuchos_Ptr.hpp> |
| 15 |
|
| 16 |
#include <Xpetra_ConfigDefs.hpp> // global_size_t |
| 17 |
#include <Xpetra_Matrix_fwd.hpp> |
| 18 |
#include <Xpetra_MultiVector_fwd.hpp> |
| 19 |
#include <Xpetra_MultiVectorFactory_fwd.hpp> |
| 20 |
#include <Xpetra_Operator_fwd.hpp> |
| 21 |
|
| 22 |
#include <MueLu_SmootherCloner.hpp> |
| 23 |
#include "MueLu_ConfigDefs.hpp" |
| 24 |
#include "MueLu_BaseClass.hpp" |
| 25 |
#include "MueLu_Hierarchy_fwd.hpp" |
| 26 |
|
| 27 |
#include "MueLu_Types.hpp" |
| 28 |
|
| 29 |
#include "MueLu_FactoryBase_fwd.hpp" |
| 30 |
#include "MueLu_FactoryManager.hpp" // no fwd declaration because constructor of FactoryManager is used as a default parameter of Setup() |
| 31 |
#include "MueLu_KeepType.hpp" |
| 32 |
#include "MueLu_Level_fwd.hpp" |
| 33 |
#include "MueLu_MasterList.hpp" |
| 34 |
#include "MueLu_NoFactory.hpp" |
| 35 |
#include "MueLu_PerfUtils_fwd.hpp" |
| 36 |
#include "MueLu_PFactory_fwd.hpp" |
| 37 |
#include "MueLu_SmootherBase_fwd.hpp" |
| 38 |
#include "MueLu_SmootherFactory_fwd.hpp" |
| 39 |
#include "MueLu_Utilities_fwd.hpp" |
| 40 |
|
| 41 |
namespace MueLu { |
| 42 |
|
| 43 |
enum class ConvergenceStatus { |
| 44 |
Converged, |
| 45 |
Unconverged, |
| 46 |
Undefined |
| 47 |
}; |
| 48 |
|
| 49 |
/*! |
| 50 |
@class Hierarchy |
| 51 |
@brief Provides methods to build a multigrid hierarchy and apply multigrid cycles. |
| 52 |
|
| 53 |
Allows users to manually populate operators at different levels within |
| 54 |
a multigrid method and push them into the hierarchy via SetLevel() |
| 55 |
and/or to supply factories for automatically generating prolongators, |
| 56 |
restrictors, and coarse level discretizations. Additionally, this class contains |
| 57 |
an apply method that supports V and W cycles. |
| 58 |
*/ |
| 59 |
template <class Scalar = DefaultScalar, |
| 60 |
class LocalOrdinal = DefaultLocalOrdinal, |
| 61 |
class GlobalOrdinal = DefaultGlobalOrdinal, |
| 62 |
class Node = DefaultNode> |
| 63 |
class Hierarchy : public BaseClass { |
| 64 |
#undef MUELU_HIERARCHY_SHORT |
| 65 |
#include "MueLu_UseShortNames.hpp" |
| 66 |
|
| 67 |
typedef Teuchos::ScalarTraits<SC> STS; |
| 68 |
typedef typename STS::magnitudeType MagnitudeType; |
| 69 |
|
| 70 |
//! Data struct for defining stopping criteria of multigrid iteration |
| 71 |
struct ConvData { |
| 72 |
ConvData() |
| 73 |
: maxIts_(1) |
| 74 |
, tol_(-STS::magnitude(STS::one())) {} |
| 75 |
ConvData(LO maxIts) |
| 76 |
: maxIts_(maxIts) |
| 77 |
, tol_(-STS::magnitude(STS::one())) {} |
| 78 |
ConvData(MagnitudeType tol) |
| 79 |
: maxIts_(10000) |
| 80 |
, tol_(tol) {} |
| 81 |
ConvData(std::pair<LO, MagnitudeType> p) |
| 82 |
: maxIts_(p.first) |
| 83 |
, tol_(p.second) {} |
| 84 |
|
| 85 |
LO maxIts_; |
| 86 |
MagnitudeType tol_; |
| 87 |
}; |
| 88 |
|
| 89 |
public: |
| 90 |
//! @name Constructors/Destructors |
| 91 |
//@{ |
| 92 |
|
| 93 |
//! Default constructor. |
| 94 |
Hierarchy(); |
| 95 |
//! Constructor that labels the hierarchy. |
| 96 |
Hierarchy(const std::string& label); |
| 97 |
|
| 98 |
//! Constructor |
| 99 |
Hierarchy(const RCP<Matrix>& A); |
| 100 |
|
| 101 |
//! Constructor |
| 102 |
Hierarchy(const RCP<Matrix>& A, const std::string& label); |
| 103 |
|
| 104 |
//! Destructor. |
| 105 |
virtual ~Hierarchy(); |
| 106 |
|
| 107 |
//@} |
| 108 |
|
| 109 |
//! @name Set/Get Methods. |
| 110 |
//@{ |
| 111 |
|
| 112 |
//! |
| 113 |
static CycleType GetDefaultCycle() { return MasterList::getDefault<std::string>("cycle type") == "V" ? VCYCLE : WCYCLE; } |
| 114 |
static int GetDefaultCycleStartLevel() { return MasterList::getDefault<int>("W cycle start level"); } |
| 115 |
static bool GetDefaultImplicitTranspose() { return MasterList::getDefault<bool>("transpose: use implicit"); } |
| 116 |
static bool GetDefaultFuseProlongationAndUpdate() { return MasterList::getDefault<bool>("fuse prolongation and update"); } |
| 117 |
static Xpetra::global_size_t GetDefaultMaxCoarseSize() { return MasterList::getDefault<int>("coarse: max size"); } |
| 118 |
static int GetDefaultMaxLevels() { return MasterList::getDefault<int>("max levels"); } |
| 119 |
static bool GetDefaultPRrebalance() { return MasterList::getDefault<bool>("repartition: rebalance P and R"); } |
| 120 |
|
| 121 |
Xpetra::global_size_t GetMaxCoarseSize() const { return maxCoarseSize_; } |
| 122 |
bool GetImplicitTranspose() const { return implicitTranspose_; } |
| 123 |
bool GetFuseProlongationAndUpdate() const { return fuseProlongationAndUpdate_; } |
| 124 |
|
| 125 |
void SetMaxCoarseSize(Xpetra::global_size_t maxCoarseSize) { maxCoarseSize_ = maxCoarseSize; } |
| 126 |
void SetPRrebalance(bool doPRrebalance) { doPRrebalance_ = doPRrebalance; } |
| 127 |
void SetPRViaCopyrebalance(bool doPRViaCopyrebalance) { doPRViaCopyrebalance_ = doPRViaCopyrebalance; } |
| 128 |
void SetImplicitTranspose(const bool& implicit) { implicitTranspose_ = implicit; } |
| 129 |
void SetFuseProlongationAndUpdate(const bool& fuse) { fuseProlongationAndUpdate_ = fuse; } |
| 130 |
|
| 131 |
//@} |
| 132 |
|
| 133 |
//! |
| 134 |
|
| 135 |
template <class S2, class LO2, class GO2, class N2> |
| 136 |
friend class Hierarchy; |
| 137 |
|
| 138 |
int LastLevelID() const { return Levels_.size() - 1; } |
| 139 |
|
| 140 |
private: |
| 141 |
void DumpCurrentGraph(int level) const; |
| 142 |
|
| 143 |
public: |
| 144 |
//! Add a level at the end of the hierarchy |
| 145 |
void AddLevel(const RCP<Level>& level); |
| 146 |
|
| 147 |
//! Add a new level at the end of the hierarchy |
| 148 |
void AddNewLevel(); |
| 149 |
|
| 150 |
//! Retrieve a certain level from hierarchy. |
| 151 |
RCP<Level>& GetLevel(const int levelID = 0); |
| 152 |
|
| 153 |
int GetNumLevels() const; |
| 154 |
int GetGlobalNumLevels() const; |
| 155 |
|
| 156 |
MagnitudeType GetRate() const { return rate_; } |
| 157 |
|
| 158 |
// This function is global |
| 159 |
double GetOperatorComplexity() const; |
| 160 |
|
| 161 |
// This function is global |
| 162 |
double GetSmootherComplexity() const; |
| 163 |
|
| 164 |
//! Helper function |
| 165 |
void CheckLevel(Level& level, int levelID); |
| 166 |
|
| 167 |
void SetMatvecParams(RCP<ParameterList> matvecParams); |
| 168 |
|
| 169 |
//! Multi-level setup phase: build a new level of the hierarchy. |
| 170 |
/*! This method is aimed to be used in a loop building the hierarchy level by level. See Hierarchy::Setup(manager, startLevel, numDesiredLevels) for an example of usage. |
| 171 |
* |
| 172 |
* @param coarseLevelID ID of the level to be built. |
| 173 |
* @param fineLevelManager defines how to build missing data of the fineLevel (example: aggregates) |
| 174 |
* @param coarseLevelManager defines how to build the level |
| 175 |
* @param nextLevelManager defines how the next coarse level will be built. This is used to post corresponding request before building the coarse level to keep useful data. |
| 176 |
|
| 177 |
CoarseLevel is considered to be the last level if: |
| 178 |
- input parameter isLastLevel == true |
| 179 |
or |
| 180 |
- Ac->getRowMap()->getGlobalNumElements() <= maxCoarseSize_ |
| 181 |
Method return true if CoarseLevel is the last level. |
| 182 |
|
| 183 |
Pre-condition: |
| 184 |
* FineLevel: |
| 185 |
- must have kept useful data (TODO: not tested yet) |
| 186 |
- must be Teuchos::null when Setup is called for finest level (Setup then automatically calls Request for "Smoother" and "CoarseSolver") |
| 187 |
* CoarseLevel: |
| 188 |
- already allocated (using Hierarchy::AddLevel()) |
| 189 |
- requests already posted |
| 190 |
(exception: for finest level (=fineLevelManager==null) requests are called within setup routine) |
| 191 |
* NextLevel: |
| 192 |
- do not need to be allocate but could (FIXME: will be deleted if lastlevel...). |
| 193 |
- should be null when Setup is called for last level |
| 194 |
|
| 195 |
Post-condition: |
| 196 |
* FineLevel: |
| 197 |
- temporary data have been used and released (this condition is not tested) |
| 198 |
* CoarseLevel: |
| 199 |
- built, requests have been used |
| 200 |
- if it is the last level (due to input parameter isLastLevel or getGlobalNumElements() <= maxCoarseSize_), |
| 201 |
then the coarse solver factory of the factory manager have been used instead of the smoother factory. |
| 202 |
* NextLevel: |
| 203 |
If input parameter isLastLevel == false: |
| 204 |
- have been allocated |
| 205 |
- requests already posted. |
| 206 |
*/ |
| 207 |
bool Setup(int coarseLevelID, const RCP<const FactoryManagerBase> fineLevelManager /* = Teuchos::null */, const RCP<const FactoryManagerBase> coarseLevelManager, |
| 208 |
const RCP<const FactoryManagerBase> nextLevelManager = Teuchos::null); |
| 209 |
|
| 210 |
//! |
| 211 |
void Setup(const FactoryManagerBase& manager = FactoryManager(), int startLevel = 0, int numDesiredLevels = GetDefaultMaxLevels()); |
| 212 |
|
| 213 |
void SetupRe(); |
| 214 |
|
| 215 |
void CheckForEmptySmoothersAndCoarseSolve(); |
| 216 |
|
| 217 |
//! Clear impermanent data from previous setup |
| 218 |
void Clear(int startLevel = 0); |
| 219 |
void ExpertClear(); |
| 220 |
|
| 221 |
//! Returns multigrid cycle type (supports VCYCLE and WCYCLE) |
| 222 |
CycleType GetCycle() const { return Cycle_; } |
| 223 |
|
| 224 |
//! Supports VCYCLE and WCYCLE types. |
| 225 |
void SetCycle(CycleType Cycle) { Cycle_ = Cycle; } |
| 226 |
|
| 227 |
void SetCycleStartLevel(int cycleStart) { WCycleStartLevel_ = cycleStart; } |
| 228 |
|
| 229 |
//! Specify damping factor alpha such that x = x + alpha*P*c, where c is the coarse grid correction. |
| 230 |
void SetProlongatorScalingFactor(double scalingFactor) { scalingFactor_ = scalingFactor; } |
| 231 |
|
| 232 |
/*! |
| 233 |
@brief Apply the multigrid preconditioner. |
| 234 |
|
| 235 |
In theory, more general cycle types than just V- and W-cycles are possible. However, |
| 236 |
the enumerated type CycleType would have to be extended. |
| 237 |
|
| 238 |
@param B right-hand side of linear problem |
| 239 |
@param X initial and final (approximate) solution of linear problem |
| 240 |
@param ConvData struct which stores convergence criteria (maximum number of multigrid iterations or stopping tolerance) |
| 241 |
@param InitialGuessIsZero Indicates whether the initial guess is zero |
| 242 |
@param startLevel index of starting level to build multigrid hierarchy (default = 0) |
| 243 |
*/ |
| 244 |
ConvergenceStatus Iterate(const MultiVector& B, MultiVector& X, ConvData conv = ConvData(), |
| 245 |
bool InitialGuessIsZero = false, LO startLevel = 0); |
| 246 |
|
| 247 |
/*! |
| 248 |
@brief Print matrices in the multigrid hierarchy to file. |
| 249 |
|
| 250 |
@param[in] start start level |
| 251 |
@param[in] end end level |
| 252 |
|
| 253 |
Default behavior is to print system and transfer matrices from the entire hierarchy. |
| 254 |
Files are named "A_0.m", "P_1.m", "R_1.m", etc, and are in matrix market coordinate format. |
| 255 |
*/ |
| 256 |
void Write(const LO& start = -1, const LO& end = -1, const std::string& suffix = ""); |
| 257 |
|
| 258 |
//@} |
| 259 |
|
| 260 |
//! @name Permanent storage |
| 261 |
//@{ |
| 262 |
|
| 263 |
//! Call Level::Keep(ename, factory) for each level of the Hierarchy. |
| 264 |
void Keep(const std::string& ename, const FactoryBase* factory = NoFactory::get()); |
| 265 |
|
| 266 |
//! Call Level::Delete(ename, factory) for each level of the Hierarchy. |
| 267 |
void Delete(const std::string& ename, const FactoryBase* factory = NoFactory::get()); |
| 268 |
|
| 269 |
//! Call Level::AddKeepFlag for each level of the Hierarchy. |
| 270 |
void AddKeepFlag(const std::string& ename, const FactoryBase* factory = NoFactory::get(), KeepType keep = MueLu::Keep); |
| 271 |
|
| 272 |
//! Call Level::RemoveKeepFlag for each level of the Hierarchy |
| 273 |
void RemoveKeepFlag(const std::string& ename, const FactoryBase* factory, KeepType keep = MueLu::All); |
| 274 |
|
| 275 |
//@} |
| 276 |
|
| 277 |
//! @name Overridden from Teuchos::Describable |
| 278 |
//@{ |
| 279 |
|
| 280 |
//! Return a simple one-line description of this object. |
| 281 |
std::string description() const; |
| 282 |
|
| 283 |
/*! @brief Print the Hierarchy with some verbosity level to a FancyOStream object. |
| 284 |
|
| 285 |
@param[in] out The Teuchos::FancyOstream. |
| 286 |
@param[in] verbLevel Controls amount of output. |
| 287 |
*/ |
| 288 |
void describe(Teuchos::FancyOStream& out, const VerbLevel verbLevel = Default) const; |
| 289 |
void describe(Teuchos::FancyOStream& out, const Teuchos::EVerbosityLevel verbLevel = Teuchos::VERB_HIGH) const; |
| 290 |
|
| 291 |
//! Hierarchy::print is local hierarchy function, thus the statistics can be different from global ones |
| 292 |
void print(std::ostream& out = std::cout, const VerbLevel verbLevel = (MueLu::Parameters | MueLu::Statistics0)) const; |
| 293 |
|
| 294 |
/*! Indicate whether the multigrid method is a preconditioner or a solver. |
| 295 |
|
| 296 |
This is used in conjunction with the verbosity level to determine whether the residuals can be printed. |
| 297 |
*/ |
| 298 |
void IsPreconditioner(const bool flag); |
| 299 |
|
| 300 |
//@} |
| 301 |
|
| 302 |
void EnableGraphDumping(const std::string& filename, int levelID = 1) { |
| 303 |
isDumpingEnabled_ = true; |
| 304 |
dumpLevel_ = levelID; |
| 305 |
dumpFile_ = filename; |
| 306 |
} |
| 307 |
|
| 308 |
void setlib(Xpetra::UnderlyingLib inlib) { lib_ = inlib; } |
| 309 |
Xpetra::UnderlyingLib lib() { return lib_; } |
| 310 |
|
| 311 |
//! force recreation of cached description_ next time description() is called: |
| 312 |
void ResetDescription() { |
| 313 |
description_ = ""; |
| 314 |
} |
| 315 |
|
| 316 |
void AllocateLevelMultiVectors(int numvecs, bool forceMapCheck = false); |
| 317 |
void DeleteLevelMultiVectors(); |
| 318 |
|
| 319 |
protected: |
| 320 |
const RCP<const FactoryManagerBase>& GetLevelManager(const int levelID) const { |
| 321 |
return levelManagers_[levelID]; |
| 322 |
} |
| 323 |
|
| 324 |
private: |
| 325 |
//! Copy constructor is not implemented. |
| 326 |
Hierarchy(const Hierarchy& h); |
| 327 |
|
| 328 |
//! Decide if the residual needs to be computed |
| 329 |
bool IsCalculationOfResidualRequired(const LO startLevel, const ConvData& conv) const; |
| 330 |
|
| 331 |
/*! |
| 332 |
\brief Decide if the multigrid iteration is converged |
| 333 |
|
| 334 |
We judge convergence by comparing the current \c residualNorm |
| 335 |
to the user given \c convergenceTolerance and then return the |
| 336 |
appropriate \c ConvergenceStatus |
| 337 |
*/ |
| 338 |
ConvergenceStatus IsConverged(const Teuchos::Array<MagnitudeType>& residualNorm, |
| 339 |
const MagnitudeType convergenceTolerance) const; |
| 340 |
|
| 341 |
//! Print \c residualNorm for this \c iteration to the screen |
| 342 |
void PrintResidualHistory(const LO iteration, |
| 343 |
const Teuchos::Array<MagnitudeType>& residualNorm) const; |
| 344 |
|
| 345 |
//! Compute the residual norm and print it depending on the verbosity level |
| 346 |
ConvergenceStatus ComputeResidualAndPrintHistory(const Operator& A, const MultiVector& X, |
| 347 |
const MultiVector& B, const LO iteration, |
| 348 |
const LO startLevel, const ConvData& conv, MagnitudeType& previousResidualNorm); |
| 349 |
|
| 350 |
//! Container for Level objects |
| 351 |
Array<RCP<Level> > Levels_; |
| 352 |
|
| 353 |
//! We replace coordinates GIDs to make them consistent with matrix GIDs, |
| 354 |
//! even if user does not do that. Ideally, though, we should completely |
| 355 |
//! remove any notion of coordinate GIDs, and deal only with LIDs, assuming |
| 356 |
//! that they are consistent with matrix block IDs |
| 357 |
void ReplaceCoordinateMap(Level& level); |
| 358 |
|
| 359 |
//! Minimum size of a matrix on any level. If we fall below that, we stop |
| 360 |
//! the coarsening |
| 361 |
Xpetra::global_size_t maxCoarseSize_; |
| 362 |
|
| 363 |
//! Potential speed up of the setup by skipping R construction, and using |
| 364 |
//! transpose matrix-matrix product for RAP |
| 365 |
bool implicitTranspose_; |
| 366 |
|
| 367 |
//! Potential speed up of the solve by fusing prolongation and update steps. |
| 368 |
//! This can lead to more iterations to round-off error accumulation. |
| 369 |
bool fuseProlongationAndUpdate_; |
| 370 |
|
| 371 |
//! Potential speed up of the setup by skipping rebalancing of P and R, and |
| 372 |
//! doing extra import during solve |
| 373 |
bool doPRrebalance_; |
| 374 |
bool doPRViaCopyrebalance_; // fully explicit, needed for CombinePFactory |
| 375 |
|
| 376 |
//! Hierarchy may be used in a standalone mode, or as a preconditioner |
| 377 |
bool isPreconditioner_; |
| 378 |
|
| 379 |
//! V- or W-cycle |
| 380 |
CycleType Cycle_; |
| 381 |
|
| 382 |
//! Level at which to start W-cycle |
| 383 |
int WCycleStartLevel_; |
| 384 |
|
| 385 |
//! Scaling factor to be applied to coarse grid correction. |
| 386 |
double scalingFactor_; |
| 387 |
|
| 388 |
//! Epetra/Tpetra mode |
| 389 |
Xpetra::UnderlyingLib lib_; |
| 390 |
|
| 391 |
//! cache description to avoid recreating in each call to description() - use ResetDescription() to force recreation in Setup, SetupRe, etc. |
| 392 |
mutable std::string description_ = ""; // mutable so that we can lazily initialize in description(), which is declared const |
| 393 |
|
| 394 |
/*! |
| 395 |
@brief Graph dumping |
| 396 |
|
| 397 |
If enabled, we dump the graph on a specified level into a specified file |
| 398 |
*/ |
| 399 |
bool isDumpingEnabled_; |
| 400 |
// -1 = dump all levels, -2 = dump nothing |
| 401 |
int dumpLevel_; |
| 402 |
std::string dumpFile_; |
| 403 |
|
| 404 |
//! Convergece rate |
| 405 |
MagnitudeType rate_; |
| 406 |
|
| 407 |
//! Level managers used during the Setup |
| 408 |
Array<RCP<const FactoryManagerBase> > levelManagers_; |
| 409 |
|
| 410 |
//! Caching (Multi)Vectors used in Hierarchy::Iterate() |
| 411 |
int sizeOfAllocatedLevelMultiVectors_; |
| 412 |
Array<RCP<MultiVector> > residual_, coarseRhs_, coarseX_, coarseImport_, coarseExport_, correction_; |
| 413 |
|
| 414 |
}; // class Hierarchy |
| 415 |
|
| 416 |
} // namespace MueLu |
| 417 |
|
| 418 |
#define MUELU_HIERARCHY_SHORT |
| 419 |
#endif // MUELU_HIERARCHY_DECL_HPP |