Ifpack2 Templated Preconditioning Package  Version 1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Ifpack2_IdentitySolver_def.hpp
1 // @HEADER
2 // *****************************************************************************
3 // Ifpack2: Templated Object-Oriented Algebraic Preconditioner Package
4 //
5 // Copyright 2009 NTESS and the Ifpack2 contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef IFPACK2_IDENTITY_SOLVER_DEF_HPP
11 #define IFPACK2_IDENTITY_SOLVER_DEF_HPP
12 
13 #include "Ifpack2_IdentitySolver_decl.hpp"
14 #include "Tpetra_Map.hpp"
15 #include "Tpetra_MultiVector.hpp"
16 #include "Tpetra_Export.hpp"
17 
18 namespace Ifpack2 {
19 
20 template<class MatrixType>
23  : matrix_ (A),
24  isInitialized_ (false),
25  isComputed_ (false),
26  numInitialize_ (0),
27  numCompute_ (0),
28  numApply_ (0),
29  initializeTime_(0.0),
30  computeTime_(0.0),
31  applyTime_(0.0)
32 {
33 }
34 
35 template<class MatrixType>
37 {
38 }
39 
40 template<class MatrixType>
42 {
43 }
44 
45 template<class MatrixType>
47 {
49  matrix_.is_null (), std::runtime_error, "Ifpack2::IdentitySolver: "
50  "You must call setMatrix() with a nonnull input matrix "
51  "before you may call initialize() or compute().");
52 
54  ! matrix_->getDomainMap ()->isCompatible (* (matrix_->getRangeMap ())),
55  std::invalid_argument,
56  "Ifpack2::IdentitySolver: The domain and range Maps "
57  "of the input matrix must be compatible.");
58 
59  // If the domain and range Maps are not the same, then we need to
60  // construct an Export from the domain Map to the range Map, so that
61  // this operator is really the identity and not a permutation.
62  if (! matrix_->getDomainMap ()->isSameAs (* (matrix_->getRangeMap ()))) {
63  export_ = Teuchos::rcp (new export_type (matrix_->getDomainMap (),
64  matrix_->getRangeMap ()));
65  }
66  else {
67  // If the Export is null, we won't do the Export in apply().
68  // Thus, we need to set it to null here as a flag.
69  export_ = Teuchos::null;
70  }
71 
72  isInitialized_ = true;
73  ++numInitialize_;
74 }
75 
76 template<class MatrixType>
78 {
80  matrix_.is_null (), std::runtime_error, "Ifpack2::IdentitySolver: "
81  "You must call setMatrix() with a nonnull input matrix "
82  "before you may call initialize() or compute().");
83 
84  if (! isInitialized_) {
85  initialize ();
86  }
87 
88  isComputed_ = true;
89  ++numCompute_;
90 }
91 
92 template<class MatrixType>
94 apply (const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& X,
95  Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y,
96  Teuchos::ETransp /*mode*/,
97  scalar_type alpha,
98  scalar_type beta) const
99 {
100  using Teuchos::RCP;
102  typedef Tpetra::MultiVector<scalar_type, local_ordinal_type,
104 
106  ! isComputed (), std::runtime_error,
107  "Ifpack2::IdentitySolver::apply: If compute() has not yet been called, "
108  "or if you have changed the matrix via setMatrix(), "
109  "you must call compute() before you may call this method.");
110 
111  // "Identity solver" does what it says: it's the identity operator.
112  // We have to Export if the domain and range Maps are not the same.
113  // Otherwise, this operator would be a permutation, not the identity.
114  if (export_.is_null ()) {
115  Y.update (alpha, X, beta);
116  }
117  else {
118  if (alpha == STS::one () && beta == STS::zero ()) { // the common case
119  Y.doExport (X, *export_, Tpetra::REPLACE);
120  }
121  else {
122  // We know that the domain and range Maps are compatible. First
123  // bring X into the range Map via Export. Then compute in place
124  // in Y.
125  MV X_tmp (Y.getMap (), Y.getNumVectors ());
126  X_tmp.doExport (X, *export_, Tpetra::REPLACE);
127  Y.update (alpha, X_tmp, beta);
128  }
129  }
130  ++numApply_;
131 }
132 
133 template <class MatrixType>
135  return(numInitialize_);
136 }
137 
138 template <class MatrixType>
140  return(numCompute_);
141 }
142 
143 template <class MatrixType>
145  return(numApply_);
146 }
147 
148 template <class MatrixType>
150  return(initializeTime_);
151 }
152 
153 template<class MatrixType>
155  return(computeTime_);
156 }
157 
158 template<class MatrixType>
160  return(applyTime_);
161 }
162 
163 template <class MatrixType>
165 {
166  std::ostringstream os;
167 
168  // Output is a valid YAML dictionary in flow style. If you don't
169  // like everything on a single line, you should call describe()
170  // instead.
171  os << "\"Ifpack2::IdentitySolver\": {";
172  if (this->getObjectLabel () != "") {
173  os << "Label: \"" << this->getObjectLabel () << "\", ";
174  }
175  os << "Initialized: " << (isInitialized () ? "true" : "false") << ", "
176  << "Computed: " << (isComputed () ? "true" : "false") << ", ";
177 
178  if (matrix_.is_null ()) {
179  os << "Matrix: null";
180  }
181  else {
182  os << "Matrix: not null"
183  << ", Global matrix dimensions: ["
184  << matrix_->getGlobalNumRows () << ", "
185  << matrix_->getGlobalNumCols () << "]";
186  }
187 
188  os << "}";
189  return os.str ();
190 }
191 
192 template <class MatrixType>
195  const Teuchos::EVerbosityLevel verbLevel) const
196 {
197  using std::endl;
198  const Teuchos::EVerbosityLevel vl
199  = (verbLevel == Teuchos::VERB_DEFAULT) ? Teuchos::VERB_LOW : verbLevel;
200 
201  if (vl != Teuchos::VERB_NONE) {
202  // By convention, describe() should always begin with a tab.
203  Teuchos::OSTab tab0 (out);
204  out << "\"Ifpack2::IdentitySolver\":" << endl;
205  Teuchos::OSTab tab1 (out);
206  out << "MatrixType: " << Teuchos::TypeNameTraits<MatrixType>::name () << endl;
207  out << "numInitialize: " << numInitialize_ << endl;
208  out << "numCompute: " << numCompute_ << endl;
209  out << "numApply: " << numApply_ << endl;
210  }
211 }
212 
213 template <class MatrixType>
215 {
217  matrix_.is_null (), std::runtime_error, "Ifpack2::IdentitySolver::getDomainMap: "
218  "The matrix is null. Please call setMatrix() with a nonnull input "
219  "before calling this method.");
220  return matrix_->getDomainMap ();
221 }
222 
223 template <class MatrixType>
225 {
227  matrix_.is_null (), std::runtime_error, "Ifpack2::IdentitySolver::getRangeMap: "
228  "The matrix is null. Please call setMatrix() with a nonnull input "
229  "before calling this method.");
230  return matrix_->getRangeMap ();
231 }
232 
233 template<class MatrixType>
236 {
237  // Check in serial or one-process mode if the matrix is square.
239  ! A.is_null () && A->getComm ()->getSize () == 1 &&
240  A->getLocalNumRows () != A->getLocalNumCols (),
241  std::runtime_error, "Ifpack2::IdentitySolver::setMatrix: If A's communicator only "
242  "contains one process, then A must be square. Instead, you provided a "
243  "matrix A with " << A->getLocalNumRows () << " rows and "
244  << A->getLocalNumCols () << " columns.");
245 
246  // It's legal for A to be null; in that case, you may not call
247  // initialize() until calling setMatrix() with a nonnull input.
248  // Regardless, setting the matrix invalidates the preconditioner.
249  isInitialized_ = false;
250  isComputed_ = false;
251  export_ = Teuchos::null;
252 
253  matrix_ = A;
254 }
255 
256 } // namespace Ifpack2
257 
258 #define IFPACK2_IDENTITYSOLVER_INSTANT(S,LO,GO,N) \
259  template class Ifpack2::IdentitySolver< Tpetra::RowMatrix<S, LO, GO, N> >;
260 
261 #endif // IFPACK2_IDENTITY_SOLVER_DEF_HPP
int getNumApply() const
Return the number of calls to apply().
Definition: Ifpack2_IdentitySolver_def.hpp:144
MatrixType::node_type node_type
Node type of the input matrix.
Definition: Ifpack2_IdentitySolver_decl.hpp:45
double getComputeTime() const
Return the time spent in compute().
Definition: Ifpack2_IdentitySolver_def.hpp:154
int getNumCompute() const
Return the number of calls to compute().
Definition: Ifpack2_IdentitySolver_def.hpp:139
virtual ~IdentitySolver()
Destructor.
Definition: Ifpack2_IdentitySolver_def.hpp:36
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
Apply the preconditioner to X, and put the result in Y.
Definition: Ifpack2_IdentitySolver_def.hpp:94
Teuchos::RCP< const map_type > getRangeMap() const
Return the Tpetra::Map object associated with the range of this operator.
Definition: Ifpack2_IdentitySolver_def.hpp:224
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void setParameters(const Teuchos::ParameterList &params)
Set this object&#39;s parameters.
Definition: Ifpack2_IdentitySolver_def.hpp:41
void initialize()
Initialize.
Definition: Ifpack2_IdentitySolver_def.hpp:46
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Teuchos::RCP< const map_type > getDomainMap() const
Return the Tpetra::Map object associated with the domain of this operator.
Definition: Ifpack2_IdentitySolver_def.hpp:214
IdentitySolver(const Teuchos::RCP< const row_matrix_type > &A)
Constructor: Takes the matrix to precondition.
Definition: Ifpack2_IdentitySolver_def.hpp:22
MatrixType::local_ordinal_type local_ordinal_type
Type of the local indices of the input matrix.
Definition: Ifpack2_IdentitySolver_decl.hpp:41
virtual void setMatrix(const Teuchos::RCP< const row_matrix_type > &A)
Set this preconditioner&#39;s matrix.
Definition: Ifpack2_IdentitySolver_def.hpp:235
std::string description() const
Return a simple one-line description of this object.
Definition: Ifpack2_IdentitySolver_def.hpp:164
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_IdentitySolver_def.hpp:194
void compute()
Compute the preconditioner.
Definition: Ifpack2_IdentitySolver_def.hpp:77
int getNumInitialize() const
Return the number of calls to initialize().
Definition: Ifpack2_IdentitySolver_def.hpp:134
MatrixType::global_ordinal_type global_ordinal_type
Type of the global indices of the input matrix.
Definition: Ifpack2_IdentitySolver_decl.hpp:43
double getInitializeTime() const
Return the time spent in initialize().
Definition: Ifpack2_IdentitySolver_def.hpp:149
MatrixType::scalar_type scalar_type
Type of the entries of the input matrix.
Definition: Ifpack2_IdentitySolver_decl.hpp:39
double getApplyTime() const
Return the time spent in apply().
Definition: Ifpack2_IdentitySolver_def.hpp:159
static std::string name()
bool is_null() const