Ifpack2 Templated Preconditioning Package  Version 1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Ifpack2_Diagonal_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 #ifndef IFPACK2_DIAGONAL_DEF_HPP
44 #define IFPACK2_DIAGONAL_DEF_HPP
45 
46 #include "Ifpack2_Diagonal_decl.hpp"
47 #include "Ifpack2_Condest.hpp"
48 
49 namespace Ifpack2 {
50 
51 template<class MatrixType>
53  : isInitialized_(false),
54  isComputed_(false),
55  domainMap_(A->getDomainMap()),
56  rangeMap_(A->getRangeMap()),
57  matrix_(A),
58  inversediag_(),
59  numInitialize_(0),
60  numCompute_(0),
61  numApply_(0),
62  condEst_ (-Teuchos::ScalarTraits<magnitudeType>::one ())
63 {
64 }
65 
66 template<class MatrixType>
67 Diagonal<MatrixType>::Diagonal(const Teuchos::RCP<const Tpetra::Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node> >& diag)
68  : isInitialized_(false),
69  isComputed_(false),
70  domainMap_(),
71  rangeMap_(),
72  matrix_(),
73  inversediag_(diag),
74  numInitialize_(0),
75  numCompute_(0),
76  numApply_(0),
77  condEst_ (-Teuchos::ScalarTraits<magnitudeType>::one ())
78 {
79 }
80 
81 template<class MatrixType>
83 {
84 }
85 
86 template<class MatrixType>
88 {
89 }
90 
91 template<class MatrixType>
93 {
94  if (isInitialized_) return;
95 
96  // Precompute diagonal offsets so we don't have to search for them later.
97  if (matrix_ != Teuchos::null) {
98  matrix_->getLocalDiagOffsets(offsets_);
99  }
100 
101  isInitialized_ = true;
102  ++numInitialize_;
103 }
104 
105 template<class MatrixType>
107 {
108  if (!isInitialized_) initialize();
109 
110  isComputed_ = false;
111 
112  if (matrix_ == Teuchos::null) {
113  isComputed_ = true;
114  return;
115  }
116 
117  // Get the diagonal entries using the precomputed offsets and invert them.
118  Teuchos::RCP<Tpetra::Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node> > tmp_vec = Teuchos::rcp(new Tpetra::Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>(matrix_->getRowMap()));
119  matrix_->getLocalDiagCopy(*tmp_vec, offsets_());
120  tmp_vec->reciprocal(*tmp_vec);
121 
122  inversediag_ = tmp_vec;
123 
124  isComputed_ = true;
125  ++numCompute_;
126 }
127 
128 template<class MatrixType>
129 void Diagonal<MatrixType>::apply(const Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& X,
130  Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& Y,
131  Teuchos::ETransp /*mode*/,
132  Scalar alpha,
133  Scalar beta) const
134 {
135  // This method will not just call applyTempl() for now to avoid doing the extra work of
136  // copying data to intermediate vectors to convert scalar types.
137 
138  TEUCHOS_TEST_FOR_EXCEPTION(!isComputed(), std::runtime_error,
139  "Ifpack2::Diagonal::apply() ERROR, compute() hasn't been called yet.");
140 
141  ++numApply_;
142  Y.elementWiseMultiply(alpha, *inversediag_, X, beta);
143 }
144 
145 template<class MatrixType>
146 template<class DomainScalar, class RangeScalar>
147 void Diagonal<MatrixType>::applyTempl(const Tpetra::MultiVector<DomainScalar,LocalOrdinal,GlobalOrdinal,Node>& X,
148  Tpetra::MultiVector<RangeScalar,LocalOrdinal,GlobalOrdinal,Node>& Y,
149  Teuchos::ETransp /*mode*/,
150  RangeScalar alpha,
151  RangeScalar beta) const
152 {
153  typedef typename Tpetra::MultiVector<RangeScalar,LocalOrdinal,GlobalOrdinal,Node> RangeMultiVectorType;
154  typedef typename Tpetra::Vector<RangeScalar,LocalOrdinal,GlobalOrdinal,Node> RangeVectorType;
155 
156  TEUCHOS_TEST_FOR_EXCEPTION(!isComputed(), std::runtime_error,
157  "Ifpack2::Diagonal::apply() ERROR, compute() hasn't been called yet.");
158 
159  TEUCHOS_TEST_FOR_EXCEPTION(X.getNumVectors() != Y.getNumVectors(), std::runtime_error,
160  "Ifpack2::Diagonal::apply() ERROR: X.getNumVectors() != Y.getNumVectors().");
161 
162  ++numApply_;
163 
164  Teuchos::RCP<RangeMultiVectorType> Xtmp = rcp(new RangeMultiVectorType(X.getMap(), X.getNumVectors()));
165  Teuchos::RCP<RangeVectorType> invtmp = rcp(new RangeVectorType(inversediag_->getMap()));
166 
167  for (size_t j = 0; j < X.getNumVectors(); ++j) {
168  Teuchos::ArrayRCP<const DomainScalar> xVals = X.getVector( j )->get1dView();
169  Teuchos::ArrayRCP<RangeScalar> xValsRange = Xtmp->getVectorNonConst( j )->get1dViewNonConst();
170  if( xVals.size() ) {
171  std::transform( xVals.begin(), xVals.end(), xValsRange.begin(), Teuchos::asFunc<RangeScalar>() );
172  }
173  }
174  Teuchos::ArrayRCP<const Scalar> invVals = inversediag_->get1dView();
175  Teuchos::ArrayRCP<RangeScalar> invValsRange = invtmp->get1dViewNonConst();
176  if( invVals.size() ) {
177  std::transform( invVals.begin(), invVals.end(), invValsRange.begin(), Teuchos::asFunc<RangeScalar>() );
178  }
179 
180  Y.elementWiseMultiply(alpha, *invtmp, *Xtmp, beta);
181 }
182 
183 template<class MatrixType>
186  CondestType CT,
187  LocalOrdinal MaxIters,
188  magnitudeType Tol,
189  const Teuchos::Ptr<const Tpetra::RowMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> > &matrix)
190 {
191  const magnitudeType minusOne = Teuchos::ScalarTraits<magnitudeType>::one ();
192 
193  if (!isComputed()) { // cannot compute right now
194  return minusOne;
195  }
196  // NOTE: this is computing the *local* condest
197  if (condEst_ == minusOne) {
198  condEst_ = Ifpack2::Condest(*this, CT, MaxIters, Tol, matrix);
199  }
200  return(condEst_);
201 }
202 
203 template <class MatrixType>
205  return(numInitialize_);
206 }
207 
208 template <class MatrixType>
210  return(numCompute_);
211 }
212 
213 template <class MatrixType>
215  return(numApply_);
216 }
217 
218 template <class MatrixType>
220  return(initializeTime_);
221 }
222 
223 template<class MatrixType>
225  return(computeTime_);
226 }
227 
228 template<class MatrixType>
230  return(applyTime_);
231 }
232 
233 template <class MatrixType>
235 {
236  return std::string("Ifpack2::Diagonal");
237 }
238 
239 template <class MatrixType>
241 {
242  if (verbLevel != Teuchos::VERB_NONE) {
243  out << this->description() << std::endl;
244  out << " numApply: " << numApply_ << std::endl;
245  }
246 }
247 
248 }//namespace Ifpack2
249 
250 #endif
std::string description() const
Return a simple one-line description of this object.
Definition: Ifpack2_Diagonal_def.hpp:234
CondestType
Ifpack2::CondestType: enum to define the type of condition number estimate.
Definition: Ifpack2_CondestType.hpp:50
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
size_type size() const
magnitudeType computeCondEst(CondestType CT=Cheap, LocalOrdinal MaxIters=1550, magnitudeType Tol=1e-9, const Teuchos::Ptr< const Tpetra::RowMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &matrix=Teuchos::null)
Computes the estimated condition number and returns the value.
Definition: Ifpack2_Diagonal_def.hpp:185
void apply(const Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &X, Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, Scalar alpha=Teuchos::ScalarTraits< Scalar >::one(), Scalar beta=Teuchos::ScalarTraits< Scalar >::zero()) const
Applies the preconditioner to X, returns the result in Y.
Definition: Ifpack2_Diagonal_def.hpp:129
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void compute()
compute the preconditioner
Definition: Ifpack2_Diagonal_def.hpp:106
void applyTempl(const Tpetra::MultiVector< DomainScalar, LocalOrdinal, GlobalOrdinal, Node > &X, Tpetra::MultiVector< RangeScalar, LocalOrdinal, GlobalOrdinal, Node > &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, RangeScalar alpha=Teuchos::ScalarTraits< Scalar >::one(), RangeScalar beta=Teuchos::ScalarTraits< Scalar >::zero()) const
Applies the preconditioner to X, returns the result in Y.
Definition: Ifpack2_Diagonal_def.hpp:147
virtual ~Diagonal()
Destructor.
Definition: Ifpack2_Diagonal_def.hpp:82
double getComputeTime() const
Returns the time spent in compute().
Definition: Ifpack2_Diagonal_def.hpp:224
int getNumCompute() const
Returns the number of calls to compute().
Definition: Ifpack2_Diagonal_def.hpp:209
double getInitializeTime() const
Returns the time spent in initialize().
Definition: Ifpack2_Diagonal_def.hpp:219
void setParameters(const Teuchos::ParameterList &params)
Sets parameters on this object.
Definition: Ifpack2_Diagonal_def.hpp:87
Diagonal(const Teuchos::RCP< const MatrixType > &A)
Constructor to create a Diagonal preconditioner using a Tpetra::CrsMatrix.
Definition: Ifpack2_Diagonal_def.hpp:52
int getNumInitialize() const
Returns the number of calls to initialize().
Definition: Ifpack2_Diagonal_def.hpp:204
double getApplyTime() const
Returns the time spent in apply().
Definition: Ifpack2_Diagonal_def.hpp:229
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_Diagonal_def.hpp:240
iterator end() const
void initialize()
Initialize.
Definition: Ifpack2_Diagonal_def.hpp:92
iterator begin() const
int getNumApply() const
Returns the number of calls to apply().
Definition: Ifpack2_Diagonal_def.hpp:214