43 #ifndef IFPACK2_LOCALFILTER_DEF_HPP
44 #define IFPACK2_LOCALFILTER_DEF_HPP
45 #include "Ifpack2_LocalFilter_decl.hpp"
48 #include "Tpetra_ConfigDefs.hpp"
49 #include "Tpetra_RowMatrix.hpp"
50 #include "Tpetra_Map.hpp"
51 #include "Tpetra_MultiVector.hpp"
52 #include "Tpetra_Vector.hpp"
56 #include "Teuchos_DefaultMpiComm.hpp"
58 #include "Teuchos_DefaultSerialComm.hpp"
63 template<
class MatrixType>
65 LocalFilter (
const Teuchos::RCP<
const Tpetra::RowMatrix<scalar_type,local_ordinal_type,global_ordinal_type,node_type> >& Matrix) :
74 typedef Tpetra::Map<local_ordinal_type, global_ordinal_type, node_type> map_type;
77 RCP<const Teuchos::Comm<int> > localComm;
86 NumRows_ = A_->getNodeNumRows();
89 LocalMap_ =
rcp (
new map_type (NumRows_, 0, localComm));
94 NumEntries_.resize(NumRows_);
98 MaxNumEntries_ = A_->getNodeMaxNumRowEntries();
99 MaxNumEntriesA_ = A_->getNodeMaxNumRowEntries();
102 Indices_.
resize(MaxNumEntries_);
103 Values_.
resize(MaxNumEntries_);
112 size_t ActualMaxNumEntries = 0;
114 for (
size_t i = 0 ; i < NumRows_ ; ++i) {
117 size_t Nnz, NewNnz = 0;
118 A_->getLocalRowCopy(i,Indices_,Values_,Nnz);
119 for (
size_t j = 0 ; j < Nnz ; ++j) {
131 if ((
size_t) Indices_[j] < NumRows_ ) ++NewNnz;
134 if (NewNnz > ActualMaxNumEntries)
135 ActualMaxNumEntries = NewNnz;
137 NumNonzeros_ += NewNnz;
138 NumEntries_[i] = NewNnz;
142 MaxNumEntries_ = ActualMaxNumEntries;
146 template<
class MatrixType>
150 template<
class MatrixType>
153 return LocalMap_->getComm ();
157 template<
class MatrixType>
160 return A_->getNode();
164 template<
class MatrixType>
165 const Teuchos::RCP<
const Tpetra::Map<
typename MatrixType::local_ordinal_type,
166 typename MatrixType::global_ordinal_type,
167 typename MatrixType::node_type> >&
174 template<
class MatrixType>
175 const Teuchos::RCP<
const Tpetra::Map<
typename MatrixType::local_ordinal_type,
176 typename MatrixType::global_ordinal_type,
177 typename MatrixType::node_type> >&
184 template<
class MatrixType>
185 const Teuchos::RCP<
const Tpetra::Map<
typename MatrixType::local_ordinal_type,
186 typename MatrixType::global_ordinal_type,
187 typename MatrixType::node_type> >&
194 template<
class MatrixType>
195 const Teuchos::RCP<
const Tpetra::Map<
typename MatrixType::local_ordinal_type,
196 typename MatrixType::global_ordinal_type,
197 typename MatrixType::node_type> >&
204 template<
class MatrixType>
205 Teuchos::RCP<
const Tpetra::RowGraph<
typename MatrixType::local_ordinal_type,
206 typename MatrixType::global_ordinal_type,
207 typename MatrixType::node_type> >
210 throw std::runtime_error(
"Ifpack2::LocalFilter: does not support getGraph.");
214 template<
class MatrixType>
221 template<
class MatrixType>
228 template<
class MatrixType>
236 template<
class MatrixType>
243 template<
class MatrixType>
246 return A_->getIndexBase();
250 template<
class MatrixType>
257 template<
class MatrixType>
264 template<
class MatrixType>
267 throw std::runtime_error(
"Ifpack2::LocalFilter does not implement getNumEntriesInGlobalRow.");
271 template<
class MatrixType>
274 return NumEntries_[localRow];
278 template<
class MatrixType>
281 return A_->getGlobalNumDiags();
285 template<
class MatrixType>
288 return A_->getNodeNumDiags();
292 template<
class MatrixType>
295 return MaxNumEntries_;
299 template<
class MatrixType>
302 return MaxNumEntries_;
306 template<
class MatrixType>
313 template<
class MatrixType>
316 return A_->isLowerTriangular();
320 template<
class MatrixType>
323 return A_->isUpperTriangular();
327 template<
class MatrixType>
330 return A_->isLocallyIndexed();
334 template<
class MatrixType>
337 return A_->isGloballyIndexed();
341 template<
class MatrixType>
344 return A_->isFillComplete();
348 template<
class MatrixType>
352 size_t &NumEntries)
const
354 throw std::runtime_error(
"Ifpack2::LocalFilter does not implement getGlobalRowCopy.");
358 template<
class MatrixType>
362 size_t &NumEntries)
const
365 NumRows_ == 0, std::invalid_argument,
366 "Ifpack2::LocalFilter::getLocalRowCopy: Invalid local row index "
367 << LocalRow <<
". This process " << LocalMap_->getComm ()->getSize ()
368 <<
" owns no rows of the matrix.");
370 LocalRow < 0 || (
size_t) LocalRow >= NumRows_,
371 std::invalid_argument,
372 "Ifpack2::LocalFilter::getLocalRowCopy: Invalid local row index "
373 << LocalRow <<
". The valid range of row indices on this process "
374 << LocalMap_->getComm ()->getSize () <<
" is "
375 <<
"[0, " << (NumRows_ - 1) <<
"].");
376 TEUCHOS_TEST_FOR_EXCEPTION(
377 (
size_t) Indices.
size() < NumEntries_[LocalRow], std::runtime_error,
378 "Ifpack2::LocalFilter::getLocalRowCopy: Invalid output array length. "
379 "The output arrays must each have length at least " << NumEntries_[LocalRow]
380 <<
" for local row " << LocalRow <<
" on process "
381 << LocalMap_->getComm ()->getSize () <<
".");
383 size_t A_NumEntries=0;
389 A_->getLocalRowCopy (LocalRow, Indices_ (), Values_ (), A_NumEntries);
393 for (
size_t j = 0 ; j < A_NumEntries; ++j) {
395 if ((
size_t) Indices_[j] < NumRows_) {
396 Indices[NumEntries] = Indices_[j];
397 Values[NumEntries] = Values_[j];
404 template<
class MatrixType>
409 throw std::runtime_error(
"Ifpack2::LocalFilter: does not support getGlobalRowView.");
413 template<
class MatrixType>
418 throw std::runtime_error(
"Ifpack2::LocalFilter: does not support getLocalRowView.");
422 template<
class MatrixType>
425 Tpetra::Vector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> temp(A_->getRowMap());
426 A_->getLocalDiagCopy(temp);
430 for(
size_t i=0; i<NumRows_; i++)
431 d_ptr[0][i] = t_ptr[0][i];
435 template<
class MatrixType>
438 throw std::runtime_error(
"Ifpack2::LocalFilter does not support leftScale.");
442 template<
class MatrixType>
445 throw std::runtime_error(
"Ifpack2::LocalFilter does not support rightScale.");
449 template<
class MatrixType>
451 Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> &Y,
461 "Ifpack2::LocalFilter::apply ERROR: X.getNumVectors() != Y.getNumVectors().");
464 X.getNumVectors() != Y.getNumVectors(), std::runtime_error,
465 "Ifpack2::LocalFilter::apply: X and Y must have the same number of columns. "
466 "X has " << X.getNumVectors () <<
" columns, but Y has "
467 << Y.getNumVectors () <<
" columns.");
470 alpha != STS::one (), std::logic_error,
471 "Ifpack2::LocalFilter::apply: This method does not currently work when alpha != 1.");
473 TEUCHOS_TEST_FOR_EXCEPTION(
474 beta != STS::zero (), std::logic_error,
475 "Ifpack2::LocalFilter::apply: This method does not currently work when beta != 0.");
482 size_t NumVectors = Y.getNumVectors();
485 for (
size_t i = 0 ; i < NumRows_ ; ++i) {
488 getLocalRowCopy(i,Indices_(),Values_(),Nnz);
490 for (
size_t j = 0 ; j < Nnz ; ++j)
491 for (
size_t k = 0 ; k < NumVectors ; ++k)
492 y_ptr[k][i] += Values_[j] * x_ptr[k][Indices_[j]];
495 for (
size_t j = 0 ; j < Nnz ; ++j)
496 for (
size_t k = 0 ; k < NumVectors ; ++k)
497 y_ptr[k][Indices_[j]] += Values_[j] * x_ptr[k][i];
500 for (
size_t j = 0 ; j < Nnz ; ++j)
501 for (
size_t k = 0 ; k < NumVectors ; ++k)
502 y_ptr[k][Indices_[j]] += STS::conjugate(Values_[j]) * x_ptr[k][i];
509 template<
class MatrixType>
516 template<
class MatrixType>
523 template<
class MatrixType>
531 const size_t numRows = NumRows_;
535 if (STS::isComplex) {
536 for (
size_t i = 0; i < numRows; ++i) {
537 size_t numEntries = 0;
538 this->getLocalRowCopy (i, ind, val, numEntries);
539 for (
size_t k = 0; k < numEntries; ++k) {
540 sumSquared += STS::real (val[k]) * STS::real (val[k]) +
541 STS::imag (val[k]) * STS::imag (val[k]);
546 for (
size_t i = 0; i < numRows; ++i) {
547 size_t numEntries = 0;
548 this->getLocalRowCopy (i, ind, val, numEntries);
549 for (
size_t k = 0; k < numEntries; ++k) {
550 sumSquared += STS::magnitude(val[k]) * STS::magnitude(val[k]);
554 return STM::squareroot (sumSquared);
558 template<
class MatrixType>
566 throw std::runtime_error(
"Ifpack2::LocalFilter does not implement getGlobalRowView.");
570 template<
class MatrixType>
578 throw std::runtime_error(
"Ifpack2::LocalFilter does not implement getLocalRowView.");
virtual void rightScale(const Tpetra::Vector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > &x)
Scales the RowMatrix on the right with the Vector x.
Definition: Ifpack2_LocalFilter_def.hpp:443
virtual size_t getNodeNumDiags() const
The number of local diagonal entries, based on global row/column index comparisons.
Definition: Ifpack2_LocalFilter_def.hpp:286
virtual magnitude_type getFrobeniusNorm() const
The Frobenius norm of the (locally filtered) matrix.
Definition: Ifpack2_LocalFilter_def.hpp:524
virtual const Teuchos::RCP< const Tpetra::Map< local_ordinal_type, global_ordinal_type, node_type > > & getRowMap() const
Returns the Map that describes the row distribution in this matrix.
Definition: Ifpack2_LocalFilter_def.hpp:168
virtual Teuchos::RCP< node_type > getNode() const
Returns the underlying Kokkos Node object.
Definition: Ifpack2_LocalFilter_def.hpp:158
virtual size_t getNodeNumRows() const
The number of rows owned on the calling process.
Definition: Ifpack2_LocalFilter_def.hpp:229
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
virtual bool isFillComplete() const
Returns true if fillComplete() has been called.
Definition: Ifpack2_LocalFilter_def.hpp:342
virtual size_t getNodeMaxNumRowEntries() const
The maximum number of entries across all rows/columns on this process.
Definition: Ifpack2_LocalFilter_def.hpp:300
virtual void apply(const Tpetra::MultiVector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > &X, Tpetra::MultiVector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, scalar_type alpha=Teuchos::ScalarTraits< scalar_type >::one(), scalar_type beta=Teuchos::ScalarTraits< scalar_type >::zero()) const
Computes the operator-multivector application.
Definition: Ifpack2_LocalFilter_def.hpp:450
MatrixType::global_ordinal_type global_ordinal_type
The type of global indices in the input MatrixType.
Definition: Ifpack2_LocalFilter_decl.hpp:125
virtual bool hasTransposeApply() const
Indicates whether this operator supports applying the adjoint operator.
Definition: Ifpack2_LocalFilter_def.hpp:510
virtual bool isLocallyIndexed() const
Whether the underlying sparse matrix is locally (opposite of globally) indexed.
Definition: Ifpack2_LocalFilter_def.hpp:328
virtual void getGlobalRowView(global_ordinal_type GlobalRow, Teuchos::ArrayView< const global_ordinal_type > &indices, Teuchos::ArrayView< const scalar_type > &values) const
Extract a const, non-persisting view of global indices in a specified row of the matrix.
Definition: Ifpack2_LocalFilter_def.hpp:405
virtual void getLocalDiagCopy(Tpetra::Vector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > &diag) const
Get a copy of the diagonal entries owned by this node, with local row indices.
Definition: Ifpack2_LocalFilter_def.hpp:423
virtual const Teuchos::RCP< const Teuchos::Comm< int > > & getComm() const
Returns the communicator.
Definition: Ifpack2_LocalFilter_def.hpp:151
virtual Teuchos::RCP< const Tpetra::RowGraph< local_ordinal_type, global_ordinal_type, node_type > > getGraph() const
The (locally filtered) matrix's graph.
Definition: Ifpack2_LocalFilter_def.hpp:208
MatrixType::local_ordinal_type local_ordinal_type
The type of local indices in the input MatrixType.
Definition: Ifpack2_LocalFilter_decl.hpp:118
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
virtual bool supportsRowViews() const
Returns true if RowViews are supported.
Definition: Ifpack2_LocalFilter_def.hpp:517
virtual global_size_t getGlobalNumDiags() const
The number of global diagonal entries, based on global row/column index comparisons.
Definition: Ifpack2_LocalFilter_def.hpp:279
virtual const Teuchos::RCP< const Tpetra::Map< local_ordinal_type, global_ordinal_type, node_type > > & getColMap() const
Returns the Map that describes the column distribution in this matrix.
Definition: Ifpack2_LocalFilter_def.hpp:178
virtual size_t getNumEntriesInGlobalRow(global_ordinal_type globalRow) const
The current number of entries on this node in the specified global row.
Definition: Ifpack2_LocalFilter_def.hpp:265
virtual void leftScale(const Tpetra::Vector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > &x)
Scales the RowMatrix on the left with the Vector x.
Definition: Ifpack2_LocalFilter_def.hpp:436
virtual global_ordinal_type getIndexBase() const
Returns the index base for global indices for this matrix.
Definition: Ifpack2_LocalFilter_def.hpp:244
virtual bool isGloballyIndexed() const
Whether the underlying sparse matrix is globally (opposite of locally) indexed.
Definition: Ifpack2_LocalFilter_def.hpp:335
virtual bool isUpperTriangular() const
Whether this matrix is upper triangular.
Definition: Ifpack2_LocalFilter_def.hpp:321
virtual size_t getGlobalMaxNumRowEntries() const
The maximum number of entries across all rows/columns on all processes.
Definition: Ifpack2_LocalFilter_def.hpp:293
virtual void getLocalRowCopy(local_ordinal_type LocalRow, const Teuchos::ArrayView< local_ordinal_type > &Indices, const Teuchos::ArrayView< scalar_type > &Values, size_t &NumEntries) const
Extract a list of entries in a specified local row of the graph. Put into storage allocated by callin...
Definition: Ifpack2_LocalFilter_def.hpp:359
void resize(size_type new_size, const value_type &x=value_type())
virtual size_t getNodeNumCols() const
The number of columns in the (locally filtered) matrix.
Definition: Ifpack2_LocalFilter_def.hpp:237
LocalFilter(const Teuchos::RCP< const Tpetra::RowMatrix< scalar_type, local_ordinal_type, global_ordinal_type, node_type > > &Matrix)
Constructor.
Definition: Ifpack2_LocalFilter_def.hpp:65
virtual ~LocalFilter()
Destructor.
Definition: Ifpack2_LocalFilter_def.hpp:147
virtual global_size_t getGlobalNumRows() const
The number of global rows in this matrix.
Definition: Ifpack2_LocalFilter_def.hpp:215
MatrixType::scalar_type scalar_type
The type of the entries of the input MatrixType.
Definition: Ifpack2_LocalFilter_decl.hpp:111
virtual size_t getNodeNumEntries() const
Returns the local number of entries in this matrix.
Definition: Ifpack2_LocalFilter_def.hpp:258
virtual size_t getNumEntriesInLocalRow(local_ordinal_type localRow) const
The current number of entries on this node in the specified local row.
Definition: Ifpack2_LocalFilter_def.hpp:272
Teuchos::ScalarTraits< scalar_type >::magnitudeType magnitude_type
The type of the magnitude (absolute value) of a matrix entry.
Definition: Ifpack2_LocalFilter_decl.hpp:139
virtual const Teuchos::RCP< const Tpetra::Map< local_ordinal_type, global_ordinal_type, node_type > > & getDomainMap() const
Returns the Map that describes the domain distribution in this matrix.
Definition: Ifpack2_LocalFilter_def.hpp:188
virtual global_size_t getGlobalNumEntries() const
Returns the global number of entries in this matrix.
Definition: Ifpack2_LocalFilter_def.hpp:251
virtual void getGlobalRowCopy(global_ordinal_type GlobalRow, const Teuchos::ArrayView< global_ordinal_type > &Indices, const Teuchos::ArrayView< scalar_type > &Values, size_t &NumEntries) const
Extract a list of entries in a specified global row of this matrix. Put into pre-allocated storage...
Definition: Ifpack2_LocalFilter_def.hpp:349
virtual global_size_t getGlobalNumCols() const
The number of global columns in this matrix.
Definition: Ifpack2_LocalFilter_def.hpp:222
virtual bool isLowerTriangular() const
Whether this matrix is lower triangular.
Definition: Ifpack2_LocalFilter_def.hpp:314
virtual void getLocalRowView(local_ordinal_type LocalRow, Teuchos::ArrayView< const local_ordinal_type > &indices, Teuchos::ArrayView< const scalar_type > &values) const
Extract a const, non-persisting view of local indices in a specified row of the matrix.
Definition: Ifpack2_LocalFilter_def.hpp:414
virtual bool hasColMap() const
Whether this matrix has a well-defined column Map.
Definition: Ifpack2_LocalFilter_def.hpp:307
virtual const Teuchos::RCP< const Tpetra::Map< local_ordinal_type, global_ordinal_type, node_type > > & getRangeMap() const
Returns the Map that describes the range distribution in this matrix.
Definition: Ifpack2_LocalFilter_def.hpp:198