10 #ifndef MUELU_STRATIMIKOSSMOOTHER_DEF_HPP
11 #define MUELU_STRATIMIKOSSMOOTHER_DEF_HPP
15 #if defined(HAVE_MUELU_STRATIMIKOS) && defined(HAVE_MUELU_THYRA)
20 #include <Xpetra_CrsMatrixWrap.hpp>
25 #include "MueLu_Utilities.hpp"
31 #include <Stratimikos_DefaultLinearSolverBuilder.hpp>
32 #include "Teuchos_AbstractFactoryStd.hpp"
34 #include <unordered_map>
38 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
39 StratimikosSmoother<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);
54 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
55 void StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::SetParameterList(
const Teuchos::ParameterList& paramList) {
56 Factory::SetParameterList(paramList);
59 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
60 void StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::DeclareInput(Level& currentLevel)
const {
61 this->Input(currentLevel,
"A");
64 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
65 void 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;
74 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
75 void 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(Teuchos::rcp_dynamic_cast<CrsMatrixWrap>(filteredA)->getCrsMatrix());
82 thyraA = Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::toThyra(Teuchos::rcp_dynamic_cast<CrsMatrixWrap>(A_)->getCrsMatrix());
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());
111 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
112 void 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;
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.");
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);
208 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
209 void 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());
223 RCP<MultiVector> Residual = Utilities::Residual(*A_, X, B);
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());
234 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
235 RCP<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());
241 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
242 std::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_ <<
"}";
252 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
257 out0 <<
"Parameter list: " << std::endl;
259 out << this->GetParameterList();
263 if (solver_ != Teuchos::null) {
265 out << *solver_ << std::endl;
268 if (verbLevel &
Debug) {
269 out0 <<
"IsSetup: " <<
Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl
271 <<
"RCP<solver_>: " << solver_ << std::endl;
275 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
276 size_t StratimikosSmoother<double, LocalOrdinal, GlobalOrdinal, Node>::getNodeSmootherComplexity()
const {
282 #endif // HAVE_MUELU_STRATIMIKOS
283 #endif // MUELU_STRATIMIKOSSMOOTHER_DEF_HPP
MueLu::DefaultLocalOrdinal LocalOrdinal
static Teuchos::RCP< MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Build(const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node >> &map, size_t NumVectors, bool zeroOut=true)
Print external lib objects.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Print additional debugging information.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
MueLu::DefaultScalar Scalar
#define MUELU_DESCRIBE
Helper macro for implementing Describable::describe() for BaseClass objects.
Print class parameters (more parameters, more verbose)
std::string toString(const T &t)