MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_MergedSmoother_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_MERGEDSMOOTHER_DEF_HPP
47 #define MUELU_MERGEDSMOOTHER_DEF_HPP
48 
50 #include "MueLu_Exceptions.hpp"
51 
52 namespace MueLu {
53 
54 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
56  : smootherList_(smootherList)
57  , reverseOrder_(false)
58  , verbose_(verbose) {
59  // TODO: check that on each method TEUCHOS_TEST_FOR_EXCEPTION(smootherList == Teuchos::null, MueLu::Exceptions::RuntimeError, "");
60 
62 }
63 
64 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
66  : reverseOrder_(src.reverseOrder_)
67  , verbose_(src.verbose_) {
68  // Deep copy of src.smootherList_
70 }
71 
72 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
74  // We need to propagate SetFactory to proper place
75  for (typename ArrayView<RCP<SmootherPrototype> >::iterator it = smootherList_.begin(); it != smootherList_.end(); it++)
76  (*it)->SetFactory(varName, factory);
77 }
78 
79 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
81  for (typename ArrayView<RCP<SmootherPrototype> >::iterator it = smootherList_.begin(); it != smootherList_.end(); ++it)
82  (*it)->DeclareInput(currentLevel);
83 }
84 
85 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
87  if (SmootherPrototype::IsSetup() == true)
88  this->GetOStream(Warnings0) << "MueLu::MergedSmoother::Setup(): Setup() has already been called";
89 
90  for (typename ArrayView<RCP<SmootherPrototype> >::iterator it = smootherList_.begin(); it != smootherList_.end(); ++it) {
91  try {
92  (*it)->Setup(level);
93 
94  } catch (MueLu::Exceptions::RuntimeError& e) {
95  std::string msg = "MueLu::MergedSmoother<>::Setup(): Runtime Error.\n One of the underlying smoother throwed the following exception: \n";
96  msg += e.what();
98  }
99  }
100 
102 }
103 
104 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
105 void MergedSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Apply(MultiVector& X, const MultiVector& B, bool InitialGuessIsZero) const {
106  TEUCHOS_TEST_FOR_EXCEPTION(SmootherPrototype::IsSetup() == false, MueLu::Exceptions::RuntimeError, "MueLu::MergedSmoother<>:Apply(): Setup() has not been called");
107 
108  typedef typename ArrayRCP<RCP<SmootherPrototype> >::size_type sz_t;
109  sz_t n = smootherList_.size(), c = (reverseOrder_ ? n - 1 : 0);
110  char d = (reverseOrder_ ? -1 : 1);
111 
112  for (sz_t i = 0; i < n; i++) // loop unifying both forward and reverse order
113  try {
114  // Be careful with nonnegative numbers
115  smootherList_[c + d * Teuchos::as<char>(i)]->Apply(X, B, InitialGuessIsZero);
116 
117  // For second and later iterations, initial guess = previous result
118  InitialGuessIsZero = false;
119 
120  } catch (MueLu::Exceptions::RuntimeError& e) {
121  std::string msg = "MueLu::MergedSmoother<>::Apply(): Runtime Error. One of the underlying smoothers throws the following exception: \n";
122  msg += e.what();
124  }
125 }
126 
127 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
129  throw Exceptions::NotImplemented("MueLu::MergedSmoother<>::Print() is not implemented");
130 }
131 
132 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
133 void MergedSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CopyParameters(RCP<SmootherPrototype> src) { // TODO: wrong prototype. We do not need an RCP here.
134  RCP<MergedSmoother> srcMergedSmoother = rcp_dynamic_cast<MergedSmoother>(src); // TODO: check if dynamic cast fails
135 
136  reverseOrder_ = srcMergedSmoother->GetReverseOrder();
137 
138  {
139  const ArrayRCP<const RCP<SmootherPrototype> >& srcSmootherList = srcMergedSmoother->GetSmootherList();
140  const ArrayRCP<const RCP<SmootherPrototype> >& thisSmootherList = smootherList_;
141  TEUCHOS_TEST_FOR_EXCEPTION(srcSmootherList == Teuchos::null, MueLu::Exceptions::RuntimeError, // might be allowed later if useful
142  "MueLu::MergedSmoother<>:CopyParameters(): thisSmootherList == Teuchos::null");
143 
144  // If the smootherList of 'this' and 'src' contains the same type of smoothers,
145  // we can transfert parameters from src to 'this' in order to tentatively reuse
146  // the current setup information of each smoothers. Note that the reuse of the
147  // setup phase of the MergedSmoother 'src' can be implemented for a larger set
148  // of cases (and more complicated cases), but it does not seems useful for now.
149 
150  bool reuse = true; // true == can we transfert parameters of smoothers one by one or do we have to copy the whole list of src?
151 
152  // test 1: same list size
153  reuse = reuse && (thisSmootherList.size() == srcSmootherList.size());
154 
155  if (reuse) {
156  // test 2: one-by-one comparison of smoother types
157  for (typename ArrayRCP<RCP<SmootherPrototype> >::size_type i = 0; i < srcSmootherList.size(); i++) {
158  // The following test should never throw in our use cases because 'src' is a prototype and
159  // 'this' is a real smoother so they don't share any data. We may allow such case later if useful.
160  TEUCHOS_TEST_FOR_EXCEPTION((thisSmootherList[i] == srcSmootherList[i]) && (thisSmootherList[i] != Teuchos::null), MueLu::Exceptions::RuntimeError,
161  "MueLu::MergedSmoother<>:CopyParameters(): internal logic error");
162 
163  // TODO
164  // reuse = reuse && ((thisSmootherList[i] == Teuchos::null && srcSmootherList[i] == Teuchos::null) ||
165  // thisSmootherList[i]->GetType() == srcSmootherList[i]->GetType());
166  }
167  }
168 
169  reuse = false; // TODO: temporary disactivated.
170 
171  if (reuse) {
172  bool isSetup = true;
173 
174  // Call CopyParameters for each smoothers and update IsSetup status of the MergedSmoother
175  for (typename ArrayRCP<RCP<SmootherPrototype> >::size_type i = 0; i < srcSmootherList.size(); i++) {
176  if (srcSmootherList[i] != Teuchos::null) {
177  TEUCHOS_TEST_FOR_EXCEPTION(srcSmootherList[i] == Teuchos::null, MueLu::Exceptions::RuntimeError, "MueLu::MergedSmoother<>:CopyParameters(): internal logic error");
178 
179  // TODO thisSmootherList[i]->CopyParameters(srcSmootherList[i]);
180  isSetup = isSetup && thisSmootherList[i]->IsSetup();
181  }
183  }
184 
185  } else {
186  // No reuse: copy srcSmootherList.
187  smootherList_ = Teuchos::null;
188  smootherList_ = SmootherListDeepCopy(srcSmootherList);
190  }
191  }
192 }
193 
194 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
197  Copy() const {
198  return rcp(new MergedSmoother(*this));
199 }
200 
201 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
204  SmootherListDeepCopy(const ArrayRCP<const RCP<SmootherPrototype> >& srcSmootherList) {
205  ArrayRCP<RCP<SmootherPrototype> > newSmootherList(srcSmootherList.size());
206 
207  for (typename ArrayRCP<RCP<SmootherPrototype> >::size_type i = 0; i < srcSmootherList.size(); i++)
208  newSmootherList[i] = srcSmootherList[i]->Copy();
209 
210  return newSmootherList;
211 }
212 
213 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
216  // FIXME: This is a placeholder
218 }
219 
220 } // namespace MueLu
221 
222 #endif // MUELU_MERGEDSMOOTHER_DEF_HPP
Important warning messages (one line)
RCP< SmootherPrototype > Copy() const
Copy method (performs a deep copy of input object)
ArrayRCP< RCP< SmootherPrototype > > smootherList_
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
size_t getNodeSmootherComplexity() const
Get a rough estimate of cost per iteration.
size_type size() const
void Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero=false) const
Apply.
void DeclareInput(Level &currentLevel) const
Input.
void CopyParameters(RCP< SmootherPrototype > src)
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
void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel=Default) const
Exception throws when you call an unimplemented method of MueLu.
ArrayRCP< RCP< SmootherPrototype > > SmootherListDeepCopy(const ArrayRCP< const RCP< SmootherPrototype > > &srcSmootherList)
bool IsSetup() const
Get the state of a smoother prototype.
const ArrayRCP< const RCP< SmootherPrototype > > GetSmootherList() const
void SetFactory(const std::string &varName, const RCP< const FactoryBase > &factory)
Custom SetFactory.
MergedSmoother(ArrayRCP< RCP< SmootherPrototype > > &smootherList, bool verbose=false)
Constructor.
Exception throws to report errors in the internal logical of the program.
void Setup(Level &level)
Set up.