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 Kokkos::resize(Indices_,MaxNumEntries_);
90 Kokkos::resize(Values_,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();
120 template<
class MatrixType>
121 Teuchos::RCP<
const Tpetra::Map<
typename MatrixType::local_ordinal_type,
122 typename MatrixType::global_ordinal_type,
123 typename MatrixType::node_type> >
126 return A_->getRowMap();
130 template<
class MatrixType>
131 Teuchos::RCP<
const Tpetra::Map<
typename MatrixType::local_ordinal_type,
132 typename MatrixType::global_ordinal_type,
133 typename MatrixType::node_type> >
136 return A_->getColMap();
140 template<
class MatrixType>
141 Teuchos::RCP<
const Tpetra::Map<
typename MatrixType::local_ordinal_type,
142 typename MatrixType::global_ordinal_type,
143 typename MatrixType::node_type> >
146 return A_->getDomainMap();
150 template<
class MatrixType>
151 Teuchos::RCP<
const Tpetra::Map<
typename MatrixType::local_ordinal_type,
152 typename MatrixType::global_ordinal_type,
153 typename MatrixType::node_type> >
156 return A_->getRangeMap();
160 template<
class MatrixType>
161 Teuchos::RCP<
const Tpetra::RowGraph<
typename MatrixType::local_ordinal_type,
162 typename MatrixType::global_ordinal_type,
163 typename MatrixType::node_type> >
166 throw std::runtime_error(
"Ifpack2::SparsityFilter: does not support getGraph.");
170 template<
class MatrixType>
177 template<
class MatrixType>
184 template<
class MatrixType>
192 template<
class MatrixType>
199 template<
class MatrixType>
202 return A_->getIndexBase();
206 template<
class MatrixType>
213 template<
class MatrixType>
220 template<
class MatrixType>
223 throw std::runtime_error(
"Ifpack2::SparsityFilter does not implement getNumEntriesInGlobalRow.");
227 template<
class MatrixType>
230 return NumEntries_[localRow];
234 template<
class MatrixType>
237 return MaxNumEntries_;
241 template<
class MatrixType>
244 return MaxNumEntries_;
248 template<
class MatrixType>
255 template<
class MatrixType>
258 return A_->isLocallyIndexed();
262 template<
class MatrixType>
265 return A_->isGloballyIndexed();
269 template<
class MatrixType>
272 return A_->isFillComplete();
276 template<
class MatrixType>
279 nonconst_global_inds_host_view_type &,
280 nonconst_values_host_view_type &,
282 throw std::runtime_error(
"Ifpack2::SparsityFilter does not implement getGlobalRowCopy.");
286 #ifdef TPETRA_ENABLE_DEPRECATED_CODE 287 template<
class MatrixType>
290 const Teuchos::ArrayView<GlobalOrdinal> &,
291 const Teuchos::ArrayView<Scalar> &,
294 throw std::runtime_error(
"Ifpack2::SparsityFilter does not implement getGlobalRowCopy.");
299 template<
class MatrixType>
302 nonconst_local_inds_host_view_type &Indices,
303 nonconst_values_host_view_type &Values,
304 size_t& NumEntries)
const 306 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.");
314 size_t A_NumEntries=0;
315 A_->getLocalRowCopy(LocalRow,Indices_,Values_,A_NumEntries);
316 magnitudeType Threshold = Teuchos::ScalarTraits<magnitudeType>::zero();
317 std::vector<magnitudeType> Values2(A_NumEntries,Teuchos::ScalarTraits<magnitudeType>::zero());
319 if (A_NumEntries > AllowedNumEntries_) {
321 for (
size_t i = 0 ; i < A_NumEntries ; ++i) {
323 if (Indices_[i] == LocalRow)
326 Values2[count] = Teuchos::ScalarTraits<Scalar>::magnitude(Values_[i]);
331 std::sort(Values2.rbegin(),Values2.rend());
333 Threshold = Values2[AllowedNumEntries_ - 1];
341 for (
size_t i = 0 ; i < A_NumEntries ; ++i) {
342 if (std::abs(Indices_[i] - LocalRow) > AllowedBandwidth_)
345 if ((Indices_[i] != LocalRow) && (Teuchos::ScalarTraits<Scalar>::magnitude(Values_[i]) < Threshold))
348 Values[NumEntries] = Values_[i];
349 Indices[NumEntries] = Indices_[i];
352 if (NumEntries > AllowedNumEntries_)
360 #ifdef TPETRA_ENABLE_DEPRECATED_CODE 361 template<
class MatrixType>
364 const Teuchos::ArrayView<LocalOrdinal> &Indices,
365 const Teuchos::ArrayView<Scalar> &Values,
366 size_t &NumEntries)
const 368 using IST =
typename row_matrix_type::impl_scalar_type;
369 nonconst_local_inds_host_view_type ind_in(Indices.data(),Indices.size());
370 nonconst_values_host_view_type val_in(reinterpret_cast<IST*>(Values.data()),Values.size());
371 getLocalRowCopy(DropRow,ind_in,val_in,NumEntries);
376 template<
class MatrixType>
378 global_inds_host_view_type &,
379 values_host_view_type &)
const 381 throw std::runtime_error(
"Ifpack2::SparsityFilter: does not support getGlobalRowView.");
384 #ifdef TPETRA_ENABLE_DEPRECATED_CODE 385 template<
class MatrixType>
387 Teuchos::ArrayView<const GlobalOrdinal> &,
388 Teuchos::ArrayView<const Scalar> &)
const 390 throw std::runtime_error(
"Ifpack2::SparsityFilter: does not support getGlobalRowView.");
395 template<
class MatrixType>
397 local_inds_host_view_type & ,
398 values_host_view_type & )
const 400 throw std::runtime_error(
"Ifpack2::SparsityFilter: does not support getLocalRowView.");
402 #ifdef TPETRA_ENABLE_DEPRECATED_CODE 403 template<
class MatrixType>
405 Teuchos::ArrayView<const LocalOrdinal> &,
406 Teuchos::ArrayView<const Scalar> &)
const 408 throw std::runtime_error(
"Ifpack2::SparsityFilter: does not support getLocalRowView.");
413 template<
class MatrixType>
417 return A_->getLocalDiagCopy(diag);
421 template<
class MatrixType>
424 throw std::runtime_error(
"Ifpack2::SparsityFilter does not support leftScale.");
428 template<
class MatrixType>
431 throw std::runtime_error(
"Ifpack2::SparsityFilter does not support rightScale.");
435 template<
class MatrixType>
437 Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &Y,
438 Teuchos::ETransp mode,
444 TEUCHOS_TEST_FOR_EXCEPTION(X.getNumVectors() != Y.getNumVectors(), std::runtime_error,
445 "Ifpack2::SparsityFilter::apply ERROR: X.getNumVectors() != Y.getNumVectors().");
447 Scalar zero = Teuchos::ScalarTraits<Scalar>::zero();
448 Teuchos::ArrayRCP<Teuchos::ArrayRCP<const Scalar> > x_ptr = X.get2dView();
449 Teuchos::ArrayRCP<Teuchos::ArrayRCP<Scalar> > y_ptr = Y.get2dViewNonConst();
452 size_t NumVectors = Y.getNumVectors();
454 for (
size_t i = 0 ; i < NumRows_ ; ++i) {
457 getLocalRowCopy(i,Indices_,Values_,Nnz);
458 Scalar* Values =
reinterpret_cast<Scalar*
>(Values_.data());
459 if (mode==Teuchos::NO_TRANS){
460 for (
size_t j = 0 ; j < Nnz ; ++j)
461 for (
size_t k = 0 ; k < NumVectors ; ++k)
462 y_ptr[k][i] += Values[j] * x_ptr[k][Indices_[j]];
464 else if (mode==Teuchos::TRANS){
465 for (
size_t j = 0 ; j < Nnz ; ++j)
466 for (
size_t k = 0 ; k < NumVectors ; ++k)
467 y_ptr[k][Indices_[j]] += Values[j] * x_ptr[k][i];
470 for (
size_t j = 0 ; j < Nnz ; ++j)
471 for (
size_t k = 0 ; k < NumVectors ; ++k)
472 y_ptr[k][Indices_[j]] += Teuchos::ScalarTraits<Scalar>::conjugate(Values[j]) * x_ptr[k][i];
479 template<
class MatrixType>
486 template<
class MatrixType>
493 template<
class MatrixType>
496 throw std::runtime_error(
"Ifpack2::SparsityFilter does not implement getFrobeniusNorm.");
501 #define IFPACK2_SPARSITYFILTER_INSTANT(S,LO,GO,N) \ 502 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:414
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:193
virtual size_t getGlobalMaxNumRowEntries() const
Returns the maximum number of entries across all rows/columns on all nodes.
Definition: Ifpack2_SparsityFilter_def.hpp:235
virtual global_size_t getGlobalNumRows() const
Returns the number of global rows in this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:171
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:377
virtual bool supportsRowViews() const
Returns true if RowViews are supported.
Definition: Ifpack2_SparsityFilter_def.hpp:487
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:278
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:249
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:221
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:263
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:256
virtual size_t getNodeMaxNumRowEntries() const
Returns the maximum number of entries across all rows/columns on this node.
Definition: Ifpack2_SparsityFilter_def.hpp:242
virtual bool hasTransposeApply() const
Indicates whether this operator supports applying the adjoint operator.
Definition: Ifpack2_SparsityFilter_def.hpp:480
virtual bool isFillComplete() const
Returns true if fillComplete() has been called.
Definition: Ifpack2_SparsityFilter_def.hpp:270
virtual mag_type getFrobeniusNorm() const
Returns the Frobenius norm of the matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:494
virtual GlobalOrdinal getIndexBase() const
Returns the index base for global indices for this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:200
virtual global_size_t getGlobalNumEntries() const
Returns the global number of entries in this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:207
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:228
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:154
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:436
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:124
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:396
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:429
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:422
virtual global_size_t getGlobalNumCols() const
Returns the number of global columns in this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:178
Preconditioners and smoothers for Tpetra sparse matrices.
Definition: Ifpack2_AdditiveSchwarz_decl.hpp:73
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:144
virtual Teuchos::RCP< const Tpetra::RowGraph< LocalOrdinal, GlobalOrdinal, Node > > getGraph() const
Returns the RowGraph associated with this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:164
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:301
virtual size_t getNodeNumEntries() const
Returns the local number of entries in this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:214
virtual size_t getNodeNumRows() const
Returns the number of rows owned on the calling node.
Definition: Ifpack2_SparsityFilter_def.hpp:185
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:134