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"
23 template <
class MatrixType>
25 size_t AllowedNumEntries,
26 LocalOrdinal AllowedBandwidth)
28 , AllowedNumEntries_(AllowedNumEntries)
29 , AllowedBandwidth_(AllowedBandwidth)
33 , MaxNumEntriesA_(0) {
36 A_->getComm()->getSize() != 1 || A_->getLocalNumRows() != A_->getGlobalNumRows(),
38 "Ifpack2::SparsityFilter: "
39 "This class may only be used when A.getComm()->getSize() == 1.");
42 NumRows_ = A_->getLocalNumRows();
47 NumEntries_.resize(NumRows_);
51 MaxNumEntries_ = A_->getLocalMaxNumRowEntries();
52 MaxNumEntriesA_ = A_->getLocalMaxNumRowEntries();
55 Kokkos::resize(Indices_, MaxNumEntries_);
56 Kokkos::resize(Values_, MaxNumEntries_);
58 size_t ActualMaxNumEntries = 0;
59 for (
size_t i = 0; i < NumRows_; ++i) {
60 NumEntries_[i] = MaxNumEntriesA_;
62 A_->getLocalRowCopy(i, Indices_, Values_, Nnz);
66 if (Nnz > ActualMaxNumEntries)
67 ActualMaxNumEntries = Nnz;
70 MaxNumEntries_ = ActualMaxNumEntries;
74 template <
class MatrixType>
78 template <
class MatrixType>
84 template <
class MatrixType>
85 Teuchos::RCP<
const Tpetra::Map<
typename MatrixType::local_ordinal_type,
86 typename MatrixType::global_ordinal_type,
87 typename MatrixType::node_type> >
89 return A_->getRowMap();
93 template <
class MatrixType>
94 Teuchos::RCP<
const Tpetra::Map<
typename MatrixType::local_ordinal_type,
95 typename MatrixType::global_ordinal_type,
96 typename MatrixType::node_type> >
98 return A_->getColMap();
102 template <
class MatrixType>
103 Teuchos::RCP<
const Tpetra::Map<
typename MatrixType::local_ordinal_type,
104 typename MatrixType::global_ordinal_type,
105 typename MatrixType::node_type> >
107 return A_->getDomainMap();
111 template <
class MatrixType>
112 Teuchos::RCP<
const Tpetra::Map<
typename MatrixType::local_ordinal_type,
113 typename MatrixType::global_ordinal_type,
114 typename MatrixType::node_type> >
116 return A_->getRangeMap();
120 template <
class MatrixType>
121 Teuchos::RCP<
const Tpetra::RowGraph<
typename MatrixType::local_ordinal_type,
122 typename MatrixType::global_ordinal_type,
123 typename MatrixType::node_type> >
125 throw std::runtime_error(
"Ifpack2::SparsityFilter: does not support getGraph.");
129 template <
class MatrixType>
135 template <
class MatrixType>
141 template <
class MatrixType>
148 template <
class MatrixType>
154 template <
class MatrixType>
156 return A_->getIndexBase();
160 template <
class MatrixType>
166 template <
class MatrixType>
172 template <
class MatrixType>
174 throw std::runtime_error(
"Ifpack2::SparsityFilter does not implement getNumEntriesInGlobalRow.");
178 template <
class MatrixType>
180 return NumEntries_[localRow];
184 template <
class MatrixType>
186 return MaxNumEntries_;
190 template <
class MatrixType>
192 return MaxNumEntries_;
196 template <
class MatrixType>
198 return A_->getBlockSize();
202 template <
class MatrixType>
208 template <
class MatrixType>
210 return A_->isLocallyIndexed();
214 template <
class MatrixType>
216 return A_->isGloballyIndexed();
220 template <
class MatrixType>
222 return A_->isFillComplete();
226 template <
class MatrixType>
229 nonconst_global_inds_host_view_type & ,
230 nonconst_values_host_view_type & ,
232 throw std::runtime_error(
"Ifpack2::SparsityFilter does not implement getGlobalRowCopy.");
236 template <
class MatrixType>
239 nonconst_local_inds_host_view_type &Indices,
240 nonconst_values_host_view_type &Values,
241 size_t &NumEntries)
const {
242 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.");
250 size_t A_NumEntries = 0;
251 A_->getLocalRowCopy(LocalRow, Indices_, Values_, A_NumEntries);
255 if (A_NumEntries > AllowedNumEntries_) {
257 for (
size_t i = 0; i < A_NumEntries; ++i) {
259 if (Indices_[i] == LocalRow)
267 std::sort(Values2.rbegin(), Values2.rend());
269 Threshold = Values2[AllowedNumEntries_ - 1];
276 for (
size_t i = 0; i < A_NumEntries; ++i) {
277 if (std::abs(Indices_[i] - LocalRow) > AllowedBandwidth_)
283 Values[NumEntries] = Values_[i];
284 Indices[NumEntries] = Indices_[i];
287 if (NumEntries > AllowedNumEntries_)
293 template <
class MatrixType>
295 global_inds_host_view_type & ,
296 values_host_view_type & )
const {
297 throw std::runtime_error(
"Ifpack2::SparsityFilter: does not support getGlobalRowView.");
301 template <
class MatrixType>
303 local_inds_host_view_type & ,
304 values_host_view_type & )
const {
305 throw std::runtime_error(
"Ifpack2::SparsityFilter: does not support getLocalRowView.");
309 template <
class MatrixType>
312 return A_->getLocalDiagCopy(diag);
316 template <
class MatrixType>
318 throw std::runtime_error(
"Ifpack2::SparsityFilter does not support leftScale.");
322 template <
class MatrixType>
324 throw std::runtime_error(
"Ifpack2::SparsityFilter does not support rightScale.");
328 template <
class MatrixType>
330 Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> &Y,
337 "Ifpack2::SparsityFilter::apply ERROR: X.getNumVectors() != Y.getNumVectors().");
344 size_t NumVectors = Y.getNumVectors();
346 for (
size_t i = 0; i < NumRows_; ++i) {
349 getLocalRowCopy(i, Indices_, Values_, Nnz);
350 Scalar *Values =
reinterpret_cast<Scalar *
>(Values_.data());
352 for (
size_t j = 0; j < Nnz; ++j)
353 for (
size_t k = 0; k < NumVectors; ++k)
354 y_ptr[k][i] += Values[j] * x_ptr[k][Indices_[j]];
356 for (
size_t j = 0; j < Nnz; ++j)
357 for (
size_t k = 0; k < NumVectors; ++k)
358 y_ptr[k][Indices_[j]] += Values[j] * x_ptr[k][i];
360 for (
size_t j = 0; j < Nnz; ++j)
361 for (
size_t k = 0; k < NumVectors; ++k)
368 template <
class MatrixType>
374 template <
class MatrixType>
380 template <
class MatrixType>
382 throw std::runtime_error(
"Ifpack2::SparsityFilter does not implement getFrobeniusNorm.");
387 #define IFPACK2_SPARSITYFILTER_INSTANT(S, LO, GO, N) \
388 template class Ifpack2::SparsityFilter<Tpetra::RowMatrix<S, LO, GO, N> >;
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:329
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:179
virtual bool hasTransposeApply() const
Indicates whether this operator supports applying the adjoint operator.
Definition: Ifpack2_SparsityFilter_def.hpp:369
virtual size_t getLocalMaxNumRowEntries() const
Returns the maximum number of entries across all rows/columns on this node.
Definition: Ifpack2_SparsityFilter_def.hpp:191
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:88
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:106
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:238
virtual size_t getLocalNumEntries() const
Returns the local number of entries in this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:167
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:294
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
virtual GlobalOrdinal getIndexBase() const
Returns the index base for global indices for this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:155
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:97
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:310
virtual LocalOrdinal getBlockSize() const
The number of degrees of freedom per mesh point.
Definition: Ifpack2_SparsityFilter_def.hpp:197
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:215
virtual Teuchos::RCP< const Tpetra::RowGraph< LocalOrdinal, GlobalOrdinal, Node > > getGraph() const
Returns the RowGraph associated with this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:124
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:209
virtual ~SparsityFilter()
Destructor.
Definition: Ifpack2_SparsityFilter_def.hpp:75
virtual global_size_t getGlobalNumRows() const
Returns the number of global rows in this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:130
virtual size_t getLocalNumCols() 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:149
virtual Teuchos::RCP< const Teuchos::Comm< int > > getComm() const
Returns the communicator.
Definition: Ifpack2_SparsityFilter_def.hpp:79
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 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:173
virtual global_size_t getGlobalNumCols() const
Returns the number of global columns in this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:136
virtual bool hasColMap() const
Indicates whether this matrix has a well-defined column map.
Definition: Ifpack2_SparsityFilter_def.hpp:203
virtual mag_type getFrobeniusNorm() const
Returns the Frobenius norm of the matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:381
virtual size_t getGlobalMaxNumRowEntries() const
Returns the maximum number of entries across all rows/columns on all nodes.
Definition: Ifpack2_SparsityFilter_def.hpp:185
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:302
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:115
virtual global_size_t getGlobalNumEntries() const
Returns the global number of entries in this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:161
virtual bool isFillComplete() const
Returns true if fillComplete() has been called.
Definition: Ifpack2_SparsityFilter_def.hpp:221
static magnitudeType magnitude(T a)
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:228
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:323
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:317
virtual bool supportsRowViews() const
Returns true if RowViews are supported.
Definition: Ifpack2_SparsityFilter_def.hpp:375
virtual size_t getLocalNumRows() const
Returns the number of rows owned on the calling node.
Definition: Ifpack2_SparsityFilter_def.hpp:142