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