MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_BlockedDirectSolver_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  * MueLu_BlockedDirectSolver_def.hpp
48  *
49  * Created on: 09.02.2014
50  * Author: tobias
51  */
52 
53 #ifndef MUELU_BLOCKEDDIRECTSOLVER_DEF_HPP_
54 #define MUELU_BLOCKEDDIRECTSOLVER_DEF_HPP_
55 
56 #include "Teuchos_ArrayViewDecl.hpp"
57 #include "Teuchos_ScalarTraits.hpp"
58 
59 #include "MueLu_ConfigDefs.hpp"
60 
61 #include <Xpetra_Matrix.hpp>
63 #include <Xpetra_MultiVectorFactory.hpp>
64 
66 #include "MueLu_MergedBlockedMatrixFactory.hpp"
67 #include "MueLu_Level.hpp"
68 #include "MueLu_Monitor.hpp"
69 #include "MueLu_DirectSolver.hpp"
70 
71 namespace MueLu {
72 
73 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
75  MergedAFact_ = Teuchos::rcp(new MergedBlockedMatrixFactory());
76  s_ = Teuchos::rcp(new DirectSolver(type, paramList));
77  type_ = "blocked direct solver (" + type + ")";
78 }
79 
80 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
82  RCP<ParameterList> validParamList = rcp(new ParameterList());
83 
84  validParamList->set<RCP<const FactoryBase> >("A", null, "Generating factory of the matrix A");
85 
86  return validParamList;
87 }
88 
89 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
91  // Note that we have a nested smoother/solver object (of type DirectSolver), so we have to declare the dependencies by hand
92  // call DeclareInput by hand, since this->Input(currentLevel, "A") would not properly free A in the release mode (dependencies)
93  // We need the blocked version of A as input for the MergedAFact_
94  currentLevel.DeclareInput("A", this->GetFactory("A").get());
95 
96  // syncronize input factory for "A" and nested representation for "A"
97  MergedAFact_->SetFactory("A", this->GetFactory("A"));
98 
99  // declare input factories for nested direct solver
100  s_->SetFactory("A", MergedAFact_);
101  s_->DeclareInput(currentLevel);
102 }
103 
104 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
106  RCP<Teuchos::FancyOStream> out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
107 
108  FactoryMonitor m(*this, "Setup BlockedDirectSolver", currentLevel);
109  if (this->IsSetup() == true)
110  this->GetOStream(Warnings0) << "MueLu::BlockedDirectSolver::Setup(): Setup() has already been called";
111 
112  // extract blocked operator A from current level
113  A_ = Factory::Get<RCP<Matrix> >(currentLevel, "A"); // A needed for extracting map extractors
114  RCP<BlockedCrsMatrix> bA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(A_);
116  "MueLu::BlockedDirectSolver::Build: input matrix A is not of type BlockedCrsMatrix.");
117 
118  s_->Setup(currentLevel);
119 
120  this->IsSetup(true);
121 }
122 
123 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
124 void BlockedDirectSolver<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero) const {
125  TEUCHOS_TEST_FOR_EXCEPTION(this->IsSetup() == false, Exceptions::RuntimeError,
126  "MueLu::BlockedDirectSolver::Apply(): Setup() has not been called");
127 
128  RCP<MultiVector> rcpX = Teuchos::rcpFromRef(X);
129  RCP<const MultiVector> rcpB = Teuchos::rcpFromRef(B);
130  RCP<BlockedMultiVector> bX = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(rcpX);
131  RCP<const BlockedMultiVector> bB = Teuchos::rcp_dynamic_cast<const BlockedMultiVector>(rcpB);
132 
133 #ifdef HAVE_MUELU_DEBUG
134  RCP<BlockedCrsMatrix> bA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(A_);
135  if (bB.is_null() == false) {
136  // TEUCHOS_TEST_FOR_EXCEPTION(A_->getRangeMap()->isSameAs(*(B.getMap())) == false, Exceptions::RuntimeError, "MueLu::BlockedDirectSolver::Apply(): The map of RHS vector B is not the same as range map of the blocked operator A. Please check the map of B and A.");
137  } else {
138  TEUCHOS_TEST_FOR_EXCEPTION(bA->getFullRangeMap()->isSameAs(*(B.getMap())) == false, Exceptions::RuntimeError, "MueLu::BlockedDirectSolver::Apply(): The map of RHS vector B is not the same as range map of the blocked operator A. Please check the map of B and A.");
139  }
140  if (bX.is_null() == false) {
141  // TEUCHOS_TEST_FOR_EXCEPTION(A_->getDomainMap()->isSameAs(*(X.getMap())) == false, Exceptions::RuntimeError, "MueLu::BlockedDirectSolver::Apply(): The map of the solution vector X is not the same as domain map of the blocked operator A. Please check the map of X and A.");
142  } else {
143  TEUCHOS_TEST_FOR_EXCEPTION(bA->getFullDomainMap()->isSameAs(*(X.getMap())) == false, Exceptions::RuntimeError, "MueLu::BlockedDirectSolver::Apply(): The map of the solution vector X is not the same as domain map of the blocked operator A. Please check the map of X and A.");
144  }
145 #endif
146 
147  if (bB.is_null() == true && bX.is_null() == true) {
148  // standard case (neither B nor X are blocked)
149  s_->Apply(X, B, InitialGuessIsZero);
150  } else if (bB.is_null() == false && bX.is_null() == false) {
151  // both B and X are blocked
152  RCP<MultiVector> mergedX = bX->Merge();
153  RCP<const MultiVector> mergedB = bB->Merge();
154  s_->Apply(*mergedX, *mergedB, InitialGuessIsZero);
155  RCP<MultiVector> xx = Teuchos::rcp(new BlockedMultiVector(bX->getBlockedMap(), mergedX));
157  X.update(one, *xx, zero);
158  } else if (bB.is_null() == true && bX.is_null() == false) {
159  // the solution vector is blocked
160  RCP<MultiVector> mergedX = bX->Merge();
161  s_->Apply(*mergedX, B, InitialGuessIsZero);
162  RCP<MultiVector> xx = Teuchos::rcp(new BlockedMultiVector(bX->getBlockedMap(), mergedX));
164  X.update(one, *xx, zero);
165  } else if (bB.is_null() == false && bX.is_null() == true) {
166  // only the RHS vector is blocked
167  RCP<const MultiVector> mergedB = bB->Merge();
168  s_->Apply(X, *mergedB, InitialGuessIsZero);
169  }
170 }
171 
172 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
175  return rcp(new BlockedDirectSolver(*this));
176 }
177 
178 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
180  std::ostringstream out;
182  out << "{type = " << type_ << "}";
183  return out.str();
184 }
185 
186 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
189 
190  if (verbLevel & Parameters0)
191  out0 << "Prec. type: " << type_ << std::endl;
192 
193  if (verbLevel & Debug)
194  out0 << "IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl;
195 }
196 
197 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
199  // FIXME: This is a placeholder
201 }
202 
203 } // namespace MueLu
204 
205 #endif /* MUELU_BLOCKEDDIRECTSOLVER_DEF_HPP_ */
Important warning messages (one line)
Exception indicating invalid cast attempted.
void Setup(Level &currentLevel)
Setup routine Call the underlaying Setup routine of the nested direct solver once the input block mat...
BlockedDirectSolver(const std::string &type="", const Teuchos::ParameterList &paramList=Teuchos::ParameterList())
Constructor.
void DeclareInput(Level &currentLevel) const
Input.
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Timer to be used in factories. Similar to Monitor but with additional timers.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Print additional debugging information.
void Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero=false) const
Apply the direct solver. Solves the linear system AX=B using the constructed solver.
RCP< const ParameterList > GetValidParameterList() const
Input.
std::string description() const
Return a simple one-line description of this object.
Class that encapsulates direct solvers. Autoselection of AmesosSmoother or Amesos2Smoother according ...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:99
RCP< SmootherPrototype > Copy() const
bool IsSetup() const
Get the state of a smoother prototype.
direct solver for nxn blocked matrices
#define MUELU_DESCRIBE
Helper macro for implementing Describable::describe() for BaseClass objects.
Print class parameters.
size_t getNodeSmootherComplexity() const
Get a rough estimate of cost per iteration.
void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel=Default) const
Print the object with some verbosity level to an FancyOStream object.
Scalar SC
Exception throws to report errors in the internal logical of the program.
void DeclareInput(const std::string &ename, const FactoryBase *factory, const FactoryBase *requestedBy=NoFactory::get())
Callback from FactoryBase::CallDeclareInput() and FactoryBase::DeclareInput()
virtual std::string description() const
Return a simple one-line description of this object.
std::string toString(const T &t)
bool is_null() const