Debian Codegraph @

packages/shylu/shylu_dd/frosch/src/SolverInterfaces/FROSch_Amesos2SolverTpetra_def.hpp

raw
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_AMESOS2SOLVERTPETRA_DEF_HPP
11 #define _FROSCH_AMESOS2SOLVERTPETRA_DEF_HPP
12
13 #include <FROSch_Amesos2SolverTpetra_decl.hpp>
14
15
16 namespace FROSch {
17
18 using namespace std;
19 using namespace Teuchos;
20 using namespace Xpetra;
21
22 template<class SC,class LO,class GO,class NO>
23 Amesos2SolverTpetra<SC,LO,GO,NO>::~Amesos2SolverTpetra()
24 {
25 Amesos2Solver_.reset();
26 }
27
28 template<class SC,class LO,class GO,class NO>
29 int Amesos2SolverTpetra<SC,LO,GO,NO>::initialize()
30 {
31 FROSCH_TIMER_START_SOLVER(initializeTime,"Amesos2SolverTpetra::initialize");
32 this->IsInitialized_ = true;
33 this->IsComputed_ = false;
34 Amesos2Solver_->symbolicFactorization();
35 return 0;
36 }
37
38 template<class SC,class LO,class GO,class NO>
39 int Amesos2SolverTpetra<SC,LO,GO,NO>::compute()
40 {
41 FROSCH_TIMER_START_SOLVER(computeTime,"Amesos2SolverTpetra::compute");
42 FROSCH_ASSERT(this->IsInitialized_,"FROSch::Amesos2SolverTpetra: !this->IsInitialized_");
43 this->IsComputed_ = true;
44 Amesos2Solver_->numericFactorization();
45 return 0;
46 }
47
48 template<class SC,class LO,class GO,class NO>
49 void Amesos2SolverTpetra<SC,LO,GO,NO>::apply(const XMultiVector &x,
50 XMultiVector &y,
51 ETransp mode,
52 SC alpha,
53 SC beta) const
54 {
55 FROSCH_TIMER_START_SOLVER(applyTime,"Amesos2SolverTpetra::apply");
56 FROSCH_ASSERT(this->IsComputed_,"FROSch::Amesos2SolverTpetra: !this->IsComputed_.");
57
58 const TpetraMultiVector<SC,LO,GO,NO> * xTpetraMultiVectorX = dynamic_cast<const TpetraMultiVector<SC,LO,GO,NO> *>(&x);
59 FROSCH_ASSERT(xTpetraMultiVectorX,"FROSch::Amesos2SolverTpetra: dynamic_cast failed.");
60 TMultiVectorPtr tpetraMultiVectorX = xTpetraMultiVectorX->getTpetra_MultiVector();
61
62 if (Y_.is_null() || Y_->getNumVectors() != x.getNumVectors()) {
63 Y_ = XMultiVectorFactory::Build(y.getMap(),x.getNumVectors());
64 }
65 const TpetraMultiVector<SC,LO,GO,NO> * xTpetraMultiVectorY = dynamic_cast<const TpetraMultiVector<SC,LO,GO,NO> *>(Y_.get());
66 FROSCH_ASSERT(xTpetraMultiVectorY,"FROSch::Amesos2SolverTpetra: dynamic_cast failed.");
67 TMultiVectorPtr tpetraMultiVectorY = xTpetraMultiVectorY->getTpetra_MultiVector();
68
69 Amesos2Solver_->setX(tpetraMultiVectorY);
70 Amesos2Solver_->setB(tpetraMultiVectorX);
71
72 FROSCH_ASSERT(mode==NO_TRANS,"FROSch::Amesos2SolverTpetra: mode!=NO_TRANS");
73 Amesos2Solver_->solve(); // What about solving with transposed matrices?
74
75 y.update(alpha,*Y_,beta);
76 }
77
78 template<class SC,class LO,class GO,class NO>
79 int Amesos2SolverTpetra<SC,LO,GO,NO>::updateMatrix(ConstXMatrixPtr k,
80 bool reuseInitialize)
81 {
82 FROSCH_TIMER_START_SOLVER(updateMatrixTime,"Amesos2SolverTpetra::updateMatrix");
83 this->K_ = k;
84 FROSCH_ASSERT(!this->K_.is_null(),"FROSch::Amesos2SolverTpetra: K_ is null.");
85
86 const CrsMatrixWrap<SC,LO,GO,NO>& crsOp = dynamic_cast<const CrsMatrixWrap<SC,LO,GO,NO>&>(*this->K_);
87 const TpetraCrsMatrix<SC,LO,GO,NO>& xTpetraMat = dynamic_cast<const TpetraCrsMatrix<SC,LO,GO,NO>&>(*crsOp.getCrsMatrix());
88 ConstTCrsMatrixPtr tpetraMat = xTpetraMat.getTpetra_CrsMatrix();
89 TEUCHOS_TEST_FOR_EXCEPT(tpetraMat.is_null());
90
91 if (reuseInitialize) {
92 Amesos2Solver_->setA(tpetraMat,Amesos2::SYMBFACT);
93 } else {
94 Amesos2Solver_->setA(tpetraMat,Amesos2::CLEAN);
95 }
96 return 0;
97 }
98
99 template<class SC,class LO,class GO,class NO>
100 Amesos2SolverTpetra<SC,LO,GO,NO>::Amesos2SolverTpetra(ConstXMatrixPtr k,
101 ParameterListPtr parameterList,
102 string description) :
103 Solver<SC,LO,GO,NO> (k,parameterList,description)
104 {
105 FROSCH_TIMER_START_SOLVER(Amesos2SolverTpetraTime,"Amesos2SolverTpetra::Amesos2SolverTpetra");
106 FROSCH_ASSERT(!this->K_.is_null(),"FROSch::Amesos2SolverTpetra: K_ is null.");
107 FROSCH_ASSERT(this->K_->getRowMap()->lib()==UseTpetra,"FROSch::Amesos2SolverTpetra: Not compatible with Epetra.")
108
109 const CrsMatrixWrap<SC,LO,GO,NO>& crsOp = dynamic_cast<const CrsMatrixWrap<SC,LO,GO,NO>&>(*this->K_);
110 const TpetraCrsMatrix<SC,LO,GO,NO>& xTpetraMat = dynamic_cast<const TpetraCrsMatrix<SC,LO,GO,NO>&>(*crsOp.getCrsMatrix());
111 ConstTCrsMatrixPtr tpetraMat = xTpetraMat.getTpetra_CrsMatrix();
112 TEUCHOS_TEST_FOR_EXCEPT(tpetraMat.is_null());
113
114 TMultiVectorPtr xTmp;
115 TMultiVectorPtr bTmp;
116
117 ParameterListPtr amesos2ParameterList = sublist(this->ParameterList_,"Amesos2");
118 if (amesos2ParameterList->isSublist(this->ParameterList_->get("Solver","Klu"))) amesos2ParameterList = sublist(amesos2ParameterList,this->ParameterList_->get("Solver","Klu"));
119 amesos2ParameterList->setName("Amesos2");
120
121 Amesos2Solver_ = Amesos2::create<TCrsMatrix,TMultiVector>(this->ParameterList_->get("Solver","Klu"),tpetraMat,xTmp,bTmp);
122 Amesos2Solver_->setParameters(amesos2ParameterList);
123 }
124
125 }
126
127 #endif