MueLu Version of the Day
Loading...
Searching...
No Matches
MueLu_StratimikosSmoother_def.hpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// MueLu: A package for multigrid based preconditioning
4//
5// Copyright 2012 NTESS and the MueLu contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#ifndef MUELU_STRATIMIKOSSMOOTHER_DEF_HPP
11#define MUELU_STRATIMIKOSSMOOTHER_DEF_HPP
12
13#include "MueLu_ConfigDefs.hpp"
14
15#if defined(HAVE_MUELU_STRATIMIKOS) && defined(HAVE_MUELU_THYRA)
16
17#include <Teuchos_ParameterList.hpp>
18
19#include <Xpetra_CrsMatrix.hpp>
20#include <Xpetra_CrsMatrixWrap.hpp>
21#include <Xpetra_Matrix.hpp>
22
24#include "MueLu_Level.hpp"
25#include "MueLu_Utilities.hpp"
26#include "MueLu_Monitor.hpp"
27#ifdef MUELU_RECURMG
29#endif
30
31#include <Stratimikos_DefaultLinearSolverBuilder.hpp>
32#include "Teuchos_AbstractFactoryStd.hpp"
33#include <unordered_map>
34
35namespace MueLu {
36
37template <class LocalOrdinal, class GlobalOrdinal, class Node>
38StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::StratimikosSmoother(const std::string type, const Teuchos::ParameterList& paramList)
39 : type_(type) {
40 std::transform(type_.begin(), type_.end(), type_.begin(), ::toupper);
41 ParameterList& pList = const_cast<ParameterList&>(paramList);
42
43 if (pList.isParameter("smoother: recurMgOnFilteredA")) {
44 recurMgOnFilteredA_ = true;
45 pList.remove("smoother: recurMgOnFilteredA");
46 }
47 bool isSupported = type_ == "STRATIMIKOS";
48 this->declareConstructionOutcome(!isSupported, "Stratimikos does not provide the smoother '" + type_ + "'.");
49 if (isSupported)
50 SetParameterList(paramList);
51}
52
53template <class LocalOrdinal, class GlobalOrdinal, class Node>
54void StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::SetParameterList(const Teuchos::ParameterList& paramList) {
55 Factory::SetParameterList(paramList);
56}
57
58template <class LocalOrdinal, class GlobalOrdinal, class Node>
59void StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::DeclareInput(Level& currentLevel) const {
60 this->Input(currentLevel, "A");
61}
62
63template <class LocalOrdinal, class GlobalOrdinal, class Node>
64void StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::Setup(Level& currentLevel) {
65 FactoryMonitor m(*this, "Setup Smoother", currentLevel);
66
67 A_ = Factory::Get<RCP<Matrix> >(currentLevel, "A");
68 SetupStratimikos(currentLevel);
69 SmootherPrototype::IsSetup(true);
70 this->GetOStream(Statistics1) << description() << std::endl;
71}
72
73template <class LocalOrdinal, class GlobalOrdinal, class Node>
74void StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::SetupStratimikos(Level& currentLevel) {
75 RCP<const Thyra::LinearOpBase<Scalar> > thyraA;
76 if (recurMgOnFilteredA_) {
77 RCP<Matrix> filteredA;
78 ExperimentalDropVertConnections(filteredA, currentLevel);
79 thyraA = Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::toThyra(toCrsMatrix(filteredA));
80 } else
81 thyraA = Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::toThyra(toCrsMatrix(A_));
82
83 // Build Stratimikos solver
84 Stratimikos::DefaultLinearSolverBuilder linearSolverBuilder;
85 if (recurMgOnFilteredA_) {
86#ifdef MUELU_RECURMG
87 Stratimikos::enableMueLu<LocalOrdinal, GlobalOrdinal, Node>(linearSolverBuilder);
88#else
89 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.");
90#endif
91 }
92
93 linearSolverBuilder.setParameterList(rcpFromRef(const_cast<ParameterList&>(this->GetParameterList())));
94
95 // Build a new "solver factory" according to the previously specified parameter list.
96 RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> > solverFactory = Thyra::createLinearSolveStrategy(linearSolverBuilder);
97 solver_ = Thyra::linearOpWithSolve(*solverFactory, thyraA);
98#ifdef dumpOutRecurMGDebug
99 char mystring[120];
100 sprintf(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());
101 fflush(stdout);
102 mystring[50] = '`';
103 mystring[65] = '"';
104 mystring[76] = '"';
105 mystring[77] = '`';
106 system(mystring);
107#endif
108}
109
110template <class LocalOrdinal, class GlobalOrdinal, class Node>
111void StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::ExperimentalDropVertConnections(RCP<Matrix>& filteredA, Level& currentLevel) {
112 // strip out the veritcal connections.
113 //
114 // There is some code, which is currently turned off (via sumDropped). That
115 // makes things more complicated. I want to keep it for now, and so it is
116 // explained here. The basic idea is to try and maintain the character
117 // of the horizontal stencil by summing dropped entries appropriately.
118 // However, this does not correspond to plane relexation, and so I am
119 // not sure it is really justified. Anyway, the picture below
120 // gives a situation with a 15-pt stencil
121 //
122 // a
123 // /
124 // /
125 // b ----c----- d
126 // /
127 // /
128 // e
129 // f dropped a & l summed into f
130 // / dropped b & m summed into g
131 // / dropped c & n summed into i
132 // g ----i----- j dropped d & o summed into j
133 // / dropped e & p summed into k
134 // /
135 // k
136 // l
137 // /
138 // /
139 // m ----n----- o
140 // /
141 // /
142 // p
143 // To do this, we use umap to record locations within the middle layer associated
144 // with each line ID (e.g. g corresponds to the 13th line). Then, when dropping (in a 2nd pass) we
145 // use lineId and umap to find where dropped entries should be summed (e.g., b corresponds to the
146 // 13th line and umap[13] points at location for g).
147 // using TST = typename Teuchos::ScalarTraits<SC>;
148
149 bool sumDropped = false;
150
151 LO dofsPerNode = A_->GetFixedBlockSize();
152
153 RCP<ParameterList> fillCompleteParams(new ParameterList); // This code taken from Build method
154 fillCompleteParams->set("No Nonlocal Changes", true); // within MueLu_FilteredAFactory_def
155 filteredA = MatrixFactory::Build(A_->getCrsGraph());
156 filteredA->resumeFill();
157
158 ArrayView<const LocalOrdinal> inds;
159 ArrayView<const Scalar> valsA;
160 ArrayView<Scalar> vals;
161 Teuchos::ArrayRCP<LocalOrdinal> TVertLineId = Factory::Get<Teuchos::ArrayRCP<LocalOrdinal> >(currentLevel, "LineDetection_VertLineIds");
162 Teuchos::ArrayRCP<LocalOrdinal> TLayerId = Factory::Get<Teuchos::ArrayRCP<LocalOrdinal> >(currentLevel, "LineDetection_Layers");
163 LocalOrdinal* VertLineId = TVertLineId.getRawPtr();
164 LocalOrdinal* LayerId = TLayerId.getRawPtr();
165 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.");
166
167 Scalar ZERO = Teuchos::ScalarTraits<Scalar>::zero();
168 for (size_t i = 0; i < A_->getRowMap()->getLocalNumElements(); i++) {
169 A_->getLocalRowView(i, inds, valsA);
170 size_t nnz = inds.size();
171 ArrayView<const Scalar> vals1;
172 filteredA->getLocalRowView(i, inds, vals1);
173 vals = ArrayView<Scalar>(const_cast<Scalar*>(vals1.getRawPtr()), nnz);
174 memcpy(vals.getRawPtr(), valsA.getRawPtr(), nnz * sizeof(Scalar));
175 size_t inode, jdof, jnode, jdof_offset;
176 inode = i / dofsPerNode;
177
178 std::unordered_map<LocalOrdinal, LocalOrdinal> umap; // umap[k] indicates where the dropped entry
179 // corresponding to kth line should be added
180 // within the row. See comments above.
181 if (sumDropped) {
182 for (size_t j = 0; j < nnz; j++) {
183 jdof = inds[j];
184 jnode = jdof / dofsPerNode;
185 jdof_offset = jdof - jnode * dofsPerNode;
186 if (LayerId[jnode] == LayerId[inode]) umap[dofsPerNode * VertLineId[jnode] + jdof_offset] = j;
187 }
188 }
189
190 // drop non-middle layer entries. When sumDropped=true, sum dropped entries to corresponding mid-layer entry
191 for (size_t j = 0; j < nnz; j++) {
192 jdof = inds[j];
193 jnode = jdof / dofsPerNode;
194 jdof_offset = jdof - jnode * dofsPerNode;
195 if (LayerId[jnode] != LayerId[inode]) {
196 if (sumDropped) {
197 if (umap.find(dofsPerNode * VertLineId[jnode + jdof_offset]) != umap.end())
198 vals[umap[dofsPerNode * VertLineId[jnode + jdof_offset]]] += vals[j];
199 }
200 vals[j] = ZERO;
201 }
202 }
203 }
204 filteredA->fillComplete(fillCompleteParams);
205}
206
207template <class LocalOrdinal, class GlobalOrdinal, class Node>
208void StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::Apply(MultiVector& X, const MultiVector& B, bool InitialGuessIsZero) const {
209 TEUCHOS_TEST_FOR_EXCEPTION(SmootherPrototype::IsSetup() == false, Exceptions::RuntimeError, "MueLu::StratimikosSmoother::Apply(): Setup() has not been called");
210
211 // Apply
212 if (InitialGuessIsZero) {
213 X.putScalar(0.0);
214 RCP<Thyra::MultiVectorBase<Scalar> > thyraX = Teuchos::rcp_const_cast<Thyra::MultiVectorBase<Scalar> >(Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::toThyraMultiVector(rcpFromRef(X)));
215 RCP<const Thyra::MultiVectorBase<Scalar> > thyraB = Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::toThyraMultiVector(rcpFromRef(B));
216 Thyra::SolveStatus<Scalar> status = Thyra::solve<Scalar>(*solver_, Thyra::NOTRANS, *thyraB, thyraX.ptr());
217 RCP<MultiVector> thyXpX = Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::toXpetra(thyraX, X.getMap()->getComm());
218 X = *thyXpX;
219
220 } else {
221 typedef Teuchos::ScalarTraits<Scalar> TST;
222 RCP<MultiVector> Residual = Utilities::Residual(*A_, X, B);
223
224 RCP<MultiVector> Cor = Xpetra::MultiVectorFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Build(X.getMap(), X.getNumVectors(), true);
225 RCP<Thyra::MultiVectorBase<Scalar> > thyraCor = Teuchos::rcp_const_cast<Thyra::MultiVectorBase<Scalar> >(Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::toThyraMultiVector(Cor));
226 RCP<const Thyra::MultiVectorBase<Scalar> > thyraRes = Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::toThyraMultiVector(Residual);
227 Thyra::SolveStatus<Scalar> status = Thyra::solve<Scalar>(*solver_, Thyra::NOTRANS, *thyraRes, thyraCor.ptr());
228 RCP<MultiVector> thyXpCor = Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::toXpetra(thyraCor, X.getMap()->getComm());
229 X.update(TST::one(), *thyXpCor, TST::one());
230 }
231}
232
233template <class LocalOrdinal, class GlobalOrdinal, class Node>
234RCP<MueLu::SmootherPrototype<double, LocalOrdinal, GlobalOrdinal, Node> > StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::Copy() const {
235 RCP<StratimikosSmoother> smoother = rcp(new StratimikosSmoother(*this));
236 smoother->SetParameterList(this->GetParameterList());
237 return smoother;
238}
239
240template <class LocalOrdinal, class GlobalOrdinal, class Node>
241std::string StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::description() const {
242 std::ostringstream out;
243 if (SmootherPrototype::IsSetup()) {
244 out << solver_->description();
245 } else {
246 out << "STRATIMIKOS {type = " << type_ << "}";
247 }
248 return out.str();
249}
250
251template <class LocalOrdinal, class GlobalOrdinal, class Node>
252void StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::print(Teuchos::FancyOStream& out, const VerbLevel verbLevel) const {
254
255 if (verbLevel & Parameters1) {
256 out0 << "Parameter list: " << std::endl;
257 Teuchos::OSTab tab2(out);
258 out << this->GetParameterList();
259 }
260
261 if (verbLevel & External)
262 if (solver_ != Teuchos::null) {
263 Teuchos::OSTab tab2(out);
264 out << *solver_ << std::endl;
265 }
266
267 if (verbLevel & Debug) {
268 out0 << "IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl
269 << "-" << std::endl
270 << "RCP<solver_>: " << solver_ << std::endl;
271 }
272}
273
274template <class LocalOrdinal, class GlobalOrdinal, class Node>
275size_t StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::getNodeSmootherComplexity() const {
276 return Teuchos::OrdinalTraits<size_t>::invalid();
277}
278
279} // namespace MueLu
280
281#endif // HAVE_MUELU_STRATIMIKOS
282#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.