10#ifndef IFPACK2_SPARSITYFILTER_DEF_HPP
11#define IFPACK2_SPARSITYFILTER_DEF_HPP
13#include "Tpetra_Map.hpp"
14#include "Tpetra_MultiVector.hpp"
15#include "Tpetra_Vector.hpp"
23template<
class MatrixType>
25 size_t AllowedNumEntries,
26 LocalOrdinal AllowedBandwidth):
28 AllowedNumEntries_(AllowedNumEntries),
29 AllowedBandwidth_(AllowedBandwidth),
37 TEUCHOS_TEST_FOR_EXCEPTION(
38 A_->getComm()->getSize() != 1 || A_->getLocalNumRows() != A_->getGlobalNumRows(),
39 std::runtime_error,
"Ifpack2::SparsityFilter: "
40 "This class may only be used when A.getComm()->getSize() == 1.");
43 NumRows_ = A_->getLocalNumRows();
48 NumEntries_.resize(NumRows_);
52 MaxNumEntries_ = A_->getLocalMaxNumRowEntries();
53 MaxNumEntriesA_ = A_->getLocalMaxNumRowEntries();
56 Kokkos::resize(Indices_,MaxNumEntries_);
57 Kokkos::resize(Values_,MaxNumEntries_);
59 size_t ActualMaxNumEntries = 0;
60 for (
size_t i = 0 ; i < NumRows_ ; ++i) {
61 NumEntries_[i] = MaxNumEntriesA_;
63 A_->getLocalRowCopy(i,Indices_,Values_,Nnz);
67 if (Nnz > ActualMaxNumEntries)
68 ActualMaxNumEntries = Nnz;
71 MaxNumEntries_ = ActualMaxNumEntries;
75template<
class MatrixType>
79template<
class MatrixType>
87template<
class MatrixType>
88Teuchos::RCP<
const Tpetra::Map<
typename MatrixType::local_ordinal_type,
89 typename MatrixType::global_ordinal_type,
90 typename MatrixType::node_type> >
93 return A_->getRowMap();
97template<
class MatrixType>
98Teuchos::RCP<
const Tpetra::Map<
typename MatrixType::local_ordinal_type,
99 typename MatrixType::global_ordinal_type,
100 typename MatrixType::node_type> >
103 return A_->getColMap();
107template<
class MatrixType>
108Teuchos::RCP<
const Tpetra::Map<
typename MatrixType::local_ordinal_type,
109 typename MatrixType::global_ordinal_type,
110 typename MatrixType::node_type> >
113 return A_->getDomainMap();
117template<
class MatrixType>
118Teuchos::RCP<
const Tpetra::Map<
typename MatrixType::local_ordinal_type,
119 typename MatrixType::global_ordinal_type,
120 typename MatrixType::node_type> >
123 return A_->getRangeMap();
127template<
class MatrixType>
128Teuchos::RCP<
const Tpetra::RowGraph<
typename MatrixType::local_ordinal_type,
129 typename MatrixType::global_ordinal_type,
130 typename MatrixType::node_type> >
133 throw std::runtime_error(
"Ifpack2::SparsityFilter: does not support getGraph.");
137template<
class MatrixType>
144template<
class MatrixType>
151template<
class MatrixType>
159template<
class MatrixType>
166template<
class MatrixType>
169 return A_->getIndexBase();
173template<
class MatrixType>
180template<
class MatrixType>
187template<
class MatrixType>
190 throw std::runtime_error(
"Ifpack2::SparsityFilter does not implement getNumEntriesInGlobalRow.");
194template<
class MatrixType>
197 return NumEntries_[localRow];
201template<
class MatrixType>
204 return MaxNumEntries_;
208template<
class MatrixType>
211 return MaxNumEntries_;
215template<
class MatrixType>
218 return A_->getBlockSize();
222template<
class MatrixType>
229template<
class MatrixType>
232 return A_->isLocallyIndexed();
236template<
class MatrixType>
239 return A_->isGloballyIndexed();
243template<
class MatrixType>
246 return A_->isFillComplete();
250template<
class MatrixType>
253 nonconst_global_inds_host_view_type &,
254 nonconst_values_host_view_type &,
256 throw std::runtime_error(
"Ifpack2::SparsityFilter does not implement getGlobalRowCopy.");
260template<
class MatrixType>
263 nonconst_local_inds_host_view_type &Indices,
264 nonconst_values_host_view_type &Values,
265 size_t& NumEntries)
const
267TEUCHOS_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.");
275 size_t A_NumEntries=0;
276 A_->getLocalRowCopy(LocalRow,Indices_,Values_,A_NumEntries);
277 magnitudeType Threshold = Teuchos::ScalarTraits<magnitudeType>::zero();
278 std::vector<magnitudeType> Values2(A_NumEntries,Teuchos::ScalarTraits<magnitudeType>::zero());
280 if (A_NumEntries > AllowedNumEntries_) {
282 for (
size_t i = 0 ; i < A_NumEntries ; ++i) {
284 if (Indices_[i] == LocalRow)
287 Values2[count] = Teuchos::ScalarTraits<Scalar>::magnitude(Values_[i]);
292 std::sort(Values2.rbegin(),Values2.rend());
294 Threshold = Values2[AllowedNumEntries_ - 1];
302 for (
size_t i = 0 ; i < A_NumEntries ; ++i) {
303 if (std::abs(Indices_[i] - LocalRow) > AllowedBandwidth_)
306 if ((Indices_[i] != LocalRow) && (Teuchos::ScalarTraits<Scalar>::magnitude(Values_[i]) < Threshold))
309 Values[NumEntries] = Values_[i];
310 Indices[NumEntries] = Indices_[i];
313 if (NumEntries > AllowedNumEntries_)
321template<
class MatrixType>
323 global_inds_host_view_type &,
324 values_host_view_type &)
const
326 throw std::runtime_error(
"Ifpack2::SparsityFilter: does not support getGlobalRowView.");
330template<
class MatrixType>
332 local_inds_host_view_type & ,
333 values_host_view_type & )
const
335 throw std::runtime_error(
"Ifpack2::SparsityFilter: does not support getLocalRowView.");
339template<
class MatrixType>
343 return A_->getLocalDiagCopy(diag);
347template<
class MatrixType>
350 throw std::runtime_error(
"Ifpack2::SparsityFilter does not support leftScale.");
354template<
class MatrixType>
357 throw std::runtime_error(
"Ifpack2::SparsityFilter does not support rightScale.");
361template<
class MatrixType>
363 Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &Y,
364 Teuchos::ETransp mode,
370 TEUCHOS_TEST_FOR_EXCEPTION(X.getNumVectors() != Y.getNumVectors(), std::runtime_error,
371 "Ifpack2::SparsityFilter::apply ERROR: X.getNumVectors() != Y.getNumVectors().");
373 Scalar zero = Teuchos::ScalarTraits<Scalar>::zero();
374 Teuchos::ArrayRCP<Teuchos::ArrayRCP<const Scalar> > x_ptr = X.get2dView();
375 Teuchos::ArrayRCP<Teuchos::ArrayRCP<Scalar> > y_ptr = Y.get2dViewNonConst();
378 size_t NumVectors = Y.getNumVectors();
380 for (
size_t i = 0 ; i < NumRows_ ; ++i) {
384 Scalar* Values =
reinterpret_cast<Scalar*
>(Values_.data());
385 if (mode==Teuchos::NO_TRANS){
386 for (
size_t j = 0 ; j < Nnz ; ++j)
387 for (
size_t k = 0 ; k < NumVectors ; ++k)
388 y_ptr[k][i] += Values[j] * x_ptr[k][Indices_[j]];
390 else if (mode==Teuchos::TRANS){
391 for (
size_t j = 0 ; j < Nnz ; ++j)
392 for (
size_t k = 0 ; k < NumVectors ; ++k)
393 y_ptr[k][Indices_[j]] += Values[j] * x_ptr[k][i];
396 for (
size_t j = 0 ; j < Nnz ; ++j)
397 for (
size_t k = 0 ; k < NumVectors ; ++k)
398 y_ptr[k][Indices_[j]] += Teuchos::ScalarTraits<Scalar>::conjugate(Values[j]) * x_ptr[k][i];
405template<
class MatrixType>
412template<
class MatrixType>
419template<
class MatrixType>
422 throw std::runtime_error(
"Ifpack2::SparsityFilter does not implement getFrobeniusNorm.");
427#define IFPACK2_SPARSITYFILTER_INSTANT(S,LO,GO,N) \
428 template class Ifpack2::SparsityFilter< Tpetra::RowMatrix<S, LO, GO, N> >;
virtual global_size_t getGlobalNumRows() const
Returns the number of global rows in this matrix.
Definition Ifpack2_SparsityFilter_def.hpp:138
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:195
virtual void getLocalRowView(LocalOrdinal LocalRow, local_inds_host_view_type &indices, values_host_view_type &values) const
Extract a const, non-persisting view of local indices in a specified row of the matrix.
Definition Ifpack2_SparsityFilter_def.hpp:331
virtual bool hasTransposeApply() const
Indicates whether this operator supports applying the adjoint operator.
Definition Ifpack2_SparsityFilter_def.hpp:406
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:111
virtual GlobalOrdinal getIndexBase() const
Returns the index base for global indices for this matrix.
Definition Ifpack2_SparsityFilter_def.hpp:167
virtual mag_type getFrobeniusNorm() const
Returns the Frobenius norm of the matrix.
Definition Ifpack2_SparsityFilter_def.hpp:420
virtual global_size_t getGlobalNumEntries() const
Returns the global number of entries in this matrix.
Definition Ifpack2_SparsityFilter_def.hpp:174
virtual Teuchos::RCP< const Teuchos::Comm< int > > getComm() const
Returns the communicator.
Definition Ifpack2_SparsityFilter_def.hpp:80
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:91
virtual size_t getLocalNumCols() const
Returns the number of columns needed to apply the forward operator on this node, i....
Definition Ifpack2_SparsityFilter_def.hpp:160
virtual LocalOrdinal getBlockSize() const
The number of degrees of freedom per mesh point.
Definition Ifpack2_SparsityFilter_def.hpp:216
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:188
virtual size_t getLocalMaxNumRowEntries() const
Returns the maximum number of entries across all rows/columns on this node.
Definition Ifpack2_SparsityFilter_def.hpp:209
virtual ~SparsityFilter()
Destructor.
Definition Ifpack2_SparsityFilter_def.hpp:76
virtual void getGlobalRowView(GlobalOrdinal GlobalRow, global_inds_host_view_type &indices, values_host_view_type &values) const
Extract a const, non-persisting view of global indices in a specified row of the matrix.
Definition Ifpack2_SparsityFilter_def.hpp:322
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:340
virtual void getGlobalRowCopy(GlobalOrdinal GlobalRow, nonconst_global_inds_host_view_type &Indices, nonconst_values_host_view_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_SparsityFilter_def.hpp:252
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:230
virtual global_size_t getGlobalNumCols() const
Returns the number of global columns in this matrix.
Definition Ifpack2_SparsityFilter_def.hpp:145
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:121
virtual bool hasColMap() const
Indicates whether this matrix has a well-defined column map.
Definition Ifpack2_SparsityFilter_def.hpp:223
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:355
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:362
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:101
virtual bool supportsRowViews() const
Returns true if RowViews are supported.
Definition Ifpack2_SparsityFilter_def.hpp:413
virtual bool isFillComplete() const
Returns true if fillComplete() has been called.
Definition Ifpack2_SparsityFilter_def.hpp:244
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:348
virtual Teuchos::RCP< const Tpetra::RowGraph< LocalOrdinal, GlobalOrdinal, Node > > getGraph() const
Returns the RowGraph associated with this matrix.
Definition Ifpack2_SparsityFilter_def.hpp:131
virtual size_t getLocalNumRows() const
Returns the number of rows owned on the calling node.
Definition Ifpack2_SparsityFilter_def.hpp:152
SparsityFilter(const Teuchos::RCP< const row_matrix_type > &Matrix, size_t AllowedNumEntries, LocalOrdinal AllowedBandwidth=-Teuchos::ScalarTraits< LocalOrdinal >::one())
Constructor.
Definition Ifpack2_SparsityFilter_def.hpp:24
virtual void getLocalRowCopy(LocalOrdinal LocalRow, nonconst_local_inds_host_view_type &Indices, nonconst_values_host_view_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_SparsityFilter_def.hpp:262
virtual size_t getGlobalMaxNumRowEntries() const
Returns the maximum number of entries across all rows/columns on all nodes.
Definition Ifpack2_SparsityFilter_def.hpp:202
virtual size_t getLocalNumEntries() const
Returns the local number of entries in this matrix.
Definition Ifpack2_SparsityFilter_def.hpp:181
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:237
Preconditioners and smoothers for Tpetra sparse matrices.
Definition Ifpack2_AdditiveSchwarz_decl.hpp:41