Thyra Version of the Day
Loading...
Searching...
No Matches
Thyra_DefaultProductVectorSpace_def.hpp
1// @HEADER
2// *****************************************************************************
3// Thyra: Interfaces and Support for Abstract Numerical Algorithms
4//
5// Copyright 2004 NTESS and the Thyra contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#ifndef THYRA_DEFAULT_PRODUCT_VECTOR_SPACE_HPP
11#define THYRA_DEFAULT_PRODUCT_VECTOR_SPACE_HPP
12
13
14#include "Thyra_DefaultProductVectorSpace_decl.hpp"
15#include "Thyra_DefaultProductVector.hpp"
16#include "Thyra_DefaultProductMultiVector.hpp"
17#include "Thyra_ProductMultiVectorBase.hpp"
18#include "Teuchos_Workspace.hpp"
19#include "Teuchos_dyn_cast.hpp"
20
21
22namespace Thyra {
23
24
25// Constructors/initializers/accessors
26
27
28template<class Scalar>
30 : numBlocks_(-1), dim_(-1), isInCore_(false)
31{}
32
33
34template<class Scalar>
36 const ArrayView<const RCP<const VectorSpaceBase<Scalar> > > &vecSpaces_in
37 )
38 : numBlocks_(-1), dim_(-1)
39{
40 initialize(vecSpaces_in);
41}
42
43
44template<class Scalar>
46 const ArrayView<const RCP<const VectorSpaceBase<Scalar> > > &vecSpaces_in
47 )
48{
49
50 //
51 // Check preconditions and compute cached quantities
52 //
53 const int nBlocks = vecSpaces_in.size();
54#ifdef TEUCHOS_DEBUG
55 TEUCHOS_TEST_FOR_EXCEPT( nBlocks == 0 );
56#endif
57 bool overallHasInCoreView = true;
58 for (int k = 0; k < nBlocks; ++k) {
59#ifdef TEUCHOS_DEBUG
61 vecSpaces_in[k].get() == NULL, std::invalid_argument
62 ,"Error, the smart pointer vecSpaces["<<k<<"] can not be NULL!"
63 );
64#endif
65 if (!vecSpaces_in[k]->hasInCoreView()) overallHasInCoreView = false;
66 }
67
68 //
69 // Setup private data members (should not throw an exception from here)
70 //
71 numBlocks_ = nBlocks;
72 vecSpaces_ = Teuchos::rcp(new vecSpaces_t);
73 *vecSpaces_ = vecSpaces_in;
74 vecSpacesOffsets_ = Teuchos::rcp(new vecSpacesOffsets_t(nBlocks+1));
75 (*vecSpacesOffsets_)[0] = 0;
76 dim_ = 0;
77 for( int k = 1; k <= nBlocks; ++k ) {
78 const Ordinal dim_km1 = vecSpaces_in[k-1]->dim();
79 (*vecSpacesOffsets_)[k] = (*vecSpacesOffsets_)[k-1] + dim_km1;
80 dim_ += dim_km1;
81 }
82 isInCore_ = overallHasInCoreView;
83
84}
85
86
87template<class Scalar>
89 const ArrayView<RCP<const VectorSpaceBase<Scalar> > > &vecSpaces_in
90 )
91{
92 TEUCHOS_TEST_FOR_EXCEPT(!is_null(vecSpaces_in)); // ToDo: Implement!
93 vecSpaces_ = Teuchos::null;
94 vecSpacesOffsets_ = Teuchos::null;
95 numBlocks_ = -1;
96 dim_ = -1;
97 isInCore_ = false;
98}
99
100
101template<class Scalar>
103 Ordinal i, int* kth_vector_space, Ordinal* kth_global_offset
104 ) const
105{
106 // Validate the preconditions
107#ifdef TEUCHOS_DEBUG
109 !(0 <= i && i < this->dim()), std::out_of_range
110 ,"VectorSpaceBlocked::get_vector_space_position(...): Error, i = "
111 << i << " is not in range [0,"<<(this->dim()-1)<<"]"
112 );
113#endif
114 *kth_vector_space = 0;
115 *kth_global_offset = 0;
116 while( *kth_vector_space < numBlocks_ ) {
117 const Ordinal off_kp1 = (*vecSpacesOffsets_)[*kth_vector_space+1];
118 if( off_kp1 > i ) {
119 *kth_global_offset = (*vecSpacesOffsets_)[*kth_vector_space];
120 break;
121 }
122 ++(*kth_vector_space);
123 }
124 TEUCHOS_TEST_FOR_EXCEPT( !(*kth_vector_space < numBlocks_) );
125}
126
127
128// Overridden from DefaultProductVectorSpace
129
130
131template<class Scalar>
133{
134 return numBlocks_;
135}
136
137
138template<class Scalar>
141{
142 TEUCHOS_TEST_FOR_EXCEPT( k < 0 || numBlocks_ < k );
143 return (*vecSpaces_)[k];
144}
145
146
147// Overridden from VectorSpaceBase
148
149
150template<class Scalar>
152{
153 return dim_;
154}
155
156
157template<class Scalar>
159 const VectorSpaceBase<Scalar>& vecSpc ) const
160{
161
162 using Teuchos::ptrFromRef;
163 using Teuchos::ptr_dynamic_cast;
164
165 const int nBlocks = this->numBlocks();
166
167 // Check for product vector interface
169 ptr_dynamic_cast<const ProductVectorSpaceBase<Scalar> >(ptrFromRef(vecSpc));
170
171 if (nonnull(pvsb)) {
172 // Validate that constituent vector spaces are compatible
173 if( nBlocks != pvsb->numBlocks() )
174 return false;
175 for( int i = 0; i < nBlocks; ++i ) {
176 if( !this->getBlock(i)->isCompatible(*pvsb->getBlock(i)) )
177 return false;
178 }
179 return true;
180 }
181
182 // Check for a single vector single vector space
183 if (nBlocks == 1) {
184 return this->getBlock(0)->isCompatible(vecSpc);
185 }
186
187 // If we get here, the RHS is not a product vector space and/or this is not
188 // a single block VS so we can assume the spaces are *not* compatible!
189 return false;
190
191}
192
193
194template<class Scalar>
197{
198 return defaultProductVector<Scalar>(Teuchos::rcpFromRef(*this));
199}
200
201
202template<class Scalar>
204 const VectorBase<Scalar> &x_in,
205 const VectorBase<Scalar> &y_in
206 ) const
207{
208 const int nBlocks = this->numBlocks();
212#ifdef TEUCHOS_DEBUG
214 nBlocks!=x.productSpace()->numBlocks()
215 || nBlocks!=y.productSpace()->numBlocks()
216 );
217#endif
218 Scalar scalarProd_rtn = Teuchos::ScalarTraits<Scalar>::zero();
219 for( int k = 0; k < nBlocks; ++k )
220 scalarProd_rtn += (*vecSpaces_)[k]->scalarProd(
221 *x.getVectorBlock(k),*y.getVectorBlock(k)
222 );
223 return scalarProd_rtn;
224}
225
226
227template<class Scalar>
229 const MultiVectorBase<Scalar> &X_in,
230 const MultiVectorBase<Scalar> &Y_in,
231 const ArrayView<Scalar> &scalarProds_out
232 ) const
233{
234 using Teuchos::as;
235 using Teuchos::Workspace;
236 const VectorSpaceBase<Scalar> &domain = *X_in.domain();
237 const Ordinal m = domain.dim();
238#ifdef TEUCHOS_DEBUG
239 TEUCHOS_TEST_FOR_EXCEPT(is_null(scalarProds_out));
240 TEUCHOS_TEST_FOR_EXCEPT( !domain.isCompatible(*Y_in.domain()) );
241 TEUCHOS_ASSERT_EQUALITY( as<Ordinal>(scalarProds_out.size()),
242 as<Ordinal>(m) )
243#endif
244 if(m==1) {
245 scalarProds_out[0] = this->scalarProd(*X_in.col(0),*Y_in.col(0));
246 return;
247 // ToDo: Remove this if(...) block once we have a DefaultProductMultiVector implementation!
248 }
250 const int nBlocks = this->numBlocks();
254#ifdef TEUCHOS_DEBUG
255 TEUCHOS_TEST_FOR_EXCEPT( nBlocks!=X.productSpace()->numBlocks() || nBlocks!=Y.productSpace()->numBlocks() );
256#endif
257 Workspace<Scalar> _scalarProds_out(wss, m, false);
258 std::fill( scalarProds_out.begin(), scalarProds_out.end(),
260 for( int k = 0; k < nBlocks; ++k ) {
261 (*vecSpaces_)[k]->scalarProds(
262 *X.getMultiVectorBlock(k), *Y.getMultiVectorBlock(k), _scalarProds_out());
263 for( int j = 0; j < m; ++j )
264 scalarProds_out[j] += _scalarProds_out[j];
265 }
266}
267
268
269template<class Scalar>
270bool DefaultProductVectorSpace<Scalar>::hasInCoreView(const Range1D& rng_in, const EViewType viewType, const EStrideType strideType) const
271{
272 const Range1D rng = full_range(rng_in,0,dim_-1);
273 // First see if rng fits in a single constituent vector
274 int kth_vector_space = -1;
275 Ordinal kth_global_offset = 0;
276 this->getVecSpcPoss(rng.lbound(),&kth_vector_space,&kth_global_offset);
277#ifdef TEUCHOS_DEBUG
278 TEUCHOS_TEST_FOR_EXCEPT( !( 0 <= kth_vector_space && kth_vector_space <= numBlocks_ ) );
279#endif
280 if( rng.lbound() + rng.size() <= kth_global_offset + (*vecSpaces_)[kth_vector_space]->dim() ) {
281 return (*vecSpaces_)[kth_vector_space]->hasInCoreView(rng_in-kth_global_offset,viewType,strideType);
282 }
283 // If we get here, rng does not fit in a single constituent vector which
284 // also means that numBlocks_ > 1 must also be true!
285 //
286 // Next, if the client is asking for a direct view then we have to return
287 // false since this range spans more than one constituent vector.
288 if( viewType == VIEW_TYPE_DIRECT )
289 return false;
290 // If we get here then hasDirectView==false and therefore we are allowed to
291 // create a copy. Therefore, if all of the constituent vectors are "in
292 // core" then we can return true.
293 if(isInCore_)
294 return true;
295 // Finally, loop through all of the constituent vectors spaned by rng and
296 // see if they are each in core.
297 //
298 // Todo: Implement this if you have to!
299 //
300 // We must give up and return false
301 return false;
302}
303
304
305template<class Scalar>
308{
309 if (dim_)
310 return (*vecSpaces_)[0]->smallVecSpcFcty(); // They should all be compatible?
311 return Teuchos::null;
312}
313
314
315template<class Scalar>
318{
319 return defaultProductMultiVector<Scalar>(Teuchos::rcpFromRef(*this),
320 numMembers);
321}
322
323
324template<class Scalar>
327{
328 // Warning! If the client uninitialized this object then changes the
329 // constituent vector spaces then we are in trouble! The client is warned
330 // in documentation!
332 pvs = productVectorSpace<Scalar>();
333 pvs->numBlocks_ = numBlocks_;
334 pvs->vecSpaces_ = vecSpaces_;
335 pvs->vecSpacesOffsets_ = vecSpacesOffsets_;
336 pvs->dim_ = dim_;
337 pvs->isInCore_ = isInCore_;
338 return pvs;
339}
340
341
342// Overridden from Teuchos::Describable
343
344
345template<class Scalar>
347{
348 std::ostringstream oss;
349 oss
351 << "dim="<<dim_
352 << ",numBlocks="<<numBlocks_
353 << "}";
354 return oss.str();
355}
356
357
358template<class Scalar>
360 Teuchos::FancyOStream &out_arg
361 ,const Teuchos::EVerbosityLevel verbLevel
362 ) const
363{
365 using Teuchos::OSTab;
366 RCP<FancyOStream> out = rcpFromRef(out_arg);
367 OSTab tab(out);
368 if (includesVerbLevel(verbLevel, Teuchos::VERB_LOW, true)) {
369 *out << this->description() << std::endl;
370 }
371 if (includesVerbLevel(verbLevel, Teuchos::VERB_MEDIUM) && numBlocks_ > 0) {
372 OSTab tab2(out);
373 *out << "Constituent vector spaces V[0], V[1], ... V[numBlocks-1]:\n";
374 OSTab tab3(out);
375 for( int k = 0; k < numBlocks_; ++k ) {
376 *out << "V["<<k<<"] = " << Teuchos::describe(*(*vecSpaces_)[k],verbLevel);
377 }
378 }
379}
380
381
382} // namespace Thyra
383
384
385#endif // THYRA_DEFAULT_PRODUCT_VECTOR_SPACE_HPP
iterator end() const
iterator begin() const
size_type size() const
virtual std::string description() const
Ordinal size() const
Ordinal lbound() const
DefaultProductVectorSpace()
Default construct to uninitialized.
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
Prints the details about the constituent vector spaces.
RCP< MultiVectorBase< Scalar > > createMembers(int numMembers) const
Returns a DefaultProductMultiVector object.
RCP< const VectorSpaceBase< Scalar > > getBlock(const int k) const
RCP< const VectorSpaceBase< Scalar > > clone() const
Clones the object as promised.
bool hasInCoreView(const Range1D &rng, const EViewType viewType, const EStrideType strideType) const
Returns true if all of the constituent vector spaces return true.
void getVecSpcPoss(Ordinal i, int *kth_vector_space, Ordinal *kth_global_offset) const
Get the position of the vector space object and its offset into a composite vector that owns the ith ...
std::string description() const
Prints just the name DefaultProductVectorSpace along with the overall dimension and the number of blo...
RCP< VectorBase< Scalar > > createMember() const
Returns a DefaultProductVector object.
virtual void initialize(const ArrayView< const RCP< const VectorSpaceBase< Scalar > > > &vecSpaces)
Initialize with a list of constituent vector spaces.
virtual void uninitialize(const ArrayView< RCP< const VectorSpaceBase< Scalar > > > &vecSpaces=Teuchos::null)
Uninitialize.
RCP< const VectorSpaceFactoryBase< Scalar > > smallVecSpcFcty() const
Returns getBlock(0)->smallVecSpcFcty().
bool isCompatible(const VectorSpaceBase< Scalar > &vecSpc) const
Returns true only if also a product vector space and all constituent vectors are compatible.
Ordinal dim() const
Returns the summation of the constituent vector spaces.
void scalarProdsImpl(const MultiVectorBase< Scalar > &X, const MultiVectorBase< Scalar > &Y, const ArrayView< Scalar > &scalarProds) const
Returns the sum of the scalar products of each of the columns of the constituent multi-vectors.
Scalar scalarProd(const VectorBase< Scalar > &x, const VectorBase< Scalar > &y) const
Returns the sum of the scalar products of the constituent vectors.
virtual RCP< const VectorSpaceBase< Scalar > > domain() const =0
Return a smart pointer for the domain space for this operator.
Interface for a collection of column vectors called a multi-vector.
RCP< const VectorBase< Scalar > > col(Ordinal j) const
Calls colImpl().
Base interface for product multi-vectors.
virtual Teuchos::RCP< const ProductVectorSpaceBase< Scalar > > productSpace() const =0
Returns the associated product vector space that represents the range.
virtual Teuchos::RCP< const MultiVectorBase< Scalar > > getMultiVectorBlock(const int k) const =0
Returns a non-persisting const view of the (zero-based) kth block multi-vector.
Base interface for product vectors.
virtual RCP< const VectorBase< Scalar > > getVectorBlock(const int k) const =0
Returns a non-persisting const view of the (zero-based) kth block vector.
Abstract interface for finite-dimensional dense vectors.
Abstract interface for objects that represent a space for vectors.
virtual Ordinal dim() const =0
Return the dimension of the vector space.
virtual bool isCompatible(const VectorSpaceBase< Scalar > &vecSpc) const =0
Compare the compatibility of two vector spaces.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
#define TEUCHOS_ASSERT_EQUALITY(val1, val2)
EStrideType
Determine if data is unit stride or non-unit stride.
EViewType
Determines if a view is a direct view of data or a detached copy of data.
Teuchos::Ordinal Ordinal
Type for the dimension of a vector space. `*.
@ VIEW_TYPE_DIRECT
The view is a direct view of data and no copies are made.
TypeTo as(const TypeFrom &t)
T_To & dyn_cast(T_From &from)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
TEUCHOSCORE_LIB_DLL_EXPORT Teuchos::RCP< WorkspaceStore > get_default_workspace_store()
TEUCHOSCORE_LIB_DLL_EXPORT bool includesVerbLevel(const EVerbosityLevel verbLevel, const EVerbosityLevel requestedVerbLevel, const bool isDefaultLevel=false)