MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_EminPFactory_def.hpp
Go to the documentation of this file.
1 #ifndef MUELU_EMINPFACTORY_DEF_HPP
2 #define MUELU_EMINPFACTORY_DEF_HPP
3 
4 #include <Xpetra_Matrix.hpp>
5 #include <Xpetra_StridedMapFactory.hpp>
6 
8 
9 #include "MueLu_CGSolver.hpp"
10 #include "MueLu_Constraint.hpp"
11 #include "MueLu_GMRESSolver.hpp"
12 #include "MueLu_MasterList.hpp"
13 #include "MueLu_Monitor.hpp"
14 #include "MueLu_PerfUtils.hpp"
15 #include "MueLu_SolverBase.hpp"
16 #include "MueLu_SteepestDescentSolver.hpp"
17 
18 namespace MueLu {
19 
20 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
22  RCP<ParameterList> validParamList = rcp(new ParameterList());
23 
24 #define SET_VALID_ENTRY(name) validParamList->setEntry(name, MasterList::getEntry(name))
25  SET_VALID_ENTRY("emin: num iterations");
26  SET_VALID_ENTRY("emin: num reuse iterations");
27  SET_VALID_ENTRY("emin: iterative method");
28  {
29  validParamList->getEntry("emin: iterative method").setValidator(rcp(new Teuchos::StringValidator(Teuchos::tuple<std::string>("cg", "sd", "gmres"))));
30  }
31 #undef SET_VALID_ENTRY
32 
33  validParamList->set<RCP<const FactoryBase> >("A", Teuchos::null, "Generating factory for the matrix A used during internal iterations");
34  validParamList->set<RCP<const FactoryBase> >("P", Teuchos::null, "Generating factory for the initial guess");
35  validParamList->set<RCP<const FactoryBase> >("Constraint", Teuchos::null, "Generating factory for constraints");
36 
37  validParamList->set<RCP<Matrix> >("P0", Teuchos::null, "Initial guess at P");
38  validParamList->set<bool>("Keep P0", false, "Keep an initial P0 (for reuse)");
39 
40  validParamList->set<RCP<Constraint> >("Constraint0", Teuchos::null, "Initial Constraint");
41  validParamList->set<bool>("Keep Constraint0", false, "Keep an initial Constraint (for reuse)");
42 
43  return validParamList;
44 }
45 
46 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
48  Input(fineLevel, "A");
49 
50  static bool isAvailableP0 = false;
51  static bool isAvailableConstraint0 = false;
52 
53  // Here is a tricky little piece of code
54  // We don't want to request (aka call Input) when we reuse and P0 is available
55  // However, we cannot run something simple like this:
56  // if (!coarseLevel.IsAvailable("P0", this))
57  // Input(coarseLevel, "P");
58  // The reason is that it works fine during the request stage, but fails
59  // in the release stage as we _construct_ P0 during Build process. Therefore,
60  // we need to understand whether we are in Request or Release mode
61  // NOTE: This is a very unique situation, please try not to propagate the
62  // mode check any further
63 
64  if (coarseLevel.GetRequestMode() == Level::REQUEST) {
65  isAvailableP0 = coarseLevel.IsAvailable("P0", this);
66  isAvailableConstraint0 = coarseLevel.IsAvailable("Constraint0", this);
67  }
68 
69  if (isAvailableP0 == false)
70  Input(coarseLevel, "P");
71 
72  if (isAvailableConstraint0 == false)
73  Input(coarseLevel, "Constraint");
74 }
75 
76 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
78  BuildP(fineLevel, coarseLevel);
79 }
80 
81 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
83  FactoryMonitor m(*this, "Prolongator minimization", coarseLevel);
84 
85  const ParameterList& pL = GetParameterList();
86 
87  // Get the matrix
88  RCP<Matrix> A = Get<RCP<Matrix> >(fineLevel, "A");
89 
90  if (restrictionMode_) {
91  SubFactoryMonitor m2(*this, "Transpose A", coarseLevel);
92 
93  A = Utilities::Transpose(*A, true);
94  }
95 
96  // Get/make initial guess
97  RCP<Matrix> P0;
98  int numIts;
99  if (coarseLevel.IsAvailable("P0", this)) {
100  // Reuse data
101  P0 = coarseLevel.Get<RCP<Matrix> >("P0", this);
102  numIts = pL.get<int>("emin: num reuse iterations");
103  GetOStream(Runtime0) << "Reusing P0" << std::endl;
104 
105  } else {
106  // Construct data
107  P0 = Get<RCP<Matrix> >(coarseLevel, "P");
108  numIts = pL.get<int>("emin: num iterations");
109  }
110  // NOTE: the main assumption here that P0 satisfies both constraints:
111  // - nonzero pattern
112  // - nullspace preservation
113 
114  // Get/make constraint operator
115  RCP<Constraint> X;
116  if (coarseLevel.IsAvailable("Constraint0", this)) {
117  // Reuse data
118  X = coarseLevel.Get<RCP<Constraint> >("Constraint0", this);
119  GetOStream(Runtime0) << "Reusing Constraint0" << std::endl;
120 
121  } else {
122  // Construct data
123  X = Get<RCP<Constraint> >(coarseLevel, "Constraint");
124  }
125  GetOStream(Runtime0) << "Number of emin iterations = " << numIts << std::endl;
126 
127  std::string solverType = pL.get<std::string>("emin: iterative method");
128  RCP<SolverBase> solver;
129  if (solverType == "cg")
130  solver = rcp(new CGSolver(numIts));
131  else if (solverType == "sd")
132  solver = rcp(new SteepestDescentSolver(numIts));
133  else if (solverType == "gmres")
134  solver = rcp(new GMRESSolver(numIts));
135 
136  RCP<Matrix> P;
137  solver->Iterate(*A, *X, *P0, P);
138 
139  // NOTE: EXPERIMENTAL and FRAGILE
140  if (!P->IsView("stridedMaps")) {
141  if (A->IsView("stridedMaps") == true) {
142  GetOStream(Runtime1) << "Using A to fillComplete P" << std::endl;
143 
144  // FIXME: X->GetPattern() actually returns a CrsGraph.
145  // CrsGraph has no knowledge of Xpetra's sup/Matrix views. As such,
146  // it has no idea about strided maps. We create one, which is
147  // most likely incorrect for many use cases.
148  std::vector<size_t> stridingInfo(1, 1);
149  RCP<const StridedMap> dMap = StridedMapFactory::Build(X->GetPattern()->getDomainMap(), stridingInfo);
150 
151  P->CreateView("stridedMaps", A->getRowMap("stridedMaps"), dMap);
152 
153  } else {
154  P->CreateView("stridedMaps", P->getRangeMap(), P->getDomainMap());
155  }
156  }
157 
158  // Level Set
159  if (!restrictionMode_) {
160  // The factory is in prolongation mode
161  Set(coarseLevel, "P", P);
162 
163  if (pL.get<bool>("Keep P0")) {
164  // NOTE: we must do Keep _before_ set as the Needs class only sets if
165  // a) data has been requested (which is not the case here), or
166  // b) data has some keep flag
167  coarseLevel.Keep("P0", this);
168  Set(coarseLevel, "P0", P);
169  }
170  if (pL.get<bool>("Keep Constraint0")) {
171  // NOTE: we must do Keep _before_ set as the Needs class only sets if
172  // a) data has been requested (which is not the case here), or
173  // b) data has some keep flag
174  coarseLevel.Keep("Constraint0", this);
175  Set(coarseLevel, "Constraint0", X);
176  }
177 
178  if (IsPrint(Statistics2)) {
179  RCP<ParameterList> params = rcp(new ParameterList());
180  params->set("printLoadBalancingInfo", true);
181  params->set("printCommInfo", true);
182  GetOStream(Statistics2) << PerfUtils::PrintMatrixInfo(*P, "P", params);
183  }
184 
185  } else {
186  // The factory is in restriction mode
187  RCP<Matrix> R;
188  {
189  SubFactoryMonitor m2(*this, "Transpose P", coarseLevel);
190 
191  R = Utilities::Transpose(*P, true);
192  }
193 
194  Set(coarseLevel, "R", R);
195 
196  if (IsPrint(Statistics2)) {
197  RCP<ParameterList> params = rcp(new ParameterList());
198  params->set("printLoadBalancingInfo", true);
199  params->set("printCommInfo", true);
200  GetOStream(Statistics2) << PerfUtils::PrintMatrixInfo(*R, "R", params);
201  }
202  }
203 }
204 
205 } // namespace MueLu
206 
207 #endif // MUELU_EMINPFACTORY_DEF_HPP
void Keep(const std::string &ename, const FactoryBase *factory)
Request to keep variable &#39;ename&#39; generated by &#39;factory&#39; after the setup phase.
#define SET_VALID_ENTRY(name)
T & Get(const std::string &ename, const FactoryBase *factory=NoFactory::get())
Get data without decrementing associated storage counter (i.e., read-only access). Usage: Level-&gt;Get&lt; RCP&lt;Matrix&gt; &gt;(&quot;A&quot;, factory) if factory == NULL =&gt; use default factory.
void Build(Level &fineLevel, Level &coarseLevel) const
Build method.
void setValidator(RCP< const ParameterEntryValidator > const &validator)
T & get(const std::string &name, T def_value)
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Implements conjugate gradient algorithm for energy-minimization.
void BuildP(Level &fineLevel, Level &coarseLevel) const
Abstract Build method.
Timer to be used in factories. Similar to Monitor but with additional timers.
One-liner description of what is happening.
RCP< const CrsGraph > GetPattern() const
Print even more statistics.
RequestMode GetRequestMode() const
Implements conjugate gradient algorithm for energy-minimization.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:99
Timer to be used in factories. Similar to SubMonitor but adds a timer level by level.
static RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Transpose(Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, bool optimizeTranspose=false, const std::string &label=std::string(), const Teuchos::RCP< Teuchos::ParameterList > &params=Teuchos::null)
static std::string PrintMatrixInfo(const Matrix &A, const std::string &msgTag, RCP< const Teuchos::ParameterList > params=Teuchos::null)
void DeclareInput(Level &fineLevel, Level &coarseLevel) const
Input.
Implements steepest descent algorithm for energy-minimization.
Description of what is happening (more verbose)
ParameterEntry & getEntry(const std::string &name)
bool IsAvailable(const std::string &ename, const FactoryBase *factory=NoFactory::get()) const
Test whether a need&#39;s value has been saved.
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.