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 template <class MatrixType>
36 }
37 
38 template <class MatrixType>
40 }
41 
42 template <class MatrixType>
45  matrix_.is_null(), std::runtime_error,
46  "Ifpack2::IdentitySolver: "
47  "You must call setMatrix() with a nonnull input matrix "
48  "before you may call initialize() or compute().");
49 
51  !matrix_->getDomainMap()->isCompatible(*(matrix_->getRangeMap())),
52  std::invalid_argument,
53  "Ifpack2::IdentitySolver: The domain and range Maps "
54  "of the input matrix must be compatible.");
55 
56  // If the domain and range Maps are not the same, then we need to
57  // construct an Export from the domain Map to the range Map, so that
58  // this operator is really the identity and not a permutation.
59  if (!matrix_->getDomainMap()->isSameAs(*(matrix_->getRangeMap()))) {
60  export_ = Teuchos::rcp(new export_type(matrix_->getDomainMap(),
61  matrix_->getRangeMap()));
62  } else {
63  // If the Export is null, we won't do the Export in apply().
64  // Thus, we need to set it to null here as a flag.
65  export_ = Teuchos::null;
66  }
67 
68  isInitialized_ = true;
69  ++numInitialize_;
70 }
71 
72 template <class MatrixType>
75  matrix_.is_null(), std::runtime_error,
76  "Ifpack2::IdentitySolver: "
77  "You must call setMatrix() with a nonnull input matrix "
78  "before you may call initialize() or compute().");
79 
80  if (!isInitialized_) {
81  initialize();
82  }
83 
84  isComputed_ = true;
85  ++numCompute_;
86 }
87 
88 template <class MatrixType>
90  apply(const Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type>& X,
91  Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type>& Y,
92  Teuchos::ETransp /*mode*/,
93  scalar_type alpha,
94  scalar_type beta) const {
95  using Teuchos::RCP;
97  typedef Tpetra::MultiVector<scalar_type, local_ordinal_type,
99  MV;
100 
102  !isComputed(), std::runtime_error,
103  "Ifpack2::IdentitySolver::apply: If compute() has not yet been called, "
104  "or if you have changed the matrix via setMatrix(), "
105  "you must call compute() before you may call this method.");
106 
107  // "Identity solver" does what it says: it's the identity operator.
108  // We have to Export if the domain and range Maps are not the same.
109  // Otherwise, this operator would be a permutation, not the identity.
110  if (export_.is_null()) {
111  Y.update(alpha, X, beta);
112  } else {
113  if (alpha == STS::one() && beta == STS::zero()) { // the common case
114  Y.doExport(X, *export_, Tpetra::REPLACE);
115  } else {
116  // We know that the domain and range Maps are compatible. First
117  // bring X into the range Map via Export. Then compute in place
118  // in Y.
119  MV X_tmp(Y.getMap(), Y.getNumVectors());
120  X_tmp.doExport(X, *export_, Tpetra::REPLACE);
121  Y.update(alpha, X_tmp, beta);
122  }
123  }
124  ++numApply_;
125 }
126 
127 template <class MatrixType>
129  return (numInitialize_);
130 }
131 
132 template <class MatrixType>
134  return (numCompute_);
135 }
136 
137 template <class MatrixType>
139  return (numApply_);
140 }
141 
142 template <class MatrixType>
144  return (initializeTime_);
145 }
146 
147 template <class MatrixType>
149  return (computeTime_);
150 }
151 
152 template <class MatrixType>
154  return (applyTime_);
155 }
156 
157 template <class MatrixType>
159  std::ostringstream os;
160 
161  // Output is a valid YAML dictionary in flow style. If you don't
162  // like everything on a single line, you should call describe()
163  // instead.
164  os << "\"Ifpack2::IdentitySolver\": {";
165  if (this->getObjectLabel() != "") {
166  os << "Label: \"" << this->getObjectLabel() << "\", ";
167  }
168  os << "Initialized: " << (isInitialized() ? "true" : "false") << ", "
169  << "Computed: " << (isComputed() ? "true" : "false") << ", ";
170 
171  if (matrix_.is_null()) {
172  os << "Matrix: null";
173  } else {
174  os << "Matrix: not null"
175  << ", Global matrix dimensions: ["
176  << matrix_->getGlobalNumRows() << ", "
177  << matrix_->getGlobalNumCols() << "]";
178  }
179 
180  os << "}";
181  return os.str();
182 }
183 
184 template <class MatrixType>
187  const Teuchos::EVerbosityLevel verbLevel) const {
188  using std::endl;
189  const Teuchos::EVerbosityLevel vl = (verbLevel == Teuchos::VERB_DEFAULT) ? Teuchos::VERB_LOW : verbLevel;
190 
191  if (vl != Teuchos::VERB_NONE) {
192  // By convention, describe() should always begin with a tab.
193  Teuchos::OSTab tab0(out);
194  out << "\"Ifpack2::IdentitySolver\":" << endl;
195  Teuchos::OSTab tab1(out);
196  out << "MatrixType: " << Teuchos::TypeNameTraits<MatrixType>::name() << endl;
197  out << "numInitialize: " << numInitialize_ << endl;
198  out << "numCompute: " << numCompute_ << endl;
199  out << "numApply: " << numApply_ << endl;
200  }
201 }
202 
203 template <class MatrixType>
206  matrix_.is_null(), std::runtime_error,
207  "Ifpack2::IdentitySolver::getDomainMap: "
208  "The matrix is null. Please call setMatrix() with a nonnull input "
209  "before calling this method.");
210  return matrix_->getDomainMap();
211 }
212 
213 template <class MatrixType>
216  matrix_.is_null(), std::runtime_error,
217  "Ifpack2::IdentitySolver::getRangeMap: "
218  "The matrix is null. Please call setMatrix() with a nonnull input "
219  "before calling this method.");
220  return matrix_->getRangeMap();
221 }
222 
223 template <class MatrixType>
226  // Check in serial or one-process mode if the matrix is square.
228  !A.is_null() && A->getComm()->getSize() == 1 &&
229  A->getLocalNumRows() != A->getLocalNumCols(),
230  std::runtime_error,
231  "Ifpack2::IdentitySolver::setMatrix: If A's communicator only "
232  "contains one process, then A must be square. Instead, you provided a "
233  "matrix A with "
234  << A->getLocalNumRows() << " rows and "
235  << A->getLocalNumCols() << " columns.");
236 
237  // It's legal for A to be null; in that case, you may not call
238  // initialize() until calling setMatrix() with a nonnull input.
239  // Regardless, setting the matrix invalidates the preconditioner.
240  isInitialized_ = false;
241  isComputed_ = false;
242  export_ = Teuchos::null;
243 
244  matrix_ = A;
245 }
246 
247 } // namespace Ifpack2
248 
249 #define IFPACK2_IDENTITYSOLVER_INSTANT(S, LO, GO, N) \
250  template class Ifpack2::IdentitySolver<Tpetra::RowMatrix<S, LO, GO, N> >;
251 
252 #endif // IFPACK2_IDENTITY_SOLVER_DEF_HPP
int getNumApply() const
Return the number of calls to apply().
Definition: Ifpack2_IdentitySolver_def.hpp:138
MatrixType::node_type node_type
Node type of the input matrix.
Definition: Ifpack2_IdentitySolver_decl.hpp:43
double getComputeTime() const
Return the time spent in compute().
Definition: Ifpack2_IdentitySolver_def.hpp:148
int getNumCompute() const
Return the number of calls to compute().
Definition: Ifpack2_IdentitySolver_def.hpp:133
virtual ~IdentitySolver()
Destructor.
Definition: Ifpack2_IdentitySolver_def.hpp:35
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:90
Teuchos::RCP< const map_type > getRangeMap() const
Return the Tpetra::Map object associated with the range of this operator.
Definition: Ifpack2_IdentitySolver_def.hpp:214
#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:39
void initialize()
Initialize.
Definition: Ifpack2_IdentitySolver_def.hpp:43
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:204
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:39
virtual void setMatrix(const Teuchos::RCP< const row_matrix_type > &A)
Set this preconditioner&#39;s matrix.
Definition: Ifpack2_IdentitySolver_def.hpp:225
std::string description() const
Return a simple one-line description of this object.
Definition: Ifpack2_IdentitySolver_def.hpp:158
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:186
void compute()
Compute the preconditioner.
Definition: Ifpack2_IdentitySolver_def.hpp:73
int getNumInitialize() const
Return the number of calls to initialize().
Definition: Ifpack2_IdentitySolver_def.hpp:128
MatrixType::global_ordinal_type global_ordinal_type
Type of the global indices of the input matrix.
Definition: Ifpack2_IdentitySolver_decl.hpp:41
double getInitializeTime() const
Return the time spent in initialize().
Definition: Ifpack2_IdentitySolver_def.hpp:143
MatrixType::scalar_type scalar_type
Type of the entries of the input matrix.
Definition: Ifpack2_IdentitySolver_decl.hpp:37
double getApplyTime() const
Return the time spent in apply().
Definition: Ifpack2_IdentitySolver_def.hpp:153
static std::string name()
bool is_null() const