43 #ifndef IFPACK2_SPARSITYFILTER_DEF_HPP 44 #define IFPACK2_SPARSITYFILTER_DEF_HPP 46 #include "Tpetra_Map.hpp" 47 #include "Tpetra_MultiVector.hpp" 48 #include "Tpetra_Vector.hpp" 56 template<
class MatrixType>
58 size_t AllowedNumEntries,
59 LocalOrdinal AllowedBandwidth):
61 AllowedNumEntries_(AllowedNumEntries),
62 AllowedBandwidth_(AllowedBandwidth),
70 TEUCHOS_TEST_FOR_EXCEPTION(
71 A_->getComm()->getSize() != 1 || A_->getNodeNumRows() != A_->getGlobalNumRows(),
72 std::runtime_error,
"Ifpack2::SparsityFilter: " 73 "This class may only be used when A.getComm()->getSize() == 1.");
76 NumRows_ = A_->getNodeNumRows();
81 NumEntries_.resize(NumRows_);
85 MaxNumEntries_ = A_->getNodeMaxNumRowEntries();
86 MaxNumEntriesA_ = A_->getNodeMaxNumRowEntries();
89 Indices_.resize(MaxNumEntries_);
90 Values_.resize(MaxNumEntries_);
92 size_t ActualMaxNumEntries = 0;
93 for (
size_t i = 0 ; i < NumRows_ ; ++i) {
94 NumEntries_[i] = MaxNumEntriesA_;
96 A_->getLocalRowCopy(i,Indices_,Values_,Nnz);
100 if (Nnz > ActualMaxNumEntries)
101 ActualMaxNumEntries = Nnz;
104 MaxNumEntries_ = ActualMaxNumEntries;
108 template<
class MatrixType>
112 template<
class MatrixType>
115 return A_->getComm();
119 template<
class MatrixType>
122 return A_->getNode();
126 template<
class MatrixType>
127 Teuchos::RCP<
const Tpetra::Map<
typename MatrixType::local_ordinal_type,
128 typename MatrixType::global_ordinal_type,
129 typename MatrixType::node_type> >
132 return A_->getRowMap();
136 template<
class MatrixType>
137 Teuchos::RCP<
const Tpetra::Map<
typename MatrixType::local_ordinal_type,
138 typename MatrixType::global_ordinal_type,
139 typename MatrixType::node_type> >
142 return A_->getColMap();
146 template<
class MatrixType>
147 Teuchos::RCP<
const Tpetra::Map<
typename MatrixType::local_ordinal_type,
148 typename MatrixType::global_ordinal_type,
149 typename MatrixType::node_type> >
152 return A_->getDomainMap();
156 template<
class MatrixType>
157 Teuchos::RCP<
const Tpetra::Map<
typename MatrixType::local_ordinal_type,
158 typename MatrixType::global_ordinal_type,
159 typename MatrixType::node_type> >
162 return A_->getRangeMap();
166 template<
class MatrixType>
167 Teuchos::RCP<
const Tpetra::RowGraph<
typename MatrixType::local_ordinal_type,
168 typename MatrixType::global_ordinal_type,
169 typename MatrixType::node_type> >
172 throw std::runtime_error(
"Ifpack2::SparsityFilter: does not support getGraph.");
176 template<
class MatrixType>
183 template<
class MatrixType>
190 template<
class MatrixType>
198 template<
class MatrixType>
205 template<
class MatrixType>
208 return A_->getIndexBase();
212 template<
class MatrixType>
219 template<
class MatrixType>
226 template<
class MatrixType>
229 throw std::runtime_error(
"Ifpack2::SparsityFilter does not implement getNumEntriesInGlobalRow.");
233 template<
class MatrixType>
236 return NumEntries_[localRow];
240 template<
class MatrixType>
243 return MaxNumEntries_;
247 template<
class MatrixType>
250 return MaxNumEntries_;
254 template<
class MatrixType>
261 template<
class MatrixType>
264 return A_->isLocallyIndexed();
268 template<
class MatrixType>
271 return A_->isGloballyIndexed();
275 template<
class MatrixType>
278 return A_->isFillComplete();
282 template<
class MatrixType>
284 const Teuchos::ArrayView<GlobalOrdinal> &,
285 const Teuchos::ArrayView<Scalar> &,
288 throw std::runtime_error(
"Ifpack2::SparsityFilter does not implement getGlobalRowCopy.");
292 template<
class MatrixType>
294 const Teuchos::ArrayView<LocalOrdinal> &Indices,
295 const Teuchos::ArrayView<Scalar> &Values,
296 size_t &NumEntries)
const 298 TEUCHOS_TEST_FOR_EXCEPTION((LocalRow < 0 || (
size_t) LocalRow >= NumRows_ || (
size_t) Indices.size() < NumEntries_[LocalRow]), std::runtime_error,
"Ifpack2::SparsityFilter::getLocalRowCopy invalid row or array size.");
306 size_t A_NumEntries=0;
307 A_->getLocalRowCopy(LocalRow,Indices_(),Values_(),A_NumEntries);
308 magnitudeType Threshold = Teuchos::ScalarTraits<magnitudeType>::zero();
309 std::vector<magnitudeType> Values2(A_NumEntries,Teuchos::ScalarTraits<magnitudeType>::zero());
311 if (A_NumEntries > AllowedNumEntries_) {
313 for (
size_t i = 0 ; i < A_NumEntries ; ++i) {
315 if (Indices_[i] == LocalRow)
318 Values2[count] = Teuchos::ScalarTraits<Scalar>::magnitude(Values_[i]);
323 std::sort(Values2.rbegin(),Values2.rend());
325 Threshold = Values2[AllowedNumEntries_ - 1];
333 for (
size_t i = 0 ; i < A_NumEntries ; ++i) {
334 if (std::abs(Indices_[i] - LocalRow) > AllowedBandwidth_)
337 if ((Indices_[i] != LocalRow) && (Teuchos::ScalarTraits<Scalar>::magnitude(Values_[i]) < Threshold))
340 Values[NumEntries] = Values_[i];
341 Indices[NumEntries] = Indices_[i];
344 if (NumEntries > AllowedNumEntries_)
351 template<
class MatrixType>
353 Teuchos::ArrayView<const GlobalOrdinal> &,
354 Teuchos::ArrayView<const Scalar> &)
const 356 throw std::runtime_error(
"Ifpack2::SparsityFilter: does not support getGlobalRowView.");
360 template<
class MatrixType>
362 Teuchos::ArrayView<const LocalOrdinal> &,
363 Teuchos::ArrayView<const Scalar> &)
const 365 throw std::runtime_error(
"Ifpack2::SparsityFilter: does not support getLocalRowView.");
369 template<
class MatrixType>
373 return A_->getLocalDiagCopy(diag);
377 template<
class MatrixType>
380 throw std::runtime_error(
"Ifpack2::SparsityFilter does not support leftScale.");
384 template<
class MatrixType>
387 throw std::runtime_error(
"Ifpack2::SparsityFilter does not support rightScale.");
391 template<
class MatrixType>
393 Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &Y,
394 Teuchos::ETransp mode,
400 TEUCHOS_TEST_FOR_EXCEPTION(X.getNumVectors() != Y.getNumVectors(), std::runtime_error,
401 "Ifpack2::SparsityFilter::apply ERROR: X.getNumVectors() != Y.getNumVectors().");
403 Scalar zero = Teuchos::ScalarTraits<Scalar>::zero();
404 Teuchos::ArrayRCP<Teuchos::ArrayRCP<const Scalar> > x_ptr = X.get2dView();
405 Teuchos::ArrayRCP<Teuchos::ArrayRCP<Scalar> > y_ptr = Y.get2dViewNonConst();
408 size_t NumVectors = Y.getNumVectors();
410 for (
size_t i = 0 ; i < NumRows_ ; ++i) {
413 getLocalRowCopy(i,Indices_(),Values_(),Nnz);
414 if (mode==Teuchos::NO_TRANS){
415 for (
size_t j = 0 ; j < Nnz ; ++j)
416 for (
size_t k = 0 ; k < NumVectors ; ++k)
417 y_ptr[k][i] += Values_[j] * x_ptr[k][Indices_[j]];
419 else if (mode==Teuchos::TRANS){
420 for (
size_t j = 0 ; j < Nnz ; ++j)
421 for (
size_t k = 0 ; k < NumVectors ; ++k)
422 y_ptr[k][Indices_[j]] += Values_[j] * x_ptr[k][i];
425 for (
size_t j = 0 ; j < Nnz ; ++j)
426 for (
size_t k = 0 ; k < NumVectors ; ++k)
427 y_ptr[k][Indices_[j]] += Teuchos::ScalarTraits<Scalar>::conjugate(Values_[j]) * x_ptr[k][i];
434 template<
class MatrixType>
441 template<
class MatrixType>
448 template<
class MatrixType>
451 throw std::runtime_error(
"Ifpack2::SparsityFilter does not implement getFrobeniusNorm.");
456 #define IFPACK2_SPARSITYFILTER_INSTANT(S,LO,GO,N) \ 457 template class Ifpack2::SparsityFilter< Tpetra::RowMatrix<S, LO, GO, N> >; virtual void getLocalDiagCopy(Tpetra::Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &diag) const
Get a copy of the diagonal entries owned by this node, with local row indices.
Definition: Ifpack2_SparsityFilter_def.hpp:370
virtual size_t getNodeNumCols() const
Returns the number of columns needed to apply the forward operator on this node, i.e., the number of elements listed in the column map.
Definition: Ifpack2_SparsityFilter_def.hpp:199
virtual size_t getGlobalMaxNumRowEntries() const
Returns the maximum number of entries across all rows/columns on all nodes.
Definition: Ifpack2_SparsityFilter_def.hpp:241
virtual global_size_t getGlobalNumRows() const
Returns the number of global rows in this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:177
virtual void getGlobalRowCopy(GlobalOrdinal GlobalRow, const Teuchos::ArrayView< GlobalOrdinal > &Indices, const Teuchos::ArrayView< Scalar > &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_SparsityFilter_def.hpp:283
virtual bool supportsRowViews() const
Returns true if RowViews are supported.
Definition: Ifpack2_SparsityFilter_def.hpp:442
virtual ~SparsityFilter()
Destructor.
Definition: Ifpack2_SparsityFilter_def.hpp:109
virtual bool hasColMap() const
Indicates whether this matrix has a well-defined column map.
Definition: Ifpack2_SparsityFilter_def.hpp:255
virtual Teuchos::RCP< const Teuchos::Comm< int > > getComm() const
Returns the communicator.
Definition: Ifpack2_SparsityFilter_def.hpp:113
SparsityFilter(const Teuchos::RCP< const row_matrix_type > &Matrix, size_t AllowedNumEntries, LocalOrdinal AllowedBandwidth=-Teuchos::ScalarTraits< LocalOrdinal >::one())
Constructor.
Definition: Ifpack2_SparsityFilter_def.hpp:57
virtual size_t getNumEntriesInGlobalRow(GlobalOrdinal globalRow) const
Returns the current number of entries on this node in the specified global row.
Definition: Ifpack2_SparsityFilter_def.hpp:227
virtual bool isGloballyIndexed() const
If matrix indices are in the global range, this function returns true. Otherwise, this function retur...
Definition: Ifpack2_SparsityFilter_def.hpp:269
virtual bool isLocallyIndexed() const
If matrix indices are in the local range, this function returns true. Otherwise, this function return...
Definition: Ifpack2_SparsityFilter_def.hpp:262
virtual void getLocalRowView(LocalOrdinal DropRow, Teuchos::ArrayView< const LocalOrdinal > &indices, Teuchos::ArrayView< const Scalar > &values) const
Extract a const, non-persisting view of local indices in a specified row of the matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:361
virtual size_t getNodeMaxNumRowEntries() const
Returns the maximum number of entries across all rows/columns on this node.
Definition: Ifpack2_SparsityFilter_def.hpp:248
virtual bool hasTransposeApply() const
Indicates whether this operator supports applying the adjoint operator.
Definition: Ifpack2_SparsityFilter_def.hpp:435
virtual bool isFillComplete() const
Returns true if fillComplete() has been called.
Definition: Ifpack2_SparsityFilter_def.hpp:276
virtual mag_type getFrobeniusNorm() const
Returns the Frobenius norm of the matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:449
virtual GlobalOrdinal getIndexBase() const
Returns the index base for global indices for this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:206
virtual global_size_t getGlobalNumEntries() const
Returns the global number of entries in this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:213
virtual size_t getNumEntriesInLocalRow(LocalOrdinal localRow) const
Returns the current number of entries on this node in the specified local row.
Definition: Ifpack2_SparsityFilter_def.hpp:234
virtual Teuchos::RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > getRangeMap() const
Returns the Map that describes the range distribution in this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:160
virtual void apply(const Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &X, Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, Scalar alpha=Teuchos::ScalarTraits< Scalar >::one(), Scalar beta=Teuchos::ScalarTraits< Scalar >::zero()) const
Computes the operator-multivector application.
Definition: Ifpack2_SparsityFilter_def.hpp:392
virtual void getGlobalRowView(GlobalOrdinal GlobalRow, Teuchos::ArrayView< const GlobalOrdinal > &indices, Teuchos::ArrayView< const Scalar > &values) const
Extract a const, non-persisting view of global indices in a specified row of the matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:352
virtual Teuchos::RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > getRowMap() const
Returns the Map that describes the row distribution in this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:130
virtual void getLocalRowCopy(LocalOrdinal DropRow, const Teuchos::ArrayView< LocalOrdinal > &Indices, const Teuchos::ArrayView< Scalar > &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_SparsityFilter_def.hpp:293
virtual void rightScale(const Tpetra::Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &x)
Scales the RowMatrix on the right with the Vector x.
Definition: Ifpack2_SparsityFilter_def.hpp:385
virtual void leftScale(const Tpetra::Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &x)
Scales the RowMatrix on the left with the Vector x.
Definition: Ifpack2_SparsityFilter_def.hpp:378
virtual global_size_t getGlobalNumCols() const
Returns the number of global columns in this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:184
Preconditioners and smoothers for Tpetra sparse matrices.
Definition: Ifpack2_AdditiveSchwarz_decl.hpp:72
virtual Teuchos::RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > getDomainMap() const
Returns the Map that describes the domain distribution in this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:150
virtual Teuchos::RCP< const Tpetra::RowGraph< LocalOrdinal, GlobalOrdinal, Node > > getGraph() const
Returns the RowGraph associated with this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:170
virtual size_t getNodeNumEntries() const
Returns the local number of entries in this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:220
virtual size_t getNodeNumRows() const
Returns the number of rows owned on the calling node.
Definition: Ifpack2_SparsityFilter_def.hpp:191
virtual Teuchos::RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > getColMap() const
Returns the Map that describes the column distribution in this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:140
virtual Teuchos::RCP< Node > getNode() const
Returns the underlying node.
Definition: Ifpack2_SparsityFilter_def.hpp:120