| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// ShyLU: Scalable Hybrid LU Preconditioner and Solver |
| 4 |
// |
| 5 |
// Copyright 2011 NTESS and the ShyLU contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
#ifndef _FROSCH_IFPACK2PRECONDITIONERTPETRA_DEF_HPP |
| 11 |
#define _FROSCH_IFPACK2PRECONDITIONERTPETRA_DEF_HPP |
| 12 |
|
| 13 |
#include <FROSch_Ifpack2PreconditionerTpetra_decl.hpp> |
| 14 |
|
| 15 |
#include "Ifpack2_Details_getCrsMatrix.hpp" |
| 16 |
#include "Ifpack2_RILUK_decl.hpp" |
| 17 |
|
| 18 |
#ifdef HAVE_SHYLU_DDFROSCH_ZOLTAN2 |
| 19 |
#include "Zoltan2_TpetraRowGraphAdapter.hpp" |
| 20 |
#include "Zoltan2_OrderingProblem.hpp" |
| 21 |
#include "Zoltan2_OrderingSolution.hpp" |
| 22 |
#endif |
| 23 |
|
| 24 |
namespace FROSch { |
| 25 |
|
| 26 |
using namespace std; |
| 27 |
using namespace Teuchos; |
| 28 |
using namespace Xpetra; |
| 29 |
|
| 30 |
template<class SC,class LO,class GO,class NO> |
| 31 |
int Ifpack2PreconditionerTpetra<SC,LO,GO,NO>::initialize() |
| 32 |
{ |
| 33 |
FROSCH_TIMER_START_SOLVER(initializeTime,"Ifpack2PreconditionerTpetra::initialize"); |
| 34 |
this->IsInitialized_ = true; |
| 35 |
this->IsComputed_ = false; |
| 36 |
Ifpack2Preconditioner_->initialize(); |
| 37 |
return 0; |
| 38 |
} |
| 39 |
|
| 40 |
template<class SC,class LO,class GO,class NO> |
| 41 |
int Ifpack2PreconditionerTpetra<SC,LO,GO,NO>::compute() |
| 42 |
{ |
| 43 |
FROSCH_TIMER_START_SOLVER(computeTime,"Ifpack2PreconditionerTpetra::compute"); |
| 44 |
FROSCH_ASSERT(this->IsInitialized_,"FROSch::Ifpack2PreconditionerTpetra: !this->IsInitialized_"); |
| 45 |
this->IsComputed_ = true; |
| 46 |
Ifpack2Preconditioner_->compute(); |
| 47 |
return 0; |
| 48 |
} |
| 49 |
|
| 50 |
template<class SC,class LO,class GO,class NO> |
| 51 |
void Ifpack2PreconditionerTpetra<SC,LO,GO,NO>::apply(const XMultiVector &x, |
| 52 |
XMultiVector &y, |
| 53 |
ETransp mode, |
| 54 |
SC alpha, |
| 55 |
SC beta) const |
| 56 |
{ |
| 57 |
FROSCH_TIMER_START_SOLVER(applyTime,"Ifpack2PreconditionerTpetra::apply"); |
| 58 |
FROSCH_ASSERT(this->IsComputed_,"FROSch::Ifpack2PreconditionerTpetra: !this->IsComputed_."); |
| 59 |
|
| 60 |
const TpetraMultiVector<SC,LO,GO,NO> * xTpetraMultiVectorX = dynamic_cast<const TpetraMultiVector<SC,LO,GO,NO> *>(&x); |
| 61 |
FROSCH_ASSERT(xTpetraMultiVectorX,"FROSch::Ifpack2PreconditionerTpetra: dynamic_cast failed."); |
| 62 |
TMultiVectorPtr tpetraMultiVectorX = xTpetraMultiVectorX->getTpetra_MultiVector(); |
| 63 |
|
| 64 |
const TpetraMultiVector<SC,LO,GO,NO> * xTpetraMultiVectorY = dynamic_cast<const TpetraMultiVector<SC,LO,GO,NO> *>(&y); |
| 65 |
FROSCH_ASSERT(xTpetraMultiVectorY,"FROSch::Ifpack2PreconditionerTpetra: dynamic_cast failed."); |
| 66 |
TMultiVectorPtr tpetraMultiVectorY = xTpetraMultiVectorY->getTpetra_MultiVector(); |
| 67 |
|
| 68 |
#ifdef HAVE_SHYLU_DDFROSCH_ZOLTAN2 |
| 69 |
if (this->useZoltan2 && this->useRILUK) { |
| 70 |
auto A = Ifpack2Preconditioner_->getMatrix(); |
| 71 |
auto filteredA = rcp_dynamic_cast<const TRowMatrixFilterType> (A); |
| 72 |
|
| 73 |
// make copies of X & Y |
| 74 |
TMultiVector ReorderedX (*tpetraMultiVectorX, Teuchos::Copy); |
| 75 |
TMultiVector ReorderedY (*tpetraMultiVectorY, Teuchos::Copy); |
| 76 |
|
| 77 |
// permute X & Y |
| 78 |
filteredA->permuteOriginalToReordered (*tpetraMultiVectorX, ReorderedX); |
| 79 |
filteredA->permuteOriginalToReordered (*tpetraMultiVectorY, ReorderedY); |
| 80 |
|
| 81 |
// solve |
| 82 |
Ifpack2Preconditioner_->apply(ReorderedX,ReorderedY,mode,alpha,beta); |
| 83 |
|
| 84 |
// permute X back |
| 85 |
filteredA->permuteReorderedToOriginal (ReorderedY, *tpetraMultiVectorY); |
| 86 |
} else |
| 87 |
#endif |
| 88 |
{ |
| 89 |
Ifpack2Preconditioner_->apply(*tpetraMultiVectorX,*tpetraMultiVectorY,mode,alpha,beta); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
template<class SC,class LO,class GO,class NO> |
| 94 |
int Ifpack2PreconditionerTpetra<SC,LO,GO,NO>::updateMatrix(ConstXMatrixPtr k, |
| 95 |
bool reuseInitialize) |
| 96 |
{ |
| 97 |
if (this->useRILUK) { |
| 98 |
const CrsMatrixWrap<SC,LO,GO,NO>& crsOp = dynamic_cast<const CrsMatrixWrap<SC,LO,GO,NO>&>(*this->K_); |
| 99 |
const TpetraCrsMatrix<SC,LO,GO,NO>& xTpetraMat = dynamic_cast<const TpetraCrsMatrix<SC,LO,GO,NO>&>(*crsOp.getCrsMatrix()); |
| 100 |
ConstTCrsMatrixPtr tpetraMat = xTpetraMat.getTpetra_CrsMatrix(); |
| 101 |
|
| 102 |
auto RILUPreconditioner = rcp_dynamic_cast<Ifpack2::RILUK<TRowMatrix>>(Ifpack2Preconditioner_); |
| 103 |
#ifdef HAVE_SHYLU_DDFROSCH_ZOLTAN2 |
| 104 |
if (this->useZoltan2) { |
| 105 |
// if K is replaced, then we need to re-wrap into matrix filter |
| 106 |
auto rowMat = rcp_dynamic_cast<const TRowMatrix>(tpetraMat); |
| 107 |
auto filteredMat = rcp(new TRowMatrixFilterType(rowMat, this->perm, this->revperm)); |
| 108 |
RILUPreconditioner->setMatrix(filteredMat); |
| 109 |
} else |
| 110 |
#endif |
| 111 |
{ |
| 112 |
RILUPreconditioner->setMatrix(tpetraMat); |
| 113 |
} |
| 114 |
return 0; |
| 115 |
} |
| 116 |
FROSCH_ASSERT(false,"FROSch::Ifpack2PreconditionerTpetra: updateMatrix() is not implemented for the Ifpack2PreconditionerTpetra yet."); |
| 117 |
} |
| 118 |
|
| 119 |
template<class SC,class LO,class GO,class NO> |
| 120 |
Ifpack2PreconditionerTpetra<SC,LO,GO,NO>::Ifpack2PreconditionerTpetra(ConstXMatrixPtr k, |
| 121 |
ParameterListPtr parameterList, |
| 122 |
string description) : |
| 123 |
Solver<SC,LO,GO,NO> (k,parameterList,description) |
| 124 |
{ |
| 125 |
FROSCH_TIMER_START_SOLVER(Ifpack2PreconditionerTpetraTime,"Ifpack2PreconditionerTpetra::Ifpack2PreconditionerTpetra"); |
| 126 |
FROSCH_ASSERT(!this->K_.is_null(),"FROSch::Ifpack2PreconditionerTpetra: K_ is null."); |
| 127 |
FROSCH_ASSERT(this->K_->getRowMap()->lib()==UseTpetra,"FROSch::Ifpack2PreconditionerTpetra: Not compatible with Epetra.") |
| 128 |
|
| 129 |
const CrsMatrixWrap<SC,LO,GO,NO>& crsOp = dynamic_cast<const CrsMatrixWrap<SC,LO,GO,NO>&>(*this->K_); |
| 130 |
const TpetraCrsMatrix<SC,LO,GO,NO>& xTpetraMat = dynamic_cast<const TpetraCrsMatrix<SC,LO,GO,NO>&>(*crsOp.getCrsMatrix()); |
| 131 |
ConstTCrsMatrixPtr tpetraMat = xTpetraMat.getTpetra_CrsMatrix(); |
| 132 |
TEUCHOS_TEST_FOR_EXCEPT(tpetraMat.is_null()); |
| 133 |
|
| 134 |
auto solverName = this->ParameterList_->get("Solver","RILUK"); |
| 135 |
this->useRILUK = (solverName == "RILUK"); |
| 136 |
this->useZoltan2 = this->ParameterList_->get("RILUK: use reordering", false); |
| 137 |
|
| 138 |
Ifpack2::Details::OneLevelFactory<TRowMatrix> ifpack2Factory; |
| 139 |
#ifdef HAVE_SHYLU_DDFROSCH_ZOLTAN2 |
| 140 |
if (this->useZoltan2 && this->useRILUK) { |
| 141 |
// pre-ordering matrix, before constructing Ifpack2Preconditioner_ with the matrix |
| 142 |
{ |
| 143 |
typedef Tpetra::RowGraph<LO, GO, NO> row_graph_type; |
| 144 |
typedef Zoltan2::TpetraRowGraphAdapter<row_graph_type> z2_adapter_type; |
| 145 |
typedef Zoltan2::OrderingProblem<z2_adapter_type> z2_ordering_problem_type; |
| 146 |
typedef Zoltan2::LocalOrderingSolution<LO> z2_ordering_solution_type; |
| 147 |
|
| 148 |
auto comm = tpetraMat->getRowMap()->getComm(); |
| 149 |
auto constActiveGraph = Teuchos::rcp_const_cast<const row_graph_type>(tpetraMat->getGraph()); |
| 150 |
z2_adapter_type Zoltan2Graph (constActiveGraph); |
| 151 |
|
| 152 |
Teuchos::ParameterList zoltan2_params = this->ParameterList_->sublist ("RILUK: reordering list"); |
| 153 |
z2_ordering_problem_type MyOrderingProblem (&Zoltan2Graph, &zoltan2_params, comm); |
| 154 |
|
| 155 |
MyOrderingProblem.solve (); |
| 156 |
z2_ordering_solution_type sol (*MyOrderingProblem.getLocalOrderingSolution()); |
| 157 |
this->perm = sol.getPermutationRCPConst (true); |
| 158 |
this->revperm = sol.getPermutationRCPConst (); |
| 159 |
} |
| 160 |
|
| 161 |
// wrap into matrix filter |
| 162 |
auto rowMat = rcp_dynamic_cast<const TRowMatrix>(tpetraMat); |
| 163 |
auto filteredMat = rcp(new TRowMatrixFilterType(rowMat, this->perm, this->revperm)); |
| 164 |
Ifpack2Preconditioner_ = ifpack2Factory.create(solverName,filteredMat); |
| 165 |
} else |
| 166 |
#endif |
| 167 |
{ |
| 168 |
Ifpack2Preconditioner_ = ifpack2Factory.create(solverName,tpetraMat); |
| 169 |
} |
| 170 |
|
| 171 |
// set ifpack2 parameters |
| 172 |
ParameterListPtr ifpack2ParameterList = sublist(this->ParameterList_,"Ifpack2"); |
| 173 |
if (ifpack2ParameterList->isSublist(solverName)) { |
| 174 |
ifpack2ParameterList = sublist(ifpack2ParameterList, solverName); |
| 175 |
} |
| 176 |
ifpack2ParameterList->setName("Ifpack2"); |
| 177 |
Ifpack2Preconditioner_->setParameters(*ifpack2ParameterList); |
| 178 |
} |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
#endif |