43 #ifndef IFPACK2_SINGLETONFILTER_DEF_HPP 44 #define IFPACK2_SINGLETONFILTER_DEF_HPP 45 #include "Ifpack2_SingletonFilter_decl.hpp" 47 #include "Tpetra_ConfigDefs.hpp" 48 #include "Tpetra_RowMatrix.hpp" 49 #include "Tpetra_Map.hpp" 50 #include "Tpetra_MultiVector.hpp" 51 #include "Tpetra_Vector.hpp" 55 template<
class MatrixType>
66 if (A_->getComm()->getSize() != 1 || A_->getNodeNumRows() != A_->getGlobalNumRows()) {
67 throw std::runtime_error(
"Ifpack2::SingeltonFilter can be used with Comm().getSize() == 1 only. This class is a tool for Ifpack2_AdditiveSchwarz, and it is not meant to be used otherwise.");
71 size_t NumRowsA_ = A_->getNodeNumRows();
75 MaxNumEntriesA_ = A_->getNodeMaxNumRowEntries();
78 Kokkos::resize(Indices_,MaxNumEntriesA_);
79 Kokkos::resize(Values_,MaxNumEntriesA_);
82 Reorder_.resize(NumRowsA_);
83 Reorder_.assign(Reorder_.size(),-1);
87 for (
size_t i = 0 ; i < NumRowsA_ ; ++i) {
89 A_->getLocalRowCopy(i,Indices_,Values_,Nnz);
91 Reorder_[i] = NumRows_++;
99 InvReorder_.resize(NumRows_);
100 for (
size_t i = 0 ; i < NumRowsA_ ; ++i) {
103 InvReorder_[Reorder_[i]] = i;
105 NumEntries_.resize(NumRows_);
106 SingletonIndex_.resize(NumSingletons_);
111 for (
size_t i = 0 ; i < NumRowsA_ ; ++i) {
113 A_->getLocalRowCopy(i,Indices_,Values_,Nnz);
114 LocalOrdinal ii = Reorder_[i];
116 NumEntries_[ii] = Nnz;
118 if (Nnz > MaxNumEntries_)
119 MaxNumEntries_ = Nnz;
122 SingletonIndex_[count] = i;
128 ReducedMap_ = Teuchos::rcp(
new Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node>(NumRows_,0,A_->getComm()) );
131 Diagonal_ = Teuchos::rcp(
new Tpetra::Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>(ReducedMap_) );
133 Tpetra::Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node> DiagonalA(A_->getRowMap());
134 A_->getLocalDiagCopy(DiagonalA);
135 const Teuchos::ArrayRCP<const Scalar> & DiagonalAview = DiagonalA.get1dView();
136 for (
size_t i = 0 ; i < NumRows_ ; ++i) {
137 LocalOrdinal ii = InvReorder_[i];
138 Diagonal_->replaceLocalValue((LocalOrdinal)i,DiagonalAview[ii]);
142 template<
class MatrixType>
145 template<
class MatrixType>
146 Teuchos::RCP<const Teuchos::Comm<int> >
149 return A_->getComm();
153 template<
class MatrixType>
154 Teuchos::RCP<
const Tpetra::Map<
typename MatrixType::local_ordinal_type,
155 typename MatrixType::global_ordinal_type,
156 typename MatrixType::node_type> >
162 template<
class MatrixType>
163 Teuchos::RCP<
const Tpetra::Map<
typename MatrixType::local_ordinal_type,
164 typename MatrixType::global_ordinal_type,
165 typename MatrixType::node_type> >
171 template<
class MatrixType>
172 Teuchos::RCP<
const Tpetra::Map<
typename MatrixType::local_ordinal_type,
173 typename MatrixType::global_ordinal_type,
174 typename MatrixType::node_type> >
180 template<
class MatrixType>
181 Teuchos::RCP<
const Tpetra::Map<
typename MatrixType::local_ordinal_type,
182 typename MatrixType::global_ordinal_type,
183 typename MatrixType::node_type> >
189 template<
class MatrixType>
190 Teuchos::RCP<
const Tpetra::RowGraph<
typename MatrixType::local_ordinal_type,
191 typename MatrixType::global_ordinal_type,
192 typename MatrixType::node_type> >
195 throw std::runtime_error(
"Ifpack2::SingletonFilter: does not support getGraph.");
198 template<
class MatrixType>
204 template<
class MatrixType>
210 template<
class MatrixType>
216 template<
class MatrixType>
222 template<
class MatrixType>
225 return A_->getIndexBase();
228 template<
class MatrixType>
234 template<
class MatrixType>
240 template<
class MatrixType>
243 throw std::runtime_error(
"Ifpack2::SingletonFilter does not implement getNumEntriesInGlobalRow.");
246 template<
class MatrixType>
249 return NumEntries_[localRow];
252 template<
class MatrixType>
255 return MaxNumEntries_;
258 template<
class MatrixType>
261 return MaxNumEntries_;
264 template<
class MatrixType>
270 template<
class MatrixType>
273 return A_->isLocallyIndexed();
276 template<
class MatrixType>
279 return A_->isGloballyIndexed();
282 template<
class MatrixType>
285 return A_->isFillComplete();
288 template<
class MatrixType>
291 nonconst_global_inds_host_view_type &,
292 nonconst_values_host_view_type &,
295 throw std::runtime_error(
"Ifpack2::SingletonFilter does not implement getGlobalRowCopy.");
298 #ifdef TPETRA_ENABLE_DEPRECATED_CODE 299 template<
class MatrixType>
301 const Teuchos::ArrayView<GlobalOrdinal> &,
302 const Teuchos::ArrayView<Scalar> &,
305 throw std::runtime_error(
"Ifpack2::SingletonFilter does not implement getGlobalRowCopy.");
309 template<
class MatrixType>
312 nonconst_local_inds_host_view_type &Indices,
313 nonconst_values_host_view_type &Values,
314 size_t& NumEntries)
const 316 TEUCHOS_TEST_FOR_EXCEPTION((LocalRow < 0 || (
size_t) LocalRow >= NumRows_ || (
size_t) Indices.size() < NumEntries_[LocalRow]), std::runtime_error,
"Ifpack2::SingletonFilter::getLocalRowCopy invalid row or array size.");
319 LocalOrdinal ARow = InvReorder_[LocalRow];
320 A_->getLocalRowCopy(ARow,Indices_,Values_,Nnz);
324 for (
size_t i = 0 ; i < Nnz ; ++i) {
325 LocalOrdinal ii = Reorder_[Indices_[i]];
327 Indices[NumEntries] = ii;
328 Values[NumEntries] = Values_[i];
335 #ifdef TPETRA_ENABLE_DEPRECATED_CODE 336 template<
class MatrixType>
338 const Teuchos::ArrayView<LocalOrdinal> &Indices,
339 const Teuchos::ArrayView<Scalar> &Values,
340 size_t &NumEntries)
const 342 using IST =
typename row_matrix_type::impl_scalar_type;
343 nonconst_local_inds_host_view_type ind_in(Indices.data(),Indices.size());
344 nonconst_values_host_view_type val_in(reinterpret_cast<IST*>(Values.data()),Values.size());
345 getLocalRowCopy(LocalRow,ind_in,val_in,NumEntries);
349 template<
class MatrixType>
351 global_inds_host_view_type &,
352 values_host_view_type &)
const 354 throw std::runtime_error(
"Ifpack2::SingletonFilter: does not support getGlobalRowView.");
357 #ifdef TPETRA_ENABLE_DEPRECATED_CODE 358 template<
class MatrixType>
360 Teuchos::ArrayView<const GlobalOrdinal> &,
361 Teuchos::ArrayView<const Scalar> &)
const 363 throw std::runtime_error(
"Ifpack2::SingletonFilter: does not support getGlobalRowView.");
367 template<
class MatrixType>
369 local_inds_host_view_type & ,
370 values_host_view_type & )
const 372 throw std::runtime_error(
"Ifpack2::SingletonFilter: does not support getLocalRowView.");
375 #ifdef TPETRA_ENABLE_DEPRECATED_CODE 376 template<
class MatrixType>
378 Teuchos::ArrayView<const LocalOrdinal> &,
379 Teuchos::ArrayView<const Scalar> &)
const 381 throw std::runtime_error(
"Ifpack2::SingletonFilter: does not support getLocalRowView.");
385 template<
class MatrixType>
389 return A_->getLocalDiagCopy(diag);
392 template<
class MatrixType>
395 throw std::runtime_error(
"Ifpack2::SingletonFilter does not support leftScale.");
398 template<
class MatrixType>
401 throw std::runtime_error(
"Ifpack2::SingletonFilter does not support rightScale.");
404 template<
class MatrixType>
406 Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &Y,
407 Teuchos::ETransp mode,
411 typedef Scalar DomainScalar;
412 typedef Scalar RangeScalar;
416 TEUCHOS_TEST_FOR_EXCEPTION(X.getNumVectors() != Y.getNumVectors(), std::runtime_error,
417 "Ifpack2::SingletonFilter::apply ERROR: X.getNumVectors() != Y.getNumVectors().");
419 RangeScalar zero = Teuchos::ScalarTraits<RangeScalar>::zero();
420 Teuchos::ArrayRCP<Teuchos::ArrayRCP<const DomainScalar> > x_ptr = X.get2dView();
421 Teuchos::ArrayRCP<Teuchos::ArrayRCP<RangeScalar> > y_ptr = Y.get2dViewNonConst();
424 size_t NumVectors = Y.getNumVectors();
427 for (
size_t i = 0 ; i < NumRows_ ; ++i) {
430 getLocalRowCopy(i,Indices_,Values_,Nnz);
431 if (mode==Teuchos::NO_TRANS){
432 for (
size_t j = 0 ; j < Nnz ; ++j)
433 for (
size_t k = 0 ; k < NumVectors ; ++k)
434 y_ptr[k][i] += (RangeScalar)Values_[j] * (RangeScalar)x_ptr[k][Indices_[j]];
436 else if (mode==Teuchos::TRANS){
437 for (
size_t j = 0 ; j < Nnz ; ++j)
438 for (
size_t k = 0 ; k < NumVectors ; ++k)
439 y_ptr[k][Indices_[j]] += (RangeScalar)Values_[j] * (RangeScalar)x_ptr[k][i];
442 for (
size_t j = 0 ; j < Nnz ; ++j)
443 for (
size_t k = 0 ; k < NumVectors ; ++k)
444 y_ptr[k][Indices_[j]] += Teuchos::ScalarTraits<RangeScalar>::conjugate((RangeScalar)Values_[j]) * (RangeScalar)x_ptr[k][i];
449 template<
class MatrixType>
455 template<
class MatrixType>
461 template<
class MatrixType>
463 Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& LHS)
465 this->
template SolveSingletonsTempl<Scalar,Scalar>(RHS, LHS);
468 template<
class MatrixType>
469 template<
class DomainScalar,
class RangeScalar>
471 Tpetra::MultiVector<RangeScalar,LocalOrdinal,GlobalOrdinal,Node>& LHS)
473 Teuchos::ArrayRCP<Teuchos::ArrayRCP<const DomainScalar> > RHS_ptr = RHS.get2dView();
474 Teuchos::ArrayRCP<Teuchos::ArrayRCP<RangeScalar> > LHS_ptr = LHS.get2dViewNonConst();
476 for (
size_t i = 0 ; i < NumSingletons_ ; ++i) {
477 LocalOrdinal ii = SingletonIndex_[i];
481 for (
size_t j = 0 ; j < Nnz ; ++j) {
482 if (Indices_[j] == ii) {
483 for (
size_t k = 0 ; k < LHS.getNumVectors() ; ++k)
484 LHS_ptr[k][ii] = (RangeScalar)RHS_ptr[k][ii] / (RangeScalar)Values_[j];
490 template<
class MatrixType>
492 const Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& RHS,
493 Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& ReducedRHS)
495 this->
template CreateReducedRHSTempl<Scalar,Scalar>(LHS, RHS, ReducedRHS);
498 template<
class MatrixType>
499 template<
class DomainScalar,
class RangeScalar>
501 const Tpetra::MultiVector<RangeScalar,LocalOrdinal,GlobalOrdinal,Node>& RHS,
502 Tpetra::MultiVector<RangeScalar,LocalOrdinal,GlobalOrdinal,Node>& ReducedRHS)
504 Teuchos::ArrayRCP<Teuchos::ArrayRCP<const RangeScalar > > RHS_ptr = RHS.get2dView();
505 Teuchos::ArrayRCP<Teuchos::ArrayRCP<const DomainScalar> > LHS_ptr = LHS.get2dView();
506 Teuchos::ArrayRCP<Teuchos::ArrayRCP<RangeScalar> > ReducedRHS_ptr = ReducedRHS.get2dViewNonConst();
508 size_t NumVectors = LHS.getNumVectors();
510 for (
size_t i = 0 ; i < NumRows_ ; ++i)
511 for (
size_t k = 0 ; k < NumVectors ; ++k)
512 ReducedRHS_ptr[k][i] = RHS_ptr[k][InvReorder_[i]];
514 for (
size_t i = 0 ; i < NumRows_ ; ++i) {
515 LocalOrdinal ii = InvReorder_[i];
519 for (
size_t j = 0 ; j < Nnz ; ++j) {
520 if (Reorder_[Indices_[j]] == -1) {
521 for (
size_t k = 0 ; k < NumVectors ; ++k)
522 ReducedRHS_ptr[k][i] -= (RangeScalar)Values_[j] * (RangeScalar)LHS_ptr[k][Indices_[j]];
528 template<
class MatrixType>
530 Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& LHS)
532 this->
template UpdateLHSTempl<Scalar,Scalar>(ReducedLHS, LHS);
535 template<
class MatrixType>
536 template<
class DomainScalar,
class RangeScalar>
538 Tpetra::MultiVector<RangeScalar,LocalOrdinal,GlobalOrdinal,Node>& LHS)
541 Teuchos::ArrayRCP<Teuchos::ArrayRCP<RangeScalar> > LHS_ptr = LHS.get2dViewNonConst();
542 Teuchos::ArrayRCP<Teuchos::ArrayRCP<const DomainScalar> > ReducedLHS_ptr = ReducedLHS.get2dView();
544 for (
size_t i = 0 ; i < NumRows_ ; ++i)
545 for (
size_t k = 0 ; k < LHS.getNumVectors() ; ++k)
546 LHS_ptr[k][InvReorder_[i]] = (RangeScalar)ReducedLHS_ptr[k][i];
549 template<
class MatrixType>
552 throw std::runtime_error(
"Ifpack2::SingletonFilter does not implement getFrobeniusNorm.");
557 #define IFPACK2_SINGLETONFILTER_INSTANT(S,LO,GO,N) \ 558 template class Ifpack2::SingletonFilter< Tpetra::RowMatrix<S, LO, GO, N> >; virtual void rightScale(const Tpetra::Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &x)
Scales the RowMatrix on the right with the Vector x.
Definition: Ifpack2_SingletonFilter_def.hpp:399
virtual ~SingletonFilter()
Destructor.
Definition: Ifpack2_SingletonFilter_def.hpp:143
virtual bool isLocallyIndexed() const
If matrix indices are in the local range, this function returns true. Otherwise, this function return...
Definition: Ifpack2_SingletonFilter_def.hpp:271
virtual global_size_t getGlobalNumRows() const
Returns the number of global rows in this matrix.
Definition: Ifpack2_SingletonFilter_def.hpp:199
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_SingletonFilter_def.hpp:350
virtual size_t getGlobalMaxNumRowEntries() const
Returns the maximum number of entries across all rows/columns on all nodes.
Definition: Ifpack2_SingletonFilter_def.hpp:253
virtual Teuchos::RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > getRangeMap() const
Returns the Map that describes the range distribution in this matrix.
Definition: Ifpack2_SingletonFilter_def.hpp:184
virtual void UpdateLHS(const Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &ReducedLHS, Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &LHS)
Updates a full LHS from a reduces LHS.
Definition: Ifpack2_SingletonFilter_def.hpp:529
virtual void CreateReducedRHS(const Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &LHS, const Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &RHS, Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &ReducedRHS)
Creates a RHS for the reduced singleton-free system.
Definition: Ifpack2_SingletonFilter_def.hpp:491
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_SingletonFilter_def.hpp:368
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_SingletonFilter_def.hpp:311
virtual Teuchos::RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > getDomainMap() const
Returns the Map that describes the domain distribution in this matrix.
Definition: Ifpack2_SingletonFilter_def.hpp:175
virtual Teuchos::RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > getRowMap() const
Returns the Map that describes the row distribution in this matrix.
Definition: Ifpack2_SingletonFilter_def.hpp:157
virtual size_t getNodeNumRows() const
Returns the number of rows owned on the calling node.
Definition: Ifpack2_SingletonFilter_def.hpp:211
virtual bool hasTransposeApply() const
Indicates whether this operator supports applying the adjoint operator.
Definition: Ifpack2_SingletonFilter_def.hpp:450
virtual Teuchos::RCP< const Teuchos::Comm< int > > getComm() const
Returns the communicator.
Definition: Ifpack2_SingletonFilter_def.hpp:147
virtual mag_type getFrobeniusNorm() const
Returns the Frobenius norm of the matrix.
Definition: Ifpack2_SingletonFilter_def.hpp:550
virtual size_t getNumEntriesInGlobalRow(GlobalOrdinal globalRow) const
Returns the current number of entries on this node in the specified global row.
Definition: Ifpack2_SingletonFilter_def.hpp:241
virtual size_t getNodeNumEntries() const
Returns the local number of entries in this matrix.
Definition: Ifpack2_SingletonFilter_def.hpp:235
virtual global_size_t getGlobalNumCols() const
Returns the number of global columns in this matrix.
Definition: Ifpack2_SingletonFilter_def.hpp:205
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_SingletonFilter_def.hpp:405
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_SingletonFilter_def.hpp:386
virtual bool hasColMap() const
Indicates whether this matrix has a well-defined column map.
Definition: Ifpack2_SingletonFilter_def.hpp:265
virtual Teuchos::RCP< const Tpetra::RowGraph< LocalOrdinal, GlobalOrdinal, Node > > getGraph() const
Returns the RowGraph associated with this matrix.
Definition: Ifpack2_SingletonFilter_def.hpp:193
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_SingletonFilter_def.hpp:217
SingletonFilter(const Teuchos::RCP< const Tpetra::RowMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &Matrix)
Constructor.
Definition: Ifpack2_SingletonFilter_def.hpp:56
virtual size_t getNumEntriesInLocalRow(LocalOrdinal localRow) const
Returns the current number of entries on this node in the specified local row.
Definition: Ifpack2_SingletonFilter_def.hpp:247
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_SingletonFilter_def.hpp:290
virtual void SolveSingletons(const Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &RHS, Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &LHS)
Solve the singleton components of the linear system.
Definition: Ifpack2_SingletonFilter_def.hpp:462
virtual bool supportsRowViews() const
Returns true if RowViews are supported.
Definition: Ifpack2_SingletonFilter_def.hpp:456
Preconditioners and smoothers for Tpetra sparse matrices.
Definition: Ifpack2_AdditiveSchwarz_decl.hpp:73
virtual Teuchos::RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > getColMap() const
Returns the Map that describes the column distribution in this matrix.
Definition: Ifpack2_SingletonFilter_def.hpp:166
Filter based on matrix entries.
Definition: Ifpack2_SingletonFilter_decl.hpp:64
virtual void leftScale(const Tpetra::Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &x)
Scales the RowMatrix on the left with the Vector x.
Definition: Ifpack2_SingletonFilter_def.hpp:393
virtual bool isFillComplete() const
Returns true if fillComplete() has been called.
Definition: Ifpack2_SingletonFilter_def.hpp:283
virtual size_t getNodeMaxNumRowEntries() const
Returns the maximum number of entries across all rows/columns on this node.
Definition: Ifpack2_SingletonFilter_def.hpp:259
virtual global_size_t getGlobalNumEntries() const
Returns the global number of entries in this matrix.
Definition: Ifpack2_SingletonFilter_def.hpp:229
virtual GlobalOrdinal getIndexBase() const
Returns the index base for global indices for this matrix.
Definition: Ifpack2_SingletonFilter_def.hpp:223
virtual bool isGloballyIndexed() const
If matrix indices are in the global range, this function returns true. Otherwise, this function retur...
Definition: Ifpack2_SingletonFilter_def.hpp:277