MueLu Version of the Day
Loading...
Searching...
No Matches
MueLu_SmootherFactory_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_SMOOTHERFACTORY_DEF_HPP
47#define MUELU_SMOOTHERFACTORY_DEF_HPP
48
50
51#include "MueLu_Level.hpp"
52#include "MueLu_Exceptions.hpp"
53#include "MueLu_SmootherPrototype.hpp"
54
55
56namespace MueLu {
57
58 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
59 SmootherFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::SmootherFactory(RCP<SmootherPrototype> preAndPostSmootherPrototype) {
60 SetSmootherPrototypes(preAndPostSmootherPrototype);
61 }
62
63 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
65 RCP<SmootherPrototype> postSmootherPrototype) {
66 SetSmootherPrototypes(preSmootherPrototype, postSmootherPrototype);
67 }
68
69 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
70 void SmootherFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::SetSmootherPrototypes(RCP<SmootherPrototype> preAndPostSmootherPrototype) {
71 preSmootherPrototype_ = postSmootherPrototype_ = preAndPostSmootherPrototype;
73 }
74
75 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
77 RCP<SmootherPrototype> postSmootherPrototype) {
78 preSmootherPrototype_ = preSmootherPrototype;
79 postSmootherPrototype_ = postSmootherPrototype;
81 }
82
83 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
85 RCP<ParameterList> validParamList = rcp(new ParameterList());
86
87 validParamList->set<bool>("keep smoother data", false, "Keep constructed smoothers for later reuse");
88
89 validParamList->set< RCP<SmootherPrototype> >("PreSmoother data", null, "Pre-smoother data for reuse");
90 validParamList->set< RCP<SmootherPrototype> >("PostSmoother data", null, "Post-smoother data for reuse");
91
92 return validParamList;
93 }
94
95 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
97 TEUCHOS_TEST_FOR_EXCEPTION(preSmootherPrototype_ != Teuchos::null && preSmootherPrototype_->IsSetup() == true,
98 Exceptions::RuntimeError, "preSmoother prototype is not a smoother prototype (IsSetup() == true)");
99 TEUCHOS_TEST_FOR_EXCEPTION(postSmootherPrototype_ != Teuchos::null && postSmootherPrototype_->IsSetup() == true,
100 Exceptions::RuntimeError, "postSmoother prototype is not a smoother prototype (IsSetup() == true)");
101 }
102
103 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
105 RCP<SmootherPrototype>& postSmootherPrototype) const {
106 preSmootherPrototype = preSmootherPrototype_;
107 postSmootherPrototype = postSmootherPrototype_;
108 }
109
110 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
112 if (preSmootherPrototype_ != Teuchos::null)
113 preSmootherPrototype_->DeclareInput(currentLevel);
114
116 postSmootherPrototype_->DeclareInput(currentLevel);
117 }
118
119 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
121 return BuildSmoother(currentLevel, BOTH);
122 }
123
124 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
126 // SmootherFactory is quite tricky because of the fact that one of the smoother prototypes may be zero.
127 // The challenge is that we have no way of knowing how user uses this factory. For instance, lets say
128 // user wants to use s1 prototype as a presmoother, and s2 as a postsmoother. They could do:
129 // (a) create SmootherFactory(s1, s2), or
130 // (b) create SmootherFactory(s1, null) and SmootherFactory(null, s2)
131 // It may also happen that somewhere somebody set presmoother factory = postsmoother factory = (a)
132 // How do you do DeclareInput in this case? It could easily introduce a bug if a user does not check
133 // whether presmoother = postsmoother. A buggy code could look like that:
134 // RCP<SmootherFactory> s = rcp(new SmootherFactory(s1,s2));
135 // level.Request("PreSmoother", s.get());
136 // level.Request("PostSmoother", s.get());
137 // Get<RCP<SmootherBase> > pre = Get<RCP<SmootherBase> >("PreSmoother", s.get());
138 // Get<RCP<SmootherBase> > post = Get<RCP<SmootherBase> >("PostSmoother", s.get());
139 // This code would call DeclareInput in request mode twice, but as the Build method generates both Pre and Post
140 // smoothers, it would call DelcareInput in release mode only once, leaving requests.
141 // This code has another problem if s2 = Teuchos::null. In that case, despite the request for PostSmoother, the factory
142 // would not generate one, and second Get would throw. The real issue here is that given a Factory pointer
143 // there is no way to be sure that this factory would generate any of "PreSmoother" or "PostSmoother", unless you are
144 // able to cast it to SmootherFactory, do GetPrototypes and to check whether any of those is Teuchos::null.
145
146 const Teuchos::ParameterList& pL = GetParameterList();
147
148 RCP<SmootherPrototype> preSmoother, postSmoother;
149 ParameterList preSmootherParams, postSmootherParams;
150
151 if ((preOrPost & PRE) && !preSmootherPrototype_.is_null()) {
152
153 if (currentLevel.IsAvailable("PreSmoother data", this))
154 preSmoother = currentLevel.Get<RCP<SmootherPrototype> >("PreSmoother data", this);
155 else
156 preSmoother = preSmootherPrototype_->Copy();
157
158 int oldRank = -1;
159 if (!currentLevel.GetComm().is_null())
160 oldRank = preSmoother->SetProcRankVerbose(currentLevel.GetComm()->getRank());
161
162 preSmoother->Setup(currentLevel);
163 preSmootherParams = preSmoother->GetParameterList();
164
165 if (oldRank != -1)
166 preSmoother->SetProcRankVerbose(oldRank);
167
168 currentLevel.Set<RCP<SmootherBase> >("PreSmoother", preSmoother, this);
169
170 if (pL.get<bool>("keep smoother data"))
171 Set(currentLevel, "PreSmoother data", preSmoother);
172 }
173
174 if ((preOrPost & POST) && !postSmootherPrototype_.is_null()) {
175 if (preOrPost == BOTH && preSmootherPrototype_ == postSmootherPrototype_) {
176 // Simple reuse
177 // Same prototypes for pre- and post-smoothers mean that we only need to call Setup only once
178 postSmoother = preSmoother;
179
180 // else if (preOrPost == BOTH &&
181 // preSmootherPrototype_ != Teuchos::null &&
182 // preSmootherPrototype_->GetType() == postSmootherPrototype_->GetType()) {
183
184 // // More complex reuse case: need implementation of CopyParameters() and a smoothers smart enough to know when parameters affect the setup phase.
185
186 // // YES: post-smoother == pre-smoother
187 // // => copy the pre-smoother to avoid the setup phase of the post-smoother.
188 // postSmoother = preSmoother->Copy();
189 // // If the post-smoother parameters are different from
190 // // pre-smoother, the parameters stored in the post-smoother
191 // // prototype are copied in the new post-smoother object.
192 // postSmoother->CopyParameters(postSmootherPrototype_);
193 // // If parameters don't influence the Setup phase (it is the case
194 // // for Jacobi, Chebyshev...), PostSmoother is already setup. Nothing
195 // // more to do. In the case of ILU, parameters of the smoother
196 // // are in fact the parameters of the Setup phase. The call to
197 // // CopyParameters resets the smoother (only if parameters are
198 // // different) and we must call Setup() again.
199 // postSmoother->Setup(currentLevel);
200 // }
201
202 // // TODO: if CopyParameters do not exist, do setup twice.
203
204 } else {
205
206 if (currentLevel.IsAvailable("PostSmoother data", this)) {
207 postSmoother = currentLevel.Get<RCP<SmootherPrototype> >("PostSmoother data", this);
208 } else {
209 // No reuse:
210 // - either we only do postsmoothing without any presmoothing
211 // - or our postsmoother is different from presmoother
212 postSmoother = postSmootherPrototype_->Copy();
213 }
214
215 int oldRank = -1;
216 if (!currentLevel.GetComm().is_null())
217 oldRank = postSmoother->SetProcRankVerbose(GetProcRankVerbose());
218
219 postSmoother->Setup(currentLevel);
220 postSmootherParams = postSmoother->GetParameterList();
221
222 if (oldRank != -1)
223 postSmoother->SetProcRankVerbose(oldRank);
224 }
225
226 currentLevel.Set<RCP<SmootherBase> >("PostSmoother", postSmoother, this);
227
228 if (pL.get<bool>("keep smoother data"))
229 Set(currentLevel, "PostSmoother data", postSmoother);
230 }
231
232 ParameterList& paramList = const_cast<ParameterList&>(this->GetParameterList());
233 if (postSmoother == preSmoother && !preSmoother.is_null()) {
234 paramList.sublist("smoother", false) = preSmoother->GetParameterList();
235
236 } else {
237 if (!preSmoother.is_null())
238 paramList.sublist("presmoother", false) = preSmootherParams;
239
240 if (!postSmoother.is_null())
241 paramList.sublist("postsmoother", false) = postSmootherParams;
242 }
243
244 } // Build()
245
246 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
248 std::ostringstream out;
250 std::string preStr = (preSmootherPrototype_ == Teuchos::null) ? "null" : preSmootherPrototype_->description();
251 std::string postStr = (preSmootherPrototype_ == postSmootherPrototype_) ? "pre" : ( (postSmootherPrototype_ == Teuchos::null) ? "null" : postSmootherPrototype_->description() );
252 out << "{pre = " << preStr << ", post = "<< postStr << "}";
253 return out.str();
254 }
255
256 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
257 void SmootherFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::describe(Teuchos::FancyOStream& out, const VerbLevel verbLevel) const {
259
260 if (verbLevel & Parameters0) {
261 out0 << "PreSmoother : ";
262 if (preSmootherPrototype_.is_null()) {
263 out0 << "null" << std::endl;
264 } else {
265 Teuchos::OSTab tab2(out);
266 preSmootherPrototype_->describe(out, verbLevel);
267 }
268
269 out0 << "PostSmoother: ";
270 if (postSmootherPrototype_ == preSmootherPrototype_) { out0 << "same as PreSmoother" << std::endl; }
271 else if (postSmootherPrototype_ == Teuchos::null) { out0 << "null" << std::endl; }
272 else {
273 Teuchos::OSTab tab2(out);
274 postSmootherPrototype_->describe(out, verbLevel);
275 out0 << "PostSmoother is different than PreSmoother (not the same object)" << std::endl;
276 }
277 }
278
279 if (verbLevel & Debug) {
280 if (preSmootherPrototype_ != Teuchos::null || postSmootherPrototype_ != Teuchos::null) { out0 << "-" << std::endl; }
281 if (preSmootherPrototype_ != Teuchos::null) { out0 << "RCP<preSmootherPrototype_> : " << preSmootherPrototype_ << std::endl; }
282 if (postSmootherPrototype_ != Teuchos::null) { out0 << "RCP<postSmootherPrototype_>: " << postSmootherPrototype_ << std::endl; }
283 }
284 }
285
286
287} // namespace MueLu
288
289//TODO: doc: setup done twice if PostSmoother object != PreSmoother object and no adv. reused capability
290
291// TODO ReUse: If only one smoother is missing, SmootherFactory can be smart and build only the missing smoother.
292// TODO (optim): we can also reuse if preOrPost = post and preSmoother available in Level
293// we can also reuse if preOrPost = pre and postSmoother available in Level
294
295#endif // MUELU_SMOOTHERFACTORY_DEF_HPP
#define MUELU_DESCRIBE
Helper macro for implementing Describable::describe() for BaseClass objects.
virtual std::string description() const
Return a simple one-line description of this object.
Exception throws to report errors in the internal logical of the program.
void Set(Level &level, const std::string &varName, const T &data) const
Class that holds all level-specific information.
bool IsAvailable(const std::string &ename, const FactoryBase *factory=NoFactory::get()) const
Test whether a need's value has been saved.
RCP< const Teuchos::Comm< int > > GetComm() const
T & Get(const std::string &ename, const FactoryBase *factory=NoFactory::get())
Get data without decrementing associated storage counter (i.e., read-only access)....
void Set(const std::string &ename, const T &entry, const FactoryBase *factory=NoFactory::get())
virtual const Teuchos::ParameterList & GetParameterList() const
void GetSmootherPrototypes(RCP< SmootherPrototype > &preSmootherPrototype, RCP< SmootherPrototype > &postSmootherPrototype) const
Get smoother prototypes.
RCP< SmootherPrototype > preSmootherPrototype_
void SetSmootherPrototypes(RCP< SmootherPrototype > preAndPostSmootherPrototype)
Set smoother prototypes.
RCP< const ParameterList > GetValidParameterList() const
Input.
std::string description() const
Return a simple one-line description of this object.
SmootherFactory(RCP< SmootherPrototype > preAndPostSmootherPrototype=Teuchos::null)
Constructor.
void DeclareInput(Level &currentLevel) const
Specifies the data that this class needs, and the factories that generate that data.
void BuildSmoother(Level &currentLevel, const PreOrPost preOrPost=BOTH) const
void describe(Teuchos::FancyOStream &out, const VerbLevel verbLevel=Default) const
RCP< SmootherPrototype > postSmootherPrototype_
void Build(Level &currentLevel) const
Creates pre and post smoothers.
int GetProcRankVerbose() const
Get proc rank used for printing. Do not use this information for any other purpose....
Namespace for MueLu classes and methods.
@ Debug
Print additional debugging information.
@ Parameters0
Print class parameters.