// @HEADER // ***************************************************************************** // Thyra: Interfaces and Support for Abstract Numerical Algorithms // // Copyright 2004 NTESS and the Thyra contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef THYRA_TPETRA_LINEAR_OP_HPP #define THYRA_TPETRA_LINEAR_OP_HPP #include "Thyra_TpetraLinearOp_decl.hpp" #include "Thyra_TpetraVectorSpace.hpp" #include "Teuchos_ScalarTraits.hpp" #include "Teuchos_TypeNameTraits.hpp" #include "Tpetra_CrsMatrix.hpp" #ifdef HAVE_THYRA_TPETRA_EPETRA # include "Thyra_EpetraThyraWrappers.hpp" #endif namespace Thyra { #ifdef HAVE_THYRA_TPETRA_EPETRA // Utilites /** \brief Default class returns null. */ template class GetTpetraEpetraRowMatrixWrapper { public: template static RCP > get(const RCP &tpetraMatrix) { return Teuchos::null; } }; // NOTE: We could support other ordinal types, but we have to // specialize the EpetraRowMatrix template<> class GetTpetraEpetraRowMatrixWrapper { public: template static RCP > get(const RCP &tpetraMatrix) { return Teuchos::rcp( new Tpetra::EpetraRowMatrix(tpetraMatrix, *get_Epetra_Comm( *convertTpetraToThyraComm(tpetraMatrix->getRowMap()->getComm()) ) ) ); } }; #endif // HAVE_THYRA_TPETRA_EPETRA template inline Teuchos::ETransp convertConjNoTransToTeuchosTransMode() { TEUCHOS_TEST_FOR_EXCEPTION( Teuchos::ScalarTraits::isComplex, Exceptions::OpNotSupported, "For complex scalars such as " + Teuchos::TypeNameTraits::name() + ", Tpetra does not support conjugation without transposition." ); return Teuchos::NO_TRANS; // For non-complex scalars, CONJ is equivalent to NOTRANS. } template inline Teuchos::ETransp convertToTeuchosTransMode(const Thyra::EOpTransp transp) { switch (transp) { case NOTRANS: return Teuchos::NO_TRANS; case CONJ: return convertConjNoTransToTeuchosTransMode(); case TRANS: return Teuchos::TRANS; case CONJTRANS: return Teuchos::CONJ_TRANS; } // Should not escape the switch TEUCHOS_TEST_FOR_EXCEPT(true); } // Constructors/initializers template TpetraLinearOp::TpetraLinearOp() {} template void TpetraLinearOp::initialize( const RCP > &rangeSpace, const RCP > &domainSpace, const RCP > &tpetraOperator ) { initializeImpl(rangeSpace, domainSpace, tpetraOperator); } template void TpetraLinearOp::constInitialize( const RCP > &rangeSpace, const RCP > &domainSpace, const RCP > &tpetraOperator ) { initializeImpl(rangeSpace, domainSpace, tpetraOperator); } template RCP > TpetraLinearOp::getTpetraOperator() { return tpetraOperator_.getNonconstObj(); } template RCP > TpetraLinearOp::getConstTpetraOperator() const { return tpetraOperator_; } // Public Overridden functions from LinearOpBase template RCP > TpetraLinearOp::range() const { return rangeSpace_; } template RCP > TpetraLinearOp::domain() const { return domainSpace_; } // Overridden from EpetraLinearOpBase #ifdef HAVE_THYRA_TPETRA_EPETRA template void TpetraLinearOp::getNonconstEpetraOpView( const Ptr > &epetraOp, const Ptr &epetraOpTransp, const Ptr &epetraOpApplyAs, const Ptr &epetraOpAdjointSupport ) { TEUCHOS_TEST_FOR_EXCEPT(true); } template void TpetraLinearOp::getEpetraOpView( const Ptr > &epetraOp, const Ptr &epetraOpTransp, const Ptr &epetraOpApplyAs, const Ptr &epetraOpAdjointSupport ) const { using Teuchos::rcp_dynamic_cast; typedef Tpetra::RowMatrix TpetraRowMatrix_t; if (nonnull(tpetraOperator_)) { if (is_null(epetraOp_)) { epetraOp_ = GetTpetraEpetraRowMatrixWrapper::get( rcp_dynamic_cast(tpetraOperator_.getConstObj(), true)); } *epetraOp = epetraOp_; *epetraOpTransp = NOTRANS; *epetraOpApplyAs = EPETRA_OP_APPLY_APPLY; *epetraOpAdjointSupport = ( tpetraOperator_->hasTransposeApply() ? EPETRA_OP_ADJOINT_SUPPORTED : EPETRA_OP_ADJOINT_UNSUPPORTED ); } else { *epetraOp = Teuchos::null; } } #endif // HAVE_THYRA_TPETRA_EPETRA // Protected Overridden functions from LinearOpBase template bool TpetraLinearOp::opSupportedImpl( Thyra::EOpTransp M_trans) const { if (is_null(tpetraOperator_)) return false; if (M_trans == NOTRANS) return true; if (M_trans == CONJ) { // For non-complex scalars, CONJ is always supported since it is equivalent to NO_TRANS. // For complex scalars, Tpetra does not support conjugation without transposition. return !Teuchos::ScalarTraits::isComplex; } return tpetraOperator_->hasTransposeApply(); } template void TpetraLinearOp::applyImpl( const Thyra::EOpTransp M_trans, const Thyra::MultiVectorBase &X_in, const Teuchos::Ptr > &Y_inout, const Scalar alpha, const Scalar beta ) const { using Teuchos::rcpFromRef; using Teuchos::rcpFromPtr; typedef TpetraOperatorVectorExtraction ConverterT; typedef Tpetra::MultiVector TpetraMultiVector_t; // Get Tpetra::MultiVector objects for X and Y const RCP tX = ConverterT::getConstTpetraMultiVector(rcpFromRef(X_in)); const RCP tY = ConverterT::getTpetraMultiVector(rcpFromPtr(Y_inout)); const Teuchos::ETransp tTransp = convertToTeuchosTransMode(M_trans); // Apply the operator tpetraOperator_->apply(*tX, *tY, tTransp, alpha, beta); Kokkos::fence(); } // Protected member functions overridden from ScaledLinearOpBase template bool TpetraLinearOp::supportsScaleLeftImpl() const { return true; } template bool TpetraLinearOp::supportsScaleRightImpl() const { return true; } template void TpetraLinearOp:: scaleLeftImpl(const VectorBase &row_scaling_in) { using Teuchos::rcpFromRef; const RCP > row_scaling = TpetraOperatorVectorExtraction::getConstTpetraVector(rcpFromRef(row_scaling_in)); const RCP > rowMatrix = Teuchos::rcp_dynamic_cast >(tpetraOperator_.getNonconstObj(),true); rowMatrix->leftScale(*row_scaling); Kokkos::fence(); } template void TpetraLinearOp:: scaleRightImpl(const VectorBase &col_scaling_in) { using Teuchos::rcpFromRef; const RCP > col_scaling = TpetraOperatorVectorExtraction::getConstTpetraVector(rcpFromRef(col_scaling_in)); const RCP > rowMatrix = Teuchos::rcp_dynamic_cast >(tpetraOperator_.getNonconstObj(),true); rowMatrix->rightScale(*col_scaling); Kokkos::fence(); } // Protected member functions overridden from RowStatLinearOpBase template bool TpetraLinearOp:: rowStatIsSupportedImpl( const RowStatLinearOpBaseUtils::ERowStat rowStat) const { if (is_null(tpetraOperator_)) return false; switch (rowStat) { case RowStatLinearOpBaseUtils::ROW_STAT_INV_ROW_SUM: case RowStatLinearOpBaseUtils::ROW_STAT_ROW_SUM: return true; default: return false; } } template void TpetraLinearOp::getRowStatImpl( const RowStatLinearOpBaseUtils::ERowStat rowStat, const Ptr > &rowStatVec_in ) const { typedef Tpetra::Vector TpetraVector_t; typedef Teuchos::ScalarTraits STS; typedef typename STS::magnitudeType MT; typedef Teuchos::ScalarTraits STM; if ( (rowStat == RowStatLinearOpBaseUtils::ROW_STAT_INV_ROW_SUM) || (rowStat == RowStatLinearOpBaseUtils::ROW_STAT_ROW_SUM) ) { TEUCHOS_ASSERT(nonnull(tpetraOperator_)); TEUCHOS_ASSERT(nonnull(rowStatVec_in)); // Currently we only support the case of row sums for a concrete // Tpetra::CrsMatrix where (1) the entire row is stored on a // single process and (2) that the domain map, the range map and // the row map are the SAME. These checks enforce that. Later on // we hope to add complete support for any mapping to the concrete // tpetra matrix types. const RCP tRowSumVec = TpetraOperatorVectorExtraction::getTpetraVector(rcpFromPtr(rowStatVec_in)); const RCP > tCrsMatrix = Teuchos::rcp_dynamic_cast >(tpetraOperator_.getConstObj(),true); // EGP: The following assert fails when row sum scaling is applied to blocked Tpetra operators, but without the assert, the correct row sum scaling is obtained. // Furthermore, no valgrind memory errors occur in this case when the assert is removed. //TEUCHOS_ASSERT(tCrsMatrix->getRowMap()->isSameAs(*tCrsMatrix->getDomainMap())); TEUCHOS_ASSERT(tCrsMatrix->getRowMap()->isSameAs(*tCrsMatrix->getRangeMap())); TEUCHOS_ASSERT(tCrsMatrix->getRowMap()->isSameAs(*tRowSumVec->getMap())); size_t numMyRows = tCrsMatrix->getLocalNumRows(); using crs_t = Tpetra::CrsMatrix; typename crs_t::local_inds_host_view_type indices; typename crs_t::values_host_view_type values; for (size_t row=0; row < numMyRows; ++row) { MT sum = STM::zero (); tCrsMatrix->getLocalRowView (row, indices, values); for (int col = 0; col < (int) values.size(); ++col) { sum += STS::magnitude (values[col]); } if (rowStat == RowStatLinearOpBaseUtils::ROW_STAT_INV_ROW_SUM) { if (sum < STM::sfmin ()) { TEUCHOS_TEST_FOR_EXCEPTION(sum == STM::zero (), std::runtime_error, "Error - Thyra::TpetraLinearOp::getRowStatImpl() - Inverse row sum " << "requested for a matrix where one of the rows has a zero row sum!"); sum = STM::one () / STM::sfmin (); } else { sum = STM::one () / sum; } } tRowSumVec->replaceLocalValue (row, Scalar (sum)); } } else { TEUCHOS_TEST_FOR_EXCEPTION(true,std::runtime_error, "Error - Thyra::TpetraLinearOp::getRowStatImpl() - Column sum support not implemented!"); } Kokkos::fence(); } // private template template void TpetraLinearOp::initializeImpl( const RCP > &rangeSpace, const RCP > &domainSpace, const RCP &tpetraOperator ) { #ifdef THYRA_DEBUG TEUCHOS_ASSERT(nonnull(rangeSpace)); TEUCHOS_ASSERT(nonnull(domainSpace)); TEUCHOS_ASSERT(nonnull(tpetraOperator)); // ToDo: Assert that spaces are comparible with tpetraOperator #endif rangeSpace_ = rangeSpace; domainSpace_ = domainSpace; tpetraOperator_ = tpetraOperator; } } // namespace Thyra #endif // THYRA_TPETRA_LINEAR_OP_HPP