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>
54 initializeTime_ (0.0),
60 isInitialized_ (false),
64 template<
class MatrixType>
67 initializeTime_ (0.0),
73 isInitialized_ (false),
77 template<
class MatrixType>
79 userInverseDiag_ (diag),
81 initializeTime_ (0.0),
87 isInitialized_ (false),
91 template<
class MatrixType>
92 Diagonal<MatrixType>::~Diagonal ()
95 template<
class MatrixType>
97 Diagonal<MatrixType>::getDomainMap ()
const
99 if (matrix_.is_null ()) {
100 if (userInverseDiag_.is_null ()) {
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>
116 Diagonal<MatrixType>::getRangeMap ()
const
118 if (matrix_.is_null ()) {
119 if (userInverseDiag_.is_null ()) {
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>::
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>::
151 if (A.
getRawPtr () != matrix_.getRawPtr ()) {
157 template<
class MatrixType>
158 void Diagonal<MatrixType>::initialize ()
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 ()) {
180 Teuchos::rcp_dynamic_cast<
const crs_matrix_type> (matrix_);
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 ()
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_;
226 Teuchos::rcp_dynamic_cast<
const crs_matrix_type> (matrix_);
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
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>::
335 out <<
"\"Ifpack2::Diagonal\":";
337 out <<
"Template parameter: "
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> >;
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
static std::string name()