Thyra Version of the Day
Loading...
Searching...
No Matches
Thyra_DetachedVectorView.hpp
1// @HEADER
2// ***********************************************************************
3//
4// Thyra: Interfaces and Support for Abstract Numerical Algorithms
5// Copyright (2004) Sandia Corporation
6//
7// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8// license for use of this work by or on behalf of the U.S. Government.
9//
10// Redistribution and use in source and binary forms, with or without
11// modification, are permitted provided that the following conditions are
12// met:
13//
14// 1. Redistributions of source code must retain the above copyright
15// notice, this list of conditions and the following disclaimer.
16//
17// 2. Redistributions in binary form must reproduce the above copyright
18// notice, this list of conditions and the following disclaimer in the
19// documentation and/or other materials provided with the distribution.
20//
21// 3. Neither the name of the Corporation nor the names of the
22// contributors may be used to endorse or promote products derived from
23// this software without specific prior written permission.
24//
25// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36//
37// Questions? Contact Roscoe A. Bartlett (bartlettra@ornl.gov)
38//
39// ***********************************************************************
40// @HEADER
41
42#include "Thyra_VectorBase.hpp"
43
44#ifndef THYRA_EXPLICIT_VECTOR_VIEW_HPP
45#define THYRA_EXPLICIT_VECTOR_VIEW_HPP
46
47
48namespace Thyra {
49
50
71template<class Scalar>
73public:
74
111 const Teuchos::RCP<const VectorBase<Scalar> > &v
112 ,const Range1D &rng = Range1D(), const bool forceUnitStride = false
113 )
114 {
115 this->initialize(v,rng,forceUnitStride);
116 }
117
154 const Range1D &rng = Range1D(), const bool forceUnitStride = false )
155 {
156 this->initialize(Teuchos::rcp(&v,false),rng,forceUnitStride);
157 }
158
163 {
164 if( sv_s_.stride() != sv_.stride() )
165 delete [] const_cast<Scalar*>(sv_.values().get());
166 v_->releaseDetachedView(&sv_s_);
167 }
168
172 const RTOpPack::ConstSubVectorView<Scalar>& sv() const { return sv_; }
173
175 Teuchos_Ordinal globalOffset() const { return sv_.globalOffset(); }
176
178 Teuchos_Ordinal subDim() const { return sv_.subDim(); }
179
183 const Scalar* values() const { return sv_.values().get(); }
184
188 ptrdiff_t stride() const { return sv_.stride(); }
189
193 const Scalar& operator[](Teuchos_Ordinal i) const { return sv_[i]; }
194
198 const Scalar& operator()(Teuchos_Ordinal i) const { return sv_(i); }
199
200private:
201
205
206 void initialize(
207 const Teuchos::RCP<const VectorBase<Scalar> > &v,
208 const Range1D &rng, const bool forceUnitStride
209 )
210 {
211 v_ = v;
212 v_->acquireDetachedView(rng,&sv_s_);
213 if( forceUnitStride && sv_s_.stride() != 1 ) {
214 TEUCHOS_TEST_FOR_EXCEPT_MSG(true, "I don't think non-unit stride has ever been tested!");
215 //const ArrayRCP<Scalar> values = Teuchos::arcp(sv_s_.subDim());
216 //Teuchos_Ordinal i; const Scalar *sv_v;
217 //for( sv_v = sv_s_.values().get(), i=0; i < sv_s_.subDim(); ++i, sv_v += sv_s_.stride() )
218 // values[i] = *sv_v;
219 //sv_.initialize(sv_s_.globalOffset(),sv_s_.subDim(),values,1);
220 }
221 else {
222 sv_ = sv_s_;
223 }
224 }
225 // Not defined and not to be called
227 ConstDetachedVectorView(const ConstDetachedVectorView<Scalar>&);
228 ConstDetachedVectorView<Scalar>& operator==(const ConstDetachedVectorView<Scalar>&);
229};
230
231
253template<class Scalar>
255public:
256
295 ,const Range1D &rng = Range1D(), const bool forceUnitStride = false
296 )
297 {
298 this->initialize(v,rng,forceUnitStride);
299 }
300
334 DetachedVectorView( VectorBase<Scalar>& v, const Range1D &rng = Range1D(), const bool forceUnitStride = false )
335 {
336 this->initialize(Teuchos::rcp(&v,false),rng,forceUnitStride);
337 }
338
343 {
344 if( sv_s_.stride() != sv_.stride() ) {
345 Teuchos::TestForTermination_terminate("I don't think non-unit stride has ever been tested!");
346 //Teuchos_Ordinal i; Scalar *sv_v; const Scalar *values;
347 //for (
348 // sv_v = sv_s_.values().get(), values = sv_.values().get(), i=0;
349 // i < sv_s_.subDim();
350 // ++i, sv_v += sv_s_.stride()
351 // )
352 //{
353 // *sv_v = *values++;
354 //}
355 //delete [] sv_.values().get();
356 }
357 v_->commitDetachedView(&sv_s_);
358 }
359
363 const RTOpPack::SubVectorView<Scalar>& sv() const { return sv_; }
364
366 Teuchos_Ordinal globalOffset() const { return sv_.globalOffset(); }
367
369 Teuchos_Ordinal subDim() const { return sv_.subDim(); }
370
374 Scalar* values() const { return sv_.values().get(); }
375
379 ptrdiff_t stride() const { return sv_.stride(); }
380
384 Scalar& operator[](Teuchos_Ordinal i) const { return sv_[i]; }
385
388 Scalar& operator()(Teuchos_Ordinal i) const { return sv_(i); }
389
390private:
391
395
396 void initialize(
398 ,const Range1D &rng, const bool forceUnitStride
399 )
400 {
401 v_ = v;
402 v_->acquireDetachedView(rng,&sv_s_);
403 if( forceUnitStride && sv_s_.stride() != 1 ) {
404 TEUCHOS_TEST_FOR_EXCEPT_MSG(true, "I don't think non-unit stride has ever been tested!");
405 //Scalar *values = new Scalar[sv_s_.subDim()];
406 //Teuchos_Ordinal i; const Scalar *sv_v;
407 //for( sv_v = sv_s_.values().get(), i=0; i < sv_s_.subDim(); ++i, sv_v += sv_s_.stride() )
408 // values[i] = *sv_v;
409 //sv_.initialize(sv_s_.globalOffset(),sv_s_.subDim(),values,1);
410 }
411 else {
412 sv_ = sv_s_;
413 }
414 }
415
416 // Not defined and not to be called
418 DetachedVectorView(const DetachedVectorView<Scalar>&);
419 DetachedVectorView<Scalar>& operator==(const DetachedVectorView<Scalar>&);
420
421};
422
423
424} // namespace Thyra
425
426
427#endif // THYRA_EXPLICIT_VECTOR_VIEW_HPP
Ordinal globalOffset() const
const ArrayRCP< const Scalar > values() const
ptrdiff_t stride() const
T * get() const
Create an explicit non-mutable (const) view of a VectorBase object.
const RTOpPack::ConstSubVectorView< Scalar > & sv() const
Returns the explicit view as an RTOpPack::ConstSubVectorView<Scalar> object.
ConstDetachedVectorView(const Teuchos::RCP< const VectorBase< Scalar > > &v, const Range1D &rng=Range1D(), const bool forceUnitStride=false)
Construct an explicit non-mutable (const) view of a subset of elements.
const Scalar & operator[](Teuchos_Ordinal i) const
Zero-based indexing: Preconditions: values()!=NULL && (0 <= i < subDim()-1).
ptrdiff_t stride() const
Return the stride between elements in the array returned from this->values().
const Scalar * values() const
Return a pointer to a Scalar array containing the explicit view.
~ConstDetachedVectorView()
Free the explicit view on the VectorBase object v passed to ConstDetachedVectorView().
Teuchos_Ordinal subDim() const
Returns the dimension of the explicit view.
Teuchos_Ordinal globalOffset() const
Returns the global offset for the explicit view.
const Scalar & operator()(Teuchos_Ordinal i) const
Zero-based indexing: Preconditions: values()!=NULL && (0 <= i < subDim()-1).
ConstDetachedVectorView(const VectorBase< Scalar > &v, const Range1D &rng=Range1D(), const bool forceUnitStride=false)
Construct an explicit non-mutable (const) view of a subset of elements.
Create an explicit mutable (non-const) view of a VectorBase object.
ptrdiff_t stride() const
Return the stride between elements in the array returned from this->values().
Scalar & operator()(Teuchos_Ordinal i) const
Zero-based indexing: Preconditions: values()!=NULL && (0 <= i < subDim()-1).
DetachedVectorView(VectorBase< Scalar > &v, const Range1D &rng=Range1D(), const bool forceUnitStride=false)
Construct an explicit mutable (non-const) view of a subset of elements.
const RTOpPack::SubVectorView< Scalar > & sv() const
Returns the explicit view as an RTOpPack::ConstSubVectorView<Scalar> object.
~DetachedVectorView()
Commits back the the explicit view on the VectorBase object v passed to DetachedVectorView().
Teuchos_Ordinal globalOffset() const
Returns the global offset for the explicit view.
Scalar * values() const
Return a pointer to a Scalar array containing the explicit view.
DetachedVectorView(const Teuchos::RCP< VectorBase< Scalar > > &v, const Range1D &rng=Range1D(), const bool forceUnitStride=false)
Construct an explicit mutable (non-const) view of a subset of elements.
Scalar & operator[](Teuchos_Ordinal i) const
Zero-based indexing: Preconditions: values()!=NULL && (0 <= i < subDim()-1).
Teuchos_Ordinal subDim() const
Returns the dimension of the explicit view.
Abstract interface for finite-dimensional dense vectors.
#define TEUCHOS_TEST_FOR_EXCEPT_MSG(throw_exception_test, msg)
Teuchos::Range1D Range1D
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
TEUCHOSCORE_LIB_DLL_EXPORT void TestForTermination_terminate(const std::string &msg)