43#ifndef IFPACK2_DIAGONAL_DEF_HPP
44#define IFPACK2_DIAGONAL_DEF_HPP
46#include "Ifpack2_Diagonal_decl.hpp"
47#include "Tpetra_CrsMatrix.hpp"
51template<
class MatrixType>
52Diagonal<MatrixType>::Diagonal (
const Teuchos::RCP<const row_matrix_type>& A) :
54 initializeTime_ (0.0),
60 isInitialized_ (false),
64template<
class MatrixType>
65Diagonal<MatrixType>::Diagonal (
const Teuchos::RCP<const crs_matrix_type>& A) :
67 initializeTime_ (0.0),
73 isInitialized_ (false),
77template<
class MatrixType>
78Diagonal<MatrixType>::Diagonal (
const Teuchos::RCP<const vector_type>& diag) :
79 userInverseDiag_ (diag),
81 initializeTime_ (0.0),
87 isInitialized_ (false),
91template<
class MatrixType>
92Diagonal<MatrixType>::~Diagonal ()
95template<
class MatrixType>
96Teuchos::RCP<const typename Diagonal<MatrixType>::map_type>
97Diagonal<MatrixType>::getDomainMap ()
const
99 if (matrix_.is_null ()) {
100 if (userInverseDiag_.is_null ()) {
101 TEUCHOS_TEST_FOR_EXCEPTION(
102 true, std::runtime_error,
"Ifpack2::Diagonal::getDomainMap: "
103 "The input matrix A is null, and you did not provide a vector of "
104 "inverse diagonal entries. Please call setMatrix() with a nonnull "
105 "input matrix before calling this method.");
107 return userInverseDiag_->getMap ();
110 return matrix_->getDomainMap ();
114template<
class MatrixType>
115Teuchos::RCP<const typename Diagonal<MatrixType>::map_type>
116Diagonal<MatrixType>::getRangeMap ()
const
118 if (matrix_.is_null ()) {
119 if (userInverseDiag_.is_null ()) {
120 TEUCHOS_TEST_FOR_EXCEPTION(
121 true, std::runtime_error,
"Ifpack2::Diagonal::getRangeMap: "
122 "The input matrix A is null, and you did not provide a vector of "
123 "inverse diagonal entries. Please call setMatrix() with a nonnull "
124 "input matrix before calling this method.");
126 return userInverseDiag_->getMap ();
129 return matrix_->getRangeMap ();
133template<
class MatrixType>
134void Diagonal<MatrixType>::
135setParameters (
const Teuchos::ParameterList& )
138template<
class MatrixType>
139void Diagonal<MatrixType>::reset ()
141 inverseDiag_ = Teuchos::null;
142 offsets_ = offsets_type ();
143 isInitialized_ =
false;
147template<
class MatrixType>
148void Diagonal<MatrixType>::
149setMatrix (
const Teuchos::RCP<const row_matrix_type>& A)
151 if (A.getRawPtr () != matrix_.getRawPtr ()) {
157template<
class MatrixType>
158void Diagonal<MatrixType>::initialize ()
162 TEUCHOS_TEST_FOR_EXCEPTION(
163 matrix_.is_null () && userInverseDiag_.is_null (), std::runtime_error,
164 "Ifpack2::Diagonal::initialize: The matrix to precondition is null, "
165 "and you did not provide a Tpetra::Vector of diagonal entries. "
166 "Please call setMatrix() with a nonnull input before calling this method.");
174 if (! matrix_.is_null ()) {
179 Teuchos::RCP<const crs_matrix_type> A_crs =
180 Teuchos::rcp_dynamic_cast<const crs_matrix_type> (matrix_);
182 if (A_crs.is_null ()) {
183 offsets_ = offsets_type ();
186 const size_t lclNumRows = A_crs->getLocalNumRows ();
187 if (offsets_.extent (0) < lclNumRows) {
188 offsets_ = offsets_type ();
189 offsets_ = offsets_type (
"offsets", lclNumRows);
191 A_crs->getCrsGraph ()->getLocalDiagOffsets (offsets_);
195 isInitialized_ =
true;
199template<
class MatrixType>
200void Diagonal<MatrixType>::compute ()
204 TEUCHOS_TEST_FOR_EXCEPTION(
205 matrix_.is_null () && userInverseDiag_.is_null (), std::runtime_error,
206 "Ifpack2::Diagonal::compute: The matrix to precondition is null, "
207 "and you did not provide a Tpetra::Vector of diagonal entries. "
208 "Please call setMatrix() with a nonnull input before calling this method.");
210 if (! isInitialized_) {
220 if (matrix_.is_null ()) {
221 inverseDiag_ = userInverseDiag_;
224 Teuchos::RCP<vector_type> tmpVec (
new vector_type (matrix_->getRowMap ()));
225 Teuchos::RCP<const crs_matrix_type> A_crs =
226 Teuchos::rcp_dynamic_cast<const crs_matrix_type> (matrix_);
227 if (A_crs.is_null ()) {
229 matrix_->getLocalDiagCopy (*tmpVec);
234 A_crs->getLocalDiagCopy (*tmpVec, offsets_);
236 tmpVec->reciprocal (*tmpVec);
237 inverseDiag_ = tmpVec;
244template<
class MatrixType>
245void Diagonal<MatrixType>::
246apply (
const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& X,
247 Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y,
250 scalar_type beta)
const
252 TEUCHOS_TEST_FOR_EXCEPTION(
253 ! isComputed (), std::runtime_error,
"Ifpack2::Diagonal::apply: You "
254 "must first call compute() before you may call apply(). Once you have "
255 "called compute(), you need not call it again unless the values in the "
256 "matrix have changed, or unless you have called setMatrix().");
263 Y.elementWiseMultiply (alpha, *inverseDiag_, X, beta);
267template <
class MatrixType>
268int Diagonal<MatrixType>::getNumInitialize()
const {
269 return numInitialize_;
272template <
class MatrixType>
273int Diagonal<MatrixType>::getNumCompute()
const {
277template <
class MatrixType>
278int Diagonal<MatrixType>::getNumApply()
const {
282template <
class MatrixType>
283double Diagonal<MatrixType>::getInitializeTime()
const {
284 return initializeTime_;
287template<
class MatrixType>
288double Diagonal<MatrixType>::getComputeTime()
const {
292template<
class MatrixType>
293double Diagonal<MatrixType>::getApplyTime()
const {
297template <
class MatrixType>
298std::string Diagonal<MatrixType>::description ()
const
300 std::ostringstream out;
305 out <<
"\"Ifpack2::Diagonal\": "
307 if (this->getObjectLabel () !=
"") {
308 out <<
"Label: \"" << this->getObjectLabel () <<
"\", ";
310 if (matrix_.is_null ()) {
311 out <<
"Matrix: null";
314 out <<
"Matrix: not null"
315 <<
", Global matrix dimensions: ["
316 << matrix_->getGlobalNumRows () <<
", "
317 << matrix_->getGlobalNumCols () <<
"]";
324template <
class MatrixType>
325void Diagonal<MatrixType>::
326describe (Teuchos::FancyOStream &out,
327 const Teuchos::EVerbosityLevel verbLevel)
const
331 const Teuchos::EVerbosityLevel vl =
332 (verbLevel == Teuchos::VERB_DEFAULT) ? Teuchos::VERB_LOW : verbLevel;
333 if (vl != Teuchos::VERB_NONE) {
334 Teuchos::OSTab tab0 (out);
335 out <<
"\"Ifpack2::Diagonal\":";
336 Teuchos::OSTab tab1 (out);
337 out <<
"Template parameter: "
338 << Teuchos::TypeNameTraits<MatrixType>::name () << endl;
339 if (this->getObjectLabel () !=
"") {
340 out <<
"Label: \"" << this->getObjectLabel () <<
"\", ";
342 out <<
"Number of initialize calls: " << numInitialize_ << endl
343 <<
"Number of compute calls: " << numCompute_ << endl
344 <<
"Number of apply calls: " << numApply_ << endl;
350#define IFPACK2_DIAGONAL_INSTANT(S,LO,GO,N) \
351 template class Ifpack2::Diagonal< Tpetra::RowMatrix<S, LO, GO, N> >;
Preconditioners and smoothers for Tpetra sparse matrices.
Definition: Ifpack2_AdditiveSchwarz_decl.hpp:74