MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_Hierarchy_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 
47 #ifndef MUELU_HIERARCHY_DEF_HPP
48 #define MUELU_HIERARCHY_DEF_HPP
49 
50 #include <time.h>
51 
52 #include <algorithm>
53 #include <sstream>
54 
55 #include <Xpetra_Matrix.hpp>
57 #include <Xpetra_Operator.hpp>
58 #include <Xpetra_IO.hpp>
59 
60 #include "MueLu_Hierarchy_decl.hpp"
61 
62 #include "MueLu_BoostGraphviz.hpp"
63 #include "MueLu_FactoryManager.hpp"
64 #include "MueLu_HierarchyUtils.hpp"
65 #include "MueLu_TopRAPFactory.hpp"
66 #include "MueLu_TopSmootherFactory.hpp"
67 #include "MueLu_Level.hpp"
68 #include "MueLu_Monitor.hpp"
69 #include "MueLu_PerfUtils.hpp"
70 #include "MueLu_PFactory.hpp"
72 #include "MueLu_SmootherFactory.hpp"
73 #include "MueLu_SmootherBase.hpp"
74 
75 #include "Teuchos_TimeMonitor.hpp"
76 
77 
78 
79 namespace MueLu {
80 
81  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
83  : maxCoarseSize_(GetDefaultMaxCoarseSize()), implicitTranspose_(GetDefaultImplicitTranspose()),
84  doPRrebalance_(GetDefaultPRrebalance()), isPreconditioner_(true), Cycle_(GetDefaultCycle()),
85  scalingFactor_(Teuchos::ScalarTraits<double>::one()), lib_(Xpetra::UseTpetra), isDumpingEnabled_(false), dumpLevel_(-1), rate_(-1),
86  sizeOfAllocatedLevelMultiVectors_(0)
87  {
88  AddLevel(rcp(new Level));
89  }
90 
91  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
93  : Hierarchy()
94  {
95  setObjectLabel(label);
96  Levels_[0]->setObjectLabel(label);
97  }
98 
99  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
101  : maxCoarseSize_(GetDefaultMaxCoarseSize()), implicitTranspose_(GetDefaultImplicitTranspose()),
102  doPRrebalance_(GetDefaultPRrebalance()), isPreconditioner_(true), Cycle_(GetDefaultCycle()),
103  scalingFactor_(Teuchos::ScalarTraits<double>::one()), isDumpingEnabled_(false), dumpLevel_(-1), rate_(-1),
104  sizeOfAllocatedLevelMultiVectors_(0)
105  {
106  lib_ = A->getDomainMap()->lib();
107 
108  RCP<Level> Finest = rcp(new Level);
109  AddLevel(Finest);
110 
111  Finest->Set("A", A);
112  }
113 
114  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
116  : Hierarchy(A)
117  {
118  setObjectLabel(label);
119  Levels_[0]->setObjectLabel(label);
120  }
121 
122  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
124  int levelID = LastLevelID() + 1; // ID of the inserted level
125 
126  if (level->GetLevelID() != -1 && (level->GetLevelID() != levelID))
127  GetOStream(Warnings1) << "Hierarchy::AddLevel(): Level with ID=" << level->GetLevelID() <<
128  " have been added at the end of the hierarchy\n but its ID have been redefined" <<
129  " because last level ID of the hierarchy was " << LastLevelID() << "." << std::endl;
130 
131  Levels_.push_back(level);
132  level->SetLevelID(levelID);
133  level->setlib(lib_);
134 
135  level->SetPreviousLevel( (levelID == 0) ? Teuchos::null : Levels_[LastLevelID() - 1] );
136  level->setObjectLabel(this->getObjectLabel());
137  }
138 
139  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
141  RCP<Level> newLevel = Levels_[LastLevelID()]->Build(); // new coarse level, using copy constructor
142  newLevel->setlib(lib_);
143  this->AddLevel(newLevel); // add to hierarchy
144  }
145 
146  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
148  TEUCHOS_TEST_FOR_EXCEPTION(levelID < 0 || levelID > LastLevelID(), Exceptions::RuntimeError,
149  "MueLu::Hierarchy::GetLevel(): invalid input parameter value: LevelID = " << levelID);
150  return Levels_[levelID];
151  }
152 
153  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
155  return Levels_.size();
156  }
157 
158  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
160  RCP<Operator> A = Levels_[0]->template Get<RCP<Operator> >("A");
161  RCP<const Teuchos::Comm<int> > comm = A->getDomainMap()->getComm();
162 
163  int numLevels = GetNumLevels();
164  int numGlobalLevels;
165  Teuchos::reduceAll(*comm, Teuchos::REDUCE_MAX, numLevels, Teuchos::ptr(&numGlobalLevels));
166 
167  return numGlobalLevels;
168  }
169 
170  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
172  double totalNnz = 0, lev0Nnz = 1;
173  for (int i = 0; i < GetNumLevels(); ++i) {
174  TEUCHOS_TEST_FOR_EXCEPTION(!(Levels_[i]->IsAvailable("A")) , Exceptions::RuntimeError,
175  "Operator complexity cannot be calculated because A is unavailable on level " << i);
176  RCP<Operator> A = Levels_[i]->template Get<RCP<Operator> >("A");
177  if (A.is_null())
178  break;
179 
180  RCP<Matrix> Am = rcp_dynamic_cast<Matrix>(A);
181  if (Am.is_null()) {
182  GetOStream(Warnings0) << "Some level operators are not matrices, operator complexity calculation aborted" << std::endl;
183  return 0.0;
184  }
185 
186  totalNnz += as<double>(Am->getGlobalNumEntries());
187  if (i == 0)
188  lev0Nnz = totalNnz;
189  }
190  return totalNnz / lev0Nnz;
191  }
192 
193  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
195  double node_sc = 0, global_sc=0;
196  double a0_nnz =0;
197  const size_t INVALID = Teuchos::OrdinalTraits<size_t>::invalid();
198  // Get cost of fine matvec
199  if (GetNumLevels() <= 0) return -1.0;
200  if (!Levels_[0]->IsAvailable("A")) return -1.0;
201 
202  RCP<Operator> A = Levels_[0]->template Get<RCP<Operator> >("A");
203  if (A.is_null()) return -1.0;
204  RCP<Matrix> Am = rcp_dynamic_cast<Matrix>(A);
205  if(Am.is_null()) return -1.0;
206  a0_nnz = as<double>(Am->getGlobalNumEntries());
207 
208  // Get smoother complexity at each level
209  for (int i = 0; i < GetNumLevels(); ++i) {
210  size_t level_sc=0;
211  if(!Levels_[i]->IsAvailable("PreSmoother")) continue;
212  RCP<SmootherBase> S = Levels_[i]->template Get<RCP<SmootherBase> >("PreSmoother");
213  if (S.is_null()) continue;
214  level_sc = S->getNodeSmootherComplexity();
215  if(level_sc == INVALID) {global_sc=-1.0;break;}
216 
217  node_sc += as<double>(level_sc);
218  }
219 
220  double min_sc=0.0;
221  RCP<const Teuchos::Comm<int> > comm =A->getDomainMap()->getComm();
222  Teuchos::reduceAll(*comm,Teuchos::REDUCE_SUM,node_sc,Teuchos::ptr(&global_sc));
223  Teuchos::reduceAll(*comm,Teuchos::REDUCE_MIN,node_sc,Teuchos::ptr(&min_sc));
224 
225  if(min_sc < 0.0) return -1.0;
226  else return global_sc / a0_nnz;
227  }
228 
229 
230 
231 
232  // Coherence checks todo in Setup() (using an helper function):
233  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
236  "MueLu::Hierarchy::CheckLevel(): wrong underlying linear algebra library.");
237  TEUCHOS_TEST_FOR_EXCEPTION(level.GetLevelID() != levelID, Exceptions::RuntimeError,
238  "MueLu::Hierarchy::CheckLevel(): wrong level ID");
239  TEUCHOS_TEST_FOR_EXCEPTION(levelID != 0 && level.GetPreviousLevel() != Levels_[levelID-1], Exceptions::RuntimeError,
240  "MueLu::Hierarchy::Setup(): wrong level parent");
241  }
242 
243  // The function uses three managers: fine, coarse and next coarse
244  // We construct the data for the coarse level, and do requests for the next coarse
245  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
247  const RCP<const FactoryManagerBase> fineLevelManager,
248  const RCP<const FactoryManagerBase> coarseLevelManager,
249  const RCP<const FactoryManagerBase> nextLevelManager) {
250  // Use PrintMonitor/TimerMonitor instead of just a FactoryMonitor to print "Level 0" instead of Hierarchy(0)
251  // Print is done after the requests for next coarse level
252 
253  TEUCHOS_TEST_FOR_EXCEPTION(LastLevelID() < coarseLevelID, Exceptions::RuntimeError,
254  "MueLu::Hierarchy:Setup(): level " << coarseLevelID << " (specified by coarseLevelID argument) "
255  "must be built before calling this function.");
256 
257  Level& level = *Levels_[coarseLevelID];
258 
259  std::string label = FormattingHelper::getColonLabel(level.getObjectLabel());
260  TimeMonitor m1(*this, label + this->ShortClassName() + ": " + "Setup (total)");
261  TimeMonitor m2(*this, label + this->ShortClassName() + ": " + "Setup" + " (total, level=" + Teuchos::toString(coarseLevelID) + ")");
262 
263  // TODO: pass coarseLevelManager by reference
264  TEUCHOS_TEST_FOR_EXCEPTION(coarseLevelManager == Teuchos::null, Exceptions::RuntimeError,
265  "MueLu::Hierarchy::Setup(): argument coarseLevelManager cannot be null");
266 
269 
270  if (levelManagers_.size() < coarseLevelID+1)
271  levelManagers_.resize(coarseLevelID+1);
272  levelManagers_[coarseLevelID] = coarseLevelManager;
273 
274  bool isFinestLevel = (fineLevelManager.is_null());
275  bool isLastLevel = (nextLevelManager.is_null());
276 
277  int oldRank = -1;
278  if (isFinestLevel) {
279  RCP<Operator> A = level.Get< RCP<Operator> >("A");
280  RCP<const Map> domainMap = A->getDomainMap();
281  RCP<const Teuchos::Comm<int> > comm = domainMap->getComm();
282 
283  // Initialize random seed for reproducibility
285 
286  // Record the communicator on the level (used for timers sync)
287  level.SetComm(comm);
288  oldRank = SetProcRankVerbose(comm->getRank());
289 
290  // Set the Hierarchy library to match that of the finest level matrix,
291  // even if it was already set
292  lib_ = domainMap->lib();
293  level.setlib(lib_);
294 
295  } else {
296  // Permeate library to a coarser level
297  level.setlib(lib_);
298 
299  Level& prevLevel = *Levels_[coarseLevelID-1];
300  oldRank = SetProcRankVerbose(prevLevel.GetComm()->getRank());
301  }
302 
303  CheckLevel(level, coarseLevelID);
304 
305  // Attach FactoryManager to the fine level
306  RCP<SetFactoryManager> SFMFine;
307  if (!isFinestLevel)
308  SFMFine = rcp(new SetFactoryManager(Levels_[coarseLevelID-1], fineLevelManager));
309 
310  if (isFinestLevel && Levels_[coarseLevelID]->IsAvailable("Coordinates"))
311  ReplaceCoordinateMap(*Levels_[coarseLevelID]);
312 
313  // Attach FactoryManager to the coarse level
314  SetFactoryManager SFMCoarse(Levels_[coarseLevelID], coarseLevelManager);
315 
316  if (isDumpingEnabled_ && dumpLevel_ == 0 && coarseLevelID == 1)
317  DumpCurrentGraph();
318 
319  RCP<TopSmootherFactory> coarseFact = rcp(new TopSmootherFactory(coarseLevelManager, "CoarseSolver"));
320  RCP<TopSmootherFactory> smootherFact = rcp(new TopSmootherFactory(coarseLevelManager, "Smoother"));
321 
322  int nextLevelID = coarseLevelID + 1;
323 
324  RCP<SetFactoryManager> SFMNext;
325  if (isLastLevel == false) {
326  // We are not at the coarsest level, so there is going to be another level ("next coarse") after this one ("coarse")
327  if (nextLevelID > LastLevelID())
328  AddNewLevel();
329  CheckLevel(*Levels_[nextLevelID], nextLevelID);
330 
331  // Attach FactoryManager to the next level (level after coarse)
332  SFMNext = rcp(new SetFactoryManager(Levels_[nextLevelID], nextLevelManager));
333  Levels_[nextLevelID]->Request(TopRAPFactory(coarseLevelManager, nextLevelManager));
334 
335  // Do smoother requests here. We don't know whether this is going to be
336  // the coarsest level or not, but we need to DeclareInput before we call
337  // coarseRAPFactory.Build(), otherwise some stuff may be erased after
338  // level releases
339  level.Request(*smootherFact);
340 
341  } else {
342  // Similar to smoother above, do the coarse solver request here. We don't
343  // know whether this is going to be the coarsest level or not, but we
344  // need to DeclareInput before we call coarseRAPFactory.Build(),
345  // otherwise some stuff may be erased after level releases. This is
346  // actually evident on ProjectorSmoother. It requires both "A" and
347  // "Nullspace". However, "Nullspace" is erased after all releases, so if
348  // we call the coarse factory request after RAP build we would not have
349  // any data, and cannot get it as we don't have previous managers. The
350  // typical trace looks like this:
351  //
352  // MueLu::Level(0)::GetFactory(Aggregates, 0): No FactoryManager
353  // during request for data " Aggregates" on level 0 by factory TentativePFactory
354  // during request for data " P" on level 1 by factory EminPFactory
355  // during request for data " P" on level 1 by factory TransPFactory
356  // during request for data " R" on level 1 by factory RAPFactory
357  // during request for data " A" on level 1 by factory TentativePFactory
358  // during request for data " Nullspace" on level 2 by factory NullspaceFactory
359  // during request for data " Nullspace" on level 2 by factory NullspacePresmoothFactory
360  // during request for data " Nullspace" on level 2 by factory ProjectorSmoother
361  // during request for data " PreSmoother" on level 2 by factory NoFactory
362  level.Request(*coarseFact);
363  }
364 
365  PrintMonitor m0(*this, "Level " + Teuchos::toString(coarseLevelID), static_cast<MsgType>(Runtime0 | Test));
366 
367  // Build coarse level hierarchy
368  RCP<Operator> Ac = Teuchos::null;
369  TopRAPFactory coarseRAPFactory(fineLevelManager, coarseLevelManager);
370 
371  if (level.IsAvailable("A")) {
372  Ac = level.Get<RCP<Operator> >("A");
373  } else if (!isFinestLevel) {
374  // We only build here, the release is done later
375  coarseRAPFactory.Build(*level.GetPreviousLevel(), level);
376  }
377 
378  if (level.IsAvailable("A"))
379  Ac = level.Get<RCP<Operator> >("A");
380  RCP<Matrix> Acm = rcp_dynamic_cast<Matrix>(Ac);
381 
382  // Record the communicator on the level
383  if (!Ac.is_null())
384  level.SetComm(Ac->getDomainMap()->getComm());
385 
386  // Test if we reach the end of the hierarchy
387  bool isOrigLastLevel = isLastLevel;
388  if (isLastLevel) {
389  // Last level as we have achieved the max limit
390  isLastLevel = true;
391 
392  } else if (Ac.is_null()) {
393  // Last level for this processor, as it does not belong to the next
394  // subcommunicator. Other processors may continue working on the
395  // hierarchy
396  isLastLevel = true;
397 
398  } else {
399  if (!Acm.is_null() && Acm->getGlobalNumRows() <= maxCoarseSize_) {
400  // Last level as the size of the coarse matrix became too small
401  GetOStream(Runtime0) << "Max coarse size (<= " << maxCoarseSize_ << ") achieved" << std::endl;
402  isLastLevel = true;
403  }
404  }
405 
406  if (!Ac.is_null() && !isFinestLevel) {
407  RCP<Operator> A = Levels_[coarseLevelID-1]->template Get< RCP<Operator> >("A");
408  RCP<Matrix> Am = rcp_dynamic_cast<Matrix>(A);
409 
410  const double maxCoarse2FineRatio = 0.8;
411  if (!Acm.is_null() && !Am.is_null() && Acm->getGlobalNumRows() > maxCoarse2FineRatio * Am->getGlobalNumRows()) {
412  // We could abort here, but for now we simply notify user.
413  // Couple of additional points:
414  // - if repartitioning is delayed until level K, but the aggregation
415  // procedure stagnates between levels K-1 and K. In this case,
416  // repartitioning could enable faster coarsening once again, but the
417  // hierarchy construction will abort due to the stagnation check.
418  // - if the matrix is small enough, we could move it to one processor.
419  GetOStream(Warnings0) << "Aggregation stagnated. Please check your matrix and/or adjust your configuration file."
420  << "Possible fixes:\n"
421  << " - reduce the maximum number of levels\n"
422  << " - enable repartitioning\n"
423  << " - increase the minimum coarse size." << std::endl;
424 
425  }
426  }
427 
428  if (isLastLevel) {
429  if (!isOrigLastLevel) {
430  // We did not expect to finish this early so we did request a smoother.
431  // We need a coarse solver instead. Do the magic.
432  level.Release(*smootherFact);
433  level.Request(*coarseFact);
434  }
435 
436  // Do the actual build, if we have any data.
437  // NOTE: this is not a great check, we may want to call Build() regardless.
438  if (!Ac.is_null())
439  coarseFact->Build(level);
440 
441  // Once the dirty deed is done, release stuff. The smoother has already
442  // been released.
443  level.Release(*coarseFact);
444 
445  } else {
446  // isLastLevel = false => isOrigLastLevel = false, meaning that we have
447  // requested the smoother. Now we need to build it and to release it.
448  // We don't need to worry about the coarse solver, as we didn't request it.
449  if (!Ac.is_null())
450  smootherFact->Build(level);
451 
452  level.Release(*smootherFact);
453  }
454 
455  if (isLastLevel == true) {
456  if (isOrigLastLevel == false) {
457  // Earlier in the function, we constructed the next coarse level, and requested data for the that level,
458  // assuming that we are not at the coarsest level. Now, we changed our mind, so we have to release those.
459  Levels_[nextLevelID]->Release(TopRAPFactory(coarseLevelManager, nextLevelManager));
460  }
461  Levels_.resize(nextLevelID);
462  }
463 
464  // I think this is the proper place for graph so that it shows every dependence
465  if (isDumpingEnabled_ && dumpLevel_ > 0 && coarseLevelID == dumpLevel_)
466  DumpCurrentGraph();
467 
468  if (!isFinestLevel) {
469  // Release the hierarchy data
470  // We release so late to help blocked solvers, as the smoothers for them need A blocks
471  // which we construct in RAPFactory
472  level.Release(coarseRAPFactory);
473  }
474 
475  if (oldRank != -1)
476  SetProcRankVerbose(oldRank);
477 
478  return isLastLevel;
479  }
480 
481  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
483  int numLevels = Levels_.size();
484  TEUCHOS_TEST_FOR_EXCEPTION(levelManagers_.size() != numLevels, Exceptions::RuntimeError,
485  "Hierarchy::SetupRe: " << Levels_.size() << " levels, but " << levelManagers_.size() << " level factory managers");
486 
487  const int startLevel = 0;
488  Clear(startLevel);
489 
490 #ifdef HAVE_MUELU_DEBUG
491  // Reset factories' data used for debugging
492  for (int i = 0; i < numLevels; i++)
493  levelManagers_[i]->ResetDebugData();
494 
495 #endif
496 
497  int levelID;
498  for (levelID = startLevel; levelID < numLevels;) {
499  bool r = Setup(levelID,
500  (levelID != 0 ? levelManagers_[levelID-1] : Teuchos::null),
501  levelManagers_[levelID],
502  (levelID+1 != numLevels ? levelManagers_[levelID+1] : Teuchos::null));
503  levelID++;
504  if (r) break;
505  }
506  // We may construct fewer levels for some reason, make sure we continue
507  // doing that in the future
508  Levels_ .resize(levelID);
509  levelManagers_.resize(levelID);
510 
511  // NOTE: All reuse cases leave all of the maps the same, meaning that we do not
512  // need to reallocated the cached multivectors for Iterate(). If this were to change,
513  // we'd want to do a DeleteLevelMultiVectors() and AllocateLevelMultiVectors() here.
514 
515  // since the # of levels, etc. may have changed, force re-determination of description during next call to description()
516  ResetDescription();
517 
518  describe(GetOStream(Statistics0), GetVerbLevel());
519  }
520 
521  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
522  void Hierarchy<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Setup(const FactoryManagerBase& manager, int startLevel, int numDesiredLevels) {
523  // Use MueLu::BaseClass::description() to avoid printing "{numLevels = 1}" (numLevels is increasing...)
524  PrintMonitor m0(*this, "Setup (" + this->MueLu::BaseClass::description() + ")", Runtime0);
525 
526  Clear(startLevel);
527 
528  // Check Levels_[startLevel] exists.
529  TEUCHOS_TEST_FOR_EXCEPTION(Levels_.size() <= startLevel, Exceptions::RuntimeError,
530  "MueLu::Hierarchy::Setup(): fine level (" << startLevel << ") does not exist");
531 
532  TEUCHOS_TEST_FOR_EXCEPTION(numDesiredLevels <= 0, Exceptions::RuntimeError,
533  "Constructing non-positive (" << numDesiredLevels << ") number of levels does not make sense.");
534 
535  // Check for fine level matrix A
536  TEUCHOS_TEST_FOR_EXCEPTION(!Levels_[startLevel]->IsAvailable("A"), Exceptions::RuntimeError,
537  "MueLu::Hierarchy::Setup(): fine level (" << startLevel << ") has no matrix A! "
538  "Set fine level matrix A using Level.Set()");
539 
540  RCP<Operator> A = Levels_[startLevel]->template Get<RCP<Operator> >("A");
541  lib_ = A->getDomainMap()->lib();
542 
543  if (IsPrint(Statistics2)) {
544  RCP<Matrix> Amat = rcp_dynamic_cast<Matrix>(A);
545 
546  if (!Amat.is_null()) {
547  RCP<ParameterList> params = rcp(new ParameterList());
548  params->set("printLoadBalancingInfo", true);
549  params->set("printCommInfo", true);
550 
551  GetOStream(Statistics2) << PerfUtils::PrintMatrixInfo(*Amat, "A0", params);
552  } else {
553  GetOStream(Warnings1) << "Fine level operator is not a matrix, statistics are not available" << std::endl;
554  }
555  }
556 
557  RCP<const FactoryManagerBase> rcpmanager = rcpFromRef(manager);
558 
559  const int lastLevel = startLevel + numDesiredLevels - 1;
560  GetOStream(Runtime0) << "Setup loop: startLevel = " << startLevel << ", lastLevel = " << lastLevel
561  << " (stop if numLevels = " << numDesiredLevels << " or Ac.size() < " << maxCoarseSize_ << ")" << std::endl;
562 
563  // Setup multigrid levels
564  int iLevel = 0;
565  if (numDesiredLevels == 1) {
566  iLevel = 0;
567  Setup(startLevel, Teuchos::null, rcpmanager, Teuchos::null); // setup finest==coarsest level (first and last managers are Teuchos::null)
568 
569  } else {
570  bool bIsLastLevel = Setup(startLevel, Teuchos::null, rcpmanager, rcpmanager); // setup finest level (level 0) (first manager is Teuchos::null)
571  if (bIsLastLevel == false) {
572  for (iLevel = startLevel + 1; iLevel < lastLevel; iLevel++) {
573  bIsLastLevel = Setup(iLevel, rcpmanager, rcpmanager, rcpmanager); // setup intermediate levels
574  if (bIsLastLevel == true)
575  break;
576  }
577  if (bIsLastLevel == false)
578  Setup(lastLevel, rcpmanager, rcpmanager, Teuchos::null); // setup coarsest level (last manager is Teuchos::null)
579  }
580  }
581 
582  // TODO: some check like this should be done at the beginning of the routine
583  TEUCHOS_TEST_FOR_EXCEPTION(iLevel != Levels_.size() - 1, Exceptions::RuntimeError,
584  "MueLu::Hierarchy::Setup(): number of level");
585 
586  // TODO: this is not exception safe: manager will still hold default
587  // factories if you exit this function with an exception
588  manager.Clean();
589 
590  describe(GetOStream(Statistics0), GetVerbLevel());
591  }
592 
593  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
595  if (startLevel < GetNumLevels())
596  GetOStream(Runtime0) << "Clearing old data (if any)" << std::endl;
597 
598  for (int iLevel = startLevel; iLevel < GetNumLevels(); iLevel++)
599  Levels_[iLevel]->Clear();
600  }
601 
602  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
604  GetOStream(Runtime0) << "Clearing old data (expert)" << std::endl;
605  for (int iLevel = 0; iLevel < GetNumLevels(); iLevel++)
606  Levels_[iLevel]->ExpertClear();
607  }
608 
609 #if defined(HAVE_MUELU_EXPERIMENTAL) && defined(HAVE_MUELU_ADDITIVE_VARIANT)
610  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
611  ReturnType Hierarchy<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Iterate(const MultiVector& B, MultiVector& X, ConvData conv,
612  bool InitialGuessIsZero, LO startLevel) {
613  LO nIts = conv.maxIts_;
614  MagnitudeType tol = conv.tol_;
615 
616  std::string prefix = this->ShortClassName() + ": ";
617  std::string levelSuffix = " (level=" + toString(startLevel) + ")";
618  std::string levelSuffix1 = " (level=" + toString(startLevel+1) + ")";
619 
620  using namespace Teuchos;
621  RCP<Time> CompTime = Teuchos::TimeMonitor::getNewCounter(prefix + "Computational Time (total)");
622  RCP<Time> Concurrent = Teuchos::TimeMonitor::getNewCounter(prefix + "Concurrent portion");
623  RCP<Time> ApplyR = Teuchos::TimeMonitor::getNewCounter(prefix + "R: Computational Time");
624  RCP<Time> ApplyPbar = Teuchos::TimeMonitor::getNewCounter(prefix + "Pbar: Computational Time");
625  RCP<Time> CompFine = Teuchos::TimeMonitor::getNewCounter(prefix + "Fine: Computational Time");
626  RCP<Time> CompCoarse = Teuchos::TimeMonitor::getNewCounter(prefix + "Coarse: Computational Time");
627  RCP<Time> ApplySum = Teuchos::TimeMonitor::getNewCounter(prefix + "Sum: Computational Time");
628  RCP<Time> Synchronize_beginning = Teuchos::TimeMonitor::getNewCounter(prefix + "Synchronize_beginning");
629  RCP<Time> Synchronize_center = Teuchos::TimeMonitor::getNewCounter(prefix + "Synchronize_center");
630  RCP<Time> Synchronize_end = Teuchos::TimeMonitor::getNewCounter(prefix + "Synchronize_end");
631 
632  RCP<Level> Fine = Levels_[0];
633  RCP<Level> Coarse;
634 
635  RCP<Operator> A = Fine->Get< RCP<Operator> >("A");
636  Teuchos::RCP< const Teuchos::Comm< int > > communicator = A->getDomainMap()->getComm();
637 
638 
639  //Synchronize_beginning->start();
640  //communicator->barrier();
641  //Synchronize_beginning->stop();
642 
643  CompTime->start();
644 
645  SC one = STS::one(), zero = STS::zero();
646 
647  bool zeroGuess = InitialGuessIsZero;
648 
649  // ======= UPFRONT DEFINITION OF COARSE VARIABLES ===========
650 
651  //RCP<const Map> origMap;
652  RCP< Operator > P;
653  RCP< Operator > Pbar;
654  RCP< Operator > R;
655  RCP< MultiVector > coarseRhs, coarseX;
656  RCP< Operator > Ac;
657  RCP<SmootherBase> preSmoo_coarse, postSmoo_coarse;
658  bool emptyCoarseSolve = true;
659  RCP<MultiVector> coarseX_prolonged = MultiVectorFactory::Build(X.getMap(), X.getNumVectors(), true);
660 
661  RCP<const Import> importer;
662 
663  if (Levels_.size() > 1) {
664  Coarse = Levels_[1];
665  if (Coarse->IsAvailable("Importer"))
666  importer = Coarse->Get< RCP<const Import> >("Importer");
667 
668  R = Coarse->Get< RCP<Operator> >("R");
669  P = Coarse->Get< RCP<Operator> >("P");
670 
671 
672  //if(Coarse->IsAvailable("Pbar"))
673  Pbar = Coarse->Get< RCP<Operator> >("Pbar");
674 
675  coarseRhs = MultiVectorFactory::Build(R->getRangeMap(), B.getNumVectors(), true);
676 
677  Ac = Coarse->Get< RCP< Operator > >("A");
678 
679  ApplyR->start();
680  R->apply(B, *coarseRhs, Teuchos::NO_TRANS, one, zero);
681  //P->apply(B, *coarseRhs, Teuchos::TRANS, one, zero);
682  ApplyR->stop();
683 
684  if (doPRrebalance_ || importer.is_null()) {
685  coarseX = MultiVectorFactory::Build(coarseRhs->getMap(), X.getNumVectors(), true);
686 
687  } else {
688 
689  RCP<TimeMonitor> ITime = rcp(new TimeMonitor(*this, prefix + "Solve : import (total)" , Timings0));
690  RCP<TimeMonitor> ILevelTime = rcp(new TimeMonitor(*this, prefix + "Solve : import" + levelSuffix1, Timings0));
691 
692  // Import: range map of R --> domain map of rebalanced Ac (before subcomm replacement)
693  RCP<MultiVector> coarseTmp = MultiVectorFactory::Build(importer->getTargetMap(), coarseRhs->getNumVectors());
694  coarseTmp->doImport(*coarseRhs, *importer, Xpetra::INSERT);
695  coarseRhs.swap(coarseTmp);
696 
697  coarseX = MultiVectorFactory::Build(importer->getTargetMap(), X.getNumVectors(), true);
698  }
699 
700  if (Coarse->IsAvailable("PreSmoother"))
701  preSmoo_coarse = Coarse->Get< RCP<SmootherBase> >("PreSmoother");
702  if (Coarse->IsAvailable("PostSmoother"))
703  postSmoo_coarse = Coarse->Get< RCP<SmootherBase> >("PostSmoother");
704  }
705 
706  // ==========================================================
707 
708 
709  MagnitudeType prevNorm = STS::magnitude(STS::one()), curNorm = STS::magnitude(STS::one());
710  rate_ = 1.0;
711 
712  for (LO i = 1; i <= nIts; i++) {
713 #ifdef HAVE_MUELU_DEBUG
714  if (A->getDomainMap()->isCompatible(*(X.getMap())) == false) {
715  std::ostringstream ss;
716  ss << "Level " << startLevel << ": level A's domain map is not compatible with X";
717  throw Exceptions::Incompatible(ss.str());
718  }
719 
720  if (A->getRangeMap()->isCompatible(*(B.getMap())) == false) {
721  std::ostringstream ss;
722  ss << "Level " << startLevel << ": level A's range map is not compatible with B";
723  throw Exceptions::Incompatible(ss.str());
724  }
725 #endif
726  }
727 
728  bool emptyFineSolve = true;
729 
730  RCP< MultiVector > fineX;
731  fineX = MultiVectorFactory::Build(X.getMap(), X.getNumVectors(), true);
732 
733  //Synchronize_center->start();
734  //communicator->barrier();
735  //Synchronize_center->stop();
736 
737  Concurrent->start();
738 
739  // NOTE: we need to check using IsAvailable before Get here to avoid building default smoother
740  if (Fine->IsAvailable("PreSmoother")) {
741  RCP<SmootherBase> preSmoo = Fine->Get< RCP<SmootherBase> >("PreSmoother");
742  CompFine->start();
743  preSmoo->Apply(*fineX, B, zeroGuess);
744  CompFine->stop();
745  emptyFineSolve = false;
746  }
747  if (Fine->IsAvailable("PostSmoother")) {
748  RCP<SmootherBase> postSmoo = Fine->Get< RCP<SmootherBase> >("PostSmoother");
749  CompFine->start();
750  postSmoo->Apply(*fineX, B, zeroGuess);
751  CompFine->stop();
752 
753  emptyFineSolve = false;
754  }
755  if (emptyFineSolve == true) {
756  GetOStream(Warnings1) << "No fine grid smoother" << std::endl;
757  // Fine grid smoother is identity
758  fineX->update(one, B, zero);
759  }
760 
761  if (Levels_.size() > 1) {
762  // NOTE: we need to check using IsAvailable before Get here to avoid building default smoother
763  if (Coarse->IsAvailable("PreSmoother")) {
764  CompCoarse->start();
765  preSmoo_coarse->Apply(*coarseX, *coarseRhs, zeroGuess);
766  CompCoarse->stop();
767  emptyCoarseSolve = false;
768 
769  }
770  if (Coarse->IsAvailable("PostSmoother")) {
771  CompCoarse->start();
772  postSmoo_coarse->Apply(*coarseX, *coarseRhs, zeroGuess);
773  CompCoarse->stop();
774  emptyCoarseSolve = false;
775 
776  }
777  if (emptyCoarseSolve == true) {
778  GetOStream(Warnings1) << "No coarse grid solver" << std::endl;
779  // Coarse operator is identity
780  coarseX->update(one, *coarseRhs, zero);
781  }
782  Concurrent->stop();
783  //Synchronize_end->start();
784  //communicator->barrier();
785  //Synchronize_end->stop();
786 
787  if (!doPRrebalance_ && !importer.is_null()) {
788  RCP<TimeMonitor> ITime = rcp(new TimeMonitor(*this, prefix + "Solve : export (total)" , Timings0));
789  RCP<TimeMonitor> ILevelTime = rcp(new TimeMonitor(*this, prefix + "Solve : export" + levelSuffix1, Timings0));
790 
791  // Import: range map of rebalanced Ac (before subcomm replacement) --> domain map of P
792  RCP<MultiVector> coarseTmp = MultiVectorFactory::Build(importer->getSourceMap(), coarseX->getNumVectors());
793  coarseTmp->doExport(*coarseX, *importer, Xpetra::INSERT);
794  coarseX.swap(coarseTmp);
795  }
796 
797  ApplyPbar->start();
798  Pbar->apply(*coarseX, *coarseX_prolonged, Teuchos::NO_TRANS, one, zero);
799  ApplyPbar->stop();
800  }
801 
802  ApplySum->start();
803  X.update(1.0, *fineX, 1.0, *coarseX_prolonged, 0.0);
804  ApplySum->stop();
805 
806  CompTime->stop();
807 
808  //communicator->barrier();
809 
810  return (tol > 0 ? Unconverged : Undefined);
811 }
812 #else
813  // ---------------------------------------- Iterate -------------------------------------------------------
814  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
816  bool InitialGuessIsZero, LO startLevel) {
817  LO nIts = conv.maxIts_;
818  MagnitudeType tol = conv.tol_;
819 
820  // These timers work as follows. "iterateTime" records total time spent in
821  // iterate. "levelTime" records time on a per level basis. The label is
822  // crafted to mimic the per-level messages used in Monitors. Note that a
823  // given level is timed with a TimeMonitor instead of a Monitor or
824  // SubMonitor. This is mainly because I want to time each level
825  // separately, and Monitors/SubMonitors print "(total) xx yy zz" ,
826  // "(sub,total) xx yy zz", respectively, which is subject to
827  // misinterpretation. The per-level TimeMonitors are stopped/started
828  // manually before/after a recursive call to Iterate. A side artifact to
829  // this approach is that the counts for intermediate level timers are twice
830  // the counts for the finest and coarsest levels.
831 
832  RCP<Level> Fine = Levels_[startLevel];
833 
834  std::string label = FormattingHelper::getColonLabel(Fine->getObjectLabel());
835  std::string prefix = label + this->ShortClassName() + ": ";
836  std::string levelSuffix = " (level=" + toString(startLevel) + ")";
837  std::string levelSuffix1 = " (level=" + toString(startLevel+1) + ")";
838 
839  bool useStackedTimer = !Teuchos::TimeMonitor::getStackedTimer().is_null();
840 
841  RCP<Monitor> iterateTime;
842  RCP<TimeMonitor> iterateTime1;
843  if (startLevel == 0)
844  iterateTime = rcp(new Monitor(*this, "Solve", label, (nIts == 1) ? None : Runtime0, Timings0));
845  else if (!useStackedTimer)
846  iterateTime1 = rcp(new TimeMonitor(*this, prefix + "Solve (total, level=" + toString(startLevel) + ")", Timings0));
847 
848  std::string iterateLevelTimeLabel = prefix + "Solve" + levelSuffix;
849  RCP<TimeMonitor> iterateLevelTime = rcp(new TimeMonitor(*this, iterateLevelTimeLabel, Timings0));
850 
851  bool zeroGuess = InitialGuessIsZero;
852 
853  RCP<Operator> A = Fine->Get< RCP<Operator> >("A");
854  using namespace Teuchos;
855  RCP<Time> CompCoarse = Teuchos::TimeMonitor::getNewCounter(prefix + "Coarse: Computational Time");
856 
857  if (A.is_null()) {
858  // This processor does not have any data for this process on coarser
859  // levels. This can only happen when there are multiple processors and
860  // we use repartitioning.
861  return Undefined;
862  }
863 
864  // If we switched the number of vectors, we'd need to reallocate here.
865  // If the number of vectors is unchanged, this is a noop.
866  // NOTE: We need to check against B because the tests in AllocateLevelMultiVectors
867  // will fail on Stokhos Scalar types (due to the so-called 'hidden dimension')
868  const BlockedMultiVector * Bblocked = dynamic_cast<const BlockedMultiVector*>(&B);
869  if(residual_.size() > startLevel &&
870  ( ( Bblocked && !Bblocked->isSameSize(*residual_[startLevel])) ||
871  (!Bblocked && !residual_[startLevel]->isSameSize(B))))
872  DeleteLevelMultiVectors();
873  AllocateLevelMultiVectors(X.getNumVectors());
874 
875  // Print residual information before iterating
876  MagnitudeType prevNorm = STS::magnitude(STS::one()), curNorm = STS::magnitude(STS::one());
877  rate_ = 1.0;
878  if (startLevel == 0 && !isPreconditioner_ &&
879  (IsPrint(Statistics1) || tol > 0)) {
880  // We calculate the residual only if we want to print it out, or if we
881  // want to stop once we achive the tolerance
883  rn = Utilities::ResidualNorm(*A, X, B,*residual_[startLevel]);
884 
885  if (tol > 0) {
886  bool passed = true;
887  for (LO k = 0; k < rn.size(); k++)
888  if (rn[k] >= tol)
889  passed = false;
890 
891  if (passed)
892  return Converged;
893  }
894 
895  if (IsPrint(Statistics1))
896  GetOStream(Statistics1) << "iter: "
897  << std::setiosflags(std::ios::left)
898  << std::setprecision(3) << 0 // iter 0
899  << " residual = "
900  << std::setprecision(10) << rn
901  << std::endl;
902  }
903 
904  SC one = STS::one(), zero = STS::zero();
905  for (LO i = 1; i <= nIts; i++) {
906 #ifdef HAVE_MUELU_DEBUG
907 #if 0 // TODO fix me
908  if (A->getDomainMap()->isCompatible(*(X.getMap())) == false) {
909  std::ostringstream ss;
910  ss << "Level " << startLevel << ": level A's domain map is not compatible with X";
911  throw Exceptions::Incompatible(ss.str());
912  }
913 
914  if (A->getRangeMap()->isCompatible(*(B.getMap())) == false) {
915  std::ostringstream ss;
916  ss << "Level " << startLevel << ": level A's range map is not compatible with B";
917  throw Exceptions::Incompatible(ss.str());
918  }
919 #endif
920 #endif
921 
922  if (startLevel == as<LO>(Levels_.size())-1) {
923  // On the coarsest level, we do either smoothing (if defined) or a direct solve.
924  RCP<TimeMonitor> CLevelTime = rcp(new TimeMonitor(*this, prefix + "Solve : coarse" + levelSuffix, Timings0));
925 
926  bool emptySolve = true;
927 
928  // NOTE: we need to check using IsAvailable before Get here to avoid building default smoother
929  if (Fine->IsAvailable("PreSmoother")) {
930  RCP<SmootherBase> preSmoo = Fine->Get< RCP<SmootherBase> >("PreSmoother");
931  CompCoarse->start();
932  preSmoo->Apply(X, B, zeroGuess);
933  CompCoarse->stop();
934  zeroGuess = false;
935  emptySolve = false;
936  }
937  if (Fine->IsAvailable("PostSmoother")) {
938  RCP<SmootherBase> postSmoo = Fine->Get< RCP<SmootherBase> >("PostSmoother");
939  CompCoarse->start();
940  postSmoo->Apply(X, B, zeroGuess);
941  CompCoarse->stop();
942  emptySolve = false;
943  }
944  if (emptySolve == true) {
945  GetOStream(Warnings1) << "No coarse grid solver" << std::endl;
946  // Coarse operator is identity
947  X.update(one, B, zero);
948  }
949 
950  } else {
951  // On intermediate levels, we do cycles
952  RCP<Level> Coarse = Levels_[startLevel+1];
953  {
954  // ============== PRESMOOTHING ==============
955  RCP<TimeMonitor> STime;
956  if (!useStackedTimer)
957  STime = rcp(new TimeMonitor(*this, prefix + "Solve : smoothing (total)" , Timings0));
958  RCP<TimeMonitor> SLevelTime = rcp(new TimeMonitor(*this, prefix + "Solve : smoothing" + levelSuffix, Timings0));
959 
960  if (Fine->IsAvailable("PreSmoother")) {
961  RCP<SmootherBase> preSmoo = Fine->Get< RCP<SmootherBase> >("PreSmoother");
962  preSmoo->Apply(X, B, zeroGuess);
963  }
964  }
965 
966  RCP<MultiVector> residual;
967  {
968  RCP<TimeMonitor> ATime;
969  if (!useStackedTimer)
970  ATime = rcp(new TimeMonitor(*this, prefix + "Solve : residual calculation (total)" , Timings0));
971  RCP<TimeMonitor> ALevelTime = rcp(new TimeMonitor(*this, prefix + "Solve : residual calculation" + levelSuffix, Timings0));
972  Utilities::Residual(*A, X, B,*residual_[startLevel]);
973  residual = residual_[startLevel];
974  }
975 
976  RCP<Operator> P = Coarse->Get< RCP<Operator> >("P");
977  if (Coarse->IsAvailable("Pbar"))
978  P = Coarse->Get< RCP<Operator> >("Pbar");
979 
980  RCP<MultiVector> coarseRhs, coarseX;
981  // const bool initializeWithZeros = true;
982  {
983  // ============== RESTRICTION ==============
984  RCP<TimeMonitor> RTime;
985  if (!useStackedTimer)
986  RTime = rcp(new TimeMonitor(*this, prefix + "Solve : restriction (total)" , Timings0));
987  RCP<TimeMonitor> RLevelTime = rcp(new TimeMonitor(*this, prefix + "Solve : restriction" + levelSuffix, Timings0));
988  coarseRhs = coarseRhs_[startLevel];
989 
990  if (implicitTranspose_) {
991  P->apply(*residual, *coarseRhs, Teuchos::TRANS, one, zero);
992 
993  } else {
994  RCP<Operator> R = Coarse->Get< RCP<Operator> >("R");
995  R->apply(*residual, *coarseRhs, Teuchos::NO_TRANS, one, zero);
996  }
997  }
998 
999  RCP<const Import> importer;
1000  if (Coarse->IsAvailable("Importer"))
1001  importer = Coarse->Get< RCP<const Import> >("Importer");
1002 
1003  coarseX = coarseX_[startLevel];
1004  if (!doPRrebalance_ && !importer.is_null()) {
1005  RCP<TimeMonitor> ITime;
1006  if (!useStackedTimer)
1007  ITime = rcp(new TimeMonitor(*this, prefix + "Solve : import (total)" , Timings0));
1008  RCP<TimeMonitor> ILevelTime = rcp(new TimeMonitor(*this, prefix + "Solve : import" + levelSuffix1, Timings0));
1009 
1010  // Import: range map of R --> domain map of rebalanced Ac (before subcomm replacement)
1011  RCP<MultiVector> coarseTmp = coarseImport_[startLevel];
1012  coarseTmp->doImport(*coarseRhs, *importer, Xpetra::INSERT);
1013  coarseRhs.swap(coarseTmp);
1014  }
1015 
1016  RCP<Operator> Ac = Coarse->Get< RCP<Operator> >("A");
1017  if (!Ac.is_null()) {
1018  RCP<const Map> origXMap = coarseX->getMap();
1019  RCP<const Map> origRhsMap = coarseRhs->getMap();
1020 
1021  // Replace maps with maps with a subcommunicator
1022  coarseRhs->replaceMap(Ac->getRangeMap());
1023  coarseX ->replaceMap(Ac->getDomainMap());
1024 
1025  {
1026  iterateLevelTime = Teuchos::null; // stop timing this level
1027 
1028  Iterate(*coarseRhs, *coarseX, 1, true, startLevel+1);
1029  // ^^ zero initial guess
1030  if (Cycle_ == WCYCLE)
1031  Iterate(*coarseRhs, *coarseX, 1, false, startLevel+1);
1032  // ^^ nonzero initial guess
1033 
1034  iterateLevelTime = rcp(new TimeMonitor(*this, iterateLevelTimeLabel)); // restart timing this level
1035  }
1036  coarseX->replaceMap(origXMap);
1037  coarseRhs->replaceMap(origRhsMap);
1038  }
1039 
1040  if (!doPRrebalance_ && !importer.is_null()) {
1041  RCP<TimeMonitor> ITime;
1042  if (!useStackedTimer)
1043  ITime = rcp(new TimeMonitor(*this, prefix + "Solve : export (total)" , Timings0));
1044  RCP<TimeMonitor> ILevelTime = rcp(new TimeMonitor(*this, prefix + "Solve : export" + levelSuffix1, Timings0));
1045 
1046  // Import: range map of rebalanced Ac (before subcomm replacement) --> domain map of P
1047  RCP<MultiVector> coarseTmp = coarseExport_[startLevel];
1048  coarseTmp->doExport(*coarseX, *importer, Xpetra::INSERT);
1049  coarseX.swap(coarseTmp);
1050  }
1051 
1052  // Update X += P * coarseX
1053  // Note that due to what may be round-off error accumulation, use of the fused kernel
1054  // P->apply(*coarseX, X, Teuchos::NO_TRANS, one, one);
1055  // can in some cases result in slightly higher iteration counts.
1056  RCP<MultiVector> correction = correction_[startLevel];
1057  {
1058  // ============== PROLONGATION ==============
1059  RCP<TimeMonitor> PTime;
1060  if (!useStackedTimer)
1061  PTime = rcp(new TimeMonitor(*this, prefix + "Solve : prolongation (total)" , Timings0));
1062  RCP<TimeMonitor> PLevelTime = rcp(new TimeMonitor(*this, prefix + "Solve : prolongation" + levelSuffix, Timings0));
1063  P->apply(*coarseX, *correction, Teuchos::NO_TRANS, one, zero);
1064  }
1065  X.update(scalingFactor_, *correction, one);
1066 
1067  {
1068  // ============== POSTSMOOTHING ==============
1069  RCP<TimeMonitor> STime;
1070  if (!useStackedTimer)
1071  STime = rcp(new TimeMonitor(*this, prefix + "Solve : smoothing (total)" , Timings0));
1072  RCP<TimeMonitor> SLevelTime = rcp(new TimeMonitor(*this, prefix + "Solve : smoothing" + levelSuffix, Timings0));
1073 
1074  if (Fine->IsAvailable("PostSmoother")) {
1075  RCP<SmootherBase> postSmoo = Fine->Get< RCP<SmootherBase> >("PostSmoother");
1076  postSmoo->Apply(X, B, false);
1077  }
1078  }
1079  }
1080  zeroGuess = false;
1081 
1082  if (startLevel == 0 && !isPreconditioner_ &&
1083  (IsPrint(Statistics1) || tol > 0)) {
1084  // We calculate the residual only if we want to print it out, or if we
1085  // want to stop once we achive the tolerance
1087  rn = Utilities::ResidualNorm(*A, X, B,*residual_[startLevel]);
1088 
1089  prevNorm = curNorm;
1090  curNorm = rn[0];
1091  rate_ = as<MagnitudeType>(curNorm / prevNorm);
1092 
1093  if (IsPrint(Statistics1))
1094  GetOStream(Statistics1) << "iter: "
1095  << std::setiosflags(std::ios::left)
1096  << std::setprecision(3) << i
1097  << " residual = "
1098  << std::setprecision(10) << rn
1099  << std::endl;
1100 
1101  if (tol > 0) {
1102  bool passed = true;
1103  for (LO k = 0; k < rn.size(); k++)
1104  if (rn[k] >= tol)
1105  passed = false;
1106 
1107  if (passed)
1108  return Converged;
1109  }
1110  }
1111  }
1112  return (tol > 0 ? Unconverged : Undefined);
1113  }
1114 #endif
1115 
1116 
1117  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1118  void Hierarchy<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Write(const LO& start, const LO& end, const std::string &suffix) {
1119  LO startLevel = (start != -1 ? start : 0);
1120  LO endLevel = (end != -1 ? end : Levels_.size()-1);
1121 
1123  "MueLu::Hierarchy::Write : startLevel must be <= endLevel");
1124 
1125  TEUCHOS_TEST_FOR_EXCEPTION(startLevel < 0 || endLevel >= Levels_.size(), Exceptions::RuntimeError,
1126  "MueLu::Hierarchy::Write bad start or end level");
1127 
1128  for (LO i = startLevel; i < endLevel + 1; i++) {
1129  RCP<Matrix> A = rcp_dynamic_cast<Matrix>(Levels_[i]-> template Get< RCP< Operator> >("A")), P, R;
1130  if (i > 0) {
1131  P = rcp_dynamic_cast<Matrix>(Levels_[i]-> template Get< RCP< Operator> >("P"));
1132  if (!implicitTranspose_)
1133  R = rcp_dynamic_cast<Matrix>(Levels_[i]-> template Get< RCP< Operator> >("R"));
1134  }
1135 
1136  if (!A.is_null()) Xpetra::IO<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Write("A_" + toString(i) + suffix + ".m", *A);
1137  if (!P.is_null()) {
1139  }
1140  if (!R.is_null()) {
1142  }
1143  }
1144  }
1145 
1146  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1147  void Hierarchy<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Keep(const std::string & ename, const FactoryBase* factory) {
1148  for (Array<RCP<Level> >::iterator it = Levels_.begin(); it != Levels_.end(); ++it)
1149  (*it)->Keep(ename, factory);
1150  }
1151 
1152  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1153  void Hierarchy<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Delete(const std::string& ename, const FactoryBase* factory) {
1154  for (Array<RCP<Level> >::iterator it = Levels_.begin(); it != Levels_.end(); ++it)
1155  (*it)->Delete(ename, factory);
1156  }
1157 
1158  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1159  void Hierarchy<Scalar, LocalOrdinal, GlobalOrdinal, Node>::AddKeepFlag(const std::string & ename, const FactoryBase* factory, KeepType keep) {
1160  for (Array<RCP<Level> >::iterator it = Levels_.begin(); it != Levels_.end(); ++it)
1161  (*it)->AddKeepFlag(ename, factory, keep);
1162  }
1163 
1164  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1166  for (Array<RCP<Level> >::iterator it = Levels_.begin(); it != Levels_.end(); ++it)
1167  (*it)->RemoveKeepFlag(ename, factory, keep);
1168  }
1169 
1170  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1172  if ( description_.empty() )
1173  {
1174  std::ostringstream out;
1175  out << BaseClass::description();
1176  out << "{#levels = " << GetGlobalNumLevels() << ", complexity = " << GetOperatorComplexity() << "}";
1177  description_ = out.str();
1178  }
1179  return description_;
1180  }
1181 
1182  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1184  describe(out, toMueLuVerbLevel(tVerbLevel));
1185  }
1186 
1187  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1189  RCP<Operator> A0 = Levels_[0]->template Get<RCP<Operator> >("A");
1190  RCP<const Teuchos::Comm<int> > comm = A0->getDomainMap()->getComm();
1191 
1192  int numLevels = GetNumLevels();
1193  RCP<Operator> Ac = Levels_[numLevels-1]->template Get<RCP<Operator> >("A");
1194  if (Ac.is_null()) {
1195  // It may happen that we do repartition on the last level, but the matrix
1196  // is small enough to satisfy "max coarse size" requirement. Then, even
1197  // though we have the level, the matrix would be null on all but one processors
1198  numLevels--;
1199  }
1200  int root = comm->getRank();
1201 
1202 #ifdef HAVE_MPI
1203  int smartData = numLevels*comm->getSize() + comm->getRank(), maxSmartData;
1204  reduceAll(*comm, Teuchos::REDUCE_MAX, smartData, Teuchos::ptr(&maxSmartData));
1205  root = maxSmartData % comm->getSize();
1206 #endif
1207 
1208  // Compute smoother complexity, if needed
1209  double smoother_comp =-1.0;
1210  if (verbLevel & (Statistics0 | Test))
1211  smoother_comp = GetSmootherComplexity();
1212 
1213  std::string outstr;
1214  if (comm->getRank() == root && verbLevel & (Statistics0 | Test)) {
1215  std::vector<Xpetra::global_size_t> nnzPerLevel;
1216  std::vector<Xpetra::global_size_t> rowsPerLevel;
1217  std::vector<int> numProcsPerLevel;
1218  bool aborted = false;
1219  for (int i = 0; i < numLevels; i++) {
1220  TEUCHOS_TEST_FOR_EXCEPTION(!(Levels_[i]->IsAvailable("A")) , Exceptions::RuntimeError,
1221  "Operator A is not available on level " << i);
1222 
1223  RCP<Operator> A = Levels_[i]->template Get<RCP<Operator> >("A");
1225  "Operator A on level " << i << " is null.");
1226 
1227  RCP<Matrix> Am = rcp_dynamic_cast<Matrix>(A);
1228  if (Am.is_null()) {
1229  GetOStream(Warnings0) << "Some level operators are not matrices, statistics calculation aborted" << std::endl;
1230  aborted = true;
1231  break;
1232  }
1233 
1234  Xpetra::global_size_t nnz = Am->getGlobalNumEntries();
1235  nnzPerLevel .push_back(nnz);
1236  rowsPerLevel .push_back(Am->getGlobalNumRows());
1237  numProcsPerLevel.push_back(Am->getRowMap()->getComm()->getSize());
1238  }
1239 
1240  if (!aborted) {
1241  std::string label = Levels_[0]->getObjectLabel();
1242  std::ostringstream oss;
1243  oss << std::setfill(' ');
1244  oss << "\n--------------------------------------------------------------------------------\n";
1245  oss << "--- Multigrid Summary " << std::setw(28) << std::left << label << "---\n";
1246  oss << "--------------------------------------------------------------------------------" << std::endl;
1247  oss << "Number of levels = " << numLevels << std::endl;
1248  oss << "Operator complexity = " << std::setprecision(2) << std::setiosflags(std::ios::fixed)
1249  << GetOperatorComplexity() << std::endl;
1250 
1251  if(smoother_comp!=-1.0) {
1252  oss << "Smoother complexity = " << std::setprecision(2) << std::setiosflags(std::ios::fixed)
1253  << smoother_comp << std::endl;
1254  }
1255 
1256  switch (Cycle_) {
1257  case VCYCLE:
1258  oss << "Cycle type = V" << std::endl;
1259  break;
1260  case WCYCLE:
1261  oss << "Cycle type = W" << std::endl;
1262  break;
1263  default:
1264  break;
1265  };
1266  oss << std::endl;
1267 
1268  Xpetra::global_size_t tt = rowsPerLevel[0];
1269  int rowspacer = 2; while (tt != 0) { tt /= 10; rowspacer++; }
1270  tt = nnzPerLevel[0];
1271  int nnzspacer = 2; while (tt != 0) { tt /= 10; nnzspacer++; }
1272  tt = numProcsPerLevel[0];
1273  int npspacer = 2; while (tt != 0) { tt /= 10; npspacer++; }
1274  oss << "level " << std::setw(rowspacer) << " rows " << std::setw(nnzspacer) << " nnz " << " nnz/row" << std::setw(npspacer) << " c ratio" << " procs" << std::endl;
1275  for (size_t i = 0; i < nnzPerLevel.size(); ++i) {
1276  oss << " " << i << " ";
1277  oss << std::setw(rowspacer) << rowsPerLevel[i];
1278  oss << std::setw(nnzspacer) << nnzPerLevel[i];
1279  oss << std::setprecision(2) << std::setiosflags(std::ios::fixed);
1280  oss << std::setw(9) << as<double>(nnzPerLevel[i]) / rowsPerLevel[i];
1281  if (i) oss << std::setw(9) << as<double>(rowsPerLevel[i-1])/rowsPerLevel[i];
1282  else oss << std::setw(9) << " ";
1283  oss << " " << std::setw(npspacer) << numProcsPerLevel[i] << std::endl;
1284  }
1285  oss << std::endl;
1286  for (int i = 0; i < GetNumLevels(); ++i) {
1287  RCP<SmootherBase> preSmoo, postSmoo;
1288  if (Levels_[i]->IsAvailable("PreSmoother"))
1289  preSmoo = Levels_[i]->template Get< RCP<SmootherBase> >("PreSmoother");
1290  if (Levels_[i]->IsAvailable("PostSmoother"))
1291  postSmoo = Levels_[i]->template Get< RCP<SmootherBase> >("PostSmoother");
1292 
1293  if (preSmoo != null && preSmoo == postSmoo)
1294  oss << "Smoother (level " << i << ") both : " << preSmoo->description() << std::endl;
1295  else {
1296  oss << "Smoother (level " << i << ") pre : "
1297  << (preSmoo != null ? preSmoo->description() : "no smoother") << std::endl;
1298  oss << "Smoother (level " << i << ") post : "
1299  << (postSmoo != null ? postSmoo->description() : "no smoother") << std::endl;
1300  }
1301 
1302  oss << std::endl;
1303  }
1304 
1305  outstr = oss.str();
1306  }
1307  }
1308 
1309 #ifdef HAVE_MPI
1310  RCP<const Teuchos::MpiComm<int> > mpiComm = rcp_dynamic_cast<const Teuchos::MpiComm<int> >(comm);
1311  MPI_Comm rawComm = (*mpiComm->getRawMpiComm())();
1312 
1313  int strLength = outstr.size();
1314  MPI_Bcast(&strLength, 1, MPI_INT, root, rawComm);
1315  if (comm->getRank() != root)
1316  outstr.resize(strLength);
1317  MPI_Bcast(&outstr[0], strLength, MPI_CHAR, root, rawComm);
1318 #endif
1319 
1320  out << outstr;
1321  }
1322 
1323  // NOTE: at some point this should be replaced by a friend operator <<
1324  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1325  void Hierarchy<Scalar, LocalOrdinal, GlobalOrdinal, Node>::print(std::ostream& out, const VerbLevel verbLevel) const {
1326  Teuchos::OSTab tab2(out);
1327  for (int i = 0; i < GetNumLevels(); ++i)
1328  Levels_[i]->print(out, verbLevel);
1329  }
1330 
1331  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1333  isPreconditioner_ = flag;
1334  }
1335 
1336  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1338  if (GetProcRankVerbose() != 0)
1339  return;
1340 #if defined(HAVE_MUELU_BOOST) && defined(HAVE_MUELU_BOOST_FOR_REAL) && defined(BOOST_VERSION) && (BOOST_VERSION >= 104400)
1341  BoostGraph graph;
1342 
1343  BoostProperties dp;
1344  dp.property("label", boost::get(boost::vertex_name, graph));
1345  dp.property("id", boost::get(boost::vertex_index, graph));
1346  dp.property("label", boost::get(boost::edge_name, graph));
1347  dp.property("color", boost::get(boost::edge_color, graph));
1348 
1349  // create local maps
1350  std::map<const FactoryBase*, BoostVertex> vindices;
1351  typedef std::map<std::pair<BoostVertex,BoostVertex>, std::string> emap; emap edges;
1352 
1353  static int call_id=0;
1354 
1355  RCP<Operator> A = Levels_[0]->template Get<RCP<Operator> >("A");
1356  int rank = A->getDomainMap()->getComm()->getRank();
1357 
1358  // printf("[%d] CMS: ----------------------\n",rank);
1359  for (int i = dumpLevel_; i <= dumpLevel_+1 && i < GetNumLevels(); i++) {
1360  edges.clear();
1361  Levels_[i]->UpdateGraph(vindices, edges, dp, graph);
1362 
1363  for (emap::const_iterator eit = edges.begin(); eit != edges.end(); eit++) {
1364  std::pair<BoostEdge, bool> boost_edge = boost::add_edge(eit->first.first, eit->first.second, graph);
1365  // printf("[%d] CMS: Hierarchy, adding edge (%d->%d) %d\n",rank,(int)eit->first.first,(int)eit->first.second,(int)boost_edge.second);
1366  // Because xdot.py views 'Graph' as a keyword
1367  if(eit->second==std::string("Graph")) boost::put("label", dp, boost_edge.first, std::string("Graph_"));
1368  else boost::put("label", dp, boost_edge.first, eit->second);
1369  if (i == dumpLevel_)
1370  boost::put("color", dp, boost_edge.first, std::string("red"));
1371  else
1372  boost::put("color", dp, boost_edge.first, std::string("blue"));
1373  }
1374  }
1375 
1376 #if 0
1377  std::ostringstream legend;
1378  legend << "< <TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\" CELLPADDING=\"4\"> \
1379  <TR><TD COLSPAN=\"2\">Legend</TD></TR> \
1380  <TR><TD><FONT color=\"red\">Level " << dumpLevel_ << "</FONT></TD><TD><FONT color=\"blue\">Level " << dumpLevel_+1 << "</FONT></TD></TR> \
1381  </TABLE> >";
1382  BoostVertex boost_vertex = boost::add_vertex(graph);
1383  boost::put("label", dp, boost_vertex, legend.str());
1384 #endif
1385 
1386  std::ofstream out(dumpFile_.c_str() +std::to_string(call_id)+std::string("_")+ std::to_string(rank) + std::string(".dot"));
1387  boost::write_graphviz_dp(out, graph, dp, std::string("id"));
1388  out.close();
1389  call_id++;
1390 #else
1391  GetOStream(Errors) << "Dependency graph output requires boost and MueLu_ENABLE_Boost_for_real" << std::endl;
1392 #endif
1393  }
1394 
1395  // Enforce that coordinate vector's map is consistent with that of A
1396  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1398  RCP<Operator> Ao = level.Get<RCP<Operator> >("A");
1399  RCP<Matrix> A = rcp_dynamic_cast<Matrix>(Ao);
1400  if (A.is_null()) {
1401  GetOStream(Warnings1) << "Hierarchy::ReplaceCoordinateMap: operator is not a matrix, skipping..." << std::endl;
1402  return;
1403  }
1404  if(Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(A) != Teuchos::null) {
1405  GetOStream(Warnings1) << "Hierarchy::ReplaceCoordinateMap: operator is a BlockedCrsMatrix, skipping..." << std::endl;
1406  return;
1407  }
1408 
1410 
1411  RCP<xdMV> coords = level.Get<RCP<xdMV> >("Coordinates");
1412 
1413  if (A->getRowMap()->isSameAs(*(coords->getMap()))) {
1414  GetOStream(Warnings1) << "Hierarchy::ReplaceCoordinateMap: matrix and coordinates maps are same, skipping..." << std::endl;
1415  return;
1416  }
1417 
1418  if (A->IsView("stridedMaps") && rcp_dynamic_cast<const StridedMap>(A->getRowMap("stridedMaps")) != Teuchos::null) {
1419  RCP<const StridedMap> stridedRowMap = rcp_dynamic_cast<const StridedMap>(A->getRowMap("stridedMaps"));
1420 
1421  // It is better to through an exceptions if maps may be inconsistent, than to ignore it and experience unfathomable breakdowns
1422  TEUCHOS_TEST_FOR_EXCEPTION(stridedRowMap->getStridedBlockId() != -1 || stridedRowMap->getOffset() != 0,
1423  Exceptions::RuntimeError, "Hierarchy::ReplaceCoordinateMap: nontrivial maps (block id = " << stridedRowMap->getStridedBlockId()
1424  << ", offset = " << stridedRowMap->getOffset() << ")");
1425  }
1426 
1427  GetOStream(Runtime1) << "Replacing coordinate map" << std::endl;
1428 
1429  size_t blkSize = A->GetFixedBlockSize();
1430 
1431  RCP<const Map> nodeMap = A->getRowMap();
1432  if (blkSize > 1) {
1433  // Create a nodal map, as coordinates have not been expanded to a DOF map yet.
1434  RCP<const Map> dofMap = A->getRowMap();
1435  GO indexBase = dofMap->getIndexBase();
1436  size_t numLocalDOFs = dofMap->getNodeNumElements();
1437  TEUCHOS_TEST_FOR_EXCEPTION(numLocalDOFs % blkSize, Exceptions::RuntimeError,
1438  "Hierarchy::ReplaceCoordinateMap: block size (" << blkSize << ") is incompatible with the number of local dofs in a row map (" << numLocalDOFs);
1439  ArrayView<const GO> GIDs = dofMap->getNodeElementList();
1440 
1441  Array<GO> nodeGIDs(numLocalDOFs/blkSize);
1442  for (size_t i = 0; i < numLocalDOFs; i += blkSize)
1443  nodeGIDs[i/blkSize] = (GIDs[i] - indexBase)/blkSize + indexBase;
1444 
1446  nodeMap = MapFactory::Build(dofMap->lib(), INVALID, nodeGIDs(), indexBase, dofMap->getComm());
1447  } else {
1448  // blkSize == 1
1449  // Check whether the length of vectors fits to the size of A
1450  // If yes, make sure that the maps are matching
1451  // If no, throw a warning but do not touch the Coordinates
1452  if(coords->getLocalLength() != A->getRowMap()->getNodeNumElements()) {
1453  GetOStream(Warnings) << "Coordinate vector does not match row map of matrix A!" << std::endl;
1454  return;
1455  }
1456  }
1457 
1459  std::vector<ArrayRCP<const typename Teuchos::ScalarTraits<Scalar>::coordinateType> > coordData;
1460  for (size_t i = 0; i < coords->getNumVectors(); i++) {
1461  coordData.push_back(coords->getData(i));
1462  coordDataView.push_back(coordData[i]());
1463  }
1464 
1465  RCP<xdMV> newCoords = Xpetra::MultiVectorFactory<typename Teuchos::ScalarTraits<Scalar>::coordinateType,LO,GO,NO>::Build(nodeMap, coordDataView(), coords->getNumVectors());
1466  level.Set("Coordinates", newCoords);
1467  }
1468 
1469  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1471  int N = Levels_.size();
1472  if( (sizeOfAllocatedLevelMultiVectors_ == numvecs && residual_.size() == N) || numvecs<=0 ) return;
1473 
1474  // If, somehow, we changed the number of levels, delete everything first
1475  if(residual_.size() != N) {
1476  DeleteLevelMultiVectors();
1477 
1478  residual_.resize(N);
1479  coarseRhs_.resize(N);
1480  coarseX_.resize(N);
1481  coarseImport_.resize(N);
1482  coarseExport_.resize(N);
1483  correction_.resize(N);
1484  }
1485 
1486  for(int i=0; i<N; i++) {
1487  RCP<Operator> A = Levels_[i]->template Get< RCP<Operator> >("A");
1488  if(!A.is_null()) {
1489  // This dance is because we allow A to have a BlockedMap and X/B to have (compatible) non-blocked map
1490  RCP<const BlockedCrsMatrix> A_as_blocked = Teuchos::rcp_dynamic_cast<const BlockedCrsMatrix>(A);
1491  RCP<const Map> Arm = A->getRangeMap();
1492  RCP<const Map> Adm = A->getDomainMap();
1493  if(!A_as_blocked.is_null()) {
1494  Adm = A_as_blocked->getFullDomainMap();
1495  }
1496 
1497  // This is zero'd by default since it is filled via an operator apply
1498  residual_[i] = MultiVectorFactory::Build(Arm, numvecs, true);
1499  correction_[i] = MultiVectorFactory::Build(Adm, numvecs, false);
1500  }
1501 
1502  if(i+1<N) {
1503  // This is zero'd by default since it is filled via an operator apply
1504  if(implicitTranspose_) {
1505  RCP<Operator> P = Levels_[i+1]->template Get< RCP<Operator> >("P");
1506  if(!P.is_null()) coarseRhs_[i] = MultiVectorFactory::Build(P->getDomainMap(),numvecs,true);
1507  } else {
1508  RCP<Operator> R = Levels_[i+1]->template Get< RCP<Operator> >("R");
1509  if(!R.is_null()) coarseRhs_[i] = MultiVectorFactory::Build(R->getRangeMap(),numvecs,true);
1510  }
1511 
1512 
1513  RCP<const Import> importer;
1514  if(Levels_[i+1]->IsAvailable("Importer"))
1515  importer = Levels_[i+1]->template Get< RCP<const Import> >("Importer");
1516  if (doPRrebalance_ || importer.is_null())
1517  coarseX_[i] = MultiVectorFactory::Build(coarseRhs_[i]->getMap(),numvecs,false);
1518  else {
1519  coarseImport_[i] = MultiVectorFactory::Build(importer->getTargetMap(), numvecs,false);
1520  coarseExport_[i] = MultiVectorFactory::Build(importer->getSourceMap(), numvecs,false);
1521  coarseX_[i] = MultiVectorFactory::Build(importer->getTargetMap(),numvecs,false);
1522  }
1523  }
1524  }
1525  sizeOfAllocatedLevelMultiVectors_ = numvecs;
1526  }
1527 
1528 
1529 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1531  if(sizeOfAllocatedLevelMultiVectors_==0) return;
1532  residual_.resize(0);
1533  coarseRhs_.resize(0);
1534  coarseX_.resize(0);
1535  coarseImport_.resize(0);
1536  coarseExport_.resize(0);
1537  correction_.resize(0);
1538  sizeOfAllocatedLevelMultiVectors_ = 0;
1539 }
1540 
1541 
1542 } //namespace MueLu
1543 
1544 #endif // MUELU_HIERARCHY_DEF_HPP
Important warning messages (one line)
void Build(Level &level) const
Build an object with this factory.
void IsPreconditioner(const bool flag)
RCP< Level > & GetPreviousLevel()
Previous level.
static void SetRandomSeed(const Teuchos::Comm< int > &comm)
High level timing information (use Teuchos::TimeMonitor::summarize() to print)
virtual std::string getObjectLabel() const
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.
RCP< Level > & GetLevel(const int levelID=0)
Retrieve a certain level from hierarchy.
std::string toString(const T &what)
Little helper function to convert non-string types to strings.
Hierarchy()
Default constructor.
virtual size_t getNodeSmootherComplexity() const =0
Compute a rough estimate of the cost to apply this smoother on this MPI rank. Return Teuchos::Ordinal...
void Release(const FactoryBase &factory)
Decrement the storage counter for all the inputs of a factory.
GlobalOrdinal GO
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
static RCP< Time > getNewCounter(const std::string &name)
void AddLevel(const RCP< Level > &level)
Add a level at the end of the hierarchy.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
short KeepType
static void Write(const std::string &fileName, const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > &M)
Print more statistics.
void AddNewLevel()
Add a new level at the end of the hierarchy.
VerbLevel toMueLuVerbLevel(const Teuchos::EVerbosityLevel verbLevel)
void swap(RCP< T > &r_ptr)
LocalOrdinal LO
One-liner description of what is happening.
void DumpCurrentGraph() const
void Clear(int startLevel=0)
Clear impermanent data from previous setup.
void SetPreviousLevel(const RCP< Level > &previousLevel)
Definition: MueLu_Level.cpp:85
Exception throws to report incompatible objects (like maps).
Integrates Teuchos::TimeMonitor with MueLu verbosity system.
void ReplaceCoordinateMap(Level &level)
void AllocateLevelMultiVectors(int numvecs)
static std::string getColonLabel(const std::string &label)
Helper function for object label.
void AddKeepFlag(const std::string &ename, const FactoryBase *factory=NoFactory::get(), KeepType keep=MueLu::Keep)
Call Level::AddKeepFlag for each level of the Hierarchy.
Print even more statistics.
void setlib(Xpetra::UnderlyingLib lib2)
Additional warnings.
void print(std::ostream &out=std::cout, const VerbLevel verbLevel=(MueLu::Parameters|MueLu::Statistics0)) const
double GetSmootherComplexity() const
Base class for factories (e.g., R, P, and A_coarse).
Print statistics that do not involve significant additional computation.
Xpetra::UnderlyingLib lib_
static RCP< Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Residual(const Xpetra::Operator< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &X, const Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &RHS)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
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.
Class that provides default factories within Needs class.
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.
Xpetra::UnderlyingLib lib()
virtual void setObjectLabel(const std::string &objectLabel)
TEUCHOS_DEPRECATED void reduceAll(const Comm< Ordinal > &comm, const EReductionType reductType, const Packet &send, Packet *globalReduct)
static std::string PrintMatrixInfo(const Matrix &A, const std::string &msgTag, RCP< const Teuchos::ParameterList > params=Teuchos::null)
size_t global_size_t
void Set(const std::string &ename, const T &entry, const FactoryBase *factory=NoFactory::get())
void Build(Level &fineLevel, Level &coarseLevel) const
Build an object with this factory.
int GetGlobalNumLevels() const
void push_back(const value_type &x)
Data struct for defining stopping criteria of multigrid iteration.
static Teuchos::Array< Magnitude > ResidualNorm(const Xpetra::Operator< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &X, const Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &RHS)
void SetLevelID(int levelID)
Set level number.
Definition: MueLu_Level.cpp:78
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.
Timer to be used in non-factories.
static const Teuchos::RCP< Teuchos::StackedTimer > & getStackedTimer()
size_type size() const
Array< RCP< Level > > Levels_
Container for Level objects.
Scalar SC
Node NO
int GetLevelID() const
Return level number.
Definition: MueLu_Level.cpp:76
Exception throws to report errors in the internal logical of the program.
An exception safe way to call the method &#39;Level::SetFactoryManager()&#39;.
void SetComm(RCP< const Teuchos::Comm< int > > const &comm)
void describe(Teuchos::FancyOStream &out, const VerbLevel verbLevel=Default) const
Print the Hierarchy with some verbosity level to a FancyOStream object.
Print all warning messages.
ReturnType Iterate(const MultiVector &B, MultiVector &X, ConvData conv=ConvData(), bool InitialGuessIsZero=false, LO startLevel=0)
Apply the multigrid preconditioner.
Description of what is happening (more verbose)
void Keep(const std::string &ename, const FactoryBase *factory=NoFactory::get())
Call Level::Keep(ename, factory) for each level of the Hierarchy.
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.
RCP< const Teuchos::Comm< int > > GetComm() const
virtual std::string description() const
Return a simple one-line description of this object.
Provides methods to build a multigrid hierarchy and apply multigrid cycles.
std::string toString(const T &t)
bool IsAvailable(const std::string &ename, const FactoryBase *factory=NoFactory::get()) const
Test whether a need&#39;s value has been saved.
void Request(const FactoryBase &factory)
Increment the storage counter for all the inputs of a factory.
bool is_null() const