43 #ifndef IFPACK2_DIAGONAL_DEF_HPP 44 #define IFPACK2_DIAGONAL_DEF_HPP 46 #include "Ifpack2_Diagonal_decl.hpp" 47 #include "Tpetra_CrsMatrix.hpp" 51 template<
class MatrixType>
52 Diagonal<MatrixType>::Diagonal (
const Teuchos::RCP<const row_matrix_type>& A) :
54 initializeTime_ (0.0),
60 isInitialized_ (false),
64 template<
class MatrixType>
65 Diagonal<MatrixType>::Diagonal (
const Teuchos::RCP<const crs_matrix_type>& A) :
67 initializeTime_ (0.0),
73 isInitialized_ (false),
77 template<
class MatrixType>
78 Diagonal<MatrixType>::Diagonal (
const Teuchos::RCP<const vector_type>& diag) :
79 userInverseDiag_ (diag),
81 initializeTime_ (0.0),
87 isInitialized_ (false),
91 template<
class MatrixType>
92 Diagonal<MatrixType>::~Diagonal ()
95 template<
class MatrixType>
96 Teuchos::RCP<const typename Diagonal<MatrixType>::map_type>
97 Diagonal<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 ();
114 template<
class MatrixType>
115 Teuchos::RCP<const typename Diagonal<MatrixType>::map_type>
116 Diagonal<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 ();
133 template<
class MatrixType>
134 void Diagonal<MatrixType>::
135 setParameters (
const Teuchos::ParameterList& )
138 template<
class MatrixType>
139 void Diagonal<MatrixType>::reset ()
141 inverseDiag_ = Teuchos::null;
142 offsets_ = offsets_type ();
143 isInitialized_ =
false;
147 template<
class MatrixType>
148 void Diagonal<MatrixType>::
149 setMatrix (
const Teuchos::RCP<const row_matrix_type>& A)
151 if (A.getRawPtr () != matrix_.getRawPtr ()) {
157 template<
class MatrixType>
158 void 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->getNodeNumRows ();
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;
199 template<
class MatrixType>
200 void 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;
244 template<
class MatrixType>
245 void Diagonal<MatrixType>::
246 apply (
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);
267 template <
class MatrixType>
268 int Diagonal<MatrixType>::getNumInitialize()
const {
269 return numInitialize_;
272 template <
class MatrixType>
273 int Diagonal<MatrixType>::getNumCompute()
const {
277 template <
class MatrixType>
278 int Diagonal<MatrixType>::getNumApply()
const {
282 template <
class MatrixType>
283 double Diagonal<MatrixType>::getInitializeTime()
const {
284 return initializeTime_;
287 template<
class MatrixType>
288 double Diagonal<MatrixType>::getComputeTime()
const {
292 template<
class MatrixType>
293 double Diagonal<MatrixType>::getApplyTime()
const {
297 template <
class MatrixType>
298 std::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 () <<
"]";
324 template <
class MatrixType>
325 void Diagonal<MatrixType>::
326 describe (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:72