// @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_MULTIVECTOR_HPP #define THYRA_TPETRA_MULTIVECTOR_HPP #include "Thyra_TpetraMultiVector_decl.hpp" #include "Thyra_TpetraVectorSpace.hpp" #include "Thyra_TpetraVector.hpp" #include "Teuchos_Assert.hpp" namespace Thyra { // Constructors/initializers/accessors template TpetraMultiVector::TpetraMultiVector() {} template void TpetraMultiVector::initialize( const RCP > &tpetraVectorSpace, const RCP > &domainSpace, const RCP > &tpetraMultiVector ) { initializeImpl(tpetraVectorSpace, domainSpace, tpetraMultiVector); } template void TpetraMultiVector::constInitialize( const RCP > &tpetraVectorSpace, const RCP > &domainSpace, const RCP > &tpetraMultiVector ) { initializeImpl(tpetraVectorSpace, domainSpace, tpetraMultiVector); } template RCP > TpetraMultiVector::getTpetraMultiVector() { return tpetraMultiVector_.getNonconstObj(); } template RCP > TpetraMultiVector::getConstTpetraMultiVector() const { return tpetraMultiVector_; } // Overridden public functions form MultiVectorAdapterBase template RCP< const ScalarProdVectorSpaceBase > TpetraMultiVector::domainScalarProdVecSpc() const { return domainSpace_; } // Overridden protected functions from MultiVectorBase template void TpetraMultiVector::assignImpl(Scalar alpha) { tpetraMultiVector_.getNonconstObj()->putScalar(alpha); } template void TpetraMultiVector:: assignMultiVecImpl(const MultiVectorBase& mv) { auto tmv = this->getConstTpetraMultiVector(Teuchos::rcpFromRef(mv)); // If the cast succeeded, call Tpetra directly. // Otherwise, fall back to the RTOp implementation. if (nonnull(tmv)) { tpetraMultiVector_.getNonconstObj()->assign(*tmv); } else { MultiVectorDefaultBase::assignMultiVecImpl(mv); } } template void TpetraMultiVector::scaleImpl(Scalar alpha) { tpetraMultiVector_.getNonconstObj()->scale(alpha); } template void TpetraMultiVector::updateImpl( Scalar alpha, const MultiVectorBase& mv ) { auto tmv = this->getConstTpetraMultiVector(Teuchos::rcpFromRef(mv)); // If the cast succeeded, call Tpetra directly. // Otherwise, fall back to the RTOp implementation. if (nonnull(tmv)) { typedef Teuchos::ScalarTraits ST; tpetraMultiVector_.getNonconstObj()->update(alpha, *tmv, ST::one()); } else { MultiVectorDefaultBase::updateImpl(alpha, mv); } } template void TpetraMultiVector::linearCombinationImpl( const ArrayView& alpha, const ArrayView > >& mv, const Scalar& beta ) { #ifdef TEUCHOS_DEBUG TEUCHOS_ASSERT_EQUALITY(alpha.size(), mv.size()); #endif // Try to cast mv to an array of this type typedef Tpetra::MultiVector TMV; Teuchos::Array > tmvs(mv.size()); RCP tmv; bool allCastsSuccessful = true; { auto mvIter = mv.begin(); auto tmvIter = tmvs.begin(); for (; mvIter != mv.end(); ++mvIter, ++tmvIter) { tmv = this->getConstTpetraMultiVector(Teuchos::rcpFromPtr(*mvIter)); if (nonnull(tmv)) { *tmvIter = tmv; } else { allCastsSuccessful = false; break; } } } // If casts succeeded, or input arrays are size 0, call Tpetra directly. // Otherwise, fall back to the RTOp implementation. auto len = tmvs.size(); if (len == 0) { tpetraMultiVector_.getNonconstObj()->scale(beta); } else if (len == 1 && allCastsSuccessful) { tpetraMultiVector_.getNonconstObj()->update(alpha[0], *tmvs[0], beta); } else if (len == 2 && allCastsSuccessful) { tpetraMultiVector_.getNonconstObj()->update(alpha[0], *tmvs[0], alpha[1], *tmvs[1], beta); } else if (allCastsSuccessful) { typedef Teuchos::ScalarTraits ST; auto tmvIter = tmvs.begin(); auto alphaIter = alpha.begin(); // Check if any entry of tmvs aliases this object's wrapped vector. // If so, replace that entry in the array with a copy. tmv = Teuchos::null; for (; tmvIter != tmvs.end(); ++tmvIter) { if (tmvIter->getRawPtr() == tpetraMultiVector_.getConstObj().getRawPtr()) { if (tmv.is_null()) { tmv = Teuchos::rcp(new TMV(*tpetraMultiVector_.getConstObj(), Teuchos::Copy)); } *tmvIter = tmv; } } tmvIter = tmvs.begin(); // We add two MVs at a time, so only scale if even num MVs, // and additionally do the first addition if odd num MVs. if ((tmvs.size() % 2) == 0) { tpetraMultiVector_.getNonconstObj()->scale(beta); } else { tpetraMultiVector_.getNonconstObj()->update(*alphaIter, *(*tmvIter), beta); ++tmvIter; ++alphaIter; } for (; tmvIter != tmvs.end(); tmvIter+=2, alphaIter+=2) { tpetraMultiVector_.getNonconstObj()->update( *alphaIter, *(*tmvIter), *(alphaIter+1), *(*(tmvIter+1)), ST::one()); } } else { MultiVectorDefaultBase::linearCombinationImpl(alpha, mv, beta); } } template void TpetraMultiVector::dotsImpl( const MultiVectorBase& mv, const ArrayView& prods ) const { auto tmv = this->getConstTpetraMultiVector(Teuchos::rcpFromRef(mv)); // If the cast succeeded, call Tpetra directly. // Otherwise, fall back to the RTOp implementation. if (nonnull(tmv)) { tpetraMultiVector_.getConstObj()->dot(*tmv, prods); } else { MultiVectorDefaultBase::dotsImpl(mv, prods); } } template void TpetraMultiVector::norms1Impl( const ArrayView::magnitudeType>& norms ) const { tpetraMultiVector_.getConstObj()->norm1(norms); } template void TpetraMultiVector::norms2Impl( const ArrayView::magnitudeType>& norms ) const { tpetraMultiVector_.getConstObj()->norm2(norms); } template void TpetraMultiVector::normsInfImpl( const ArrayView::magnitudeType>& norms ) const { tpetraMultiVector_.getConstObj()->normInf(norms); } template RCP > TpetraMultiVector::colImpl(Ordinal j) const { #ifdef TEUCHOS_DEBUG TEUCHOS_ASSERT_IN_RANGE_UPPER_EXCLUSIVE(j, 0, this->domain()->dim()); #endif return constTpetraVector( tpetraVectorSpace_, tpetraMultiVector_->getVector(j) ); } template RCP > TpetraMultiVector::nonconstColImpl(Ordinal j) { #ifdef TEUCHOS_DEBUG TEUCHOS_ASSERT_IN_RANGE_UPPER_EXCLUSIVE(j, 0, this->domain()->dim()); #endif return tpetraVector( tpetraVectorSpace_, tpetraMultiVector_.getNonconstObj()->getVectorNonConst(j) ); } template RCP > TpetraMultiVector::contigSubViewImpl( const Range1D& col_rng_in ) const { #ifdef THYRA_DEFAULT_SPMD_MULTI_VECTOR_VERBOSE_TO_ERROR_OUT std::cerr << "\nTpetraMultiVector::subView(Range1D) const called!\n"; #endif const Range1D colRng = this->validateColRange(col_rng_in); const RCP > tpetraView = this->getConstTpetraMultiVector()->subView(colRng); const RCP > viewDomainSpace = tpetraVectorSpace( Tpetra::createLocalMapWithNode( tpetraView->getNumVectors(), tpetraView->getMap()->getComm() ) ); return constTpetraMultiVector( tpetraVectorSpace_, viewDomainSpace, tpetraView ); } template RCP > TpetraMultiVector::nonconstContigSubViewImpl( const Range1D& col_rng_in ) { #ifdef THYRA_DEFAULT_SPMD_MULTI_VECTOR_VERBOSE_TO_ERROR_OUT std::cerr << "\nTpetraMultiVector::subView(Range1D) called!\n"; #endif const Range1D colRng = this->validateColRange(col_rng_in); const RCP > tpetraView = this->getTpetraMultiVector()->subViewNonConst(colRng); const RCP > viewDomainSpace = tpetraVectorSpace( Tpetra::createLocalMapWithNode( tpetraView->getNumVectors(), tpetraView->getMap()->getComm() ) ); return tpetraMultiVector( tpetraVectorSpace_, viewDomainSpace, tpetraView ); } template RCP > TpetraMultiVector::nonContigSubViewImpl( const ArrayView& cols_in ) const { #ifdef THYRA_DEFAULT_SPMD_MULTI_VECTOR_VERBOSE_TO_ERROR_OUT std::cerr << "\nTpetraMultiVector::subView(ArrayView) const called!\n"; #endif // Tpetra wants col indices as size_t Array cols(cols_in.size()); for (Array::size_type i = 0; i < cols.size(); ++i) cols[i] = static_cast(cols_in[i]); const RCP > tpetraView = this->getConstTpetraMultiVector()->subView(cols()); const RCP > viewDomainSpace = tpetraVectorSpace( Tpetra::createLocalMapWithNode( tpetraView->getNumVectors(), tpetraView->getMap()->getComm() ) ); return constTpetraMultiVector( tpetraVectorSpace_, viewDomainSpace, tpetraView ); } template RCP > TpetraMultiVector::nonconstNonContigSubViewImpl( const ArrayView& cols_in ) { #ifdef THYRA_DEFAULT_SPMD_MULTI_VECTOR_VERBOSE_TO_ERROR_OUT std::cerr << "\nTpetraMultiVector::subView(ArrayView) called!\n"; #endif // Tpetra wants col indices as size_t Array cols(cols_in.size()); for (Array::size_type i = 0; i < cols.size(); ++i) cols[i] = static_cast(cols_in[i]); const RCP > tpetraView = this->getTpetraMultiVector()->subViewNonConst(cols()); const RCP > viewDomainSpace = tpetraVectorSpace( Tpetra::createLocalMapWithNode( tpetraView->getNumVectors(), tpetraView->getMap()->getComm() ) ); return tpetraMultiVector( tpetraVectorSpace_, viewDomainSpace, tpetraView ); } template void TpetraMultiVector:: mvMultiReductApplyOpImpl( const RTOpPack::RTOpT &primary_op, const ArrayView > > &multi_vecs, const ArrayView > > &targ_multi_vecs, const ArrayView > &reduct_objs, const Ordinal primary_global_offset ) const { MultiVectorAdapterBase::mvMultiReductApplyOpImpl( primary_op, multi_vecs, targ_multi_vecs, reduct_objs, primary_global_offset); } template void TpetraMultiVector:: acquireDetachedMultiVectorViewImpl( const Range1D &rowRng, const Range1D &colRng, RTOpPack::ConstSubMultiVectorView* sub_mv ) const { SpmdMultiVectorDefaultBase:: acquireDetachedMultiVectorViewImpl(rowRng, colRng, sub_mv); } template void TpetraMultiVector:: acquireNonconstDetachedMultiVectorViewImpl( const Range1D &rowRng, const Range1D &colRng, RTOpPack::SubMultiVectorView* sub_mv ) { SpmdMultiVectorDefaultBase:: acquireNonconstDetachedMultiVectorViewImpl(rowRng, colRng, sub_mv); } template void TpetraMultiVector:: commitNonconstDetachedMultiVectorViewImpl( RTOpPack::SubMultiVectorView* sub_mv ) { SpmdMultiVectorDefaultBase:: commitNonconstDetachedMultiVectorViewImpl(sub_mv); } /* ToDo: Implement these? template RCP > TpetraMultiVector::nonContigSubViewImpl( const ArrayView &cols ) const { THYRA_DEBUG_ASSERT_MV_COLS("nonContigSubViewImpl(cols)", cols); const int numCols = cols.size(); const ArrayRCP localValuesView = createContiguousCopy(cols); return defaultSpmdMultiVector( spmdRangeSpace_, createSmallScalarProdVectorSpaceBase(spmdRangeSpace_, numCols), localValuesView ); } template RCP > TpetraMultiVector::nonconstNonContigSubViewImpl( const ArrayView &cols ) { THYRA_DEBUG_ASSERT_MV_COLS("nonContigSubViewImpl(cols)", cols); const int numCols = cols.size(); const ArrayRCP localValuesView = createContiguousCopy(cols); const Ordinal localSubDim = spmdRangeSpace_->localSubDim(); RCP > copyBackView = copyBackSpmdMultiVectorEntries(cols, localValuesView.getConst(), localSubDim, localValues_.create_weak(), leadingDim_); return Teuchos::rcpWithEmbeddedObjPreDestroy( new TpetraMultiVector( spmdRangeSpace_, createSmallScalarProdVectorSpaceBase(spmdRangeSpace_, numCols), localValuesView), copyBackView ); } */ // Overridden protected members from SpmdMultiVectorBase template RCP > TpetraMultiVector::spmdSpaceImpl() const { return tpetraVectorSpace_; } template void TpetraMultiVector::getNonconstLocalMultiVectorDataImpl( const Ptr > &localValues, const Ptr &leadingDim ) { *localValues = tpetraMultiVector_.getNonconstObj()->get1dViewNonConst(); *leadingDim = tpetraMultiVector_->getStride(); } template void TpetraMultiVector::getLocalMultiVectorDataImpl( const Ptr > &localValues, const Ptr &leadingDim ) const { *localValues = tpetraMultiVector_->get1dView(); *leadingDim = tpetraMultiVector_->getStride(); } template void TpetraMultiVector::euclideanApply( const EOpTransp M_trans, const MultiVectorBase &X, const Ptr > &Y, const Scalar alpha, const Scalar beta ) const { // Try to extract Tpetra objects from X and Y typedef Tpetra::MultiVector TMV; Teuchos::RCP X_tpetra = this->getConstTpetraMultiVector(Teuchos::rcpFromRef(X)); Teuchos::RCP Y_tpetra = this->getTpetraMultiVector(Teuchos::rcpFromPtr(Y)); // If the cast succeeded, call Tpetra directly. // Otherwise, fall back to the default implementation. if (nonnull(X_tpetra) && nonnull(Y_tpetra)) { typedef Teuchos::ScalarTraits ST; TEUCHOS_TEST_FOR_EXCEPTION(ST::isComplex && (M_trans == CONJ), std::logic_error, "Error, conjugation without transposition is not allowed for complex scalar types!"); Teuchos::ETransp trans = Teuchos::NO_TRANS; switch (M_trans) { case NOTRANS: trans = Teuchos::NO_TRANS; break; case CONJ: trans = Teuchos::NO_TRANS; break; case TRANS: trans = Teuchos::TRANS; break; case CONJTRANS: trans = Teuchos::CONJ_TRANS; break; } Y_tpetra->multiply(trans, Teuchos::NO_TRANS, alpha, *tpetraMultiVector_.getConstObj(), *X_tpetra, beta); Kokkos::fence(); } else { SpmdMultiVectorDefaultBase::euclideanApply(M_trans, X, Y, alpha, beta); } } // private template template void TpetraMultiVector::initializeImpl( const RCP > &tpetraVectorSpace, const RCP > &domainSpace, const RCP &tpetraMultiVector ) { #ifdef THYRA_DEBUG TEUCHOS_ASSERT(nonnull(tpetraVectorSpace)); TEUCHOS_ASSERT(nonnull(domainSpace)); TEUCHOS_ASSERT(nonnull(tpetraMultiVector)); // ToDo: Check to make sure that tpetraMultiVector is compatible with // tpetraVectorSpace. #endif tpetraVectorSpace_ = tpetraVectorSpace; domainSpace_ = domainSpace; tpetraMultiVector_.initialize(tpetraMultiVector); this->updateSpmdSpace(); } template RCP > TpetraMultiVector:: getTpetraMultiVector(const RCP >& mv) const { using Teuchos::rcp_dynamic_cast; typedef Thyra::TpetraMultiVector TMV; typedef Thyra::TpetraVector TV; RCP tmv = rcp_dynamic_cast(mv); if (nonnull(tmv)) { return tmv->getTpetraMultiVector(); } RCP tv = rcp_dynamic_cast(mv); if (nonnull(tv)) { return tv->getTpetraVector(); } return Teuchos::null; } template RCP > TpetraMultiVector:: getConstTpetraMultiVector(const RCP >& mv) const { using Teuchos::rcp_dynamic_cast; typedef Thyra::TpetraMultiVector TMV; typedef Thyra::TpetraVector TV; RCP tmv = rcp_dynamic_cast(mv); if (nonnull(tmv)) { return tmv->getConstTpetraMultiVector(); } RCP tv = rcp_dynamic_cast(mv); if (nonnull(tv)) { return tv->getConstTpetraVector(); } return Teuchos::null; } } // end namespace Thyra #endif // THYRA_TPETRA_MULTIVECTOR_HPP