42 #ifndef THYRA_DEFAULT_PRODUCT_VECTOR_DEF_HPP
43 #define THYRA_DEFAULT_PRODUCT_VECTOR_DEF_HPP
46 #include "Thyra_DefaultProductVector_decl.hpp"
47 #include "Thyra_DefaultProductVectorSpace.hpp"
48 #include "Teuchos_Workspace.hpp"
57 template <
class Scalar>
65 template <
class Scalar>
75 template <
class Scalar>
81 numBlocks_ = productSpace_in->numBlocks();
82 productSpace_ = productSpace_in;
83 vecs_.resize(numBlocks_);
84 for(
int k = 0; k < numBlocks_; ++k )
85 vecs_[k].initialize(createMember(productSpace_in->getBlock(k)));
89 template <
class Scalar>
98 as<Ordinal>(vecs.size()) );
100 numBlocks_ = productSpace_in->numBlocks();
101 productSpace_ = productSpace_in;
102 vecs_.resize(numBlocks_);
103 for(
int k = 0; k < numBlocks_; ++k )
104 vecs_[k].initialize(vecs[k]);
108 template <
class Scalar>
117 as<Ordinal>(vecs.size()) );
119 numBlocks_ = productSpace_in->numBlocks();
120 productSpace_ = productSpace_in;
121 vecs_.resize(numBlocks_);
122 for(
int k = 0; k < numBlocks_; ++k )
123 vecs_[k].initialize(vecs[k]);
127 template <
class Scalar>
130 productSpace_ = Teuchos::null;
139 template<
class Scalar>
143 std::ostringstream oss;
147 <<
"dim="<<(
nonnull(vs) ? vs->dim() : 0)
148 <<
", numBlocks = "<<numBlocks_
154 template<
class Scalar>
162 using Teuchos::describe;
170 *out << this->description() << std::endl;
178 <<
"dim=" << this->space()->dim()
182 <<
"numBlocks="<< numBlocks_ << std::endl
183 <<
"Constituent vector objects v[0], v[1], ... v[numBlocks-1]:\n";
185 for(
int k = 0; k < numBlocks_; ++k ) {
186 *out <<
"v["<<k<<
"] = " << describe(*vecs_[k].getConstObj(),verbLevel);
199 template <
class Scalar>
212 template <
class Scalar>
228 template <
class Scalar>
235 return vecs_[k].getNonconstObj();
239 template <
class Scalar>
246 return vecs_[k].getConstObj();
253 template <
class Scalar>
257 return productSpace_;
261 template <
class Scalar>
267 return vecs_[k].isConst();
271 template <
class Scalar>
275 return getNonconstVectorBlock(k);
279 template <
class Scalar>
283 return getVectorBlock(k);
290 template <
class Scalar>
294 return productSpace_;
310 template <
class Scalar>
322 for(
int k = 0; k < numBlocks_; ++k) {
323 vecs_[k].getNonconstObj()->abs(*pv->getVectorBlock(k));
331 template <
class Scalar>
343 for(
int k = 0; k < numBlocks_; ++k) {
344 vecs_[k].getNonconstObj()->reciprocal(*pv->getVectorBlock(k));
352 template <
class Scalar>
364 for(
int k = 0; k < numBlocks_; ++k) {
365 vecs_[k].getNonconstObj()->ele_wise_scale(*pv->getVectorBlock(k));
373 template <
class Scalar>
387 typename ST::magnitudeType norm = ST::magnitude(ST::zero());
388 for(
int k = 0; k < numBlocks_; ++k) {
389 typename ST::magnitudeType subNorm
390 = vecs_[k].getConstObj()->norm_2(*pv->getVectorBlock(k));
391 norm += subNorm*subNorm;
393 return ST::magnitude(ST::squareroot(norm));
400 template <
class Scalar>
417 using Teuchos::ptr_dynamic_cast;
418 using Teuchos::describe;
423 const Ordinal n = productSpace_->dim();
424 const int num_vecs = vecs.size();
425 const int num_targ_vecs = targ_vecs.size();
430 for(
int k = 0; k < num_vecs; ++k) {
431 test_failed = !this->space()->isCompatible(*vecs[k]->space());
434 ,
"DefaultProductVector::applyOp(...): Error vecs["<<k<<
"]->space() = "
435 <<vecs[k]->space()->description()<<
"\' is not compatible with this "
436 <<
"vector space = "<<this->space()->description()<<
"!"
439 for(
int k = 0; k < num_targ_vecs; ++k) {
440 test_failed = !this->space()->isCompatible(*targ_vecs[k]->space());
443 ,
"DefaultProductVector::applyOp(...): Error targ_vecs["<<k<<
"]->space() = "
444 <<targ_vecs[k]->space()->description()<<
"\' is not compatible with this "
445 <<
"vector space = "<<this->space()->description()<<
"!"
459 for(
int k = 0; k < num_vecs; ++k) {
461 castOrCreateProductVectorBase<Scalar>(rcpFromPtr(vecs[k]));
462 vecs_args[k] = vecs_args_store[k].ptr();
467 for(
int k = 0; k < num_targ_vecs; ++k) {
468 targ_vecs_args_store[k] =
469 castOrCreateNonconstProductVectorBase<Scalar>(rcpFromPtr(targ_vecs[k]));
470 targ_vecs_args[k] = targ_vecs_args_store[k].ptr();
478 Ordinal num_elements_remaining = dim;
479 const int numBlocks = productSpace_->numBlocks();
481 sub_vecs_rcps(num_vecs);
485 sub_targ_vecs_rcps(num_targ_vecs);
487 sub_targ_vecs(num_targ_vecs);
489 for(
int k = 0; k < numBlocks; ++k) {
490 const Ordinal dim_k = productSpace_->getBlock(k)->dim();
492 for(
int i = 0; i < num_vecs; ++i ) {
493 sub_vecs_rcps[i] = vecs_args[i]->getVectorBlock(k);
494 sub_vecs[i] = sub_vecs_rcps[i].ptr();
497 for(
int j = 0; j < num_targ_vecs; ++j ) {
498 sub_targ_vecs_rcps[j] = targ_vecs_args[j]->getNonconstVectorBlock(k);
499 sub_targ_vecs[j] = sub_targ_vecs_rcps[j].ptr();
501 Thyra::applyOp<Scalar>(
502 op, sub_vecs(), sub_targ_vecs(),
504 global_offset_in + g_off
507 num_elements_remaining -= dim_k;
520 template <
class Scalar>
527 int kth_vector_space = -1;
529 productSpace_->getVecSpcPoss(rng.
lbound(),&kth_vector_space,&kth_global_offset);
535 <= kth_global_offset + vecs_[kth_vector_space].getConstObj()->space()->dim()
540 &*vecs_[kth_vector_space].getConstObj()
541 )->acquireDetachedView( rng - kth_global_offset, sub_vec );
555 template <
class Scalar>
560 if( sub_vec->
values().
get() == NULL )
return;
561 int kth_vector_space = -1;
563 productSpace_->getVecSpcPoss(sub_vec->
globalOffset(),&kth_vector_space,&kth_global_offset);
569 <= kth_global_offset + vecs_[kth_vector_space].getConstObj()->space()->dim()
574 vecs_[kth_vector_space].getConstObj()->releaseDetachedView(sub_vec);
583 template <
class Scalar>
590 int kth_vector_space = -1;
592 productSpace_->getVecSpcPoss(rng.
lbound(),&kth_vector_space,&kth_global_offset);
598 <= kth_global_offset + vecs_[kth_vector_space].getConstObj()->space()->dim()
602 vecs_[kth_vector_space].getConstObj()->acquireDetachedView(
603 rng - kth_global_offset, sub_vec
618 template <
class Scalar>
623 if( sub_vec->
values().
get() == NULL )
return;
624 int kth_vector_space = -1;
626 productSpace_->getVecSpcPoss(sub_vec->
globalOffset(),&kth_vector_space,&kth_global_offset);
632 <= kth_global_offset + vecs_[kth_vector_space].getConstObj()->space()->dim()
637 vecs_[kth_vector_space].getNonconstObj()->commitDetachedView(sub_vec);
646 template <
class Scalar>
651 int kth_vector_space = -1;
653 productSpace_->getVecSpcPoss(sub_vec.
globalOffset(),&kth_vector_space,&kth_global_offset);
659 <= kth_global_offset + vecs_[kth_vector_space].getConstObj()->space()->dim()
665 vecs_[kth_vector_space].getNonconstObj()->setSubVector(sub_vec_g);
679 template <
class Scalar>
682 for(
int k = 0; k < numBlocks_; ++k) {
683 vecs_[k].getNonconstObj()->assign(alpha);
688 template <
class Scalar>
702 for(
int k = 0; k < numBlocks_; ++k) {
703 vecs_[k].getNonconstObj()->assign(*pv->getMultiVectorBlock(k));
711 template <
class Scalar>
714 for(
int k = 0; k < numBlocks_; ++k) {
715 vecs_[k].getNonconstObj()->scale(alpha);
720 template <
class Scalar>
735 for(
int k = 0; k < numBlocks_; ++k) {
736 vecs_[k].getNonconstObj()->update(alpha, *pv->getMultiVectorBlock(k));
744 template <
class Scalar>
756 bool allCastsSuccessful =
true;
758 for (
Ordinal i = 0; i < mv.size(); ++i) {
761 allCastsSuccessful =
false;
766 TEUCHOS_ASSERT(productSpace_->isCompatible(*pvs[i]->productSpace()));
770 if (allCastsSuccessful) {
773 for (
int k = 0; k < numBlocks_; ++k) {
774 for (
Ordinal i = 0; i < pvs.size(); ++i) {
775 blocks_rcp[i] = pvs[i]->getMultiVectorBlock(k);
776 blocks[i] = blocks_rcp[i].ptr();
778 vecs_[k].getNonconstObj()->linear_combination(alpha, blocks(), beta);
786 template <
class Scalar>
803 for(
int k = 0; k < numBlocks_; ++k) {
805 vecs_[k].getConstObj()->dots(*pv->getMultiVectorBlock(k), Teuchos::arrayView(&prod, 1));
814 template <
class Scalar>
823 norms[0] = ST::magnitude(ST::zero());
824 for(
int k = 0; k < numBlocks_; ++k) {
825 norms[0] += vecs_[k].getConstObj()->norm_1();
830 template <
class Scalar>
839 norms[0] = ST::magnitude(ST::zero());
840 for(
int k = 0; k < numBlocks_; ++k) {
841 typename ST::magnitudeType subNorm
842 = vecs_[k].getConstObj()->norm_2();
843 norms[0] += subNorm*subNorm;
845 norms[0] = ST::magnitude(ST::squareroot(norms[0]));
849 template <
class Scalar>
858 norms[0] = ST::magnitude(ST::zero());
859 for(
int k = 0; k < numBlocks_; ++k) {
860 typename ST::magnitudeType subNorm = vecs_[k].getConstObj()->norm_inf();
861 if (subNorm > norms[0]) {
871 template<
class Scalar>
873 Thyra::castOrCreateNonconstProductVectorBase(
const RCP<VectorBase<Scalar> > v)
875 using Teuchos::rcp_dynamic_cast;
876 using Teuchos::tuple;
877 const RCP<ProductVectorBase<Scalar> > prod_v =
878 rcp_dynamic_cast<ProductVectorBase<Scalar> >(v);
882 return defaultProductVector<Scalar>(
883 productVectorSpace<Scalar>(tuple(v->space())()),
889 template<
class Scalar>
891 Thyra::castOrCreateProductVectorBase(
const RCP<
const VectorBase<Scalar> > v)
893 using Teuchos::rcp_dynamic_cast;
894 using Teuchos::tuple;
895 const RCP<const ProductVectorBase<Scalar> > prod_v =
896 rcp_dynamic_cast<
const ProductVectorBase<Scalar> >(v);
900 return defaultProductVector<Scalar>(
901 productVectorSpace<Scalar>(tuple(v->space())()),
911 #define THYRA_DEFAULT_PRODUCT_VECTOR_INSTANT(SCALAR) \
913 template class DefaultProductVector<SCALAR >; \
915 template RCP<ProductVectorBase<SCALAR > > \
916 castOrCreateNonconstProductVectorBase(const RCP<VectorBase<SCALAR > > v); \
918 template RCP<const ProductVectorBase<SCALAR > > \
919 castOrCreateProductVectorBase(const RCP<const VectorBase<SCALAR > > v); \
923 #endif // THYRA_DEFAULT_PRODUCT_VECTOR_DEF_HPP
virtual void norms2Impl(const ArrayView< typename ScalarTraits< Scalar >::magnitudeType > &norms) const
void setSubVector(const RTOpPack::SparseSubVectorT< Scalar > &sub_vec)
Calls setSubVectorImpl().
Base interface for product multi-vectors.
virtual void normsInfImpl(const ArrayView< typename ScalarTraits< Scalar >::magnitudeType > &norms) const
void setSubVectorImpl(const RTOpPack::SparseSubVectorT< Scalar > &sub_vec)
void commitNonconstDetachedVectorViewImpl(RTOpPack::SubVectorView< Scalar > *sub_vec)
bool is_null(const boost::shared_ptr< T > &p)
Base interface for product vectors.
Thrown if vector spaces are incompatible.
basic_OSTab< char > OSTab
virtual void releaseDetachedVectorViewImpl(RTOpPack::ConstSubVectorView< Scalar > *sub_vec) const
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
virtual void updateImpl(Scalar alpha, const MultiVectorBase< Scalar > &mv)
RCP< const VectorBase< Scalar > > getVectorBlock(const int k) const
virtual void linearCombinationImpl(const ArrayView< const Scalar > &alpha, const ArrayView< const Ptr< const MultiVectorBase< Scalar > > > &mv, const Scalar &beta)
virtual void assignMultiVecImpl(const MultiVectorBase< Scalar > &mv)
Default implementation of assign(MV) using RTOps.
basic_FancyOStream< char > FancyOStream
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
virtual void eleWiseScaleImpl(const VectorBase< Scalar > &x)
Default implementation of ele_wise_scale using RTOps.
virtual void reciprocalImpl(const VectorBase< Scalar > &x)
Default implementation of reciprocal using RTOps.
virtual void linearCombinationImpl(const ArrayView< const Scalar > &alpha, const ArrayView< const Ptr< const MultiVectorBase< Scalar > > > &mv, const Scalar &beta)
Default implementation of linear_combination using RTOps.
void uninitialize()
Uninitialize.
virtual void absImpl(const VectorBase< Scalar > &x)
Default implementation of abs using RTOps.
void releaseDetachedVectorViewImpl(RTOpPack::ConstSubVectorView< Scalar > *sub_vec) const
virtual void scaleImpl(Scalar alpha)
virtual void absImpl(const VectorBase< Scalar > &x)
virtual void reciprocalImpl(const VectorBase< Scalar > &x)
virtual void assignMultiVecImpl(const MultiVectorBase< Scalar > &mv)
const ArrayRCP< Scalar > values() const
void setNonconstBlock(int i, const RCP< VectorBase< Scalar > > &b)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
virtual void dotsImpl(const MultiVectorBase< Scalar > &mv, const ArrayView< Scalar > &prods) const
Teuchos::Ordinal Ordinal
Type for the dimension of a vector space. `*.
Interface for a collection of column vectors called a multi-vector.
void applyOpImpl(const RTOpPack::RTOpT< Scalar > &op, const ArrayView< const Ptr< const VectorBase< Scalar > > > &vecs, const ArrayView< const Ptr< VectorBase< Scalar > > > &targ_vecs, const Ptr< RTOpPack::ReductTarget > &reduct_obj, const Ordinal global_offset) const
DefaultProductVector()
Construct to uninitialized.
RCP< const VectorSpaceBase< Scalar > > space() const
void acquireNonconstDetachedVectorViewImpl(const Range1D &rng, RTOpPack::SubVectorView< Scalar > *sub_vec)
virtual std::string description() const
Abstract interface for finite-dimensional dense vectors.
std::string description() const
virtual void eleWiseScaleImpl(const VectorBase< Scalar > &x)
bool blockIsConst(const int k) const
RCP< VectorBase< Scalar > > getNonconstVectorBlock(const int k)
Ordinal globalOffset() const
Teuchos_Ordinal subDim() const
void setGlobalOffset(Ordinal globalOffset_in)
void setGlobalOffset(Teuchos_Ordinal globalOffset_in)
virtual RCP< const VectorSpaceBase< Scalar > > domain() const =0
Return a smart pointer for the domain space for this operator.
bool nonnull(const boost::shared_ptr< T > &p)
RCP< MultiVectorBase< Scalar > > getNonconstMultiVectorBlock(const int k)
TypeTo as(const TypeFrom &t)
virtual Teuchos::ScalarTraits< Scalar >::magnitudeType norm2WeightedImpl(const VectorBase< Scalar > &x) const
Default implementation of norm_2 (weighted) using RTOps.
virtual void acquireDetachedVectorViewImpl(const Range1D &rng, RTOpPack::ConstSubVectorView< Scalar > *sub_vec) const
Teuchos_Ordinal globalOffset() const
#define TEUCHOS_ASSERT(assertion_test)
virtual void assignImpl(Scalar alpha)
virtual void acquireNonconstDetachedVectorViewImpl(const Range1D &rng, RTOpPack::SubVectorView< Scalar > *sub_vec)
void acquireDetachedVectorViewImpl(const Range1D &rng, RTOpPack::ConstSubVectorView< Scalar > *sub_vec) const
#define TEUCHOS_ASSERT_EQUALITY(val1, val2)
RCP< const ProductVectorSpaceBase< Scalar > > productSpace() const
void setBlock(int i, const RCP< const VectorBase< Scalar > > &b)
virtual Teuchos::ScalarTraits< Scalar >::magnitudeType norm2WeightedImpl(const VectorBase< Scalar > &x) const
void initialize(const RCP< const DefaultProductVectorSpace< Scalar > > &productSpace)
Initialize.
virtual void dotsImpl(const MultiVectorBase< Scalar > &mv, const ArrayView< Scalar > &prods) const
Default implementation of dots using RTOps.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
RCP< const MultiVectorBase< Scalar > > getMultiVectorBlock(const int k) const
const ArrayRCP< const Scalar > values() const
virtual void updateImpl(Scalar alpha, const MultiVectorBase< Scalar > &mv)
Default implementation of update using RTOps.
virtual void norms1Impl(const ArrayView< typename ScalarTraits< Scalar >::magnitudeType > &norms) const
virtual void commitNonconstDetachedVectorViewImpl(RTOpPack::SubVectorView< Scalar > *sub_vec)