MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_Hierarchy_decl.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // MueLu: A package for multigrid based preconditioning
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact
39 // Jonathan Hu (jhu@sandia.gov)
40 // Andrey Prokopenko (aprokop@sandia.gov)
41 // Ray Tuminaro (rstumin@sandia.gov)
42 //
43 // ***********************************************************************
44 //
45 // @HEADER
46 #ifndef MUELU_HIERARCHY_DECL_HPP
47 #define MUELU_HIERARCHY_DECL_HPP
48 
50 #include <Teuchos_Ptr.hpp>
51 
52 #include <Xpetra_ConfigDefs.hpp> // global_size_t
53 #include <Xpetra_Matrix_fwd.hpp>
56 #include <Xpetra_Operator_fwd.hpp>
57 
58 #include <MueLu_SmootherCloner.hpp>
59 #include "MueLu_ConfigDefs.hpp"
60 #include "MueLu_BaseClass.hpp"
61 #include "MueLu_Hierarchy_fwd.hpp"
62 
63 #include "MueLu_Types.hpp"
64 
66 #include "MueLu_FactoryManager.hpp" // no fwd declaration because constructor of FactoryManager is used as a default parameter of Setup()
67 #include "MueLu_KeepType.hpp"
68 #include "MueLu_Level_fwd.hpp"
69 #include "MueLu_MasterList.hpp"
70 #include "MueLu_NoFactory.hpp"
71 #include "MueLu_PerfUtils_fwd.hpp"
72 #include "MueLu_PFactory_fwd.hpp"
75 #include "MueLu_Utilities_fwd.hpp"
76 
77 namespace MueLu {
78 
79 enum class ConvergenceStatus {
80  Converged,
82  Undefined
83 };
84 
95 template <class Scalar = DefaultScalar,
98  class Node = DefaultNode>
99 class Hierarchy : public BaseClass {
100 #undef MUELU_HIERARCHY_SHORT
101 #include "MueLu_UseShortNames.hpp"
102 
105 
107  struct ConvData {
109  : maxIts_(1)
110  , tol_(-STS::magnitude(STS::one())) {}
111  ConvData(LO maxIts)
112  : maxIts_(maxIts)
113  , tol_(-STS::magnitude(STS::one())) {}
115  : maxIts_(10000)
116  , tol_(tol) {}
117  ConvData(std::pair<LO, MagnitudeType> p)
118  : maxIts_(p.first)
119  , tol_(p.second) {}
120 
123  };
124 
125  public:
127 
128 
130  Hierarchy();
132  Hierarchy(const std::string& label);
133 
135  Hierarchy(const RCP<Matrix>& A);
136 
138  Hierarchy(const RCP<Matrix>& A, const std::string& label);
139 
141  virtual ~Hierarchy() {}
142 
144 
146 
147 
149  static CycleType GetDefaultCycle() { return MasterList::getDefault<std::string>("cycle type") == "V" ? VCYCLE : WCYCLE; }
150  static int GetDefaultCycleStartLevel() { return MasterList::getDefault<int>("W cycle start level"); }
151  static bool GetDefaultImplicitTranspose() { return MasterList::getDefault<bool>("transpose: use implicit"); }
152  static bool GetDefaultFuseProlongationAndUpdate() { return MasterList::getDefault<bool>("fuse prolongation and update"); }
153  static Xpetra::global_size_t GetDefaultMaxCoarseSize() { return MasterList::getDefault<int>("coarse: max size"); }
154  static int GetDefaultMaxLevels() { return MasterList::getDefault<int>("max levels"); }
155  static bool GetDefaultPRrebalance() { return MasterList::getDefault<bool>("repartition: rebalance P and R"); }
156 
158  bool GetImplicitTranspose() const { return implicitTranspose_; }
160 
161  void SetMaxCoarseSize(Xpetra::global_size_t maxCoarseSize) { maxCoarseSize_ = maxCoarseSize; }
162  void SetPRrebalance(bool doPRrebalance) { doPRrebalance_ = doPRrebalance; }
163  void SetPRViaCopyrebalance(bool doPRViaCopyrebalance) { doPRViaCopyrebalance_ = doPRViaCopyrebalance; }
164  void SetImplicitTranspose(const bool& implicit) { implicitTranspose_ = implicit; }
165  void SetFuseProlongationAndUpdate(const bool& fuse) { fuseProlongationAndUpdate_ = fuse; }
166 
168 
170 
171  template <class S2, class LO2, class GO2, class N2>
172  friend class Hierarchy;
173 
174  private:
175  int LastLevelID() const { return Levels_.size() - 1; }
176  void DumpCurrentGraph(int level) const;
177 
178  public:
180  void AddLevel(const RCP<Level>& level);
181 
183  void AddNewLevel();
184 
186  RCP<Level>& GetLevel(const int levelID = 0);
187 
188  int GetNumLevels() const;
189  int GetGlobalNumLevels() const;
190 
191  MagnitudeType GetRate() const { return rate_; }
192 
193  // This function is global
194  double GetOperatorComplexity() const;
195 
196  // This function is global
197  double GetSmootherComplexity() const;
198 
200  void CheckLevel(Level& level, int levelID);
201 
202  void SetMatvecParams(RCP<ParameterList> matvecParams);
203 
205 
242  bool Setup(int coarseLevelID, const RCP<const FactoryManagerBase> fineLevelManager /* = Teuchos::null */, const RCP<const FactoryManagerBase> coarseLevelManager,
243  const RCP<const FactoryManagerBase> nextLevelManager = Teuchos::null);
244 
246  void Setup(const FactoryManagerBase& manager = FactoryManager(), int startLevel = 0, int numDesiredLevels = GetDefaultMaxLevels());
247 
248  void SetupRe();
249 
251 
253  void Clear(int startLevel = 0);
254  void ExpertClear();
255 
257  CycleType GetCycle() const { return Cycle_; }
258 
260  void SetCycle(CycleType Cycle) { Cycle_ = Cycle; }
261 
262  void SetCycleStartLevel(int cycleStart) { WCycleStartLevel_ = cycleStart; }
263 
265  void SetProlongatorScalingFactor(double scalingFactor) { scalingFactor_ = scalingFactor; }
266 
279  ConvergenceStatus Iterate(const MultiVector& B, MultiVector& X, ConvData conv = ConvData(),
280  bool InitialGuessIsZero = false, LO startLevel = 0);
281 
291  void Write(const LO& start = -1, const LO& end = -1, const std::string& suffix = "");
292 
294 
296 
297 
299  void Keep(const std::string& ename, const FactoryBase* factory = NoFactory::get());
300 
302  void Delete(const std::string& ename, const FactoryBase* factory = NoFactory::get());
303 
305  void AddKeepFlag(const std::string& ename, const FactoryBase* factory = NoFactory::get(), KeepType keep = MueLu::Keep);
306 
308  void RemoveKeepFlag(const std::string& ename, const FactoryBase* factory, KeepType keep = MueLu::All);
309 
311 
313 
314 
316  std::string description() const;
317 
323  void describe(Teuchos::FancyOStream& out, const VerbLevel verbLevel = Default) const;
325 
327  void print(std::ostream& out = std::cout, const VerbLevel verbLevel = (MueLu::Parameters | MueLu::Statistics0)) const;
328 
333  void IsPreconditioner(const bool flag);
334 
336 
337  void EnableGraphDumping(const std::string& filename, int levelID = 1) {
338  isDumpingEnabled_ = true;
339  dumpLevel_ = levelID;
340  dumpFile_ = filename;
341  }
342 
343  void setlib(Xpetra::UnderlyingLib inlib) { lib_ = inlib; }
345 
348  description_ = "";
349  }
350 
351  void AllocateLevelMultiVectors(int numvecs, bool forceMapCheck = false);
353 
354  protected:
355  const RCP<const FactoryManagerBase>& GetLevelManager(const int levelID) const {
356  return levelManagers_[levelID];
357  }
358 
359  private:
361  Hierarchy(const Hierarchy& h);
362 
364  bool IsCalculationOfResidualRequired(const LO startLevel, const ConvData& conv) const;
365 
374  const MagnitudeType convergenceTolerance) const;
375 
377  void PrintResidualHistory(const LO iteration,
378  const Teuchos::Array<MagnitudeType>& residualNorm) const;
379 
381  ConvergenceStatus ComputeResidualAndPrintHistory(const Operator& A, const MultiVector& X,
382  const MultiVector& B, const LO iteration,
383  const LO startLevel, const ConvData& conv, MagnitudeType& previousResidualNorm);
384 
387 
392  void ReplaceCoordinateMap(Level& level);
393 
397 
401 
405 
409  bool doPRViaCopyrebalance_; // fully explicit, needed for CombinePFactory
410 
413 
416 
419 
422 
425 
427  mutable std::string description_ = ""; // mutable so that we can lazily initialize in description(), which is declared const
428 
435  // -1 = dump all levels, -2 = dump nothing
437  std::string dumpFile_;
438 
441 
444 
448 
449 }; // class Hierarchy
450 
451 } // namespace MueLu
452 
453 #define MUELU_HIERARCHY_SHORT
454 #endif // MUELU_HIERARCHY_DECL_HPP
void IsPreconditioner(const bool flag)
Array< RCP< MultiVector > > coarseExport_
This class specifies the default factory that should generate some data on a Level if the data does n...
MueLu::DefaultLocalOrdinal LocalOrdinal
RCP< Level > & GetLevel(const int levelID=0)
Retrieve a certain level from hierarchy.
Tpetra::KokkosClassic::DefaultNode::DefaultNodeType DefaultNode
Hierarchy()
Default constructor.
Array< RCP< MultiVector > > coarseImport_
static bool GetDefaultFuseProlongationAndUpdate()
Print parameters.
void AddLevel(const RCP< Level > &level)
Add a level at the end of the hierarchy.
void CheckForEmptySmoothersAndCoarseSolve()
void PrintResidualHistory(const LO iteration, const Teuchos::Array< MagnitudeType > &residualNorm) const
Print residualNorm for this iteration to the screen.
short KeepType
int sizeOfAllocatedLevelMultiVectors_
Caching (Multi)Vectors used in Hierarchy::Iterate()
int WCycleStartLevel_
Level at which to start W-cycle.
void SetMaxCoarseSize(Xpetra::global_size_t maxCoarseSize)
void DumpCurrentGraph(int level) const
Xpetra::global_size_t GetMaxCoarseSize() const
void AddNewLevel()
Add a new level at the end of the hierarchy.
LocalOrdinal LO
static Xpetra::global_size_t GetDefaultMaxCoarseSize()
void SetCycle(CycleType Cycle)
Supports VCYCLE and WCYCLE types.
void Clear(int startLevel=0)
Clear impermanent data from previous setup.
void SetFuseProlongationAndUpdate(const bool &fuse)
bool GetFuseProlongationAndUpdate() const
bool GetImplicitTranspose() const
const RCP< const FactoryManagerBase > & GetLevelManager(const int levelID) const
Teuchos::ScalarTraits< SC > STS
MueLu::DefaultNode Node
std::string description_
cache description to avoid recreating in each call to description() - use ResetDescription() to force...
MagnitudeType rate_
Convergece rate.
static const NoFactory * get()
ConvergenceStatus Iterate(const MultiVector &B, MultiVector &X, ConvData conv=ConvData(), bool InitialGuessIsZero=false, LO startLevel=0)
Apply the multigrid preconditioner.
void ReplaceCoordinateMap(Level &level)
void AddKeepFlag(const std::string &ename, const FactoryBase *factory=NoFactory::get(), KeepType keep=MueLu::Keep)
Call Level::AddKeepFlag for each level of the Hierarchy.
void print(std::ostream &out=std::cout, const VerbLevel verbLevel=(MueLu::Parameters|MueLu::Statistics0)) const
Hierarchy::print is local hierarchy function, thus the statistics can be different from global ones...
Xpetra::UnderlyingLib lib()
double GetSmootherComplexity() const
Base class for factories (e.g., R, P, and A_coarse).
Print statistics that do not involve significant additional computation.
ConvData(std::pair< LO, MagnitudeType > p)
Xpetra::UnderlyingLib lib_
Epetra/Tpetra mode.
static CycleType GetDefaultCycle()
MueLu::DefaultScalar Scalar
Tpetra::Details::DefaultTypes::scalar_type DefaultScalar
MueLu::DefaultGlobalOrdinal GlobalOrdinal
void Write(const LO &start=-1, const LO &end=-1, const std::string &suffix="")
Print matrices in the multigrid hierarchy to file.
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:99
std::string description() const
Return a simple one-line description of this object.
void SetCycleStartLevel(int cycleStart)
Class that provides default factories within Needs class.
void ResetDescription()
force recreation of cached description_ next time description() is called:
void RemoveKeepFlag(const std::string &ename, const FactoryBase *factory, KeepType keep=MueLu::All)
Call Level::RemoveKeepFlag for each level of the Hierarchy.
void CheckLevel(Level &level, int levelID)
Helper function.
Always keep data, even accross run. This flag is set by Level::Keep(). This flag is propagated to coa...
static bool GetDefaultImplicitTranspose()
void AllocateLevelMultiVectors(int numvecs, bool forceMapCheck=false)
void SetPRrebalance(bool doPRrebalance)
size_t global_size_t
void SetMatvecParams(RCP< ParameterList > matvecParams)
int GetGlobalNumLevels() const
void SetPRViaCopyrebalance(bool doPRViaCopyrebalance)
static int GetDefaultMaxLevels()
CycleType GetCycle() const
Returns multigrid cycle type (supports VCYCLE and WCYCLE)
Base class for MueLu classes.
void SetImplicitTranspose(const bool &implicit)
CycleType Cycle_
V- or W-cycle.
virtual ~Hierarchy()
Destructor.
Data struct for defining stopping criteria of multigrid iteration.
Array< RCP< MultiVector > > coarseRhs_
STS::magnitudeType MagnitudeType
void Delete(const std::string &ename, const FactoryBase *factory=NoFactory::get())
Call Level::Delete(ename, factory) for each level of the Hierarchy.
Array< RCP< Level > > Levels_
Container for Level objects.
bool isPreconditioner_
Hierarchy may be used in a standalone mode, or as a preconditioner.
static bool GetDefaultPRrebalance()
bool IsCalculationOfResidualRequired(const LO startLevel, const ConvData &conv) const
Decide if the residual needs to be computed.
MagnitudeType GetRate() const
static int GetDefaultCycleStartLevel()
void SetProlongatorScalingFactor(double scalingFactor)
Specify damping factor alpha such that x = x + alpha*P*c, where c is the coarse grid correction...
void describe(Teuchos::FancyOStream &out, const VerbLevel verbLevel=Default) const
Print the Hierarchy with some verbosity level to a FancyOStream object.
double scalingFactor_
Scaling factor to be applied to coarse grid correction.
Array< RCP< MultiVector > > residual_
void Keep(const std::string &ename, const FactoryBase *factory=NoFactory::get())
Call Level::Keep(ename, factory) for each level of the Hierarchy.
ConvergenceStatus IsConverged(const Teuchos::Array< MagnitudeType > &residualNorm, const MagnitudeType convergenceTolerance) const
Decide if the multigrid iteration is converged.
bool isDumpingEnabled_
Graph dumping.
void setlib(Xpetra::UnderlyingLib inlib)
double GetOperatorComplexity() const
bool Setup(int coarseLevelID, const RCP< const FactoryManagerBase > fineLevelManager, const RCP< const FactoryManagerBase > coarseLevelManager, const RCP< const FactoryManagerBase > nextLevelManager=Teuchos::null)
Multi-level setup phase: build a new level of the hierarchy.
Array< RCP< MultiVector > > coarseX_
Array< RCP< MultiVector > > correction_
void EnableGraphDumping(const std::string &filename, int levelID=1)
ConvergenceStatus ComputeResidualAndPrintHistory(const Operator &A, const MultiVector &X, const MultiVector &B, const LO iteration, const LO startLevel, const ConvData &conv, MagnitudeType &previousResidualNorm)
Compute the residual norm and print it depending on the verbosity level.
Provides methods to build a multigrid hierarchy and apply multigrid cycles.
Array< RCP< const FactoryManagerBase > > levelManagers_
Level managers used during the Setup.
Xpetra::global_size_t maxCoarseSize_