Debian Codegraph @

packages/trilinoscouplings/src/ifpack/Ifpack_ML.cpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Trilinos: An Object-Oriented Solver Framework
4 //
5 // Copyright 2001-2024 NTESS and the Trilinos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #include "Ifpack_ConfigDefs.h"
11 #ifdef HAVE_IFPACK_ML
12 #include "Ifpack_Preconditioner.h"
13 #include "Ifpack_ML.h"
14 #include "Ifpack_Condest.h"
15 #include "Epetra_MultiVector.h"
16 #include "Epetra_Map.h"
17 #include "Epetra_Comm.h"
18 #include "ml_include.h"
19 #include "Epetra_LinearProblem.h"
20 #include "Epetra_RowMatrix.h"
21 #include "Epetra_Time.h"
22 #include "Teuchos_ParameterList.hpp"
23
24 static bool FirstTime = true;
25
26 //==============================================================================
27 Ifpack_ML::Ifpack_ML(Epetra_RowMatrix* Matrix) :
28 Matrix_(Teuchos::rcp( Matrix, false )),
29 Label_("ML"),
30 IsInitialized_(false),
31 IsComputed_(false),
32 UseTranspose_(false),
33 NumInitialize_(0),
34 NumCompute_(0),
35 NumApplyInverse_(0),
36 InitializeTime_(0.0),
37 ComputeTime_(0.0),
38 ApplyInverseTime_(0.0),
39 ComputeFlops_(0),
40 ApplyInverseFlops_(0),
41 Condest_(-1.0)
42 {
43 }
44
45 //==============================================================================
46 int Ifpack_ML::SetParameters(Teuchos::ParameterList& List)
47 {
48
49 List_ = List;
50 return(0);
51 }
52
53 //==============================================================================
54 int Ifpack_ML::Initialize()
55 {
56
57 IsInitialized_ = false;
58 IsComputed_ = false;
59
60 if (Matrix_ == Teuchos::null)
61 IFPACK_CHK_ERR(-1);
62
63 // only square matrices
64 if (Matrix_->NumGlobalRows() != Matrix_->NumGlobalCols())
65 IFPACK_CHK_ERR(-1);
66
67 // at least one nonzero
68 if (Matrix_->NumMyNonzeros() == 0)
69 IFPACK_CHK_ERR(-1);
70
71 if (Time_ == Teuchos::null)
72 Time_ = Teuchos::rcp( new Epetra_Time(Comm()) );
73
74 MLPrec_ = Teuchos::rcp( new ML_Epetra::MultiLevelPreconditioner(Matrix(),
75 List_, false) );
76
77 // if empty, give up.
78 if (MLPrec_ == Teuchos::null)
79 IFPACK_CHK_ERR(-1);
80
81 IsInitialized_ = true;
82 ++NumInitialize_;
83 InitializeTime_ += Time_->ElapsedTime();
84 return(0);
85 }
86
87 //==============================================================================
88 int Ifpack_ML::Compute()
89 {
90
91 if (!IsInitialized())
92 IFPACK_CHK_ERR(Initialize());
93
94 IsComputed_ = false;
95 Time_->ResetStartTime();
96
97 if (Matrix_ == Teuchos::null)
98 IFPACK_CHK_ERR(-1);
99
100 IFPACK_CHK_ERR(MLPrec_->ComputePreconditioner());
101
102 IsComputed_ = true;
103 ++NumCompute_;
104 ComputeTime_ += Time_->ElapsedTime();
105 return(0);
106 }
107
108 //==============================================================================
109 int Ifpack_ML::SetUseTranspose(bool UseTranspose)
110 {
111 UseTranspose_ = UseTranspose;
112 return(0);
113 }
114
115 //==============================================================================
116 int Ifpack_ML::
117 Apply(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
118 {
119 // check for maps ???
120 IFPACK_CHK_ERR(Matrix_->Apply(X,Y));
121 return(0);
122 }
123
124 //==============================================================================
125 int Ifpack_ML::
126 ApplyInverse(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
127 {
128
129 if (IsComputed() == false)
130 IFPACK_CHK_ERR(-1);
131
132 int numVectors = X.NumVectors();
133 if (numVectors != Y.NumVectors())
134 IFPACK_CHK_ERR(-1); // wrong input
135
136
137 Time_->ResetStartTime();
138
139 // AztecOO gives X and Y pointing to the same memory location,
140 // need to create an auxiliary vector, Xcopy
141 Teuchos::RefCountPtr<const Epetra_MultiVector> Xcopy;
142 if (X.Pointers()[0] == Y.Pointers()[0])
143 Xcopy = Teuchos::rcp( new Epetra_MultiVector(X) );
144 else
145 Xcopy = Teuchos::rcp( &X, false );
146
147 for (int i=0; i<numVectors; i++) {
148
149 IFPACK_CHK_ERR(MLPrec_->ApplyInverse(*Xcopy,Y));
150 }
151
152 ++NumApplyInverse_;
153 ApplyInverseTime_ += Time_->ElapsedTime();
154
155 return(0);
156 }
157
158 //==============================================================================
159 double Ifpack_ML::NormInf() const
160 {
161 return(-1.0);
162 }
163
164 //==============================================================================
165 const char* Ifpack_ML::Label() const
166 {
167 return((char*)Label_.c_str());
168 }
169
170 //==============================================================================
171 bool Ifpack_ML::UseTranspose() const
172 {
173 return(UseTranspose_);
174 }
175
176 //==============================================================================
177 bool Ifpack_ML::HasNormInf() const
178 {
179 return(false);
180 }
181
182 //==============================================================================
183 const Epetra_Comm & Ifpack_ML::Comm() const
184 {
185 return(Matrix_->Comm());
186 }
187
188 //==============================================================================
189 const Epetra_Map & Ifpack_ML::OperatorDomainMap() const
190 {
191 return(Matrix_->OperatorDomainMap());
192 }
193
194 //==============================================================================
195 const Epetra_Map & Ifpack_ML::OperatorRangeMap() const
196 {
197 return(Matrix_->OperatorRangeMap());
198 }
199
200 //==============================================================================
201 double Ifpack_ML::Condest(const Ifpack_CondestType CT,
202 const int MaxIters, const double Tol,
203 Epetra_RowMatrix* Matrix)
204 {
205
206 if (!IsComputed()) // cannot compute right now
207 return(-1.0);
208
209 if (Condest_ == -1.0)
210 Condest_ = Ifpack_Condest(*this, CT, MaxIters, Tol, Matrix);
211
212 return(Condest_);
213 }
214
215 //==============================================================================
216 std::ostream& Ifpack_ML::Print(std::ostream& os) const
217 {
218 if (!Comm().MyPID()) {
219 os << endl;
220 os << "================================================================================" << endl;
221 os << "Ifpack_ML: " << Label () << endl << endl;
222 os << "Condition number estimate = " << Condest() << endl;
223 os << "Global number of rows = " << Matrix_->NumGlobalRows() << endl;
224 os << endl;
225 os << "Phase # calls Total Time (s) Total MFlops MFlops/s" << endl;
226 os << "----- ------- -------------- ------------ --------" << endl;
227 os << "Initialize() " << std::setw(5) << NumInitialize_
228 << " " << std::setw(15) << InitializeTime_
229 << " 0.0 0.0" << endl;
230 os << "Compute() " << std::setw(5) << NumCompute_
231 << " " << std::setw(15) << ComputeTime_
232 << " " << std::setw(15) << 1.0e-6 * ComputeFlops_;
233 if (ComputeTime_ != 0.0)
234 os << " " << std::setw(15) << 1.0e-6 * ComputeFlops_ / ComputeTime_ << endl;
235 else
236 os << " " << std::setw(15) << 0.0 << endl;
237 os << "ApplyInverse() " << std::setw(5) << NumApplyInverse_
238 << " " << std::setw(15) << ApplyInverseTime_
239 << " " << std::setw(15) << 1.0e-6 * ApplyInverseFlops_;
240 if (ApplyInverseTime_ != 0.0)
241 os << " " << std::setw(15) << 1.0e-6 * ApplyInverseFlops_ / ApplyInverseTime_ << endl;
242 else
243 os << " " << std::setw(15) << 0.0 << endl;
244 os << "================================================================================" << endl;
245 os << endl;
246 }
247
248 return(os);
249 }
250 #endif // HAVE_IFPACK_ML