// @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_SPACE_HPP #define THYRA_TPETRA_VECTOR_SPACE_HPP #include "Thyra_TpetraVectorSpace_decl.hpp" #include "Thyra_TpetraThyraWrappers.hpp" #include "Thyra_TpetraVector.hpp" #include "Thyra_TpetraMultiVector.hpp" #include "Thyra_TpetraEuclideanScalarProd.hpp" #include "Tpetra_Details_StaticView.hpp" namespace Thyra { template RCP > TpetraVectorSpace::create() { const RCP vs(new this_t); vs->weakSelfPtr_ = vs.create_weak(); return vs; } template void TpetraVectorSpace::initialize( const RCP > &tpetraMap ) { comm_ = convertTpetraToThyraComm(tpetraMap->getComm()); tpetraMap_ = tpetraMap; this->updateState(tpetraMap->getGlobalNumElements(), !tpetraMap->isDistributed()); this->setScalarProd(tpetraEuclideanScalarProd()); } // Utility functions template RCP> TpetraVectorSpace ::createLocallyReplicatedVectorSpace(int size) const { return tpetraVectorSpace( Tpetra::createLocalMapWithNode( size, tpetraMap_->getComm() ) ); } // Overridden from VectorSpace template RCP > TpetraVectorSpace::createMember() const { return tpetraVector( weakSelfPtr_.create_strong().getConst(), Teuchos::rcp( new Tpetra::Vector(tpetraMap_, false) ) ); } template RCP< MultiVectorBase > TpetraVectorSpace::createMembers(int numMembers) const { return tpetraMultiVector( weakSelfPtr_.create_strong().getConst(), this->createLocallyReplicatedVectorSpace(numMembers), Teuchos::rcp( new Tpetra::MultiVector( tpetraMap_, numMembers, false) ) ); } template class CopyTpetraMultiVectorViewBack { public: CopyTpetraMultiVectorViewBack( RCP > mv, const RTOpPack::SubMultiVectorView &raw_mv ) :mv_(mv), raw_mv_(raw_mv) { RCP > tmv = Teuchos::rcp_dynamic_cast >(mv_,true)->getTpetraMultiVector(); bool inUse = Teuchos::get_extra_data(tmv,"inUse"); TEUCHOS_TEST_FOR_EXCEPTION(inUse, std::runtime_error, "Cannot use the cached vector simultaneously more than once."); inUse = true; Teuchos::set_extra_data(inUse,"inUse",Teuchos::outArg(tmv), Teuchos::POST_DESTROY, false); } ~CopyTpetraMultiVectorViewBack() { RTOpPack::ConstSubMultiVectorView smv; mv_->acquireDetachedView(Range1D(),Range1D(),&smv); RTOpPack::assign_entries( Teuchos::outArg(raw_mv_), smv ); mv_->releaseDetachedView(&smv); bool inUse = false; RCP > tmv = Teuchos::rcp_dynamic_cast >(mv_,true)->getTpetraMultiVector(); Teuchos::set_extra_data(inUse,"inUse",Teuchos::outArg(tmv), Teuchos::POST_DESTROY, false); } private: RCP > mv_; const RTOpPack::SubMultiVectorView raw_mv_; }; template RCP< MultiVectorBase > TpetraVectorSpace::createCachedMembersView( const RTOpPack::SubMultiVectorView &raw_mv, const bool initialize) const { #ifdef TEUCHOS_DEBUG TEUCHOS_TEST_FOR_EXCEPT( raw_mv.subDim() != this->dim() ); #endif // Create a multi-vector RCP< MultiVectorBase > mv; if (!tpetraMap_->isDistributed()) { if (tpetraMV_.is_null() || (tpetraMV_->getNumVectors() != size_t (raw_mv.numSubCols()))) { if (!tpetraMV_.is_null()) // The MV is already allocated. If we are still using it, then very bad things can happen. TEUCHOS_TEST_FOR_EXCEPTION(Teuchos::get_extra_data(tpetraMV_,"inUse"), std::runtime_error, "Cannot use the cached vector simultaneously more than once."); using IST = typename Tpetra::MultiVector::impl_scalar_type; using DT = typename Tpetra::MultiVector::device_type; auto dv = ::Tpetra::Details::getStatic2dDualView (tpetraMap_->getGlobalNumElements(), raw_mv.numSubCols()); tpetraMV_ = Teuchos::rcp(new Tpetra::MultiVector(tpetraMap_, dv)); bool inUse = false; Teuchos::set_extra_data(inUse,"inUse",Teuchos::outArg(tpetraMV_)); } if (tpetraDomainSpace_.is_null() || raw_mv.numSubCols() != tpetraDomainSpace_->localSubDim()) tpetraDomainSpace_ = tpetraVectorSpace(Tpetra::createLocalMapWithNode(raw_mv.numSubCols(), tpetraMap_->getComm())); mv = tpetraMultiVector(weakSelfPtr_.create_strong().getConst(), tpetraDomainSpace_, tpetraMV_); } else { mv = this->createMembers(raw_mv.numSubCols()); bool inUse = false; RCP > tmv = Teuchos::rcp_dynamic_cast >(mv,true)->getTpetraMultiVector(); Teuchos::set_extra_data(inUse,"inUse",Teuchos::outArg(tmv)); } if (initialize) { // Copy initial values in raw_mv into multi-vector RTOpPack::SubMultiVectorView smv; mv->acquireDetachedView(Range1D(),Range1D(),&smv); RTOpPack::assign_entries( Ptr >(Teuchos::outArg(smv)), raw_mv ); mv->commitDetachedView(&smv); } // Setup smart pointer to multi-vector to copy view back out just before multi-vector is destroyed Teuchos::set_extra_data( // We create a duplicate of the RCP, otherwise the ref count does not go to zero. Teuchos::rcp(new CopyTpetraMultiVectorViewBack(Teuchos::rcpFromRef(*mv),raw_mv)), "CopyTpetraMultiVectorViewBack", Teuchos::outArg(mv), Teuchos::PRE_DESTROY ); return mv; } template RCP > TpetraVectorSpace::createCachedMembersView( const RTOpPack::ConstSubMultiVectorView &raw_mv ) const { #ifdef TEUCHOS_DEBUG TEUCHOS_TEST_FOR_EXCEPT( raw_mv.subDim() != this->dim() ); #endif // Create a multi-vector RCP< MultiVectorBase > mv; if (!tpetraMap_->isDistributed()) { if (tpetraMV_.is_null() || (tpetraMV_->getNumVectors() != size_t (raw_mv.numSubCols()))) { if (!tpetraMV_.is_null()) // The MV is already allocated. If we are still using it, then very bad things can happen. TEUCHOS_TEST_FOR_EXCEPTION(Teuchos::get_extra_data(tpetraMV_,"inUse"), std::runtime_error, "Cannot use the cached vector simultaneously more than once."); using IST = typename Tpetra::MultiVector::impl_scalar_type; using DT = typename Tpetra::MultiVector::device_type; auto dv = ::Tpetra::Details::getStatic2dDualView (tpetraMap_->getGlobalNumElements(), raw_mv.numSubCols()); tpetraMV_ = Teuchos::rcp(new Tpetra::MultiVector(tpetraMap_, dv)); bool inUse = false; Teuchos::set_extra_data(inUse,"inUse",Teuchos::outArg(tpetraMV_)); } if (tpetraDomainSpace_.is_null() || raw_mv.numSubCols() != tpetraDomainSpace_->localSubDim()) tpetraDomainSpace_ = tpetraVectorSpace(Tpetra::createLocalMapWithNode(raw_mv.numSubCols(), tpetraMap_->getComm())); mv = tpetraMultiVector(weakSelfPtr_.create_strong().getConst(), tpetraDomainSpace_, tpetraMV_); } else { mv = this->createMembers(raw_mv.numSubCols()); bool inUse = false; RCP > tmv = Teuchos::rcp_dynamic_cast >(mv,true)->getTpetraMultiVector(); Teuchos::set_extra_data(inUse,"inUse",Teuchos::outArg(tmv)); } // Copy values in raw_mv into multi-vector RTOpPack::SubMultiVectorView smv; mv->acquireDetachedView(Range1D(),Range1D(),&smv); RTOpPack::assign_entries( Ptr >(Teuchos::outArg(smv)), raw_mv ); mv->commitDetachedView(&smv); return mv; } template bool TpetraVectorSpace::hasInCoreView( const Range1D& rng_in, const EViewType viewType, const EStrideType strideType ) const { const Range1D rng = full_range(rng_in,0,this->dim()-1); const Ordinal l_localOffset = this->localOffset(); const Ordinal myLocalSubDim = tpetraMap_.is_null () ? static_cast (0) : tpetraMap_->getLocalNumElements (); return ( l_localOffset<=rng.lbound() && rng.ubound() RCP< const VectorSpaceBase > TpetraVectorSpace::clone() const { return tpetraVectorSpace(tpetraMap_); } template RCP > TpetraVectorSpace::getTpetraMap() const { return tpetraMap_; } // Overridden from SpmdVectorSpaceDefaultBase template RCP > TpetraVectorSpace::getComm() const { return comm_; } template Ordinal TpetraVectorSpace::localSubDim() const { return tpetraMap_.is_null () ? static_cast (0) : static_cast (tpetraMap_->getLocalNumElements ()); } // private template TpetraVectorSpace::TpetraVectorSpace() { // The base classes should automatically default initialize to a safe // uninitialized state. } } // end namespace Thyra #endif // THYRA_TPETRA_VECTOR_SPACE_HPP