MueLu  Version of the Day
MueLu_RAPFactory_def.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_RAPFACTORY_DEF_HPP
47 #define MUELU_RAPFACTORY_DEF_HPP
48 
49 
50 #include <sstream>
51 
52 #include <Xpetra_Matrix.hpp>
53 #include <Xpetra_MatrixFactory.hpp>
54 #include <Xpetra_MatrixMatrix.hpp>
55 #include <Xpetra_Vector.hpp>
56 #include <Xpetra_VectorFactory.hpp>
57 
59 
60 #include "MueLu_MasterList.hpp"
61 #include "MueLu_Monitor.hpp"
62 #include "MueLu_PerfUtils.hpp"
64 //#include "MueLu_Utilities.hpp"
65 
66 namespace MueLu {
67 
68  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
70  : hasDeclaredInput_(false) { }
71 
72  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
74  RCP<ParameterList> validParamList = rcp(new ParameterList());
75 
76 #define SET_VALID_ENTRY(name) validParamList->setEntry(name, MasterList::getEntry(name))
77  SET_VALID_ENTRY("transpose: use implicit");
78  SET_VALID_ENTRY("rap: fix zero diagonals");
79 #undef SET_VALID_ENTRY
80  validParamList->set< RCP<const FactoryBase> >("A", null, "Generating factory of the matrix A used during the prolongator smoothing process");
81  validParamList->set< RCP<const FactoryBase> >("P", null, "Prolongator factory");
82  validParamList->set< RCP<const FactoryBase> >("R", null, "Restrictor factory");
83 
84  validParamList->set< bool > ("CheckMainDiagonal", false, "Check main diagonal for zeros");
85  validParamList->set< bool > ("RepairMainDiagonal", false, "Repair zeros on main diagonal");
86 
87  // Make sure we don't recursively validate options for the matrixmatrix kernels
88  ParameterList norecurse;
89  norecurse.disableRecursiveValidation();
90  validParamList->set<ParameterList> ("matrixmatrix: kernel params", norecurse, "MatrixMatrix kernel parameters");
91 
92  return validParamList;
93  }
94 
95  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
97  const Teuchos::ParameterList& pL = GetParameterList();
98  if (pL.get<bool>("transpose: use implicit") == false)
99  Input(coarseLevel, "R");
100 
101  Input(fineLevel, "A");
102  Input(coarseLevel, "P");
103 
104  // call DeclareInput of all user-given transfer factories
105  for (std::vector<RCP<const FactoryBase> >::const_iterator it = transferFacts_.begin(); it != transferFacts_.end(); ++it)
106  (*it)->CallDeclareInput(coarseLevel);
107 
108  hasDeclaredInput_ = true;
109  }
110 
111  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
113  const bool doTranspose = true;
114  const bool doFillComplete = true;
115  const bool doOptimizeStorage = true;
116  {
117  FactoryMonitor m(*this, "Computing Ac", coarseLevel);
118  std::ostringstream levelstr;
119  levelstr << coarseLevel.GetLevelID();
120 
121  TEUCHOS_TEST_FOR_EXCEPTION(hasDeclaredInput_ == false, Exceptions::RuntimeError,
122  "MueLu::RAPFactory::Build(): CallDeclareInput has not been called before Build!");
123 
124  const Teuchos::ParameterList& pL = GetParameterList();
125  RCP<Matrix> A = Get< RCP<Matrix> >(fineLevel, "A");
126  RCP<Matrix> P = Get< RCP<Matrix> >(coarseLevel, "P"), AP, Ac;
127 
128  // Reuse pattern if available (multiple solve)
129  RCP<ParameterList> APparams;
130  if(pL.isSublist("matrixmatrix: kernel params"))
131  APparams=rcp(new ParameterList(pL.sublist("matrixmatrix: kernel params")));
132  else
133  APparams= rcp(new ParameterList);
134 
135  // By default, we don't need global constants for A*P
136  APparams->set("compute global constants: temporaries",APparams->get("compute global constants: temporaries",false));
137  APparams->set("compute global constants",APparams->get("compute global constants",false));
138 
139  if (coarseLevel.IsAvailable("AP reuse data", this)) {
140  GetOStream(static_cast<MsgType>(Runtime0 | Test)) << "Reusing previous AP data" << std::endl;
141 
142  APparams = coarseLevel.Get< RCP<ParameterList> >("AP reuse data", this);
143 
144  if (APparams->isParameter("graph"))
145  AP = APparams->get< RCP<Matrix> >("graph");
146  }
147 
148  {
149  SubFactoryMonitor subM(*this, "MxM: A x P", coarseLevel);
150 
151  AP = MatrixMatrix::Multiply(*A, !doTranspose, *P, !doTranspose, AP, GetOStream(Statistics2),
152  doFillComplete, doOptimizeStorage, std::string("MueLu::A*P-")+levelstr.str(), APparams);
153  }
154 
155  // Reuse coarse matrix memory if available (multiple solve)
156  RCP<ParameterList> RAPparams;
157  if(pL.isSublist("matrixmatrix: kernel params")) RAPparams=rcp(new ParameterList(pL.sublist("matrixmatrix: kernel params")));
158  else RAPparams= rcp(new ParameterList);
159 
160  if (coarseLevel.IsAvailable("RAP reuse data", this)) {
161  GetOStream(static_cast<MsgType>(Runtime0 | Test)) << "Reusing previous RAP data" << std::endl;
162 
163  RAPparams = coarseLevel.Get< RCP<ParameterList> >("RAP reuse data", this);
164 
165  if (RAPparams->isParameter("graph"))
166  Ac = RAPparams->get< RCP<Matrix> >("graph");
167 
168  // Some eigenvalue may have been cached with the matrix in the previous run.
169  // As the matrix values will be updated, we need to reset the eigenvalue.
170  Ac->SetMaxEigenvalueEstimate(-Teuchos::ScalarTraits<SC>::one());
171  }
172 
173  // We *always* need global constants for the RAP, but not for the temps
174  RAPparams->set("compute global constants: temporaries",RAPparams->get("compute global constants: temporaries",false));
175  RAPparams->set("compute global constants",true);
176 
177  // Allow optimization of storage.
178  // This is necessary for new faster Epetra MM kernels.
179  // Seems to work with matrix modifications to repair diagonal entries.
180 
181  if (pL.get<bool>("transpose: use implicit") == true) {
182  SubFactoryMonitor m2(*this, "MxM: P' x (AP) (implicit)", coarseLevel);
183 
184  Ac = MatrixMatrix::Multiply(*P, doTranspose, *AP, !doTranspose, Ac, GetOStream(Statistics2),
185  doFillComplete, doOptimizeStorage, std::string("MueLu::R*(AP)-implicit-")+levelstr.str(), RAPparams);
186 
187  } else {
188  RCP<Matrix> R = Get< RCP<Matrix> >(coarseLevel, "R");
189 
190  SubFactoryMonitor m2(*this, "MxM: R x (AP) (explicit)", coarseLevel);
191 
192  Ac = MatrixMatrix::Multiply(*R, !doTranspose, *AP, !doTranspose, Ac, GetOStream(Statistics2),
193  doFillComplete, doOptimizeStorage, std::string("MueLu::R*(AP)-explicit-")+levelstr.str(), RAPparams);
194  }
195 
197 
198  if (IsPrint(Statistics1)) {
199  RCP<ParameterList> params = rcp(new ParameterList());;
200  params->set("printLoadBalancingInfo", true);
201  params->set("printCommInfo", true);
202  GetOStream(Statistics1) << PerfUtils::PrintMatrixInfo(*Ac, "Ac", params);
203  }
204 
205  Set(coarseLevel, "A", Ac);
206 
207  APparams->set("graph", AP);
208  Set(coarseLevel, "AP reuse data", APparams);
209  RAPparams->set("graph", Ac);
210  Set(coarseLevel, "RAP reuse data", RAPparams);
211  }
212 
213  if (transferFacts_.begin() != transferFacts_.end()) {
214  SubFactoryMonitor m(*this, "Projections", coarseLevel);
215 
216  // call Build of all user-given transfer factories
217  for (std::vector<RCP<const FactoryBase> >::const_iterator it = transferFacts_.begin(); it != transferFacts_.end(); ++it) {
218  RCP<const FactoryBase> fac = *it;
219  GetOStream(Runtime0) << "RAPFactory: call transfer factory: " << fac->description() << std::endl;
220  fac->CallBuild(coarseLevel);
221  // Coordinates transfer is marginally different from all other operations
222  // because it is *optional*, and not required. For instance, we may need
223  // coordinates only on level 4 if we start repartitioning from that level,
224  // but we don't need them on level 1,2,3. As our current Hierarchy setup
225  // assumes propagation of dependencies only through three levels, this
226  // means that we need to rely on other methods to propagate optional data.
227  //
228  // The method currently used is through RAP transfer factories, which are
229  // simply factories which are called at the end of RAP with a single goal:
230  // transfer some fine data to coarser level. Because these factories are
231  // kind of outside of the mainline factories, they behave different. In
232  // particular, we call their Build method explicitly, rather than through
233  // Get calls. This difference is significant, as the Get call is smart
234  // enough to know when to release all factory dependencies, and Build is
235  // dumb. This led to the following CoordinatesTransferFactory sequence:
236  // 1. Request level 0
237  // 2. Request level 1
238  // 3. Request level 0
239  // 4. Release level 0
240  // 5. Release level 1
241  //
242  // The problem is missing "6. Release level 0". Because it was missing,
243  // we had outstanding request on "Coordinates", "Aggregates" and
244  // "CoarseMap" on level 0.
245  //
246  // This was fixed by explicitly calling Release on transfer factories in
247  // RAPFactory. I am still unsure how exactly it works, but now we have
248  // clear data requests for all levels.
249  coarseLevel.Release(*fac);
250  }
251  }
252 
253  }
254 
255  // Plausibility check: no zeros on diagonal
256  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
258  const Teuchos::ParameterList& pL = GetParameterList();
259  bool repairZeroDiagonals = pL.get<bool>("RepairMainDiagonal") || pL.get<bool>("rap: fix zero diagonals");
260  bool checkAc = pL.get<bool>("CheckMainDiagonal")|| pL.get<bool>("rap: fix zero diagonals"); ;
261 
262  if (!checkAc && !repairZeroDiagonals)
263  return;
264 
265  SC zero = Teuchos::ScalarTraits<SC>::zero(), one = Teuchos::ScalarTraits<SC>::one();
266 
267  Teuchos::RCP<Teuchos::ParameterList> p = Teuchos::rcp(new Teuchos::ParameterList());
268  p->set("DoOptimizeStorage", true);
269 
270  RCP<const Map> rowMap = Ac->getRowMap();
271  RCP<Vector> diagVec = VectorFactory::Build(rowMap);
272  Ac->getLocalDiagCopy(*diagVec);
273 
274  LO lZeroDiags = 0;
275  Teuchos::ArrayRCP< Scalar > diagVal = diagVec->getDataNonConst(0);
276 
277  for (size_t i = 0; i < rowMap->getNodeNumElements(); i++) {
278  if (diagVal[i] == zero) {
279  lZeroDiags++;
280  }
281  }
282  GO gZeroDiags;
283  MueLu_sumAll(rowMap->getComm(), Teuchos::as<GO>(lZeroDiags), gZeroDiags);
284 
285  if (repairZeroDiagonals && gZeroDiags > 0) {
286  // TAW: If Ac has empty rows, put a 1 on the diagonal of Ac. Be aware that Ac might have empty rows AND columns.
287  // The columns might not exist in the column map at all.
288  //
289  // It would be nice to add the entries to the original matrix Ac. But then we would have to use
290  // insertLocalValues. However we cannot add new entries for local column indices that do not exist in the column map
291  // of Ac (at least Epetra is not able to do this). With Tpetra it is also not possible to add new entries after the
292  // FillComplete call with a static map, since the column map already exists and the diagonal entries are completely missing.
293  //
294  // Here we build a diagonal matrix with zeros on the diagonal and ones on the diagonal for the rows where Ac has empty rows
295  // We have to build a new matrix to be able to use insertGlobalValues. Then we add the original matrix Ac to our new block
296  // diagonal matrix and use the result as new (non-singular) matrix Ac.
297  // This is very inefficient.
298  //
299  // If you know something better, please let me know.
300  RCP<Matrix> fixDiagMatrix = MatrixFactory::Build(rowMap, 1);
301  for (size_t r = 0; r < rowMap->getNodeNumElements(); r++) {
302  if (diagVal[r] == zero) {
303  GO grid = rowMap->getGlobalElement(r);
304  Teuchos::ArrayRCP<GO> indout(1,grid);
305  Teuchos::ArrayRCP<SC> valout(1, one);
306  fixDiagMatrix->insertGlobalValues(grid,indout.view(0, 1), valout.view(0, 1));
307  }
308  }
309  {
310  Teuchos::TimeMonitor m1(*Teuchos::TimeMonitor::getNewTimer("CheckRepairMainDiagonal: fillComplete1"));
311  if(rowMap->lib() == Xpetra::UseTpetra) Ac->resumeFill(); // TODO needed for refactored Tpetra because of the isFillActive flag???
312  Ac->fillComplete(p);
313  }
314  MatrixMatrix::TwoMatrixAdd(*Ac, false, 1.0, *fixDiagMatrix, 1.0);
315  if (Ac->IsView("stridedMaps"))
316  fixDiagMatrix->CreateView("stridedMaps", Ac);
317 
318  Ac = Teuchos::null; // free singular coarse level matrix
319  Ac = fixDiagMatrix; // set fixed non-singular coarse level matrix
320 
321  // call fillComplete with optimized storage option set to true
322  // This is necessary for new faster Epetra MM kernels.
323  {
324  Teuchos::TimeMonitor m1(*Teuchos::TimeMonitor::getNewTimer("CheckRepairMainDiagonal: fillComplete2"));
325  if(rowMap->lib() == Xpetra::UseTpetra) Ac->resumeFill(); // TODO needed for refactored Tpetra because of the isFillActive flag???
326  Ac->fillComplete(p);
327  }
328  } // end repair
329 
330 
331 
332  // print some output
333  if (IsPrint(Warnings0))
334  GetOStream(Warnings0) << "RAPFactory (WARNING): " << (repairZeroDiagonals ? "repaired " : "found ")
335  << gZeroDiags << " zeros on main diagonal of Ac." << std::endl;
336 
337 #ifdef HAVE_MUELU_DEBUG // only for debugging
338  // check whether Ac has been repaired...
339  Ac->getLocalDiagCopy(*diagVec);
340  Teuchos::ArrayRCP< Scalar > diagVal2 = diagVec->getDataNonConst(0);
341  for (size_t r = 0; r < Ac->getRowMap()->getNodeNumElements(); r++) {
342  if (diagVal2[r] == zero) {
343  GetOStream(Errors,-1) << "Error: there are zeros left on diagonal after repair..." << std::endl;
344  break;
345  }
346  }
347 #endif
348  }
349 
350  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
352  // check if it's a TwoLevelFactoryBase based transfer factory
353  TEUCHOS_TEST_FOR_EXCEPTION(Teuchos::rcp_dynamic_cast<const TwoLevelFactoryBase>(factory) == Teuchos::null, Exceptions::BadCast,
354  "MueLu::RAPFactory::AddTransferFactory: Transfer factory is not derived from TwoLevelFactoryBase. "
355  "This is very strange. (Note: you can remove this exception if there's a good reason for)");
356  TEUCHOS_TEST_FOR_EXCEPTION(hasDeclaredInput_, Exceptions::RuntimeError, "MueLu::RAPFactory::AddTransferFactory: Factory is being added after we have already declared input");
357  transferFacts_.push_back(factory);
358  }
359 
360 } //namespace MueLu
361 
362 #define MUELU_RAPFACTORY_SHORT
363 #endif // MUELU_RAPFACTORY_DEF_HPP
Important warning messages (one line)
#define SET_VALID_ENTRY(name)
Exception indicating invalid cast attempted.
#define MueLu_sumAll(rcpComm, in, out)
Teuchos::FancyOStream & GetOStream(MsgType type, int thisProcRankOnly=0) const
Get an output stream for outputting the input message type.
std::vector< RCP< const FactoryBase > > transferFacts_
list of user-defined transfer Factories
void Release(const FactoryBase &factory)
Decrement the storage counter for all the inputs of a factory.
Timer to be used in factories. Similar to Monitor but with additional timers.
Print more statistics.
One-liner description of what is happening.
void AddTransferFactory(const RCP< const FactoryBase > &factory)
Add transfer factory in the end of list of transfer factories in RepartitionAcFactory.
Namespace for MueLu classes and methods.
void CheckRepairMainDiagonal(RCP< Matrix > &Ac) const
bool IsPrint(MsgType type, int thisProcRankOnly=-1) const
Find out whether we need to print out information for a specific message type.
Print skeleton for the run, i.e. factory calls and used parameters.
Print even more statistics.
int GetLevelID() const
Return level number.
Definition: MueLu_Level.cpp:76
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
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 std::string PrintMatrixInfo(const Matrix &A, const std::string &msgTag, RCP< const Teuchos::ParameterList > params=Teuchos::null)
virtual const Teuchos::ParameterList & GetParameterList() const
void Input(Level &level, const std::string &varName) const
void DeclareInput(Level &fineLevel, Level &coarseLevel) const
Input.
Exception throws to report errors in the internal logical of the program.
void Set(Level &level, const std::string &varName, const T &data) const
void Build(Level &fineLevel, Level &coarseLevel) const
Build an object with this factory.