MueLu Version of the Day
Loading...
Searching...
No Matches
MueLu_BlockedJacobiSmoother_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#ifndef MUELU_BLOCKEDJACOBISMOOTHER_DEF_HPP_
48#define MUELU_BLOCKEDJACOBISMOOTHER_DEF_HPP_
49
50#include "Teuchos_ArrayViewDecl.hpp"
51#include "Teuchos_ScalarTraits.hpp"
52
53#include "MueLu_ConfigDefs.hpp"
54
55#include <Xpetra_BlockReorderManager.hpp>
56#include <Xpetra_Matrix.hpp>
57#include <Xpetra_BlockedCrsMatrix.hpp>
58#include <Xpetra_ReorderedBlockedCrsMatrix.hpp>
59#include <Xpetra_ReorderedBlockedMultiVector.hpp>
60#include <Xpetra_MultiVectorFactory.hpp>
61
63#include "MueLu_Level.hpp"
64#include "MueLu_Monitor.hpp"
65#include "MueLu_HierarchyUtils.hpp"
67
68namespace MueLu {
69
70 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
72 : type_("blocked Jacobi"), A_(Teuchos::null)
73 {
74 FactManager_.reserve(10); // TODO fix me!
75 }
76
77 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
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", Teuchos::null, "Generating factory of the matrix A");
85 validParamList->set<Scalar>("Damping factor", 1.0, "Damping/Scaling factor in Jacobi");
86 validParamList->set<LocalOrdinal>("Sweeps", 1, "Number of sweeps (default = 1)");
87
88 return validParamList;
89 }
90
91 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
92 void BlockedJacobiSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::AddFactoryManager(RCP<const FactoryManagerBase> FactManager, int pos) {
93 TEUCHOS_TEST_FOR_EXCEPTION(pos < 0, Exceptions::RuntimeError, "MueLu::BlockedJacobiSmoother::AddFactoryManager: parameter \'pos\' must not be negative! error.");
94
95 size_t myPos = Teuchos::as<size_t>(pos);
96
97 if (myPos < FactManager_.size()) {
98 // replace existing entris in FactManager_ vector
99 FactManager_.at(myPos) = FactManager;
100 } else if( myPos == FactManager_.size()) {
101 // add new Factory manager in the end of the vector
102 FactManager_.push_back(FactManager);
103 } else { // if(myPos > FactManager_.size())
104 RCP<Teuchos::FancyOStream> out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
105 *out << "Warning: cannot add new FactoryManager at proper position " << pos << ". The FactoryManager is just appended to the end. Check this!" << std::endl;
106
107 // add new Factory manager in the end of the vector
108 FactManager_.push_back(FactManager);
109 }
110 }
111
112 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
114 //this->Input(currentLevel, "A");
115 // TODO: check me: why is this->Input not freeing properly A in release mode?
116 currentLevel.DeclareInput("A",this->GetFactory("A").get());
117
118 // loop over all factory managers for the subblocks of blocked operator A
119 std::vector<Teuchos::RCP<const FactoryManagerBase> >::const_iterator it;
120 for(it = FactManager_.begin(); it!=FactManager_.end(); ++it) {
121 SetFactoryManager currentSFM (rcpFromRef(currentLevel), *it);
122
123 // request "Smoother" for current subblock row.
124 currentLevel.DeclareInput("PreSmoother",(*it)->GetFactory("Smoother").get());
125
126 // request "A" for current subblock row (only needed for Thyra mode)
127 currentLevel.DeclareInput("A",(*it)->GetFactory("A").get());
128 }
129 }
130
131 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
133
134 RCP<Teuchos::FancyOStream> out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
135
136 FactoryMonitor m(*this, "Setup blocked Jacobi Smoother", currentLevel);
137 if (SmootherPrototype::IsSetup() == true) this->GetOStream(Warnings0) << "MueLu::BlockedJacobiSmoother::Setup(): Setup() has already been called";
138
139 // extract blocked operator A from current level
140 A_ = Factory::Get< RCP<Matrix> >(currentLevel, "A"); // A needed for extracting map extractors
141 RCP<BlockedCrsMatrix> bA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(A_);
142 TEUCHOS_TEST_FOR_EXCEPTION(bA==Teuchos::null, Exceptions::BadCast, "MueLu::BlockedPFactory::Build: input matrix A is not of type BlockedCrsMatrix! error.");
143
144 // plausibility check
145 TEUCHOS_TEST_FOR_EXCEPTION(bA->Rows() != FactManager_.size(), Exceptions::RuntimeError, "MueLu::BlockedJacobiSmoother::Setup: number of block rows of A is " << bA->Rows() << " and does not match number of SubFactoryManagers " << FactManager_.size() << ". error.");
146 TEUCHOS_TEST_FOR_EXCEPTION(bA->Cols() != FactManager_.size(), Exceptions::RuntimeError, "MueLu::BlockedJacobiSmoother::Setup: number of block cols of A is " << bA->Cols() << " and does not match number of SubFactoryManagers " << FactManager_.size() << ". error.");
147
148 // store map extractors
149 rangeMapExtractor_ = bA->getRangeMapExtractor();
150 domainMapExtractor_ = bA->getDomainMapExtractor();
151
152 // loop over all factory managers for the subblocks of blocked operator A
153 std::vector<Teuchos::RCP<const FactoryManagerBase> >::const_iterator it;
154 for(it = FactManager_.begin(); it!=FactManager_.end(); ++it) {
155 SetFactoryManager currentSFM (rcpFromRef(currentLevel), *it);
156
157 // extract Smoother for current block row
158 RCP<const SmootherBase> Smoo = currentLevel.Get< RCP<SmootherBase> >("PreSmoother",(*it)->GetFactory("Smoother").get());
159 Inverse_.push_back(Smoo);
160
161 // store whether subblock matrix is blocked or not!
162 RCP<Matrix> Aii = currentLevel.Get< RCP<Matrix> >("A",(*it)->GetFactory("A").get());
163 bIsBlockedOperator_.push_back(Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(Aii)!=Teuchos::null);
164 }
165
167 }
168
169 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
170 void BlockedJacobiSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Apply(MultiVector &X, const MultiVector& B, bool InitialGuessIsZero) const
171 {
172 TEUCHOS_TEST_FOR_EXCEPTION(SmootherPrototype::IsSetup() == false, Exceptions::RuntimeError, "MueLu::BlockedJacobiSmoother::Apply(): Setup() has not been called");
173
174#if 0 // def HAVE_MUELU_DEBUG
175 // TODO simplify this debug check
176 RCP<MultiVector> rcpDebugX = Teuchos::rcpFromRef(X);
177 RCP<const MultiVector> rcpDebugB = Teuchos::rcpFromRef(B);
178 RCP<BlockedMultiVector> rcpBDebugX = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(rcpDebugX);
179 RCP<const BlockedMultiVector> rcpBDebugB = Teuchos::rcp_dynamic_cast<const BlockedMultiVector>(rcpDebugB);
180 //RCP<BlockedCrsMatrix> bA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(A_);
181 if(rcpBDebugB.is_null() == false) {
182 //this->GetOStream(Runtime1) << "BlockedJacobi: B is a BlockedMultiVector of size " << B.getMap()->getGlobalNumElements() << " with " << rcpBDebugB->getBlockedMap()->getNumMaps() << " blocks." << std::endl;
183 //TEUCHOS_TEST_FOR_EXCEPTION(A_->getRangeMap()->isSameAs(*(B.getMap())) == false, Exceptions::RuntimeError, "MueLu::BlockedJacobiSmoother::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.");
184 } else {
185 //this->GetOStream(Runtime1) << "BlockedJacobi: B is a MultiVector of size " << B.getMap()->getGlobalNumElements() << std::endl;
186 //TEUCHOS_TEST_FOR_EXCEPTION(bA->getFullRangeMap()->isSameAs(*(B.getMap())) == false, Exceptions::RuntimeError, "MueLu::BlockedJacobiSmoother::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.");
187 }
188 if(rcpBDebugX.is_null() == false) {
189 //this->GetOStream(Runtime1) << "BlockedJacobi: X is a BlockedMultiVector of size " << X.getMap()->getGlobalNumElements() << " with " << rcpBDebugX->getBlockedMap()->getNumMaps() << " blocks." << std::endl;
190 //TEUCHOS_TEST_FOR_EXCEPTION(A_->getDomainMap()->isSameAs(*(X.getMap())) == false, Exceptions::RuntimeError, "MueLu::BlockedJacobiSmoother::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.");
191 } else {
192 //this->GetOStream(Runtime1) << "BlockedJacobi: X is a MultiVector of size " << X.getMap()->getGlobalNumElements() << std::endl;
193 //TEUCHOS_TEST_FOR_EXCEPTION(bA->getFullDomainMap()->isSameAs(*(X.getMap())) == false, Exceptions::RuntimeError, "MueLu::BlockedJacobiSmoother::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.");
194 }
195
196
197#endif
198 SC zero = Teuchos::ScalarTraits<SC>::zero(), one = Teuchos::ScalarTraits<SC>::one();
199
200 // Input variables used for the rest of the algorithm
201 RCP<MultiVector> rcpX = Teuchos::rcpFromRef(X);
202 RCP<const MultiVector> rcpB = Teuchos::rcpFromRef(B);
203
204 // make sure that both rcpX and rcpB are BlockedMultiVector objects
205 bool bCopyResultX = false;
206 bool bReorderX = false;
207 bool bReorderB = false;
208 RCP<BlockedCrsMatrix> bA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(A_);
209 MUELU_TEST_FOR_EXCEPTION(bA.is_null() == true, Exceptions::RuntimeError, "MueLu::BlockedGaussSeidelSmoother::Apply(): A_ must be a BlockedCrsMatrix");
210 RCP<BlockedMultiVector> bX = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(rcpX);
211 RCP<const BlockedMultiVector> bB = Teuchos::rcp_dynamic_cast<const BlockedMultiVector>(rcpB);
212
213 // check the type of operator
214 RCP<Xpetra::ReorderedBlockedCrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> > rbA =
215 Teuchos::rcp_dynamic_cast<Xpetra::ReorderedBlockedCrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> >(bA);
216
217 if(rbA.is_null() == false) {
218 // A is a ReorderedBlockedCrsMatrix and thus uses nested maps, retrieve BlockedCrsMatrix and use plain blocked
219 // map for the construction of the blocked multivectors
220
221 // check type of X vector
222 if (bX.is_null() == true) {
223 RCP<MultiVector> vectorWithBlockedMap = Teuchos::rcp(new BlockedMultiVector(rbA->getBlockedCrsMatrix()->getBlockedDomainMap(), rcpX));
224 rcpX.swap(vectorWithBlockedMap);
225 bCopyResultX = true;
226 bReorderX = true;
227 }
228
229 // check type of B vector
230 if (bB.is_null() == true) {
231 RCP<const MultiVector> vectorWithBlockedMap = Teuchos::rcp(new BlockedMultiVector(rbA->getBlockedCrsMatrix()->getBlockedRangeMap(), rcpB));
232 rcpB.swap(vectorWithBlockedMap);
233 bReorderB = true;
234 }
235 }
236 else {
237 // A is a BlockedCrsMatrix and uses a plain blocked map
238 if (bX.is_null() == true) {
239 RCP<MultiVector> vectorWithBlockedMap = Teuchos::rcp(new BlockedMultiVector(bA->getBlockedDomainMap(), rcpX));
240 rcpX.swap(vectorWithBlockedMap);
241 bCopyResultX = true;
242 }
243
244 if(bB.is_null() == true) {
245 RCP<const MultiVector> vectorWithBlockedMap = Teuchos::rcp(new BlockedMultiVector(bA->getBlockedRangeMap(),rcpB));
246 rcpB.swap(vectorWithBlockedMap);
247 }
248 }
249
250 // we now can guarantee that X and B are blocked multi vectors
251 bX = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(rcpX);
252 bB = Teuchos::rcp_dynamic_cast<const BlockedMultiVector>(rcpB);
253
254 // Finally we can do a reordering of the blocked multivectors if A is a ReorderedBlockedCrsMatrix
255 if(rbA.is_null() == false) {
256
257 Teuchos::RCP<const Xpetra::BlockReorderManager > brm = rbA->getBlockReorderManager();
258
259 // check type of X vector
260 if(bX->getBlockedMap()->getNumMaps() != bA->getDomainMapExtractor()->NumMaps() || bReorderX) {
261 // X is a blocked multi vector but incompatible to the reordered blocked operator A
262 Teuchos::RCP<MultiVector> reorderedBlockedVector = buildReorderedBlockedMultiVector(brm, bX);
263 rcpX.swap(reorderedBlockedVector);
264 }
265
266 if(bB->getBlockedMap()->getNumMaps() != bA->getRangeMapExtractor()->NumMaps() || bReorderB) {
267 // B is a blocked multi vector but incompatible to the reordered blocked operator A
268 Teuchos::RCP<const MultiVector> reorderedBlockedVector = buildReorderedBlockedMultiVector(brm, bB);
269 rcpB.swap(reorderedBlockedVector);
270 }
271 }
272
273 // Throughout the rest of the algorithm rcpX and rcpB are used for solution vector and RHS
274
275 RCP<MultiVector> residual = MultiVectorFactory::Build(rcpB->getMap(), rcpB->getNumVectors());
276 RCP<MultiVector> tempres = MultiVectorFactory::Build(rcpB->getMap(), rcpB->getNumVectors());
277
278 // extract parameters from internal parameter list
279 const ParameterList & pL = Factory::GetParameterList();
280 LocalOrdinal nSweeps = pL.get<LocalOrdinal>("Sweeps");
281 Scalar omega = pL.get<Scalar>("Damping factor");
282
283 // outer Richardson loop
284 for (LocalOrdinal run = 0; run < nSweeps; ++run) {
285 residual->update(1.0,*rcpB,0.0); // r = B
286 if(InitialGuessIsZero == false || run > 0){
287 bA->apply(*rcpX, *residual, Teuchos::NO_TRANS, -1.0, 1.0);
288 }
289 // one sweep of Jacobi: loop over all block rows
290 for(size_t i = 0; i<Inverse_.size(); i++) {
291
292 // extract corresponding subvectors from X and residual
293 bool bRangeThyraMode = rangeMapExtractor_->getThyraMode();
294 bool bDomainThyraMode = domainMapExtractor_->getThyraMode();
295 Teuchos::RCP<MultiVector> Xi = domainMapExtractor_->ExtractVector(rcpX, i, bDomainThyraMode);
296 Teuchos::RCP<MultiVector> ri = rangeMapExtractor_->ExtractVector(residual, i, bRangeThyraMode);
297 Teuchos::RCP<MultiVector> tXi = domainMapExtractor_->getVector(i, X.getNumVectors(), bDomainThyraMode);
298
299 // apply solver/smoother
300 Inverse_.at(i)->Apply(*tXi, *ri, false);
301
302 // update vector
303 if( InitialGuessIsZero && run == 0 ){
304 Xi->update(omega,*tXi,0.0); // X_{i+1} = X_i + omega \Delta X_i
305 } else {
306 Xi->update(omega,*tXi,1.0); // X_{i+1} = X_i + omega \Delta X_i
307 }
308
309 }
310 }
311
312 if (bCopyResultX == true) {
313 RCP<MultiVector> Xmerged = bX->Merge();
314 X.update(one, *Xmerged, zero);
315 }
316 }
317
318 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
319 RCP<MueLu::SmootherPrototype<Scalar, LocalOrdinal, GlobalOrdinal, Node> > BlockedJacobiSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Copy() const {
320 return rcp( new BlockedJacobiSmoother(*this) );
321 }
322
323 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
325 std::ostringstream out;
327 out << "{type = " << type_ << "}";
328 return out.str();
329 }
330
331 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
332 void BlockedJacobiSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::print(Teuchos::FancyOStream &out, const VerbLevel verbLevel) const {
334
335 // extract parameters from internal parameter list
336 const ParameterList & pL = Factory::GetParameterList();
337 LocalOrdinal nSweeps = pL.get<LocalOrdinal>("Sweeps");
338 Scalar omega = pL.get<Scalar>("Damping factor");
339
340 if (verbLevel & Parameters0) {
341 out0 << "Prec. type: " << type_ << " Sweeps: " << nSweeps << " damping: " << omega << std::endl;
342 }
343
344 if (verbLevel & Debug) {
345 out0 << "IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl;
346 }
347 }
348
349 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
351 // FIXME: This is a placeholder
352 return Teuchos::OrdinalTraits<size_t>::invalid();
353 }
354
355} // namespace MueLu
356
357#endif /* MUELU_BLOCKEDJACOBISMOOTHER_DEF_HPP_ */
#define MUELU_DESCRIBE
Helper macro for implementing Describable::describe() for BaseClass objects.
#define MUELU_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
MueLu::DefaultLocalOrdinal LocalOrdinal
MueLu::DefaultScalar Scalar
void Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero=false) const
Apply the direct solver.
RCP< const MapExtractorClass > domainMapExtractor_
RCP< const ParameterList > GetValidParameterList() const
Input.
void Setup(Level &currentLevel)
Setup routine In the Setup method the Inverse_ vector is filled with the corresponding SmootherBase o...
RCP< Matrix > A_
internal blocked operator "A" generated by AFact_
std::vector< Teuchos::RCP< const SmootherBase > > Inverse_
vector of smoother/solver factories
std::string description() const
Return a simple one-line description of this object.
void DeclareInput(Level &currentLevel) const
Input.
std::vector< Teuchos::RCP< const FactoryManagerBase > > FactManager_
vector of factory managers
void AddFactoryManager(RCP< const FactoryManagerBase > FactManager, int pos)
Add a factory manager.
RCP< const MapExtractorClass > rangeMapExtractor_
domain map extractor (from A_ generated by AFact)
void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel=Default) const
Print the object with some verbosity level to an FancyOStream object.
size_t getNodeSmootherComplexity() const
Get a rough estimate of cost per iteration.
std::vector< bool > bIsBlockedOperator_
vector storing whether sub-block is a blocked operator (needed for nested blocked smoothers using Thy...
virtual std::string description() const
Return a simple one-line description of this object.
Exception indicating invalid cast attempted.
Exception throws to report errors in the internal logical of the program.
Timer to be used in factories. Similar to Monitor but with additional timers.
T Get(Level &level, const std::string &varName) const
const RCP< const FactoryBase > GetFactory(const std::string &varName) const
Default implementation of FactoryAcceptor::GetFactory().
Class that holds all level-specific information.
void DeclareInput(const std::string &ename, const FactoryBase *factory, const FactoryBase *requestedBy=NoFactory::get())
Callback from FactoryBase::CallDeclareInput() and FactoryBase::DeclareInput().
T & Get(const std::string &ename, const FactoryBase *factory=NoFactory::get())
Get data without decrementing associated storage counter (i.e., read-only access)....
virtual const Teuchos::ParameterList & GetParameterList() const =0
An exception safe way to call the method 'Level::SetFactoryManager()'.
bool IsSetup() const
Get the state of a smoother prototype.
Teuchos::FancyOStream & GetOStream(MsgType type, int thisProcRankOnly=0) const
Get an output stream for outputting the input message type.
Namespace for MueLu classes and methods.
@ Warnings0
Important warning messages (one line).
@ Debug
Print additional debugging information.
@ Parameters0
Print class parameters.