10 #ifndef IFPACK2_DIAGONAL_DEF_HPP
11 #define IFPACK2_DIAGONAL_DEF_HPP
13 #include "Ifpack2_Diagonal_decl.hpp"
14 #include "Tpetra_CrsMatrix.hpp"
18 template<
class MatrixType>
21 initializeTime_ (0.0),
27 isInitialized_ (false),
31 template<
class MatrixType>
34 initializeTime_ (0.0),
40 isInitialized_ (false),
44 template<
class MatrixType>
46 userInverseDiag_ (diag),
48 initializeTime_ (0.0),
54 isInitialized_ (false),
58 template<
class MatrixType>
59 Diagonal<MatrixType>::~Diagonal ()
62 template<
class MatrixType>
64 Diagonal<MatrixType>::getDomainMap ()
const
66 if (matrix_.is_null ()) {
67 if (userInverseDiag_.is_null ()) {
69 true, std::runtime_error,
"Ifpack2::Diagonal::getDomainMap: "
70 "The input matrix A is null, and you did not provide a vector of "
71 "inverse diagonal entries. Please call setMatrix() with a nonnull "
72 "input matrix before calling this method.");
74 return userInverseDiag_->getMap ();
77 return matrix_->getDomainMap ();
81 template<
class MatrixType>
83 Diagonal<MatrixType>::getRangeMap ()
const
85 if (matrix_.is_null ()) {
86 if (userInverseDiag_.is_null ()) {
88 true, std::runtime_error,
"Ifpack2::Diagonal::getRangeMap: "
89 "The input matrix A is null, and you did not provide a vector of "
90 "inverse diagonal entries. Please call setMatrix() with a nonnull "
91 "input matrix before calling this method.");
93 return userInverseDiag_->getMap ();
96 return matrix_->getRangeMap ();
100 template<
class MatrixType>
101 void Diagonal<MatrixType>::
105 template<
class MatrixType>
106 void Diagonal<MatrixType>::reset ()
108 inverseDiag_ = Teuchos::null;
109 offsets_ = offsets_type ();
110 isInitialized_ =
false;
114 template<
class MatrixType>
115 void Diagonal<MatrixType>::
118 if (A.
getRawPtr () != matrix_.getRawPtr ()) {
124 template<
class MatrixType>
125 void Diagonal<MatrixType>::initialize ()
130 matrix_.is_null () && userInverseDiag_.is_null (), std::runtime_error,
131 "Ifpack2::Diagonal::initialize: The matrix to precondition is null, "
132 "and you did not provide a Tpetra::Vector of diagonal entries. "
133 "Please call setMatrix() with a nonnull input before calling this method.");
141 if (! matrix_.is_null ()) {
147 Teuchos::rcp_dynamic_cast<
const crs_matrix_type> (matrix_);
150 offsets_ = offsets_type ();
153 const size_t lclNumRows = A_crs->getLocalNumRows ();
154 if (offsets_.extent (0) < lclNumRows) {
155 offsets_ = offsets_type ();
156 offsets_ = offsets_type (
"offsets", lclNumRows);
158 A_crs->getCrsGraph ()->getLocalDiagOffsets (offsets_);
162 isInitialized_ =
true;
166 template<
class MatrixType>
167 void Diagonal<MatrixType>::compute ()
172 matrix_.is_null () && userInverseDiag_.is_null (), std::runtime_error,
173 "Ifpack2::Diagonal::compute: The matrix to precondition is null, "
174 "and you did not provide a Tpetra::Vector of diagonal entries. "
175 "Please call setMatrix() with a nonnull input before calling this method.");
177 if (! isInitialized_) {
187 if (matrix_.is_null ()) {
188 inverseDiag_ = userInverseDiag_;
193 Teuchos::rcp_dynamic_cast<
const crs_matrix_type> (matrix_);
196 matrix_->getLocalDiagCopy (*tmpVec);
201 A_crs->getLocalDiagCopy (*tmpVec, offsets_);
203 tmpVec->reciprocal (*tmpVec);
204 inverseDiag_ = tmpVec;
211 template<
class MatrixType>
212 void Diagonal<MatrixType>::
213 apply (
const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& X,
214 Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y,
217 scalar_type beta)
const
220 ! isComputed (), std::runtime_error,
"Ifpack2::Diagonal::apply: You "
221 "must first call compute() before you may call apply(). Once you have "
222 "called compute(), you need not call it again unless the values in the "
223 "matrix have changed, or unless you have called setMatrix().");
230 Y.elementWiseMultiply (alpha, *inverseDiag_, X, beta);
234 template <
class MatrixType>
235 int Diagonal<MatrixType>::getNumInitialize()
const {
236 return numInitialize_;
239 template <
class MatrixType>
240 int Diagonal<MatrixType>::getNumCompute()
const {
244 template <
class MatrixType>
245 int Diagonal<MatrixType>::getNumApply()
const {
249 template <
class MatrixType>
250 double Diagonal<MatrixType>::getInitializeTime()
const {
251 return initializeTime_;
254 template<
class MatrixType>
255 double Diagonal<MatrixType>::getComputeTime()
const {
259 template<
class MatrixType>
260 double Diagonal<MatrixType>::getApplyTime()
const {
264 template <
class MatrixType>
265 std::string Diagonal<MatrixType>::description ()
const
267 std::ostringstream out;
272 out <<
"\"Ifpack2::Diagonal\": "
274 if (this->getObjectLabel () !=
"") {
275 out <<
"Label: \"" << this->getObjectLabel () <<
"\", ";
277 if (matrix_.is_null ()) {
278 out <<
"Matrix: null";
281 out <<
"Matrix: not null"
282 <<
", Global matrix dimensions: ["
283 << matrix_->getGlobalNumRows () <<
", "
284 << matrix_->getGlobalNumCols () <<
"]";
291 template <
class MatrixType>
292 void Diagonal<MatrixType>::
302 out <<
"\"Ifpack2::Diagonal\":";
304 out <<
"Template parameter: "
306 if (this->getObjectLabel () !=
"") {
307 out <<
"Label: \"" << this->getObjectLabel () <<
"\", ";
309 out <<
"Number of initialize calls: " << numInitialize_ << endl
310 <<
"Number of compute calls: " << numCompute_ << endl
311 <<
"Number of apply calls: " << numApply_ << endl;
317 #define IFPACK2_DIAGONAL_INSTANT(S,LO,GO,N) \
318 template class Ifpack2::Diagonal< Tpetra::RowMatrix<S, LO, GO, N> >;
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
static std::string name()