43#ifndef IFPACK2_REORDERFILTER_DEF_HPP
44#define IFPACK2_REORDERFILTER_DEF_HPP
45#include "Ifpack2_ReorderFilter_decl.hpp"
48#include "Tpetra_ConfigDefs.hpp"
49#include "Tpetra_RowMatrix.hpp"
50#include "Tpetra_Map.hpp"
51#include "Tpetra_MultiVector.hpp"
52#include "Tpetra_Vector.hpp"
56template<
class MatrixType>
59 const Teuchos::ArrayRCP<local_ordinal_type>& perm,
60 const Teuchos::ArrayRCP<local_ordinal_type>& reverseperm)
63 reverseperm_ (reverseperm)
65 TEUCHOS_TEST_FOR_EXCEPTION(
66 A_.is_null (), std::invalid_argument,
67 "Ifpack2::ReorderFilter: The input matrix is null.");
70 TEUCHOS_TEST_FOR_EXCEPTION(
71 A_->getComm()->getSize() != 1, std::invalid_argument,
72 "Ifpack2::ReorderFilter: This class may only be used if the input matrix's "
73 "communicator has one process. This class is an implementation detail of "
74 "Ifpack2::AdditiveSchwarz, and it is not meant to be used otherwise.");
76 TEUCHOS_TEST_FOR_EXCEPTION(
77 A_->getLocalNumRows () != A_->getGlobalNumRows (),
78 std::invalid_argument,
79 "Ifpack2::ReorderFilter: The input matrix is not square.");
82 Kokkos::resize(Indices_,A_->getLocalMaxNumRowEntries ());
83 Kokkos::resize(Values_,A_->getLocalMaxNumRowEntries ());
87template<
class MatrixType>
91template<
class MatrixType>
100template<
class MatrixType>
101Teuchos::RCP<const typename ReorderFilter<MatrixType>::map_type>
104 TEUCHOS_TEST_FOR_EXCEPTION(
105 A_.is_null (), std::runtime_error,
"Ifpack2::ReorderFilter::"
106 "getRowMap: The matrix A is null, so there is no row Map.");
108 return A_->getRowMap ();
112template<
class MatrixType>
113Teuchos::RCP<const typename ReorderFilter<MatrixType>::map_type>
116 TEUCHOS_TEST_FOR_EXCEPTION(
117 A_.is_null (), std::runtime_error,
"Ifpack2::ReorderFilter::"
118 "getColMap: The matrix A is null, so there is no column Map.");
120 return A_->getColMap();
124template<
class MatrixType>
125Teuchos::RCP<const typename ReorderFilter<MatrixType>::map_type>
128 TEUCHOS_TEST_FOR_EXCEPTION(
129 A_.is_null (), std::runtime_error,
"Ifpack2::ReorderFilter::"
130 "getDomainMap: The matrix A is null, so there is no domain Map.");
132 return A_->getDomainMap();
136template<
class MatrixType>
137Teuchos::RCP<const typename ReorderFilter<MatrixType>::map_type>
140 TEUCHOS_TEST_FOR_EXCEPTION(
141 A_.is_null (), std::runtime_error,
"Ifpack2::ReorderFilter::"
142 "getRangeMap: The matrix A is null, so there is no range Map.");
144 return A_->getRangeMap();
148template<
class MatrixType>
149Teuchos::RCP<
const Tpetra::RowGraph<
typename MatrixType::local_ordinal_type,
150 typename MatrixType::global_ordinal_type,
151 typename MatrixType::node_type> >
154 throw std::runtime_error(
"Ifpack2::ReorderFilter: does not support getGraph.");
158template<
class MatrixType>
161 return A_->getGlobalNumRows();
165template<
class MatrixType>
168 return A_->getGlobalNumCols();
172template<
class MatrixType>
175 return A_->getLocalNumRows();
179template<
class MatrixType>
182 return A_->getLocalNumCols();
186template<
class MatrixType>
189 return A_->getIndexBase();
193template<
class MatrixType>
196 return A_->getGlobalNumEntries();
200template<
class MatrixType>
203 return A_->getLocalNumEntries();
206template<
class MatrixType>
209 return A_->getBlockSize();
212template<
class MatrixType>
216 if (A_.is_null () || A_->getRowMap ().is_null ()) {
217 return Teuchos::OrdinalTraits<size_t>::invalid ();
220 const local_ordinal_type lclRow =
221 A_->getRowMap ()->getLocalElement (globalRow);
222 if (lclRow == Teuchos::OrdinalTraits<local_ordinal_type>::invalid ()) {
224 return static_cast<size_t> (0);
226 const local_ordinal_type origLclRow = reverseperm_[lclRow];
227 return A_->getNumEntriesInLocalRow (origLclRow);
232template<
class MatrixType>
238 if (A_->getRowMap ()->isNodeLocalElement (localRow)) {
240 const local_ordinal_type localReorderedRow = reverseperm_[localRow];
241 return A_->getNumEntriesInLocalRow (localReorderedRow);
244 return static_cast<size_t> (0);
249template<
class MatrixType>
252 return A_->getGlobalMaxNumRowEntries();
256template<
class MatrixType>
259 return A_->getLocalMaxNumRowEntries();
263template<
class MatrixType>
270template<
class MatrixType>
273 return A_->isLocallyIndexed();
277template<
class MatrixType>
280 return A_->isGloballyIndexed();
284template<
class MatrixType>
287 return A_->isFillComplete();
291template<
class MatrixType>
294 nonconst_global_inds_host_view_type &globalInd,
295 nonconst_values_host_view_type &val,
296 size_t& numEntries)
const
298 using Teuchos::Array;
299 using Teuchos::ArrayView;
300 using Teuchos::av_reinterpret_cast;
301 typedef local_ordinal_type LO;
302 typedef Teuchos::OrdinalTraits<LO> OTLO;
304 const map_type& rowMap = * (A_->getRowMap ());
305 const local_ordinal_type localRow = rowMap.getLocalElement (globalRow);
306 TEUCHOS_TEST_FOR_EXCEPTION(
307 localRow == OTLO::invalid (), std::invalid_argument,
"Ifpack2::Reorder"
308 "Filter::getGlobalRowCopy: The given global row index " << globalRow
309 <<
" is not owned by the calling process with rank "
310 << rowMap.getComm ()->getRank () <<
".");
317 for (
size_t k = 0; k < numEntries; ++k) {
318 globalInd[k] = rowMap.getGlobalElement (Indices_[k]);
323template<
class MatrixType>
326 nonconst_local_inds_host_view_type &Indices,
327 nonconst_values_host_view_type &Values,
328 size_t& NumEntries)
const
331 TEUCHOS_TEST_FOR_EXCEPTION(
332 ! A_->getRowMap ()->isNodeLocalElement (LocalRow),
333 std::invalid_argument,
334 "Ifpack2::ReorderFilter::getLocalRowCopy: The given local row index "
335 << LocalRow <<
" is not a valid local row index on the calling process "
336 "with rank " << A_->getRowMap ()->getComm ()->getRank () <<
".");
340 const local_ordinal_type origLclRow = reverseperm_[LocalRow];
341 const size_t numEntries = A_->getNumEntriesInLocalRow (origLclRow);
343 TEUCHOS_TEST_FOR_EXCEPTION(
344 static_cast<size_t> (Indices.size ()) < numEntries ||
345 static_cast<size_t> (Values.size ()) < numEntries,
346 std::invalid_argument,
347 "Ifpack2::ReorderFilter::getLocalRowCopy: The given array views are not "
348 "long enough to store all the data in the given row " << LocalRow
349 <<
". Indices.size() = " << Indices.size () <<
", Values.size() = "
350 << Values.size () <<
", but the (original) row has " << numEntries
353 A_->getLocalRowCopy (origLclRow, Indices, Values, NumEntries);
358 for (
size_t i = 0; i < NumEntries; ++i) {
359 Indices[i] = perm_[Indices[i]];
364template<
class MatrixType>
366 global_inds_host_view_type &,
367 values_host_view_type &)
const
369 throw std::runtime_error(
"Ifpack2::ReorderFilter: does not support getGlobalRowView.");
374template<
class MatrixType>
376 local_inds_host_view_type & ,
377 values_host_view_type & )
const
379 throw std::runtime_error(
"Ifpack2::ReorderFilter: does not support getLocalRowView.");
384template<
class MatrixType>
386getLocalDiagCopy (Tpetra::Vector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> &diag)
const
389 return A_->getLocalDiagCopy(diag);
393template<
class MatrixType>
396 throw std::runtime_error(
"Ifpack2::ReorderFilter does not support leftScale.");
400template<
class MatrixType>
403 throw std::runtime_error(
"Ifpack2::ReorderFilter does not support rightScale.");
407template<
class MatrixType>
409apply (
const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> &X,
410 Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> &Y,
411 Teuchos::ETransp mode,
413 scalar_type beta)
const
415 typedef Teuchos::ScalarTraits<scalar_type> STS;
417 TEUCHOS_TEST_FOR_EXCEPTION(
418 alpha != STS::one () || beta != STS::zero (), std::logic_error,
419 "Ifpack2::ReorderFilter::apply is only implemented for alpha = 1 and "
420 "beta = 0. You set alpha = " << alpha <<
" and beta = " << beta <<
".");
424 TEUCHOS_TEST_FOR_EXCEPTION(
425 X.getNumVectors() != Y.getNumVectors(), std::runtime_error,
426 "Ifpack2::ReorderFilter::apply: X.getNumVectors() != Y.getNumVectors().");
428 const scalar_type zero = STS::zero ();
429 Teuchos::ArrayRCP<Teuchos::ArrayRCP<const scalar_type> > x_ptr = X.get2dView();
430 Teuchos::ArrayRCP<Teuchos::ArrayRCP<scalar_type> > y_ptr = Y.get2dViewNonConst();
433 const size_t NumVectors = Y.getNumVectors ();
435 for (
size_t i = 0; i < A_->getLocalNumRows (); ++i) {
439 scalar_type* Values =
reinterpret_cast<scalar_type*
>(Values_.data());
440 if (mode == Teuchos::NO_TRANS) {
441 for (
size_t j = 0; j < Nnz; ++j) {
442 for (
size_t k = 0; k < NumVectors; ++k) {
443 y_ptr[k][i] += Values[j] * x_ptr[k][Indices_[j]];
447 else if (mode == Teuchos::TRANS) {
448 for (
size_t j = 0; j < Nnz; ++j) {
449 for (
size_t k = 0; k < NumVectors; ++k) {
450 y_ptr[k][Indices_[j]] += Values[j] * x_ptr[k][i];
455 for (
size_t j = 0; j < Nnz; ++j) {
456 for (
size_t k = 0; k < NumVectors; ++k) {
457 y_ptr[k][Indices_[j]] += STS::conjugate(Values[j]) * x_ptr[k][i];
465template<
class MatrixType>
472template<
class MatrixType>
479template<
class MatrixType>
483 return A_->getFrobeniusNorm ();
487template<
class MatrixType>
489permuteOriginalToReordered (
const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> &originalX,
490 Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> &reorderedY)
const
492 this->
template permuteOriginalToReorderedTempl<scalar_type,scalar_type>(originalX, reorderedY);
496template<
class MatrixType>
497template<
class DomainScalar,
class RangeScalar>
498void ReorderFilter<MatrixType>::permuteOriginalToReorderedTempl(
const Tpetra::MultiVector<DomainScalar,local_ordinal_type,global_ordinal_type,node_type> &originalX,
499 Tpetra::MultiVector<RangeScalar,local_ordinal_type,global_ordinal_type,node_type> &reorderedY)
const
501 TEUCHOS_TEST_FOR_EXCEPTION(originalX.getNumVectors() != reorderedY.getNumVectors(), std::runtime_error,
502 "Ifpack2::ReorderFilter::permuteOriginalToReordered ERROR: X.getNumVectors() != Y.getNumVectors().");
504 Teuchos::ArrayRCP<Teuchos::ArrayRCP<const DomainScalar> > x_ptr = originalX.get2dView();
505 Teuchos::ArrayRCP<Teuchos::ArrayRCP<RangeScalar> > y_ptr = reorderedY.get2dViewNonConst();
509 for(
size_t k=0; k < originalX.getNumVectors(); k++)
512 y_ptr[k][perm_[i]*blockSize + j] = (RangeScalar)x_ptr[k][i*blockSize + j];
516template<
class MatrixType>
518 Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> &originalY)
const
520 this->
template permuteReorderedToOriginalTempl<scalar_type,scalar_type>(reorderedX, originalY);
524template<
class MatrixType>
525template<
class DomainScalar,
class RangeScalar>
528 Tpetra::MultiVector<RangeScalar,local_ordinal_type,global_ordinal_type,node_type> &originalY)
const
530 TEUCHOS_TEST_FOR_EXCEPTION(
531 reorderedX.getNumVectors() != originalY.getNumVectors(),
533 "Ifpack2::ReorderFilter::permuteReorderedToOriginal: "
534 "X.getNumVectors() != Y.getNumVectors().");
536#ifdef HAVE_IFPACK2_DEBUG
538 typedef Teuchos::ScalarTraits<DomainScalar> STS;
539 typedef Teuchos::ScalarTraits<magnitude_type> STM;
540 Teuchos::Array<magnitude_type> norms (reorderedX.getNumVectors ());
541 reorderedX.norm2 (norms ());
544 j < reorderedX.getNumVectors (); ++j) {
545 if (STM::isnaninf (norms[j])) {
550 TEUCHOS_TEST_FOR_EXCEPTION(
551 ! good, std::runtime_error,
"Ifpack2::ReorderFilter::"
552 "permuteReorderedToOriginalTempl: The 2-norm of the input reorderedX is "
557 Teuchos::ArrayRCP<Teuchos::ArrayRCP<const DomainScalar> > x_ptr = reorderedX.get2dView();
558 Teuchos::ArrayRCP<Teuchos::ArrayRCP<RangeScalar> > y_ptr = originalY.get2dViewNonConst();
562 for (
size_t k = 0; k < reorderedX.getNumVectors (); ++k) {
565 y_ptr[k][reverseperm_[i]*blockSize + j] = (RangeScalar) x_ptr[k][i*blockSize + j];
570#ifdef HAVE_IFPACK2_DEBUG
572 typedef Teuchos::ScalarTraits<RangeScalar> STS;
573 typedef Teuchos::ScalarTraits<magnitude_type> STM;
574 Teuchos::Array<magnitude_type> norms (originalY.getNumVectors ());
575 originalY.norm2 (norms ());
578 j < originalY.getNumVectors (); ++j) {
579 if (STM::isnaninf (norms[j])) {
584 TEUCHOS_TEST_FOR_EXCEPTION(
585 ! good, std::runtime_error,
"Ifpack2::ReorderFilter::"
586 "permuteReorderedToOriginalTempl: The 2-norm of the output originalY is "
594#define IFPACK2_REORDERFILTER_INSTANT(S,LO,GO,N) \
595 template class Ifpack2::ReorderFilter< Tpetra::RowMatrix<S, LO, GO, N> >;
Wraps a Tpetra::RowMatrix in a filter that reorders local rows and columns.
Definition Ifpack2_ReorderFilter_decl.hpp:70
virtual local_ordinal_type getBlockSize() const
The number of degrees of freedom per mesh point.
Definition Ifpack2_ReorderFilter_def.hpp:207
virtual size_t getLocalNumCols() const
Returns the number of columns needed to apply the forward operator on this node, i....
Definition Ifpack2_ReorderFilter_def.hpp:180
virtual size_t getNumEntriesInGlobalRow(global_ordinal_type globalRow) const
The current number of entries in this matrix, stored on the calling process, in the row whose global ...
Definition Ifpack2_ReorderFilter_def.hpp:214
virtual mag_type getFrobeniusNorm() const
Returns the Frobenius norm of the matrix.
Definition Ifpack2_ReorderFilter_def.hpp:480
virtual size_t getGlobalMaxNumRowEntries() const
Returns the maximum number of entries across all rows/columns on all nodes.
Definition Ifpack2_ReorderFilter_def.hpp:250
virtual Teuchos::RCP< const map_type > getColMap() const
Returns the Map that describes the column distribution in this matrix.
Definition Ifpack2_ReorderFilter_def.hpp:114
virtual void rightScale(const Tpetra::Vector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > &x)
Scales the RowMatrix on the right with the Vector x.
Definition Ifpack2_ReorderFilter_def.hpp:401
virtual bool hasColMap() const
Indicates whether this matrix has a well-defined column map.
Definition Ifpack2_ReorderFilter_def.hpp:264
virtual size_t getLocalMaxNumRowEntries() const
Returns the maximum number of entries across all rows/columns on this node.
Definition Ifpack2_ReorderFilter_def.hpp:257
virtual void permuteReorderedToOriginal(const Tpetra::MultiVector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > &reorderedX, Tpetra::MultiVector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > &originalY) const
Permute multivector: reordered-to-original.
Definition Ifpack2_ReorderFilter_def.hpp:517
virtual void permuteOriginalToReordered(const Tpetra::MultiVector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > &originalX, Tpetra::MultiVector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > &reorderedY) const
Permute multivector: original-to-reordered.
Definition Ifpack2_ReorderFilter_def.hpp:489
virtual Teuchos::RCP< const map_type > getDomainMap() const
Returns the Map that describes the domain distribution in this matrix.
Definition Ifpack2_ReorderFilter_def.hpp:126
virtual bool supportsRowViews() const
Returns true if RowViews are supported.
Definition Ifpack2_ReorderFilter_def.hpp:473
virtual global_ordinal_type getIndexBase() const
Returns the index base for global indices for this matrix.
Definition Ifpack2_ReorderFilter_def.hpp:187
virtual void getLocalDiagCopy(Tpetra::Vector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > &diag) const
Get a copy of the diagonal entries owned by this node, with local row indices.
Definition Ifpack2_ReorderFilter_def.hpp:386
virtual bool isGloballyIndexed() const
If matrix indices are in the global range, this function returns true. Otherwise, this function retur...
Definition Ifpack2_ReorderFilter_def.hpp:278
virtual Teuchos::RCP< const map_type > getRowMap() const
Returns the Map that describes the row distribution in this matrix.
Definition Ifpack2_ReorderFilter_def.hpp:102
virtual size_t getLocalNumRows() const
Returns the number of rows owned on the calling node.
Definition Ifpack2_ReorderFilter_def.hpp:173
virtual Teuchos::RCP< const map_type > getRangeMap() const
Returns the Map that describes the range distribution in this matrix.
Definition Ifpack2_ReorderFilter_def.hpp:138
virtual Teuchos::RCP< const Teuchos::Comm< int > > getComm() const
The matrix's communicator.
Definition Ifpack2_ReorderFilter_def.hpp:92
ReorderFilter(const Teuchos::RCP< const row_matrix_type > &A, const Teuchos::ArrayRCP< local_ordinal_type > &perm, const Teuchos::ArrayRCP< local_ordinal_type > &reverseperm)
Constructor.
Definition Ifpack2_ReorderFilter_def.hpp:58
virtual bool isFillComplete() const
Returns true if fillComplete() has been called.
Definition Ifpack2_ReorderFilter_def.hpp:285
virtual bool isLocallyIndexed() const
If matrix indices are in the local range, this function returns true. Otherwise, this function return...
Definition Ifpack2_ReorderFilter_def.hpp:271
virtual size_t getLocalNumEntries() const
Returns the local number of entries in this matrix.
Definition Ifpack2_ReorderFilter_def.hpp:201
virtual ~ReorderFilter()
Destructor.
Definition Ifpack2_ReorderFilter_def.hpp:88
virtual void getGlobalRowCopy(global_ordinal_type 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_ReorderFilter_def.hpp:293
virtual void getLocalRowView(local_ordinal_type 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_ReorderFilter_def.hpp:375
virtual size_t getNumEntriesInLocalRow(local_ordinal_type localRow) const
The current number of entries in this matrix, stored on the calling process, in the row whose local i...
Definition Ifpack2_ReorderFilter_def.hpp:234
virtual void getLocalRowCopy(local_ordinal_type 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_ReorderFilter_def.hpp:325
virtual Teuchos::RCP< const Tpetra::RowGraph< local_ordinal_type, global_ordinal_type, node_type > > getGraph() const
Returns the RowGraph associated with this matrix.
Definition Ifpack2_ReorderFilter_def.hpp:152
virtual void apply(const Tpetra::MultiVector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > &X, Tpetra::MultiVector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, scalar_type alpha=Teuchos::ScalarTraits< scalar_type >::one(), scalar_type beta=Teuchos::ScalarTraits< scalar_type >::zero()) const
, where Op(A) is either A, , or .
Definition Ifpack2_ReorderFilter_def.hpp:409
virtual global_size_t getGlobalNumRows() const
Returns the number of global rows in this matrix.
Definition Ifpack2_ReorderFilter_def.hpp:159
virtual bool hasTransposeApply() const
Whether apply() can apply the transpose or conjugate transpose.
Definition Ifpack2_ReorderFilter_def.hpp:466
virtual global_size_t getGlobalNumCols() const
Returns the number of global columns in this matrix.
Definition Ifpack2_ReorderFilter_def.hpp:166
virtual global_size_t getGlobalNumEntries() const
Returns the global number of entries in this matrix.
Definition Ifpack2_ReorderFilter_def.hpp:194
virtual void leftScale(const Tpetra::Vector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > &x)
Scales the RowMatrix on the left with the Vector x.
Definition Ifpack2_ReorderFilter_def.hpp:394
virtual void getGlobalRowView(global_ordinal_type 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_ReorderFilter_def.hpp:365
Preconditioners and smoothers for Tpetra sparse matrices.
Definition Ifpack2_AdditiveSchwarz_decl.hpp:74