Ifpack2 Templated Preconditioning Package  Version 1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Ifpack2_Krylov_def.hpp
1 /*@HEADER
2 // ***********************************************************************
3 //
4 // Ifpack2: Tempated Object-Oriented Algebraic Preconditioner Package
5 // Copyright (2009) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 //@HEADER
41 */
42 
43 //-----------------------------------------------------
44 // Ifpack2::KRYLOV is based on the Krylov iterative
45 // solvers in Belos.
46 // written by Paul Tsuji.
47 //-----------------------------------------------------
48 
49 #ifndef IFPACK2_KRYLOV_DEF_HPP
50 #define IFPACK2_KRYLOV_DEF_HPP
51 
52 namespace Ifpack2 {
53 
54 //Definitions for the Krylov methods:
55 //==============================================================================
56 template <class MatrixType, class PrecType>
57 Krylov<MatrixType,PrecType>::Krylov(const Teuchos::RCP<const Tpetra::RowMatrix<scalar_type,local_ordinal_type,global_ordinal_type,node_type> >& A) :
58  A_(A),
59  Comm_(A->getRowMap()->getComm()),
60  // Default values
61  IterationType_(1),
62  Iterations_(5),
63  ResidualTolerance_(0.001),
64  BlockSize_(1),
65  ZeroStartingSolution_(true),
66  PreconditionerType_(1),
67  // General
68  Condest_(-1.0),
69  IsInitialized_(false),
70  IsComputed_(false),
71  NumInitialize_(0),
72  NumCompute_(0),
73  NumApply_(0),
74  InitializeTime_(0.0),
75  ComputeTime_(0.0),
76  ApplyTime_(0.0),
77  Time_("Ifpack2::Krylov"),
78  NumMyRows_(-1),
79  NumGlobalNonzeros_(0)
80 {
81  TEUCHOS_TEST_FOR_EXCEPTION(A_ == Teuchos::null, std::runtime_error,
82  Teuchos::typeName(*this) << "::Krylov(): input matrix reference was null.");
83 }
84 
85 //==========================================================================
86 template <class MatrixType, class PrecType>
88 }
89 
90 //==========================================================================
91 template <class MatrixType, class PrecType>
93  using Teuchos::as;
96  // Read in parameters
97  Ifpack2::getParameter(params, "krylov: iteration type",IterationType_);
98  Ifpack2::getParameter(params, "krylov: number of iterations",Iterations_);
99  Ifpack2::getParameter(params, "krylov: residual tolerance",ResidualTolerance_);
100  Ifpack2::getParameter(params, "krylov: block size",BlockSize_);
101  Ifpack2::getParameter(params, "krylov: zero starting solution",ZeroStartingSolution_);
102  Ifpack2::getParameter(params, "krylov: preconditioner type",PreconditionerType_);
103  params_=params;
104 }
105 
106 //==========================================================================
107 template <class MatrixType, class PrecType>
110  return(Comm_);
111 }
112 
113 //==========================================================================
114 template <class MatrixType, class PrecType>
117  return(A_);
118 }
119 
120 //==========================================================================
121 template <class MatrixType, class PrecType>
124 {
125  return A_->getDomainMap();
126 }
127 
128 //==========================================================================
129 template <class MatrixType, class PrecType>
132 {
133  return A_->getRangeMap();
134 }
135 
136 //==============================================================================
137 template <class MatrixType, class PrecType>
139  return true;
140 }
141 
142 //==========================================================================
143 template <class MatrixType, class PrecType>
145  return(NumInitialize_);
146 }
147 
148 //==========================================================================
149 template <class MatrixType, class PrecType>
151  return(NumCompute_);
152 }
153 
154 //==========================================================================
155 template <class MatrixType, class PrecType>
157  return(NumApply_);
158 }
159 
160 //==========================================================================
161 template <class MatrixType, class PrecType>
163  return(InitializeTime_);
164 }
165 
166 //==========================================================================
167 template <class MatrixType, class PrecType>
169  return(ComputeTime_);
170 }
171 
172 //==========================================================================
173 template <class MatrixType, class PrecType>
175  return(ApplyTime_);
176 }
177 
178 //=============================================================================
179 template <class MatrixType, class PrecType>
183  local_ordinal_type MaxIters,
184  magnitude_type Tol,
185  const Teuchos::Ptr<const Tpetra::RowMatrix<scalar_type,local_ordinal_type,global_ordinal_type,node_type> > &matrix) {
186  if (!isComputed()) { // cannot compute right now
187  return(-1.0);
188  }
189  // NOTE: this is computing the *local* condest
190  if (Condest_ == -1.0) {
191  Condest_ = Ifpack2::Condest(*this, CT, MaxIters, Tol, matrix);
192  }
193  return(Condest_);
194 }
195 
196 //==========================================================================
197 template <class MatrixType, class PrecType>
199  // clear any previous allocation
200  IsInitialized_ = false;
201  IsComputed_ = false;
202  Time_.start(true);
203  // check only in serial
204  TEUCHOS_TEST_FOR_EXCEPTION(Comm_->getSize() == 1 && A_->getNodeNumRows() != A_->getNodeNumCols(), std::runtime_error, "Ifpack2::Krylov::Initialize ERROR, matrix must be square");
205  NumMyRows_ = A_->getNodeNumRows();
206  // Belos parameter list
207  belosList_ = Teuchos::rcp( new Teuchos::ParameterList("GMRES") );
208  belosList_->set("Maximum Iterations",Iterations_);
209  belosList_->set("Convergence Tolerance",ResidualTolerance_);
210  if(PreconditionerType_==0) {
211  // no preconditioner
212  }
213  else if(PreconditionerType_==1) {
214  ifpack2_prec_=Teuchos::rcp( new Relaxation<MatrixType>(A_) );
215  }
216  else if(PreconditionerType_==2) {
217  ifpack2_prec_=Teuchos::rcp( new ILUT<MatrixType>(A_) );
218  }
219  else if(PreconditionerType_==3) {
220  int overlaplevel=0;
221  Ifpack2::getParameter(params_, "schwarz: overlap level",overlaplevel);
222  ifpack2_prec_=Teuchos::rcp( new AdditiveSchwarz< MatrixType,ILUT<MatrixType> >(A_,overlaplevel) );
223  }
224  else if(PreconditionerType_==4) {
225  ifpack2_prec_=Teuchos::rcp( new Chebyshev<MatrixType>(A_) );
226  }
227  if(PreconditionerType_>0) {
228  ifpack2_prec_->initialize();
229  ifpack2_prec_->setParameters(params_);
230  }
232  belosProblem_ -> setOperator(A_);
233  if(IterationType_==1) {
234  belosSolver_ =
235  Teuchos::rcp( new Belos::BlockGmresSolMgr<scalar_type,TMV,TOP> (belosProblem_, belosList_) );
236  }
237  else {
238  belosSolver_ =
239  Teuchos::rcp( new Belos::BlockCGSolMgr<scalar_type,TMV,TOP> (belosProblem_, belosList_) );
240  }
241  IsInitialized_ = true;
242  ++NumInitialize_;
243  Time_.stop();
244  InitializeTime_ += Time_.totalElapsedTime();
245 }
246 
247 //==========================================================================
248 template <class MatrixType, class PrecType>
250  using Teuchos::as;
251  //--------------------------------------------------------------------------
252  // Ifpack2::Krylov
253  // Computes necessary preconditioner info
254  //--------------------------------------------------------------------------
255  if (!isInitialized()) {
256  initialize();
257  }
258  Time_.start(true);
259  if(PreconditionerType_>0) {
260  ifpack2_prec_->compute();
261  belosProblem_->setLeftPrec(ifpack2_prec_);
262  }
263  IsComputed_ = true;
264  ++NumCompute_;
265  Time_.stop();
266  ComputeTime_ += Time_.totalElapsedTime();
267 }
268 
269 //==========================================================================
270 template <class MatrixType, class PrecType>
271 void Krylov<MatrixType,PrecType>::apply(const Tpetra::MultiVector<typename MatrixType::scalar_type,
272  typename MatrixType::local_ordinal_type,
273  typename MatrixType::global_ordinal_type,
274  typename MatrixType::node_type>& X,
275  Tpetra::MultiVector<typename MatrixType::scalar_type,
276  typename MatrixType::local_ordinal_type,
277  typename MatrixType::global_ordinal_type,
278  typename MatrixType::node_type>& Y,
279  Teuchos::ETransp mode,
280  typename MatrixType::scalar_type alpha,
281  typename MatrixType::scalar_type beta) const
282 {
283  TEUCHOS_TEST_FOR_EXCEPTION(!isComputed(), std::runtime_error,
284  "Ifpack2::Krylov::apply() ERROR, compute() hasn't been called yet.");
285 
286  TEUCHOS_TEST_FOR_EXCEPTION(X.getNumVectors() != Y.getNumVectors(), std::runtime_error,
287  "Ifpack2::Krylov::apply() ERROR, X.getNumVectors() != Y.getNumVectors().");
288 
289  Time_.start(true);
290 
291  // If X and Y are pointing to the same memory location,
292  // we need to create an auxiliary vector, Xcopy
294  if (X.getLocalMV().getValues() == Y.getLocalMV().getValues()) {
295  Xcopy = Teuchos::rcp( new Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>(X) );
296  }
297  else {
298  Xcopy = Teuchos::rcp( &X, false );
299  }
300 
302  Teuchos::rcp( new Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> (Y) );
303  if(ZeroStartingSolution_==true) {
304  Zeros->putScalar((scalar_type) 0.0);
305  }
306 
307  // Set left and right hand sides for Belos
308  belosProblem_->setProblem(Zeros,Xcopy);
309  // iterative solve
310  belosSolver_->solve();
311 
312  Y=*Zeros;
313  ++NumApply_;
314  Time_.stop();
315  ApplyTime_ += Time_.totalElapsedTime();
316 }
317 
318 //=============================================================================
319 template <class MatrixType, class PrecType>
321  std::ostringstream oss;
323  if (isInitialized()) {
324  if (isComputed()) {
325  oss << "{status = initialized, computed";
326  }
327  else {
328  oss << "{status = initialized, not computed";
329  }
330  }
331  else {
332  oss << "{status = not initialized, not computed";
333  }
334  oss << ", global rows = " << A_->getGlobalNumRows()
335  << ", global cols = " << A_->getGlobalNumCols()
336  << "}";
337  return oss.str();
338 }
339 
340 //=============================================================================
341 template <class MatrixType, class PrecType>
343  using std::endl;
344  using std::setw;
345  using Teuchos::VERB_DEFAULT;
346  using Teuchos::VERB_NONE;
347  using Teuchos::VERB_LOW;
348  using Teuchos::VERB_MEDIUM;
349  using Teuchos::VERB_HIGH;
350  using Teuchos::VERB_EXTREME;
351  Teuchos::EVerbosityLevel vl = verbLevel;
352  if (vl == VERB_DEFAULT) vl = VERB_LOW;
353  const int myImageID = Comm_->getRank();
354  Teuchos::OSTab tab(out);
355  // none: print nothing
356  // low: print O(1) info from node 0
357  // medium:
358  // high:
359  // extreme:
360  if (vl != VERB_NONE && myImageID == 0) {
361  }
362 }
363 
364 }//namespace Ifpack2
365 
366 #endif /* IFPACK2_KRYLOV_DEF_HPP */
virtual ~Krylov()
Krylov Destructor.
Definition: Ifpack2_Krylov_def.hpp:87
CondestType
Ifpack2::CondestType: enum to define the type of condition number estimate.
Definition: Ifpack2_CondestType.hpp:50
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print the object with some verbosity level to an FancyOStream object.
Definition: Ifpack2_Krylov_def.hpp:342
double getComputeTime() const
Returns the time spent in Compute().
Definition: Ifpack2_Krylov_def.hpp:168
int getNumInitialize() const
Returns the number of calls to Initialize().
Definition: Ifpack2_Krylov_def.hpp:144
Krylov(const Teuchos::RCP< const Tpetra::RowMatrix< scalar_type, local_ordinal_type, global_ordinal_type, node_type > > &A)
Krylov explicit constuctor with Tpetra::RowMatrix input.
Definition: Ifpack2_Krylov_def.hpp:57
const Teuchos::RCP< const Tpetra::Map< local_ordinal_type, global_ordinal_type, node_type > > & getRangeMap() const
Tpetra::Map representing the range of this operator.
Definition: Ifpack2_Krylov_def.hpp:131
std::string description() const
Return a simple one-line description of this object.
Definition: Ifpack2_Krylov_def.hpp:320
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void getParameter(const Teuchos::ParameterList &params, const std::string &name, T &value)
Set a value from a ParameterList if a parameter with the specified name exists.
Definition: Ifpack2_Parameters.hpp:59
magnitude_type computeCondEst(CondestType CT=Cheap, local_ordinal_type MaxIters=1550, magnitude_type Tol=1e-9, const Teuchos::Ptr< const Tpetra::RowMatrix< scalar_type, local_ordinal_type, global_ordinal_type, node_type > > &Matrix_in=Teuchos::null)
Computes the estimated condition number and returns the value.
Definition: Ifpack2_Krylov_def.hpp:182
const Teuchos::RCP< const Teuchos::Comm< int > > & getComm() const
Returns the Tpetra::BlockMap object associated with the range of this matrix operator.
Definition: Ifpack2_Krylov_def.hpp:109
void apply(const Tpetra::MultiVector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > &X, Tpetra::MultiVector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, scalar_type alpha=Teuchos::ScalarTraits< scalar_type >::one(), scalar_type beta=Teuchos::ScalarTraits< scalar_type >::zero()) const
Returns the result of a few iterations of CG/GMRES on a Tpetra::MultiVector X in Y.
Definition: Ifpack2_Krylov_def.hpp:271
ILUT incomplete factorization of a Tpetra sparse matrix.
Definition: Ifpack2_ILUT_decl.hpp:96
void setParameters(const Teuchos::ParameterList &params)
Set parameters for the preconditioner.
Definition: Ifpack2_Krylov_def.hpp:92
double getApplyTime() const
Returns the time spent in apply().
Definition: Ifpack2_Krylov_def.hpp:174
Teuchos::RCP< const Tpetra::RowMatrix< scalar_type, local_ordinal_type, global_ordinal_type, node_type > > getMatrix() const
Returns a reference to the matrix to be preconditioned.
Definition: Ifpack2_Krylov_def.hpp:116
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
MatrixType::local_ordinal_type local_ordinal_type
The type of local indices in the input MatrixType.
Definition: Ifpack2_Krylov_decl.hpp:127
const Teuchos::RCP< const Tpetra::Map< local_ordinal_type, global_ordinal_type, node_type > > & getDomainMap() const
Tpetra::Map representing the domain of this operator.
Definition: Ifpack2_Krylov_def.hpp:123
virtual std::string description() const
void compute()
Setup iteration.
Definition: Ifpack2_Krylov_def.hpp:249
MatrixType::scalar_type scalar_type
The type of the entries of the input MatrixType.
Definition: Ifpack2_Krylov_decl.hpp:117
bool hasTransposeApply() const
Whether this object&#39;s apply() method can apply the transpose (or conjugate transpose, if applicable).
Definition: Ifpack2_Krylov_def.hpp:138
int getNumApply() const
Returns the number of calls to apply().
Definition: Ifpack2_Krylov_def.hpp:156
void initialize()
Initialize Krylov preconditioner object.
Definition: Ifpack2_Krylov_def.hpp:198
TypeTo as(const TypeFrom &t)
double getInitializeTime() const
Returns the time spent in Initialize().
Definition: Ifpack2_Krylov_def.hpp:162
Teuchos::ScalarTraits< scalar_type >::magnitudeType magnitude_type
The type of the magnitude (absolute value) of a matrix entry.
Definition: Ifpack2_Krylov_decl.hpp:148
Additive Schwarz domain decomposition for Tpetra::RowMatrix objects.
Definition: Ifpack2_AdditiveSchwarz_decl.hpp:123
int getNumCompute() const
Returns the number of calls to Compute().
Definition: Ifpack2_Krylov_def.hpp:150
Relaxation preconditioners for Tpetra::RowMatrix and Tpetra::CrsMatrix sparse matrices.
Definition: Ifpack2_Relaxation_decl.hpp:231
std::string typeName(const T &t)