MueLu Version of the Day
Loading...
Searching...
No Matches
MueLu_StratimikosSmoother_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_STRATIMIKOSSMOOTHER_DEF_HPP
47#define MUELU_STRATIMIKOSSMOOTHER_DEF_HPP
48
49#include "MueLu_ConfigDefs.hpp"
50
51#if defined(HAVE_MUELU_STRATIMIKOS) && defined(HAVE_MUELU_THYRA)
52
53#include <Teuchos_ParameterList.hpp>
54
55#include <Xpetra_CrsMatrix.hpp>
56#include <Xpetra_CrsMatrixWrap.hpp>
57#include <Xpetra_Matrix.hpp>
58
60#include "MueLu_Level.hpp"
61#include "MueLu_Utilities.hpp"
62#include "MueLu_Monitor.hpp"
63#ifdef MUELU_RECURMG
65#endif
66
67#include <Stratimikos_DefaultLinearSolverBuilder.hpp>
68#include "Teuchos_AbstractFactoryStd.hpp"
69#include <Teuchos_ParameterList.hpp>
70#include <unordered_map>
71
72
73namespace MueLu {
74
75 template <class LocalOrdinal, class GlobalOrdinal, class Node>
76 StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::StratimikosSmoother(const std::string type, const Teuchos::ParameterList& paramList)
77 : type_(type)
78 {
79 std::transform(type_.begin(), type_.end(), type_.begin(), ::toupper);
80 ParameterList& pList = const_cast<ParameterList&>(paramList);
81
82 if (pList.isParameter("smoother: recurMgOnFilteredA")) {
83 recurMgOnFilteredA_ = true;
84 pList.remove("smoother: recurMgOnFilteredA");
85 }
86 bool isSupported = type_ == "STRATIMIKOS";
87 this->declareConstructionOutcome(!isSupported, "Stratimikos does not provide the smoother '" + type_ + "'.");
88 if (isSupported)
89 SetParameterList(paramList);
90 }
91
92 template <class LocalOrdinal, class GlobalOrdinal, class Node>
93 void StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::SetParameterList(const Teuchos::ParameterList& paramList) {
94 Factory::SetParameterList(paramList);
95 }
96
97 template <class LocalOrdinal, class GlobalOrdinal, class Node>
98 void StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::DeclareInput(Level& currentLevel) const {
99 this->Input(currentLevel, "A");
100 }
101
102 template <class LocalOrdinal, class GlobalOrdinal, class Node>
103 void StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::Setup(Level& currentLevel) {
104 FactoryMonitor m(*this, "Setup Smoother", currentLevel);
105
106 A_ = Factory::Get< RCP<Matrix> >(currentLevel, "A");
107 SetupStratimikos(currentLevel);
108 SmootherPrototype::IsSetup(true);
109 this->GetOStream(Statistics1) << description() << std::endl;
110 }
111
112 template <class LocalOrdinal, class GlobalOrdinal, class Node>
113 void StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::SetupStratimikos(Level& currentLevel) {
114
115 RCP<const Thyra::LinearOpBase<Scalar> > thyraA;
116 if (recurMgOnFilteredA_) {
117 RCP<Matrix> filteredA;
118 ExperimentalDropVertConnections(filteredA, currentLevel);
119 thyraA = Xpetra::ThyraUtils<Scalar,LocalOrdinal,GlobalOrdinal,Node>::toThyra(Teuchos::rcp_dynamic_cast<CrsMatrixWrap>(filteredA)->getCrsMatrix());
120 }
121 else thyraA = Xpetra::ThyraUtils<Scalar,LocalOrdinal,GlobalOrdinal,Node>::toThyra(Teuchos::rcp_dynamic_cast<CrsMatrixWrap>(A_)->getCrsMatrix());
122
123 // Build Stratimikos solver
124 Stratimikos::DefaultLinearSolverBuilder linearSolverBuilder;
125 if (recurMgOnFilteredA_) {
126#ifdef MUELU_RECURMG
128#else
129 TEUCHOS_TEST_FOR_EXCEPTION( true , Exceptions::RuntimeError, "MueLu::StratimikosSmoother:: must compile with MUELU_RECURMG defined. Unfortunately, cmake does not always produce a proper link.txt file (which sometimes requires libmuelu.a before and after libmuelu-interface.a). After configuring, run script muelu/utils/misc/patchLinkForRecurMG to change link.txt files manually. If you want to create test example, add -DMUELU_RECURMG=ON to cmake arguments.");
130#endif
131 }
132
133 linearSolverBuilder.setParameterList(rcpFromRef(const_cast<ParameterList&>(this->GetParameterList())));
134
135
136 // Build a new "solver factory" according to the previously specified parameter list.
137 RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> > solverFactory = Thyra::createLinearSolveStrategy(linearSolverBuilder);
138 solver_ = Thyra::linearOpWithSolve(*solverFactory, thyraA);
139#ifdef dumpOutRecurMGDebug
140char mystring[120];
141sprintf(mystring,"for i in A_[0123456789].m P_[0123456789].m; do T=Xecho $i | sed Xs/.m$/%d.m/XX; mv $i $T; done", (int) currentLevel.GetLevelID()); fflush(stdout);
142mystring[50]='`'; mystring[65]='"'; mystring[76]='"'; mystring[77]='`';
143system(mystring);
144#endif
145 }
146
147 template <class LocalOrdinal, class GlobalOrdinal, class Node>
148 void StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::ExperimentalDropVertConnections(RCP<Matrix> & filteredA, Level& currentLevel) {
149
150 // strip out the veritcal connections.
151 //
152 // There is some code, which is currently turned off (via sumDropped). That
153 // makes things more complicated. I want to keep it for now, and so it is
154 // explained here. The basic idea is to try and maintain the character
155 // of the horizontal stencil by summing dropped entries appropriately.
156 // However, this does not correspond to plane relexation, and so I am
157 // not sure it is really justified. Anyway, the picture below
158 // gives a situation with a 15-pt stencil
159 //
160 // a
161 // /
162 // /
163 // b ----c----- d
164 // /
165 // /
166 // e
167 // f dropped a & l summed into f
168 // / dropped b & m summed into g
169 // / dropped c & n summed into i
170 // g ----i----- j dropped d & o summed into j
171 // / dropped e & p summed into k
172 // /
173 // k
174 // l
175 // /
176 // /
177 // m ----n----- o
178 // /
179 // /
180 // p
181 // To do this, we use umap to record locations within the middle layer associated
182 // with each line ID (e.g. g corresponds to the 13th line). Then, when dropping (in a 2nd pass) we
183 // use lineId and umap to find where dropped entries should be summed (e.g., b corresponds to the
184 // 13th line and umap[13] points at location for g).
185 // using TST = typename Teuchos::ScalarTraits<SC>;
186
187 bool sumDropped = false;
188
189 LO dofsPerNode = A_->GetFixedBlockSize();
190
191
192 RCP<ParameterList> fillCompleteParams(new ParameterList); // This code taken from Build method
193 fillCompleteParams->set("No Nonlocal Changes", true); // within MueLu_FilteredAFactory_def
194 filteredA = MatrixFactory::Build(A_->getCrsGraph());
195 filteredA->resumeFill();
196
197 ArrayView<const LocalOrdinal> inds;
198 ArrayView<const Scalar> valsA;
199 ArrayView<Scalar> vals;
200 Teuchos::ArrayRCP<LocalOrdinal> TVertLineId = Factory::Get< Teuchos::ArrayRCP<LocalOrdinal> > (currentLevel, "LineDetection_VertLineIds");
201 Teuchos::ArrayRCP<LocalOrdinal> TLayerId = Factory::Get< Teuchos::ArrayRCP<LocalOrdinal> > (currentLevel, "LineDetection_Layers");
202 LocalOrdinal* VertLineId = TVertLineId.getRawPtr();
203 LocalOrdinal* LayerId = TLayerId.getRawPtr();
204 TEUCHOS_TEST_FOR_EXCEPTION( (LayerId == NULL) || (VertLineId == NULL), Exceptions::RuntimeError, "MueLu::StratimikosSmoother:: no line information found on this level. Cannot use recurMgOnFilteredA on this level.");
205
206 Scalar ZERO = Teuchos::ScalarTraits<Scalar>::zero();
207 for (size_t i = 0; i < A_->getRowMap()->getLocalNumElements(); i++) {
208 A_->getLocalRowView(i, inds, valsA);
209 size_t nnz = inds.size();
210 ArrayView<const Scalar> vals1;
211 filteredA->getLocalRowView(i, inds, vals1);
212 vals = ArrayView<Scalar>(const_cast<Scalar*>(vals1.getRawPtr()), nnz);
213 memcpy(vals.getRawPtr(), valsA.getRawPtr(), nnz*sizeof(Scalar));
214 size_t inode, jdof, jnode, jdof_offset;
215 inode = i/dofsPerNode;
216
217 std::unordered_map<LocalOrdinal, LocalOrdinal> umap; // umap[k] indicates where the dropped entry
218 // corresponding to kth line should be added
219 // within the row. See comments above.
220 if (sumDropped) {
221 for (size_t j = 0; j < nnz; j++) {
222 jdof = inds[j];
223 jnode = jdof/dofsPerNode;
224 jdof_offset = jdof - jnode*dofsPerNode;
225 if ( LayerId[jnode] == LayerId[inode] ) umap[dofsPerNode*VertLineId[jnode]+jdof_offset] = j;
226 }
227 }
228
229 // drop non-middle layer entries. When sumDropped=true, sum dropped entries to corresponding mid-layer entry
230 for (size_t j = 0; j < nnz; j++) {
231 jdof = inds[j];
232 jnode = jdof/dofsPerNode;
233 jdof_offset = jdof - jnode*dofsPerNode;
234 if ( LayerId[jnode] != LayerId[inode] ) {
235 if (sumDropped) {
236 if (umap.find(dofsPerNode*VertLineId[jnode + jdof_offset]) != umap.end())
237 vals[umap[dofsPerNode*VertLineId[jnode + jdof_offset]]] += vals[j];
238 }
239 vals[j] = ZERO;
240 }
241 }
242 }
243 filteredA->fillComplete(fillCompleteParams);
244
245 }
246
247 template <class LocalOrdinal, class GlobalOrdinal, class Node>
248 void StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::Apply(MultiVector& X, const MultiVector& B, bool InitialGuessIsZero) const {
249 TEUCHOS_TEST_FOR_EXCEPTION(SmootherPrototype::IsSetup() == false, Exceptions::RuntimeError, "MueLu::StratimikosSmoother::Apply(): Setup() has not been called");
250
251 // Apply
252 if (InitialGuessIsZero) {
253 X.putScalar(0.0);
254 RCP< Thyra::MultiVectorBase<Scalar> > thyraX = Teuchos::rcp_const_cast<Thyra::MultiVectorBase<Scalar> >(Xpetra::ThyraUtils<Scalar,LocalOrdinal,GlobalOrdinal,Node>::toThyraMultiVector(rcpFromRef(X)));
255 RCP<const Thyra::MultiVectorBase<Scalar> > thyraB = Xpetra::ThyraUtils<Scalar,LocalOrdinal,GlobalOrdinal,Node>::toThyraMultiVector(rcpFromRef(B));
256 Thyra::SolveStatus<Scalar> status = Thyra::solve<Scalar>(*solver_, Thyra::NOTRANS, *thyraB, thyraX.ptr());
257 RCP<MultiVector> thyXpX = Xpetra::ThyraUtils<Scalar,LocalOrdinal,GlobalOrdinal,Node>::toXpetra(thyraX, X.getMap()->getComm());
258 X = *thyXpX;
259
260 } else {
261 typedef Teuchos::ScalarTraits<Scalar> TST;
262 RCP<MultiVector> Residual = Utilities::Residual(*A_, X, B);
263
264 RCP<MultiVector> Cor = Xpetra::MultiVectorFactory<Scalar,LocalOrdinal,GlobalOrdinal,Node>::Build(X.getMap(),X.getNumVectors(), true);
265 RCP< Thyra::MultiVectorBase<Scalar> > thyraCor = Teuchos::rcp_const_cast<Thyra::MultiVectorBase<Scalar> >(Xpetra::ThyraUtils<Scalar,LocalOrdinal,GlobalOrdinal,Node>::toThyraMultiVector(Cor));
266 RCP<const Thyra::MultiVectorBase<Scalar> > thyraRes = Xpetra::ThyraUtils<Scalar,LocalOrdinal,GlobalOrdinal,Node>::toThyraMultiVector(Residual);
267 Thyra::SolveStatus<Scalar> status = Thyra::solve<Scalar>(*solver_, Thyra::NOTRANS, *thyraRes, thyraCor.ptr());
268 RCP<MultiVector> thyXpCor = Xpetra::ThyraUtils<Scalar,LocalOrdinal,GlobalOrdinal,Node>::toXpetra(thyraCor, X.getMap()->getComm());
269 X.update(TST::one(), *thyXpCor, TST::one());
270 }
271
272 }
273
274
275 template <class LocalOrdinal, class GlobalOrdinal, class Node>
276 RCP<MueLu::SmootherPrototype<double, LocalOrdinal, GlobalOrdinal, Node> > StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::Copy() const {
277 RCP<StratimikosSmoother> smoother = rcp(new StratimikosSmoother(*this) );
278 smoother->SetParameterList(this->GetParameterList());
279 return smoother;
280 }
281
282 template <class LocalOrdinal, class GlobalOrdinal, class Node>
283 std::string StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::description() const {
284 std::ostringstream out;
285 if (SmootherPrototype::IsSetup()) {
286 out << solver_->description();
287 } else {
288 out << "STRATIMIKOS {type = " << type_ << "}";
289 }
290 return out.str();
291 }
292
293 template <class LocalOrdinal, class GlobalOrdinal, class Node>
294 void StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::print(Teuchos::FancyOStream &out, const VerbLevel verbLevel) const {
296
297 if (verbLevel & Parameters1) {
298 out0 << "Parameter list: " << std::endl;
299 Teuchos::OSTab tab2(out);
300 out << this->GetParameterList();
301 }
302
303 if (verbLevel & External)
304 if (solver_ != Teuchos::null) {
305 Teuchos::OSTab tab2(out);
306 out << *solver_ << std::endl;
307 }
308
309 if (verbLevel & Debug) {
310 out0 << "IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl
311 << "-" << std::endl
312 << "RCP<solver_>: " << solver_ << std::endl;
313 }
314 }
315
316 template <class LocalOrdinal, class GlobalOrdinal, class Node>
317 size_t StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::getNodeSmootherComplexity() const {
318 return Teuchos::OrdinalTraits<size_t>::invalid();
319 }
320
321
322} // namespace MueLu
323
324#endif // HAVE_MUELU_STRATIMIKOS
325#endif // MUELU_STRATIMIKOSSMOOTHER_DEF_HPP
#define MUELU_DESCRIBE
Helper macro for implementing Describable::describe() for BaseClass objects.
MueLu::DefaultLocalOrdinal LocalOrdinal
MueLu::DefaultScalar Scalar
Namespace for MueLu classes and methods.
void enableMueLu(LinearSolverBuilder< Scalar > &builder, const std::string &stratName="MueLu")