Thyra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Thyra_VectorSpaceDefaultBase_def.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 #ifndef THYRA_VECTOR_SPACE_DEFAULT_BASE_DEF_HPP
43 #define THYRA_VECTOR_SPACE_DEFAULT_BASE_DEF_HPP
44 
45 #include "Thyra_VectorSpaceDefaultBase_decl.hpp"
46 #include "Thyra_VectorSpaceFactoryBase.hpp"
47 #include "Thyra_VectorSpaceBase.hpp"
48 #include "Thyra_VectorBase.hpp"
49 #include "Thyra_MultiVectorStdOps.hpp"
50 #include "Thyra_DefaultColumnwiseMultiVector.hpp"
51 
52 
53 namespace Thyra {
54 
55 
56 // Helper classes
57 
58 
59 template<class Scalar>
60 class CopyVectorViewBack {
61 public:
62  CopyVectorViewBack( const VectorBase<Scalar> *v, const RTOpPack::SubVectorView<Scalar> &raw_v )
63  :v_(v), raw_v_(raw_v)
64  {}
65  ~CopyVectorViewBack()
66  {
68  v_->acquireDetachedView(Range1D(),&sv);
69  RTOpPack::assign_entries<Scalar>( Teuchos::outArg(raw_v_), sv );
70  v_->releaseDetachedView(&sv);
71  }
72 private:
73  const VectorBase<Scalar> *v_;
75 };
76 
77 
78 template<class Scalar>
79 class CopyMultiVectorViewBack {
80 public:
81  CopyMultiVectorViewBack( const MultiVectorBase<Scalar> *mv, const RTOpPack::SubMultiVectorView<Scalar> &raw_mv )
82  :mv_(mv), raw_mv_(raw_mv)
83  {}
84  ~CopyMultiVectorViewBack()
85  {
87  mv_->acquireDetachedView(Range1D(),Range1D(),&smv);
88  RTOpPack::assign_entries<Scalar>( Teuchos::outArg(raw_mv_), smv );
89  mv_->releaseDetachedView(&smv);
90  }
91 private:
92  const MultiVectorBase<Scalar> *mv_;
94 };
95 
96 
97 // Overridden protected functions from VectorSpaceBase
98 
99 
100 template<class Scalar>
103 {
104  return Teuchos::rcp(
106  Teuchos::rcp(this,false),
107  this->smallVecSpcFcty()->createVecSpc(numMembers)));
108  // ToDo: Use the "self object-reference" idiom ot fix this!
109 }
110 
111 
112 template<class Scalar>
115  const RTOpPack::SubVectorView<Scalar> &raw_v ) const
116 {
117 #ifdef TEUCHOS_DEBUG
118  TEUCHOS_TEST_FOR_EXCEPT( raw_v.subDim() != this->dim() );
119 #endif
120  // Create a vector
121  RCP<VectorBase<Scalar> > v = this->createMember();
122  // Copy initial values in raw_v into vector
124  v->acquireDetachedView(Range1D(),&sv);
125  RTOpPack::assign_entries<Scalar>(
126  Teuchos::ptr_implicit_cast<const RTOpPack::SubVectorView<Scalar> >(Teuchos::outArg(sv)),
128  v->commitDetachedView(&sv);
129  // Setup smart pointer to vector to copy view back out just before vector is destroyed
130  Teuchos::set_extra_data(
131  Teuchos::rcp(new CopyVectorViewBack<Scalar>(&*v,raw_v)),
132  "CopyVectorViewBack",
133  Teuchos::outArg(v),
134  Teuchos::PRE_DESTROY
135  );
136  return v;
137 }
138 
139 
140 template<class Scalar>
143 {
144 #ifdef TEUCHOS_DEBUG
145  TEUCHOS_TEST_FOR_EXCEPT( raw_v.subDim() != this->dim() );
146 #endif
147  // Create a vector
148  RCP<VectorBase<Scalar> > v = this->createMember();
149  // Copy initial values in raw_v into vector
151  v->acquireDetachedView(Range1D(),&sv);
152  RTOpPack::assign_entries<Scalar>(
153  Teuchos::ptr_implicit_cast<const RTOpPack::SubVectorView<Scalar> >(Teuchos::outArg(sv)),
154  raw_v );
155  v->commitDetachedView(&sv);
156  return v;
157 }
158 
159 
160 template<class Scalar>
163  const RTOpPack::SubMultiVectorView<Scalar> &raw_mv ) const
164 {
165 #ifdef TEUCHOS_DEBUG
166  TEUCHOS_TEST_FOR_EXCEPT( raw_mv.subDim() != this->dim() );
167 #endif
168  // Create a multi-vector
169  RCP< MultiVectorBase<Scalar> > mv = this->createMembers(raw_mv.numSubCols());
170  // Copy initial values in raw_mv into multi-vector
172  mv->acquireDetachedView(Range1D(),Range1D(),&smv);
173  RTOpPack::assign_entries<Scalar>(
174  Ptr<const RTOpPack::SubMultiVectorView<Scalar> >(Teuchos::outArg(smv)),
175  raw_mv
176  );
177  mv->commitDetachedView(&smv);
178  // Setup smart pointer to multi-vector to copy view back out just before multi-vector is destroyed
179  Teuchos::set_extra_data(
180  Teuchos::rcp(new CopyMultiVectorViewBack<Scalar>(&*mv,raw_mv)),
181  "CopyMultiVectorViewBack",
182  Teuchos::outArg(mv),
183  Teuchos::PRE_DESTROY
184  );
185  return mv;
186 }
187 
188 
189 template<class Scalar>
192  const RTOpPack::ConstSubMultiVectorView<Scalar> &raw_mv ) const
193 {
194 #ifdef TEUCHOS_DEBUG
195  TEUCHOS_TEST_FOR_EXCEPT( raw_mv.subDim() != this->dim() );
196 #endif
197  // Create a multi-vector
199  this->createMembers(raw_mv.numSubCols());
200  // Copy values in raw_mv into multi-vector
202  mv->acquireDetachedView(Range1D(),Range1D(),&smv);
203  RTOpPack::assign_entries<Scalar>(
204  Ptr<const RTOpPack::SubMultiVectorView<Scalar> >(Teuchos::outArg(smv)),
205  raw_mv );
206  mv->commitDetachedView(&smv);
207  return mv;
208 }
209 
210 
211 } // end namespace Thyra
212 
213 
214 #endif // THYRA_VECTOR_SPACE_DEFAULT_BASE_DEF_HPP
RCP< VectorBase< Scalar > > createMemberView(const RTOpPack::SubVectorView< Scalar > &raw_v) const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Default subclass for MultiVectorBase implemented using columns of separate abstract vectors...
RCP< MultiVectorBase< Scalar > > createMembers(int numMembers) const
RCP< MultiVectorBase< Scalar > > createMembersView(const RTOpPack::SubMultiVectorView< Scalar > &raw_mv) const
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
Teuchos::Range1D Range1D