MueLu  Version of the Day
MueLu_AmesosSmoother.cpp
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 #include <algorithm>
47 
48 #include "MueLu_ConfigDefs.hpp"
49 
50 #if defined(HAVE_MUELU_EPETRA) && defined(HAVE_MUELU_AMESOS)
51 
52 #include <Epetra_LinearProblem.h>
53 
54 #include <Amesos_config.h>
55 #include <Amesos.h>
56 #include <Amesos_BaseSolver.h>
57 
58 #include "MueLu_AmesosSmoother.hpp"
59 
60 #include "MueLu_Level.hpp"
61 #include "MueLu_Utilities.hpp"
62 #include "MueLu_Monitor.hpp"
63 
64 namespace MueLu {
65 
66  AmesosSmoother::AmesosSmoother(const std::string& type, const Teuchos::ParameterList& paramList)
67  : type_(type) {
68  this->SetParameterList(paramList);
69 
70  if (!type_.empty()) {
71  // Transform string to "Abcde" notation
72  std::transform(type_.begin(), type_.end(), type_.begin(), ::tolower);
73  std::transform(type_.begin(), ++type_.begin(), type_.begin(), ::toupper);
74  }
75  if (type_ == "Amesos_klu") type_ = "Klu";
76  if (type_ == "Klu2") type_ = "Klu";
77  if (type_ == "Amesos_umfpack") type_ = "Umfpack";
78  if (type_ == "Superlu_dist") type_ = "Superludist";
79  if (type_ == "Amesos_mumps") type_ = "Mumps";
80 
81  // Try to come up with something availble
82  // Order corresponds to our preference
83  // TODO: It would be great is Amesos provides directly this kind of logic for us
84  std::string oldtype = type_;
85  if (type_ == "" || Amesos().Query(type_) == false) {
86 #if defined(HAVE_AMESOS_SUPERLU)
87  type_ = "Superlu";
88 #elif defined(HAVE_AMESOS_KLU)
89  type_ = "Klu";
90 #elif defined(HAVE_AMESOS_SUPERLUDIST)
91  type_ = "Superludist";
92 #elif defined(HAVE_AMESOS_UMFPACK)
93  type_ = "Umfpack";
94 #else
95  throw Exceptions::RuntimeError("Amesos has been compiled without SuperLU_DIST, SuperLU, Umfpack or Klu. By default, MueLu tries"
96  "to use one of these libraries. Amesos must be compiled with one of these solvers, "
97  "or a valid Amesos solver has to be specified explicitly.");
98 #endif
99  if (oldtype != "")
100  this->GetOStream(Warnings0) << "MueLu::AmesosSmoother: \"" << oldtype << "\" is not available. Using \"" << type_ << "\" instead" << std::endl;
101  else
102  this->GetOStream(Runtime1) << "MueLu::AmesosSmoother: using \"" << type_ << "\"" << std::endl;
103  }
104  }
105 
107 
108  void AmesosSmoother::DeclareInput(Level &currentLevel) const {
109  Input(currentLevel, "A");
110  }
111 
112  void AmesosSmoother::Setup(Level& currentLevel) {
113  FactoryMonitor m(*this, "Setup Smoother", currentLevel);
114 
115  if (SmootherPrototype::IsSetup() == true)
116  GetOStream(Warnings0) << "MueLu::AmesosSmoother::Setup(): Setup() has already been called" << std::endl;
117 
118  A_ = Get< RCP<Matrix> >(currentLevel, "A");
119 
120  RCP<Epetra_CrsMatrix> epA = Utils::Op2NonConstEpetraCrs(A_);
121  linearProblem_ = rcp( new Epetra_LinearProblem() );
122  linearProblem_->SetOperator(epA.get());
123 
124  Amesos factory;
125  prec_ = rcp(factory.Create(type_, *linearProblem_));
126  TEUCHOS_TEST_FOR_EXCEPTION(prec_ == Teuchos::null, Exceptions::RuntimeError, "MueLu::AmesosSmoother::Setup(): Solver '" + type_ + "' not supported by Amesos");
127 
128  // set Reindex flag, if A is distributed with non-contiguous maps
129  // unfortunately there is no reindex for Amesos2, yet. So, this only works for Epetra based problems
130  if (A_->getRowMap()->isDistributed() == true && A_->getRowMap()->isContiguous() == false)
131  const_cast<ParameterList&>(this->GetParameterList()).set("Reindex", true);
132 
133  const ParameterList& paramList = this->GetParameterList();
134  RCP<ParameterList> precList = this->RemoveFactoriesFromList(paramList);
135 
136  prec_->SetParameters(*precList);
137 
138  const_cast<ParameterList&>(paramList).setParameters(*precList);
139 
140  int r = prec_->NumericFactorization();
141  TEUCHOS_TEST_FOR_EXCEPTION(r != 0, Exceptions::RuntimeError, "MueLu::AmesosSmoother::Setup(): Amesos solver returns value of " +
142  Teuchos::Utils::toString(r) + " during NumericFactorization()");
143 
145  }
146 
147  void AmesosSmoother::Apply(MultiVector& X, const MultiVector& B, bool InitialGuessIsZero) const {
148  TEUCHOS_TEST_FOR_EXCEPTION(SmootherPrototype::IsSetup() == false, Exceptions::RuntimeError, "MueLu::AmesosSmoother::Apply(): Setup() has not been called");
149 
150  Epetra_MultiVector &epX = Utils::MV2NonConstEpetraMV(X);
151  Epetra_MultiVector const &epB = Utils::MV2EpetraMV(B);
152  //Epetra_LinearProblem takes the right-hand side as a non-const pointer.
153  //I think this const_cast is safe because Amesos won't modify the rhs.
154  Epetra_MultiVector &nonconstB = const_cast<Epetra_MultiVector&>(epB);
155 
156  linearProblem_->SetLHS(&epX);
157  linearProblem_->SetRHS(&nonconstB);
158 
159  prec_->Solve();
160 
161  // Don't keep pointers to our vectors in the Epetra_LinearProblem.
162  linearProblem_->SetLHS(0);
163  linearProblem_->SetRHS(0);
164  }
165 
166  RCP<MueLu::SmootherPrototype<double,int,int> > AmesosSmoother::Copy() const {
167  return rcp( new AmesosSmoother(*this) );
168  }
169 
170  std::string AmesosSmoother::description() const {
171  std::ostringstream out;
173  out << "{type = " << type_ << "}";
174  return out.str();
175  }
176 
177  //using MueLu::Describable::describe; // overloading, not hiding
178  void AmesosSmoother::print(Teuchos::FancyOStream& out, const VerbLevel verbLevel) const {
180 
181  if (verbLevel & Parameters0)
182  out0 << "Prec. type: " << type_ << std::endl;
183 
184  if (verbLevel & Parameters1) {
185  out0 << "Parameter list: " << std::endl;
186  Teuchos::OSTab tab2(out);
187  out << this->GetParameterList();
188  }
189 
190  if (verbLevel & External)
191  if (prec_ != Teuchos::null) {
192  prec_->PrintStatus();
193  prec_->PrintTiming();
194  }
195 
196  if (verbLevel & Debug) {
197  out0 << "IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl
198  << "-" << std::endl
199  << "RCP<A_>: " << A_ << std::endl
200  << "RCP<linearProblem__>: " << linearProblem_ << std::endl
201  << "RCP<prec_>: " << prec_ << std::endl;
202  }
203  }
204 
205 } // namespace MueLu
206 
207 #endif // HAVE_MUELU_EPETRA && HAVE_MUELU_AMESOS
void DeclareInput(Level &currentLevel) const
Input.
Important warning messages (one line)
RCP< Epetra_LinearProblem > linearProblem_
Problem that Amesos uses internally.
static RCP< Epetra_MultiVector > MV2NonConstEpetraMV(RCP< MultiVector > Vec)
std::string toString(const T &what)
Little helper function to convert non-string types to strings.
Print external lib objects.
Timer to be used in factories. Similar to Monitor but with additional timers.
void Setup(Level &currentLevel)
Set up the direct solver. This creates the underlying Amesos solver object according to the parameter...
Print additional debugging information.
AmesosSmoother(const std::string &type="", const Teuchos::ParameterList &paramList=Teuchos::ParameterList())
Constructor.
std::string type_
amesos-specific key phrase that denote smoother type
Namespace for MueLu classes and methods.
virtual const Teuchos::ParameterList & GetParameterList() const
RCP< Matrix > A_
Matrix. Not used directly, but held inside of linearProblem_. So we have to keep an RCP pointer to it...
RCP< Amesos_BaseSolver > prec_
pointer to Amesos solver object
static RCP< const Epetra_MultiVector > MV2EpetraMV(RCP< MultiVector > const Vec)
Helper utility to pull out the underlying Epetra objects from an Xpetra object.
RCP< ParameterList > RemoveFactoriesFromList(const ParameterList &list) const
virtual void SetParameterList(const ParameterList &paramList)
Set parameters from a parameter list and return with default values.
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:99
bool IsSetup() const
Get the state of a smoother prototype.
#define MUELU_DESCRIBE
Helper macro for implementing Describable::describe() for BaseClass objects.
std::string description() const
Return a simple one-line description of this object.
Teuchos::FancyOStream & GetOStream(MsgType type, int thisProcRankOnly=0) const
Get an output stream for outputting the input message type.
Print class parameters.
Amesos_BaseSolver * Create(const char *ClassType, const Epetra_LinearProblem &LinearProblem)
virtual ~AmesosSmoother()
Destructor.
void Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero=false) const
Apply the direct solver.
Print class parameters (more parameters, more verbose)
void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel=Default) const
Print the object with some verbosity level to an FancyOStream object.
Exception throws to report errors in the internal logical of the program.
static RCP< Epetra_CrsMatrix > Op2NonConstEpetraCrs(RCP< Matrix > Op)
Description of what is happening (more verbose)
void Input(Level &level, const std::string &varName) const
RCP< SmootherPrototype > Copy() const
virtual std::string description() const
Return a simple one-line description of this object.
bool Query(const char *ClassType)