43 #ifndef IFPACK2_CONTAINER_HPP
44 #define IFPACK2_CONTAINER_HPP
49 #include "Ifpack2_ConfigDefs.hpp"
50 #include "Tpetra_RowMatrix.hpp"
51 #include "Teuchos_Describable.hpp"
52 #include <Ifpack2_Partitioner.hpp>
53 #include <Tpetra_Map.hpp>
54 #include <Tpetra_Experimental_BlockCrsMatrix_decl.hpp>
58 #include <type_traits>
60 #ifndef DOXYGEN_SHOULD_SKIP_THIS
65 #endif // DOXYGEN_SHOULD_SKIP_THIS
113 template<
class MatrixType>
118 typedef typename MatrixType::scalar_type scalar_type;
119 typedef typename MatrixType::local_ordinal_type local_ordinal_type;
120 typedef typename MatrixType::global_ordinal_type global_ordinal_type;
121 typedef typename MatrixType::node_type node_type;
122 typedef Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type> mv_type;
123 typedef Tpetra::Vector<scalar_type, local_ordinal_type, global_ordinal_type, node_type> vector_type;
124 typedef Tpetra::Map<local_ordinal_type, global_ordinal_type, node_type> map_type;
126 typedef Tpetra::Import<local_ordinal_type, global_ordinal_type, node_type> import_type;
128 typedef Tpetra::Experimental::BlockCrsMatrix<scalar_type, local_ordinal_type, global_ordinal_type, node_type> block_crs_matrix_type;
129 typedef Tpetra::RowMatrix<scalar_type, local_ordinal_type, global_ordinal_type, node_type> row_matrix_type;
131 static_assert(std::is_same<MatrixType, row_matrix_type>::value,
132 "Ifpack2::Container: Please use MatrixType = Tpetra::RowMatrix.");
135 typedef typename Kokkos::Details::ArithTraits<scalar_type>::val_type
impl_scalar_type;
139 typedef typename mv_type::dual_view_type::t_host HostView;
153 scalar_type DampingFactor) :
170 Teuchos::rcp_dynamic_cast<
const block_crs_matrix_type>(
inputMatrix_);
207 const block_crs_matrix_type* global_bcrs =
208 Teuchos::rcp_dynamic_cast<
const block_crs_matrix_type>(
inputMatrix_).
get();
214 for(local_ordinal_type i = 0; i < localRows.
size(); i++)
257 local_ordinal_type totalBlockRows = 0;
263 local_ordinal_type rowsInBlock = partitions[i].size();
266 totalBlockRows += rowsInBlock;
270 local_ordinal_type iter = 0;
280 void getMatDiag()
const
296 void DoJacobi(HostView& X, HostView& Y,
int stride)
const;
297 void DoOverlappingJacobi(HostView& X, HostView& Y, HostView& W,
int stride)
const;
298 void DoGaussSeidel(HostView& X, HostView& Y, HostView& Y2,
int stride)
const;
299 void DoSGS(HostView& X, HostView& Y, HostView& Y2,
int stride)
const;
347 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
348 HostView XView = X.template getLocalView<Kokkos::HostSpace>();
349 HostView YView = Y.template getLocalView<Kokkos::HostSpace>();
351 HostView XView = X.getLocalViewHost();
352 HostView YView = Y.getLocalViewHost();
354 this->
apply (XView, YView, 0, X.getStride());
392 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
393 HostView XView = X.template getLocalView<Kokkos::HostSpace>();
394 HostView YView = Y.template getLocalView<Kokkos::HostSpace>();
395 HostView WView = W.template getLocalView<Kokkos::HostSpace>();
397 HostView XView = X.getLocalViewHost();
398 HostView YView = Y.getLocalViewHost();
399 HostView WView = W.getLocalViewHost();
404 virtual void clearBlocks();
407 virtual std::ostream&
print (std::ostream& os)
const = 0;
451 template <
class MatrixType>
453 operator<< (std::ostream& os, const Ifpack2::Container<MatrixType>& obj)
455 return obj.print (os);
458 template <
class MatrixType>
459 void Container<MatrixType>::DoJacobi(HostView& X, HostView& Y,
int stride)
const
461 const scalar_type one = STS::one();
464 size_t numVecs = X.extent(1);
466 for (local_ordinal_type i = 0; i < numBlocks_; i++)
469 if(blockRows_[i] != 1 || hasBlockCrs_)
471 if(blockRows_[i] == 0 )
477 local_ordinal_type LRID = partitions_[partitionIndices_[i]];
479 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
480 HostView diagView = Diag_->template getLocalView<Kokkos::HostSpace>();
482 HostView diagView = Diag_->getLocalViewHost();
484 impl_scalar_type d = (impl_scalar_type) one / diagView(LRID, 0);
485 for(
size_t nv = 0; nv < numVecs; nv++)
487 impl_scalar_type x = X(LRID, nv);
494 template <
class MatrixType>
495 void Container<MatrixType>::DoOverlappingJacobi(HostView& X, HostView& Y, HostView& W,
int stride)
const
498 for(local_ordinal_type i = 0; i < numBlocks_; i++)
501 if(blockRows_[i] == 0)
503 if(blockRows_[i] != 1)
504 weightedApply(X, Y, W, i, stride,
Teuchos::NO_TRANS, DampingFactor_, STS::one());
508 template<
class MatrixType>
509 void Container<MatrixType>::DoGaussSeidel(HostView& X, HostView& Y, HostView& Y2,
int stride)
const
517 using Teuchos::rcpFromRef;
519 const scalar_type one = STS::one();
520 const size_t Length = inputMatrix_->getNodeMaxNumRowEntries();
521 auto numVecs = X.extent(1);
522 Array<scalar_type> Values;
523 Array<local_ordinal_type> Indices;
524 Indices.resize(Length);
526 Values.resize(bcrsBlockSize_ * bcrsBlockSize_ * Length);
531 HostView Resid(
"", X.extent(0), X.extent(1));
532 for(local_ordinal_type i = 0; i < numBlocks_; i++)
534 if(blockRows_[i] > 1 || hasBlockCrs_)
536 if (blockRows_[i] == 0)
539 ArrayView<const local_ordinal_type> localRows = getLocalRows (i);
540 const size_t localNumRows = blockRows_[i];
541 for(
size_t j = 0; j < localNumRows; j++)
543 const local_ordinal_type LID = localRows[j];
545 inputMatrix_->getLocalRowCopy(LID, Indices(), Values(), NumEntries);
546 for(
size_t m = 0; m < numVecs; m++)
550 for (
int localR = 0; localR < bcrsBlockSize_; localR++)
551 Resid(LID * bcrsBlockSize_ + localR, m) = X(LID * bcrsBlockSize_ + localR, m);
552 for (
size_t k = 0; k < NumEntries; ++k)
554 const local_ordinal_type col = Indices[k];
555 for (
int localR = 0; localR < bcrsBlockSize_; localR++)
557 for(
int localC = 0; localC < bcrsBlockSize_; localC++)
559 Resid(LID * bcrsBlockSize_ + localR, m) -=
560 Values[k * bcrsBlockSize_ * bcrsBlockSize_ + localR + localC * bcrsBlockSize_]
561 * Y2(col * bcrsBlockSize_ + localC, m);
568 Resid(LID, m) = X(LID, m);
569 for (
size_t k = 0; k < NumEntries; ++k)
571 const local_ordinal_type col = Indices[k];
572 Resid(LID, m) -= Values[k] * Y2(col, m);
585 else if(blockRows_[i] == 1)
589 local_ordinal_type LRID = partitionIndices_[i];
591 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
592 HostView diagView = Diag_->template getLocalView<Kokkos::HostSpace>();
594 HostView diagView = Diag_->getLocalViewHost();
596 impl_scalar_type d = (impl_scalar_type) one / diagView(LRID, 0);
597 for(
size_t m = 0; m < numVecs; m++)
599 impl_scalar_type x = X(LRID, m);
600 impl_scalar_type newy = x * d;
607 auto numMyRows = inputMatrix_->getNodeNumRows();
608 for (
size_t m = 0; m < numVecs; ++m)
610 for (
size_t i = 0; i < numMyRows * bcrsBlockSize_; ++i)
618 template<
class MatrixType>
619 void Container<MatrixType>::DoSGS(HostView& X, HostView& Y, HostView& Y2,
int stride)
const
628 using Teuchos::rcpFromRef;
629 const scalar_type one = STS::one();
630 auto numVecs = X.extent(1);
631 const size_t Length = inputMatrix_->getNodeMaxNumRowEntries();
632 Array<scalar_type> Values;
633 Array<local_ordinal_type> Indices(Length);
634 Values.resize(bcrsBlockSize_ * bcrsBlockSize_ * Length);
639 HostView Resid(
"", X.extent(0), X.extent(1));
641 for(local_ordinal_type i = 0; i < numBlocks_; i++)
643 if(blockRows_[i] != 1 || hasBlockCrs_)
645 if(blockRows_[i] == 0)
648 ArrayView<const local_ordinal_type> localRows = getLocalRows(i);
649 for(local_ordinal_type j = 0; j < blockRows_[i]; j++)
651 const local_ordinal_type LID = localRows[j];
653 inputMatrix_->getLocalRowCopy(LID, Indices(), Values(), NumEntries);
655 for(
size_t m = 0; m < numVecs; m++)
659 for(
int localR = 0; localR < bcrsBlockSize_; localR++)
660 Resid(LID * bcrsBlockSize_ + localR, m) = X(LID * bcrsBlockSize_ + localR, m);
661 for(
size_t k = 0; k < NumEntries; ++k)
663 const local_ordinal_type col = Indices[k];
664 for (
int localR = 0; localR < bcrsBlockSize_; localR++)
666 for(
int localC = 0; localC < bcrsBlockSize_; localC++)
667 Resid(LID * bcrsBlockSize_ + localR, m) -=
668 Values[k * (bcrsBlockSize_ * bcrsBlockSize_) + (localR + localC * bcrsBlockSize_)]
669 * Y2(col * bcrsBlockSize_ + localC, m);
675 Resid(LID, m) = X(LID, m);
676 for(
size_t k = 0; k < NumEntries; k++)
678 local_ordinal_type col = Indices[k];
679 Resid(LID, m) -= Values[k] * Y2(col, m);
695 local_ordinal_type LRID = partitions_[partitionIndices_[i]];
697 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
698 HostView diagView = Diag_->template getLocalView<Kokkos::HostSpace>();
700 HostView diagView = Diag_->getLocalViewHost();
702 impl_scalar_type d = (impl_scalar_type) one / diagView(LRID, 0);
703 for(
size_t m = 0; m < numVecs; m++)
705 impl_scalar_type x = X(LRID, m);
706 impl_scalar_type newy = x * d;
718 for(local_ordinal_type i = numBlocks_; i > 0; --i)
720 if(hasBlockCrs_ || blockRows_[i-1] != 1)
722 if(blockRows_[i - 1] == 0)
725 ArrayView<const local_ordinal_type> localRows = getLocalRows(i-1);
726 for(local_ordinal_type j = 0; j < blockRows_[i-1]; j++)
728 const local_ordinal_type LID = localRows[j];
730 inputMatrix_->getLocalRowCopy(LID, Indices(), Values(), NumEntries);
732 for (
size_t m = 0; m < numVecs; m++)
736 for(
int localR = 0; localR < bcrsBlockSize_; localR++)
737 Resid(LID * bcrsBlockSize_ + localR, m) = X(LID * bcrsBlockSize_ + localR, m);
738 for(
size_t k = 0; k < NumEntries; ++k)
740 const local_ordinal_type col = Indices[k];
741 for(
int localR = 0; localR < bcrsBlockSize_; localR++)
743 for(
int localC = 0; localC < bcrsBlockSize_; localC++)
744 Resid(LID*bcrsBlockSize_+localR, m) -=
745 Values[k * (bcrsBlockSize_ * bcrsBlockSize_) + (localR + localC * bcrsBlockSize_)]
746 * Y2(col * bcrsBlockSize_ + localC, m);
752 Resid(LID, m) = X(LID, m);
753 for(
size_t k = 0; k < NumEntries; ++k)
755 local_ordinal_type col = Indices[k];
756 Resid(LID, m) -= Values[k] * Y2(col, m);
774 auto numMyRows = inputMatrix_->getNodeNumRows();
775 for (
size_t m = 0; m < numVecs; ++m)
777 for (
size_t i = 0; i < numMyRows * bcrsBlockSize_; ++i)
785 template<
class MatrixType>
786 void Container<MatrixType>::clearBlocks()
791 partitionIndices_.clear();
792 Diag_ = Teuchos::null;
803 template<
class MatrixType>
807 static std::string name () {
808 return std::string (
"Ifpack2::Container<") +
812 static std::string concreteName (const ::Ifpack2::Container<MatrixType>&) {
819 #endif // IFPACK2_CONTAINER_HPP
Teuchos::ArrayView< const local_ordinal_type > getLocalRows(int blockIndex) const
Local indices of the rows of the input matrix that belong to this block.
Definition: Ifpack2_Container.hpp:236
int OverlapLevel_
Number of rows of overlap for adjacent blocks.
Definition: Ifpack2_Container.hpp:433
global_ordinal_type NumGlobalNonzeros_
Number of nonzeros in input matrix.
Definition: Ifpack2_Container.hpp:443
virtual void initialize()=0
Do all set-up operations that only require matrix structure.
int numBlocks_
The number of blocks (partitions) in the container.
Definition: Ifpack2_Container.hpp:421
Teuchos::Array< local_ordinal_type > blockRows_
Number of rows in each block.
Definition: Ifpack2_Container.hpp:425
static std::string getName()
Definition: Ifpack2_Container.hpp:411
global_ordinal_type NumGlobalRows_
Number of global rows in input matrix.
Definition: Ifpack2_Container.hpp:441
bool hasBlockCrs_
Whether the input matrix is a BlockCRS matrix.
Definition: Ifpack2_Container.hpp:445
scalar_type DampingFactor_
Damping factor, passed to apply() as alpha.
Definition: Ifpack2_Container.hpp:435
Ifpack2::Partitioner:
Definition: Ifpack2_Partitioner.hpp:179
int bcrsBlockSize_
If hasBlockCrs_, the number of DOFs per vertex. Otherwise 1.
Definition: Ifpack2_Container.hpp:447
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
#define TEUCHOS_TEST_FOR_EXCEPT_MSG(throw_exception_test, msg)
virtual bool isComputed() const =0
Return true if the container has been successfully computed.
local_ordinal_type NumLocalRows_
Number of local rows in input matrix.
Definition: Ifpack2_Container.hpp:439
virtual void weightedApply(HostView &X, HostView &Y, HostView &W, int blockIndex, int stride, Teuchos::ETransp mode=Teuchos::NO_TRANS, scalar_type alpha=Teuchos::ScalarTraits< scalar_type >::one(), scalar_type beta=Teuchos::ScalarTraits< scalar_type >::zero()) const =0
Compute Y := alpha * diag(D) * M^{-1} (diag(D) * X) + beta*Y.
Teuchos::RCP< const row_matrix_type > inputMatrix_
The input matrix to the constructor.
Definition: Ifpack2_Container.hpp:418
Teuchos::RCP< vector_type > Diag_
Diagonal elements.
Definition: Ifpack2_Container.hpp:429
void resize(size_type new_size, const value_type &x=value_type())
void applyMV(mv_type &X, mv_type &Y) const
Wrapper for apply with MVs, used in unit tests (never called by BlockRelaxation)
Definition: Ifpack2_Container.hpp:345
Container(const Teuchos::RCP< const row_matrix_type > &matrix, const Teuchos::Array< Teuchos::Array< local_ordinal_type > > &partitions, const Teuchos::RCP< const import_type > &importer, int OverlapLevel, scalar_type DampingFactor)
Constructor.
Definition: Ifpack2_Container.hpp:149
Teuchos::RCP< const Tpetra::Import< local_ordinal_type, global_ordinal_type, node_type > > Importer_
Importer for importing off-process elements of MultiVectors.
Definition: Ifpack2_Container.hpp:437
bool IsParallel_
Whether the problem is distributed across multiple MPI processes.
Definition: Ifpack2_Container.hpp:431
void weightedApplyMV(mv_type &X, mv_type &Y, vector_type &W)
Wrapper for weightedApply with MVs, used in unit tests (never called by BlockRelaxation) ...
Definition: Ifpack2_Container.hpp:388
virtual void applyInverseJacobi(const mv_type &, mv_type &, bool, int) const
Compute Y := (1 - a) Y + a D^{-1} (X - R*Y).
Definition: Ifpack2_Container.hpp:339
Teuchos::Array< local_ordinal_type > partitionIndices_
Starting index in partitions_ of local row indices for each block.
Definition: Ifpack2_Container.hpp:427
virtual void compute()=0
Extract the local diagonal block and prepare the solver.
Interface for creating and solving a local linear problem.
Definition: Ifpack2_Container.hpp:114
virtual void apply(HostView &X, HostView &Y, int blockIndex, int stride, Teuchos::ETransp mode=Teuchos::NO_TRANS, scalar_type alpha=Teuchos::ScalarTraits< scalar_type >::one(), scalar_type beta=Teuchos::ScalarTraits< scalar_type >::zero()) const =0
Compute Y := alpha * M^{-1} X + beta*Y.
virtual void setParameters(const Teuchos::ParameterList &List)=0
Set parameters.
virtual std::ostream & print(std::ostream &os) const =0
Print basic information about the container to os.
virtual ~Container()
Destructor.
Definition: Ifpack2_Container.hpp:219
Teuchos::Array< local_ordinal_type > partitions_
Local indices of the rows of the input matrix that belong to this block.
Definition: Ifpack2_Container.hpp:423
virtual bool isInitialized() const =0
Return true if the container has been successfully initialized.
void setBlockSizes(const Teuchos::Array< Teuchos::Array< local_ordinal_type > > &partitions)
Initialize arrays with information about block sizes.
Definition: Ifpack2_Container.hpp:253
Kokkos::Details::ArithTraits< scalar_type >::val_type impl_scalar_type
Internal representation of Scalar in Kokkos::View.
Definition: Ifpack2_Container.hpp:132
Container(const Teuchos::RCP< const row_matrix_type > &matrix, const Teuchos::Array< local_ordinal_type > &localRows)
Constructor for single block.
Definition: Ifpack2_Container.hpp:190
static std::string name()