46#ifndef MUELU_STRATIMIKOSSMOOTHER_DEF_HPP
47#define MUELU_STRATIMIKOSSMOOTHER_DEF_HPP
51#if defined(HAVE_MUELU_STRATIMIKOS) && defined(HAVE_MUELU_THYRA)
53#include <Teuchos_ParameterList.hpp>
55#include <Xpetra_CrsMatrix.hpp>
56#include <Xpetra_CrsMatrixWrap.hpp>
57#include <Xpetra_Matrix.hpp>
61#include "MueLu_Utilities.hpp"
67#include <Stratimikos_DefaultLinearSolverBuilder.hpp>
68#include "Teuchos_AbstractFactoryStd.hpp"
69#include <Teuchos_ParameterList.hpp>
70#include <unordered_map>
75 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
76 StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::StratimikosSmoother(
const std::string type,
const Teuchos::ParameterList& paramList)
79 std::transform(type_.begin(), type_.end(), type_.begin(), ::toupper);
80 ParameterList& pList =
const_cast<ParameterList&
>(paramList);
82 if (pList.isParameter(
"smoother: recurMgOnFilteredA")) {
83 recurMgOnFilteredA_ =
true;
84 pList.remove(
"smoother: recurMgOnFilteredA");
86 bool isSupported = type_ ==
"STRATIMIKOS";
87 this->declareConstructionOutcome(!isSupported,
"Stratimikos does not provide the smoother '" + type_ +
"'.");
89 SetParameterList(paramList);
92 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
93 void StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::SetParameterList(
const Teuchos::ParameterList& paramList) {
94 Factory::SetParameterList(paramList);
97 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
98 void StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::DeclareInput(Level& currentLevel)
const {
99 this->Input(currentLevel,
"A");
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);
106 A_ = Factory::Get< RCP<Matrix> >(currentLevel,
"A");
107 SetupStratimikos(currentLevel);
108 SmootherPrototype::IsSetup(
true);
109 this->GetOStream(Statistics1) << description() << std::endl;
112 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
113 void StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::SetupStratimikos(Level& currentLevel) {
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());
121 else thyraA = Xpetra::ThyraUtils<Scalar,LocalOrdinal,GlobalOrdinal,Node>::toThyra(Teuchos::rcp_dynamic_cast<CrsMatrixWrap>(A_)->getCrsMatrix());
124 Stratimikos::DefaultLinearSolverBuilder linearSolverBuilder;
125 if (recurMgOnFilteredA_) {
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.");
133 linearSolverBuilder.setParameterList(rcpFromRef(
const_cast<ParameterList&
>(this->GetParameterList())));
137 RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> > solverFactory = Thyra::createLinearSolveStrategy(linearSolverBuilder);
138 solver_ = Thyra::linearOpWithSolve(*solverFactory, thyraA);
139#ifdef dumpOutRecurMGDebug
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]=
'`';
147 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
148 void StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::ExperimentalDropVertConnections(RCP<Matrix> & filteredA, Level& currentLevel) {
187 bool sumDropped =
false;
189 LO dofsPerNode = A_->GetFixedBlockSize();
192 RCP<ParameterList> fillCompleteParams(
new ParameterList);
193 fillCompleteParams->set(
"No Nonlocal Changes",
true);
194 filteredA = MatrixFactory::Build(A_->getCrsGraph());
195 filteredA->resumeFill();
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");
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.");
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;
217 std::unordered_map<LocalOrdinal, LocalOrdinal> umap;
221 for (
size_t j = 0; j < nnz; j++) {
223 jnode = jdof/dofsPerNode;
224 jdof_offset = jdof - jnode*dofsPerNode;
225 if ( LayerId[jnode] == LayerId[inode] ) umap[dofsPerNode*VertLineId[jnode]+jdof_offset] = j;
230 for (
size_t j = 0; j < nnz; j++) {
232 jnode = jdof/dofsPerNode;
233 jdof_offset = jdof - jnode*dofsPerNode;
234 if ( LayerId[jnode] != LayerId[inode] ) {
236 if (umap.find(dofsPerNode*VertLineId[jnode + jdof_offset]) != umap.end())
237 vals[umap[dofsPerNode*VertLineId[jnode + jdof_offset]]] += vals[j];
243 filteredA->fillComplete(fillCompleteParams);
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");
252 if (InitialGuessIsZero) {
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());
261 typedef Teuchos::ScalarTraits<Scalar> TST;
262 RCP<MultiVector> Residual = Utilities::Residual(*A_, X, B);
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());
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());
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();
288 out <<
"STRATIMIKOS {type = " << type_ <<
"}";
293 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
294 void StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::print(Teuchos::FancyOStream &out,
const VerbLevel verbLevel)
const {
297 if (verbLevel & Parameters1) {
298 out0 <<
"Parameter list: " << std::endl;
299 Teuchos::OSTab tab2(out);
300 out << this->GetParameterList();
303 if (verbLevel & External)
304 if (solver_ != Teuchos::null) {
305 Teuchos::OSTab tab2(out);
306 out << *solver_ << std::endl;
309 if (verbLevel & Debug) {
310 out0 <<
"IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl
312 <<
"RCP<solver_>: " << solver_ << std::endl;
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();
#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")