MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_BelosSmoother_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_BELOSSMOOTHER_DEF_HPP
47 #define MUELU_BELOSSMOOTHER_DEF_HPP
48 
49 #include "MueLu_ConfigDefs.hpp"
50 
51 #if defined(HAVE_MUELU_BELOS)
52 
54 
55 #include <Xpetra_CrsMatrix.hpp>
56 #include <Xpetra_Matrix.hpp>
57 #include <Xpetra_MultiVectorFactory.hpp>
58 #ifdef HAVE_XPETRA_TPETRA
59 #include <Xpetra_TpetraMultiVector.hpp>
60 #endif
61 
63 #include "MueLu_Level.hpp"
64 #include "MueLu_Utilities.hpp"
65 #include "MueLu_Monitor.hpp"
66 
67 #include <BelosLinearProblem.hpp>
68 #include <BelosSolverFactory.hpp>
69 
70 namespace MueLu {
71 
72 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
74  : type_(type) {
75  bool solverSupported = false;
76  Belos::SolverFactory<Scalar, tMV, tOP> tFactory;
77  solverSupported = solverSupported || tFactory.isSupported(type);
78  // if (!solverSupported) {
79  // Teuchos::Array<std::string> supportedSolverNames = factory.supportedSolverNames();
80 
81  // std::ostringstream outString;
82  // outString << "[";
83  // for (auto iter = supportedSolverNames.begin(); iter != supportedSolverNames.end(); ++iter) {
84  // outString << "\"" << *iter << "\"";
85  // if (iter + 1 != supportedSolverNames.end()) {
86  // outString << ", ";
87  // }
88  // }
89  // outString << "]";
90 
91  // TEUCHOS_TEST_FOR_EXCEPTION(true, Exceptions::RuntimeError, "Belos does not provide the solver '" << type_ << "'.\nSupported Solvers: " << outString.str());
92  // }
93  this->declareConstructionOutcome(!solverSupported, "Belos does not provide the smoother '" + type_ + "'.");
94  if (solverSupported)
95  SetParameterList(paramList);
96 }
97 
98 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
100  Factory::SetParameterList(paramList);
101 }
102 
103 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
105  this->Input(currentLevel, "A");
106 }
107 
108 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
110  FactoryMonitor m(*this, "Setup Smoother", currentLevel);
111 
112  A_ = Factory::Get<RCP<Matrix> >(currentLevel, "A");
113  SetupBelos(currentLevel);
115  this->GetOStream(Statistics1) << description() << std::endl;
116 }
117 
118 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
120  bool useTpetra = A_->getRowMap()->lib() == Xpetra::UseTpetra;
121 
122  if (useTpetra) {
123  tBelosProblem_ = rcp(new Belos::LinearProblem<Scalar, tMV, tOP>());
125  tBelosProblem_->setOperator(tA);
126 
127  Belos::SolverFactory<SC, tMV, tOP> solverFactory;
128  tSolver_ = solverFactory.create(type_, rcpFromRef(const_cast<ParameterList&>(this->GetParameterList())));
129  tSolver_->setProblem(tBelosProblem_);
130  } else {
131  TEUCHOS_ASSERT(false);
132  }
133 }
134 
135 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
136 void BelosSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Apply(MultiVector& X, const MultiVector& B, bool InitialGuessIsZero) const {
137  TEUCHOS_TEST_FOR_EXCEPTION(SmootherPrototype::IsSetup() == false, Exceptions::RuntimeError, "MueLu::BelosSmoother::Apply(): Setup() has not been called");
138 
139  if (A_->getRowMap()->lib() == Xpetra::UseTpetra) {
140  if (InitialGuessIsZero) {
141  X.putScalar(0.0);
142 
145 
146  tBelosProblem_->setInitResVec(tpB);
147  tBelosProblem_->setProblem(tpX, tpB);
148  tSolver_->solve();
149 
150  } else {
151  typedef Teuchos::ScalarTraits<Scalar> TST;
152  RCP<MultiVector> Residual = Utilities::Residual(*A_, X, B);
153  RCP<MultiVector> Correction = MultiVectorFactory::Build(A_->getDomainMap(), X.getNumVectors());
154 
157 
158  tBelosProblem_->setInitResVec(tpB);
159  tBelosProblem_->setProblem(tpX, tpB);
160  tSolver_->solve();
161 
162  X.update(TST::one(), *Correction, TST::one());
163  }
164  } else {
165  TEUCHOS_ASSERT(false);
166  }
167 }
168 
169 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
171  RCP<BelosSmoother> smoother = rcp(new BelosSmoother(*this));
172  smoother->SetParameterList(this->GetParameterList());
173  return smoother;
174 }
175 
176 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
178  std::ostringstream out;
180  if (A_->getRowMap()->lib() == Xpetra::UseTpetra) {
181  out << tSolver_->description();
182  }
183  } else {
184  out << "BELOS {type = " << type_ << "}";
185  }
186  return out.str();
187 }
188 
189 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
192 
193  if (verbLevel & Parameters1) {
194  out0 << "Parameter list: " << std::endl;
195  Teuchos::OSTab tab2(out);
196  out << this->GetParameterList();
197  }
198 
199  if (verbLevel & External) {
200  if (tSolver_ != Teuchos::null) {
201  Teuchos::OSTab tab2(out);
202  out << *tSolver_ << std::endl;
203  }
204  }
205 
206  if (verbLevel & Debug) {
207  if (A_->getRowMap()->lib() == Xpetra::UseTpetra) {
208  out0 << "IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl
209  << "-" << std::endl
210  << "RCP<solver_>: " << tSolver_ << std::endl;
211  }
212  }
213 }
214 
215 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
218 }
219 
220 } // namespace MueLu
221 
222 #endif // HAVE_MUELU_BELOS
223 #endif // MUELU_BELOSSMOOTHER_DEF_HPP
static RCP< const Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > MV2TpetraMV(RCP< Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node >> const vec)
Helper utility to pull out the underlying Tpetra objects from an Xpetra object.
Print external lib objects.
void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel=Default) const
Print the object with some verbosity level to an FancyOStream object.
Timer to be used in factories. Similar to Monitor but with additional timers.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Class that encapsulates Belos smoothers.
Print more statistics.
Print additional debugging information.
void Setup(Level &currentLevel)
Set up the smoother.
size_t getNodeSmootherComplexity() const
For diagnostic purposes.
RCP< SmootherPrototype > Copy() const
static RCP< MultiVector > Residual(const Xpetra::Operator< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const MultiVector &X, const MultiVector &RHS)
virtual void SetParameterList(const Teuchos::ParameterList &paramList)
Set parameters from a parameter list and return with default values.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void declareConstructionOutcome(bool fail, std::string msg)
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:99
void SetupBelos(Level &currentLevel)
friend class BelosSmoother
Constructor.
bool IsSetup() const
Get the state of a smoother prototype.
#define MUELU_DESCRIBE
Helper macro for implementing Describable::describe() for BaseClass objects.
void Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero=false) const
Apply the preconditioner.
Print class parameters (more parameters, more verbose)
Exception throws to report errors in the internal logical of the program.
#define TEUCHOS_ASSERT(assertion_test)
void SetParameterList(const Teuchos::ParameterList &paramList)
Set parameters from a parameter list and return with default values.
static RCP< Tpetra::CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op2NonConstTpetraCrs(RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node >> Op)
static RCP< Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > MV2NonConstTpetraMV(RCP< Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node >> vec)
void DeclareInput(Level &currentLevel) const
Input.
std::string toString(const T &t)
std::string description() const
Return a simple one-line description of this object.