// @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_VECTOR_HPP #define THYRA_TPETRA_VECTOR_HPP #include "Thyra_TpetraVector_decl.hpp" #include "Thyra_TpetraMultiVector.hpp" namespace Thyra { // Constructors/initializers/accessors template TpetraVector::TpetraVector() {} template void TpetraVector::initialize( const RCP > &tpetraVectorSpace, const RCP > &tpetraVector ) { initializeImpl(tpetraVectorSpace, tpetraVector); } template void TpetraVector::constInitialize( const RCP > &tpetraVectorSpace, const RCP > &tpetraVector ) { initializeImpl(tpetraVectorSpace, tpetraVector); } template RCP > TpetraVector::getTpetraVector() { return tpetraVector_.getNonconstObj(); } template RCP > TpetraVector::getConstTpetraVector() const { return tpetraVector_; } // Overridden from VectorDefaultBase template RCP > TpetraVector::domain() const { if (domainSpace_.is_null()) { domainSpace_ = tpetraVectorSpace( Tpetra::createLocalMapWithNode( 1, tpetraVector_.getConstObj()->getMap()->getComm() ) ); } return domainSpace_; } // Overridden from SpmdMultiVectorBase template RCP > TpetraVector::spmdSpaceImpl() const { return tpetraVectorSpace_; } // Overridden from SpmdVectorBase template void TpetraVector::getNonconstLocalVectorDataImpl( const Ptr > &localValues ) { *localValues = tpetraVector_.getNonconstObj()->get1dViewNonConst(); } template void TpetraVector::getLocalVectorDataImpl( const Ptr > &localValues ) const { *localValues = tpetraVector_->get1dView(); } // Overridden from VectorBase template void TpetraVector::randomizeImpl( Scalar l, Scalar u ) { // Tpetra randomizes with different seed for each proc, so need a global // reduction to get locally-replicated random vector same on each proc. if (!tpetraVector_.getNonconstObj()->isDistributed()) { auto comm = tpetraVector_.getNonconstObj()->getMap()->getComm(); if (tpetraVector_.getConstObj()->getMap()->getComm()->getRank() == 0) tpetraVector_.getNonconstObj()->randomize(l, u); else tpetraVector_.getNonconstObj()->putScalar(Teuchos::ScalarTraits::zero()); tpetraVector_.getNonconstObj()->reduce(); } else { tpetraVector_.getNonconstObj()->randomize(l, u); } } template void TpetraVector::absImpl( const VectorBase& x ) { auto tx = this->getConstTpetraVector(Teuchos::rcpFromRef(x)); // If the cast succeeded, call Tpetra directly. // Otherwise, fall back to the RTOp implementation. if (nonnull(tx)) { tpetraVector_.getNonconstObj()->abs(*tx); } else { VectorDefaultBase::absImpl(x); } } template void TpetraVector::reciprocalImpl( const VectorBase& x ) { auto tx = this->getConstTpetraVector(Teuchos::rcpFromRef(x)); // If the cast succeeded, call Tpetra directly. // Otherwise, fall back to the RTOp implementation. if (nonnull(tx)) { tpetraVector_.getNonconstObj()->reciprocal(*tx); } else { VectorDefaultBase::reciprocalImpl(x); } } template void TpetraVector::eleWiseScaleImpl( const VectorBase& x ) { auto tx = this->getConstTpetraVector(Teuchos::rcpFromRef(x)); // If the cast succeeded, call Tpetra directly. // Otherwise, fall back to the RTOp implementation. if (nonnull(tx)) { typedef Teuchos::ScalarTraits ST; tpetraVector_.getNonconstObj()->elementWiseMultiply( ST::one(), *tx, *tpetraVector_.getConstObj(), ST::zero()); } else { VectorDefaultBase::eleWiseScaleImpl(x); } } template typename Teuchos::ScalarTraits::magnitudeType TpetraVector::norm2WeightedImpl( const VectorBase& x ) const { auto tx = this->getConstTpetraVector(Teuchos::rcpFromRef(x)); // If the cast succeeded, call Tpetra directly. // Otherwise, fall back to the RTOp implementation. if (nonnull(tx)) { // Weighted 2-norm function for Tpetra vector seems to be deprecated... typedef Teuchos::ScalarTraits ST; RCP > temp = Tpetra::createVector(tx->getMap()); temp->elementWiseMultiply( ST::one(), *tx, *tpetraVector_.getConstObj(), ST::zero()); return ST::magnitude(ST::squareroot(tpetraVector_.getConstObj()->dot(*temp))); } else { return VectorDefaultBase::norm2WeightedImpl(x); } } template void TpetraVector::applyOpImpl( const RTOpPack::RTOpT &op, const ArrayView > > &vecs, const ArrayView > > &targ_vecs, const Ptr &reduct_obj, const Ordinal global_offset ) const { SpmdVectorDefaultBase::applyOpImpl(op, vecs, targ_vecs, reduct_obj, global_offset); } template void TpetraVector:: acquireDetachedVectorViewImpl( const Range1D& rng, RTOpPack::ConstSubVectorView* sub_vec ) const { SpmdVectorDefaultBase::acquireDetachedVectorViewImpl(rng, sub_vec); } template void TpetraVector:: acquireNonconstDetachedVectorViewImpl( const Range1D& rng, RTOpPack::SubVectorView* sub_vec ) { SpmdVectorDefaultBase::acquireNonconstDetachedVectorViewImpl(rng, sub_vec); } template void TpetraVector:: commitNonconstDetachedVectorViewImpl( RTOpPack::SubVectorView* sub_vec ) { SpmdVectorDefaultBase::commitNonconstDetachedVectorViewImpl(sub_vec); } // Overridden protected functions from MultiVectorBase template void TpetraVector::assignImpl(Scalar alpha) { tpetraVector_.getNonconstObj()->putScalar(alpha); } template void TpetraVector:: assignMultiVecImpl(const MultiVectorBase& mv) { auto tmv = this->getConstTpetraMultiVector(Teuchos::rcpFromRef(mv)); // If cast succeeded, call Tpetra directly. // Otherwise, fall back to the RTOp implementation. if (nonnull(tmv)) { tpetraVector_.getNonconstObj()->assign(*tmv); } else { MultiVectorDefaultBase::assignMultiVecImpl(mv); } } template void TpetraVector::scaleImpl(Scalar alpha) { tpetraVector_.getNonconstObj()->scale(alpha); } template void TpetraVector::updateImpl( Scalar alpha, const MultiVectorBase& mv ) { auto tmv = this->getConstTpetraMultiVector(Teuchos::rcpFromRef(mv)); // If cast succeeded, call Tpetra directly. // Otherwise, fall back to the RTOp implementation. typedef Teuchos::ScalarTraits ST; if (nonnull(tmv)) { tpetraVector_.getNonconstObj()->update(alpha, *tmv, ST::one()); } else { MultiVectorDefaultBase::updateImpl(alpha, mv); } } template void TpetraVector::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 Tpetra objects 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 = mv.size(); if (len == 0) { tpetraVector_.getNonconstObj()->scale(beta); } else if (len == 1 && allCastsSuccessful) { tpetraVector_.getNonconstObj()->update(alpha[0], *tmvs[0], beta); } else if (len == 2 && allCastsSuccessful) { tpetraVector_.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() == tpetraVector_.getConstObj().getRawPtr()) { if (tmv.is_null()) { tmv = Teuchos::rcp(new TpetraMultiVector_t( *tpetraVector_.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) { tpetraVector_.getNonconstObj()->scale(beta); } else { tpetraVector_.getNonconstObj()->update(*alphaIter, *(*tmvIter), beta); ++tmvIter; ++alphaIter; } for (; tmvIter != tmvs.end(); tmvIter+=2, alphaIter+=2) { tpetraVector_.getNonconstObj()->update( *alphaIter, *(*tmvIter), *(alphaIter+1), *(*(tmvIter+1)), ST::one()); } } else { MultiVectorDefaultBase::linearCombinationImpl(alpha, mv, beta); } } template void TpetraVector::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)) { tpetraVector_.getConstObj()->dot(*tmv, prods); } else { MultiVectorDefaultBase::dotsImpl(mv, prods); } } template void TpetraVector::norms1Impl( const ArrayView::magnitudeType>& norms ) const { tpetraVector_.getConstObj()->norm1(norms); } template void TpetraVector::norms2Impl( const ArrayView::magnitudeType>& norms ) const { tpetraVector_.getConstObj()->norm2(norms); } template void TpetraVector::normsInfImpl( const ArrayView::magnitudeType>& norms ) const { tpetraVector_.getConstObj()->normInf(norms); } template void TpetraVector::applyImpl( 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, *tpetraVector_.getConstObj(), *X_tpetra, beta); Kokkos::fence(); } else { VectorDefaultBase::applyImpl(M_trans, X, Y, alpha, beta); } } // private template template void TpetraVector::initializeImpl( const RCP > &tpetraVectorSpace, const RCP &tpetraVector ) { #ifdef TEUCHOS_DEBUG TEUCHOS_ASSERT(nonnull(tpetraVectorSpace)); TEUCHOS_ASSERT(nonnull(tpetraVector)); #endif tpetraVectorSpace_ = tpetraVectorSpace; tpetraVector_.initialize(tpetraVector); this->updateSpmdSpace(); } template RCP > TpetraVector:: getTpetraMultiVector(const RCP >& mv) const { using Teuchos::rcp_dynamic_cast; typedef TpetraMultiVector TMV; typedef 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 > TpetraVector:: getConstTpetraMultiVector(const RCP >& mv) const { using Teuchos::rcp_dynamic_cast; typedef TpetraMultiVector TMV; typedef 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; } template RCP > TpetraVector:: getTpetraVector(const RCP >& v) const { typedef TpetraVector TV; RCP tv = Teuchos::rcp_dynamic_cast(v); if (nonnull(tv)) { return tv->getTpetraVector(); } else { return Teuchos::null; } } template RCP > TpetraVector:: getConstTpetraVector(const RCP >& v) const { typedef TpetraVector TV; RCP tv = Teuchos::rcp_dynamic_cast(v); if (nonnull(tv)) { return tv->getConstTpetraVector(); } else { return Teuchos::null; } } } // end namespace Thyra #endif // THYRA_TPETRA_VECTOR_HPP