10#ifndef MUELU_STRATIMIKOSSMOOTHER_DEF_HPP
11#define MUELU_STRATIMIKOSSMOOTHER_DEF_HPP
15#if defined(HAVE_MUELU_STRATIMIKOS) && defined(HAVE_MUELU_THYRA)
17#include <Teuchos_ParameterList.hpp>
19#include <Xpetra_CrsMatrix.hpp>
20#include <Xpetra_CrsMatrixWrap.hpp>
21#include <Xpetra_Matrix.hpp>
25#include "MueLu_Utilities.hpp"
31#include <Stratimikos_DefaultLinearSolverBuilder.hpp>
32#include "Teuchos_AbstractFactoryStd.hpp"
33#include <Teuchos_ParameterList.hpp>
34#include <unordered_map>
38template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
39StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::StratimikosSmoother(
const std::string type,
const Teuchos::ParameterList& paramList)
41 std::transform(type_.begin(), type_.end(), type_.begin(), ::toupper);
42 ParameterList& pList =
const_cast<ParameterList&
>(paramList);
44 if (pList.isParameter(
"smoother: recurMgOnFilteredA")) {
45 recurMgOnFilteredA_ =
true;
46 pList.remove(
"smoother: recurMgOnFilteredA");
48 bool isSupported = type_ ==
"STRATIMIKOS";
49 this->declareConstructionOutcome(!isSupported,
"Stratimikos does not provide the smoother '" + type_ +
"'.");
51 SetParameterList(paramList);
54template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
55void StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::SetParameterList(
const Teuchos::ParameterList& paramList) {
56 Factory::SetParameterList(paramList);
59template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
60void StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::DeclareInput(Level& currentLevel)
const {
61 this->Input(currentLevel,
"A");
64template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
65void StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::Setup(Level& currentLevel) {
66 FactoryMonitor m(*
this,
"Setup Smoother", currentLevel);
68 A_ = Factory::Get<RCP<Matrix> >(currentLevel,
"A");
69 SetupStratimikos(currentLevel);
70 SmootherPrototype::IsSetup(
true);
71 this->GetOStream(Statistics1) << description() << std::endl;
74template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
75void StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::SetupStratimikos(Level& currentLevel) {
76 RCP<const Thyra::LinearOpBase<Scalar> > thyraA;
77 if (recurMgOnFilteredA_) {
78 RCP<Matrix> filteredA;
79 ExperimentalDropVertConnections(filteredA, currentLevel);
80 thyraA = Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::toThyra(toCrsMatrix(filteredA));
82 thyraA = Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::toThyra(toCrsMatrix(A_));
85 Stratimikos::DefaultLinearSolverBuilder linearSolverBuilder;
86 if (recurMgOnFilteredA_) {
88 Stratimikos::enableMueLu<LocalOrdinal, GlobalOrdinal, Node>(linearSolverBuilder);
90 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.");
94 linearSolverBuilder.setParameterList(rcpFromRef(
const_cast<ParameterList&
>(this->GetParameterList())));
97 RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> > solverFactory = Thyra::createLinearSolveStrategy(linearSolverBuilder);
98 solver_ = Thyra::linearOpWithSolve(*solverFactory, thyraA);
99#ifdef dumpOutRecurMGDebug
101 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());
111template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
112void StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::ExperimentalDropVertConnections(RCP<Matrix>& filteredA, Level& currentLevel) {
150 bool sumDropped =
false;
152 LO dofsPerNode = A_->GetFixedBlockSize();
154 RCP<ParameterList> fillCompleteParams(
new ParameterList);
155 fillCompleteParams->set(
"No Nonlocal Changes",
true);
156 filteredA = MatrixFactory::Build(A_->getCrsGraph());
157 filteredA->resumeFill();
159 ArrayView<const LocalOrdinal> inds;
160 ArrayView<const Scalar> valsA;
161 ArrayView<Scalar> vals;
162 Teuchos::ArrayRCP<LocalOrdinal> TVertLineId = Factory::Get<Teuchos::ArrayRCP<LocalOrdinal> >(currentLevel,
"LineDetection_VertLineIds");
163 Teuchos::ArrayRCP<LocalOrdinal> TLayerId = Factory::Get<Teuchos::ArrayRCP<LocalOrdinal> >(currentLevel,
"LineDetection_Layers");
166 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.");
168 Scalar ZERO = Teuchos::ScalarTraits<Scalar>::zero();
169 for (
size_t i = 0; i < A_->getRowMap()->getLocalNumElements(); i++) {
170 A_->getLocalRowView(i, inds, valsA);
171 size_t nnz = inds.size();
172 ArrayView<const Scalar> vals1;
173 filteredA->getLocalRowView(i, inds, vals1);
174 vals = ArrayView<Scalar>(
const_cast<Scalar*
>(vals1.getRawPtr()), nnz);
175 memcpy(vals.getRawPtr(), valsA.getRawPtr(), nnz *
sizeof(
Scalar));
176 size_t inode, jdof, jnode, jdof_offset;
177 inode = i / dofsPerNode;
179 std::unordered_map<LocalOrdinal, LocalOrdinal> umap;
183 for (
size_t j = 0; j < nnz; j++) {
185 jnode = jdof / dofsPerNode;
186 jdof_offset = jdof - jnode * dofsPerNode;
187 if (LayerId[jnode] == LayerId[inode]) umap[dofsPerNode * VertLineId[jnode] + jdof_offset] = j;
192 for (
size_t j = 0; j < nnz; j++) {
194 jnode = jdof / dofsPerNode;
195 jdof_offset = jdof - jnode * dofsPerNode;
196 if (LayerId[jnode] != LayerId[inode]) {
198 if (umap.find(dofsPerNode * VertLineId[jnode + jdof_offset]) != umap.end())
199 vals[umap[dofsPerNode * VertLineId[jnode + jdof_offset]]] += vals[j];
205 filteredA->fillComplete(fillCompleteParams);
208template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
209void StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::Apply(MultiVector& X,
const MultiVector& B,
bool InitialGuessIsZero)
const {
210 TEUCHOS_TEST_FOR_EXCEPTION(SmootherPrototype::IsSetup() ==
false, Exceptions::RuntimeError,
"MueLu::StratimikosSmoother::Apply(): Setup() has not been called");
213 if (InitialGuessIsZero) {
215 RCP<Thyra::MultiVectorBase<Scalar> > thyraX = Teuchos::rcp_const_cast<Thyra::MultiVectorBase<Scalar> >(Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::toThyraMultiVector(rcpFromRef(X)));
216 RCP<const Thyra::MultiVectorBase<Scalar> > thyraB = Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::toThyraMultiVector(rcpFromRef(B));
217 Thyra::SolveStatus<Scalar> status = Thyra::solve<Scalar>(*solver_, Thyra::NOTRANS, *thyraB, thyraX.ptr());
218 RCP<MultiVector> thyXpX = Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::toXpetra(thyraX, X.getMap()->getComm());
222 typedef Teuchos::ScalarTraits<Scalar> TST;
223 RCP<MultiVector> Residual = Utilities::Residual(*A_, X, B);
225 RCP<MultiVector> Cor = Xpetra::MultiVectorFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Build(X.getMap(), X.getNumVectors(),
true);
226 RCP<Thyra::MultiVectorBase<Scalar> > thyraCor = Teuchos::rcp_const_cast<Thyra::MultiVectorBase<Scalar> >(Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::toThyraMultiVector(Cor));
227 RCP<const Thyra::MultiVectorBase<Scalar> > thyraRes = Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::toThyraMultiVector(Residual);
228 Thyra::SolveStatus<Scalar> status = Thyra::solve<Scalar>(*solver_, Thyra::NOTRANS, *thyraRes, thyraCor.ptr());
229 RCP<MultiVector> thyXpCor = Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::toXpetra(thyraCor, X.getMap()->getComm());
230 X.update(TST::one(), *thyXpCor, TST::one());
234template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
235RCP<MueLu::SmootherPrototype<double, LocalOrdinal, GlobalOrdinal, Node> > StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::Copy()
const {
236 RCP<StratimikosSmoother> smoother = rcp(
new StratimikosSmoother(*
this));
237 smoother->SetParameterList(this->GetParameterList());
241template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
242std::string StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::description()
const {
243 std::ostringstream out;
244 if (SmootherPrototype::IsSetup()) {
245 out << solver_->description();
247 out <<
"STRATIMIKOS {type = " << type_ <<
"}";
252template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
253void StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::print(Teuchos::FancyOStream& out,
const VerbLevel verbLevel)
const {
256 if (verbLevel & Parameters1) {
257 out0 <<
"Parameter list: " << std::endl;
258 Teuchos::OSTab tab2(out);
259 out << this->GetParameterList();
262 if (verbLevel & External)
263 if (solver_ != Teuchos::null) {
264 Teuchos::OSTab tab2(out);
265 out << *solver_ << std::endl;
268 if (verbLevel & Debug) {
269 out0 <<
"IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl
271 <<
"RCP<solver_>: " << solver_ << std::endl;
275template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
276size_t StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::getNodeSmootherComplexity()
const {
277 return Teuchos::OrdinalTraits<size_t>::invalid();
#define MUELU_DESCRIBE
Helper macro for implementing Describable::describe() for BaseClass objects.
MueLu::DefaultLocalOrdinal LocalOrdinal
MueLu::DefaultScalar Scalar
Namespace for MueLu classes and methods.