MOOCHO (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AbstractLinAlgPack_VectorMutableDense.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
5 // Copyright (2003) 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 (rabartl@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #include <typeinfo>
43 #include <stdexcept>
44 
50 #include "Teuchos_Workspace.hpp"
51 #include "Teuchos_Assert.hpp"
52 
53 #ifdef TEUCHOS_DEBUG
54 #define CLASS_MEMBER_PTRS \
55 const VectorMutableDense *_this = this; \
56 const DVectorSlice *_v; \
57 const release_resource_ptr_t *_v_release; \
58 const VectorSpaceSerial *_space;
59 #else
60 #define CLASS_MEMBER_PTRS
61 #endif
62 
63 namespace AbstractLinAlgPack {
64 
66  const size_type dim
67  )
68  :space_(dim)
69 {
71  this->initialize(dim);
72 }
73 
75  DVectorSlice v
76  ,const release_resource_ptr_t& v_release
77  )
78  :space_(v.dim())
79 {
81  this->initialize(v,v_release);
82 }
83 
85  const size_type dim
86  )
87 {
89  namespace rcp = MemMngPack;
90  namespace rmp = MemMngPack;
92  vec_ptr_t vec_ptr = Teuchos::rcp(new DVector(dim));
93  this->initialize(
94  (*vec_ptr)()
95  ,Teuchos::rcp(
96  new rmp::ReleaseResource_ref_count_ptr<DVector>(
97  vec_ptr
98  )
99  )
100  );
101 }
102 
104  DVectorSlice v
105  ,const release_resource_ptr_t& v_release
106  )
107 {
109  v_.bind(v);
110  v_release_ = v_release;
111  space_.initialize(v.dim());
112  this->has_changed();
113 }
114 
115 // Overridden from Vector
116 
118 {
120  return space_;
121 }
122 
124  const RTOpPack::RTOp& op
125  ,const size_t num_vecs, const Vector* vecs[]
126  ,const size_t num_targ_vecs, VectorMutable* targ_vecs[]
127  ,RTOpPack::ReductTarget *reduct_obj
128  ,const index_type first_ele_in, const index_type sub_dim_in, const index_type global_offset_in
129  ) const
130 {
132 #ifdef TEUCHOS_DEBUG
134  "VectorMutableDense::apply_op(...)"
135  ,op,num_vecs,vecs,num_targ_vecs,targ_vecs,reduct_obj,first_ele_in,sub_dim_in,global_offset_in
136  );
137 #endif
138  this->apply_op_serial(
139  op,num_vecs,vecs,num_targ_vecs,targ_vecs,reduct_obj
140  ,first_ele_in,sub_dim_in,global_offset_in
141  );
142 }
143 
145 {
146  return v_.dim();
147 }
148 
150 {
151  return v_(i);
152 }
153 
155  const Range1D& rng_in, RTOpPack::SubVector* sub_vec
156  ) const
157 {
159  const size_type this_dim = v_.dim();
160  const Range1D rng = RangePack::full_range(rng_in,1,this_dim);
162  rng.ubound() > this_dim, std::out_of_range
163  ,"VectorMutableDense::get_sub_vector(...) : Error, "
164  "rng = ["<<rng.lbound()<<","<<rng.ubound()<<"] "
165  "is not in the range [1,this->dim()] = [1," << this_dim << "]!" );
166  // Just return the dense view regardless of spare_or_dense argument
167  sub_vec->initialize(
168  rng.lbound()-1 // global_offset
169  ,rng.size() // sub_dim
170  ,v_.raw_ptr()+v_.stride()*(rng.lbound()-1) // values
171  ,v_.stride()
172  );
173 }
174 
176 {
177  sub_vec->set_uninitialized(); // No memory to deallocate!
178 }
179 
180 // Overridden from VectorMutable
181 
184 {
186  v_ = alpha;
187  this->has_changed();
188  return *this;
189 }
190 
193 {
195  if( const VectorMutableDense *vp = dynamic_cast<const VectorMutableDense*>(&v) )
196  v_ = vp->v_;
197  else
198  return VectorMutable::operator=(v); // Try the default implementation?
199  this->has_changed();
200  return *this;
201 }
202 
205 {
207  if( const VectorMutableDense *vp = dynamic_cast<const VectorMutableDense*>(&v) )
208  v_ = vp->v_;
209  else
210  return VectorMutable::operator=(v); // Try the default implementation?
211  this->has_changed();
212  return *this;
213 }
214 
216 {
218  v_(i) = val;
219  this->has_changed();
220 }
221 
224 {
226  namespace rcp = MemMngPack;
227  const size_type this_dim = this->dim();
228  const Range1D rng = RangePack::full_range( rng_in, 1, this_dim );
229 #ifdef TEUCHOS_DEBUG
231  rng.ubound() > this_dim, std::out_of_range
232  ,"VectorMutableDense::sub_view(...) : Error, "
233  "rng = ["<<rng.lbound()<<","<<rng.ubound()<<"] "
234  "is not in the range [1,this->dim()] = [1," << this_dim << "]!" );
235 #endif
236  if( rng == Range1D(1,this_dim) )
237  return Teuchos::rcp( this, false );
238  this->has_changed(); // This will result in a change in the vector
239  return Teuchos::rcp( new VectorMutableDense( v_(rng), Teuchos::null ) );
240 }
241 
243  const Range1D& rng_in, RTOpPack::MutableSubVector* sub_vec )
244 {
246  const size_type this_dim = v_.dim();
247  const Range1D rng = RangePack::full_range(rng_in,1,this_dim);
248 #ifdef TEUCHOS_DEBUG
250  rng.ubound() > this_dim, std::out_of_range
251  ,"VectorMutableDense::get_sub_vector(...) : Error, "
252  "rng = ["<<rng.lbound()<<","<<rng.ubound()<<"] "
253  "is not in the range [1,this->dim()] = [1," << this_dim << "]!" );
254 #endif
255  sub_vec->initialize(
256  rng.lbound()-1 // global_offset
257  ,rng.size() // sub_dim
258  ,v_.raw_ptr()+v_.stride()*(rng.lbound()-1) // values
259  ,v_.stride()
260  );
261 }
262 
264 {
266  sub_vec->set_uninitialized(); // No memory to deallocate!
267  this->has_changed(); // Be aware of any final changes!
268 }
269 
271 {
273  VectorMutable::set_sub_vector(sub_vec); // ToDo: Provide specialized implementation?
274 }
275 
277  value_type alpha
278  ,const GenPermMatrixSlice &P
279  ,BLAS_Cpp::Transp P_trans
280  ,const Vector &x
281  ,value_type beta
282  )
283 {
285  VectorDenseEncap x_de(x);
286  AbstractLinAlgPack::Vp_StMtV( &v_, alpha, P, P_trans, x_de(), beta );
287 }
288 
289 } // end namespace AbstractLinAlgPack
DVector "Adaptor" subclass for DenseLinAlgPack::DVectorSlice or DenseLinAlgPack::DVector objects...
virtual void set_sub_vector(const RTOpPack::SparseSubVector &sub_vec)
Set a specific sub-vector.
Abstract interface for immutable, finite dimensional, coordinate vectors {abstract}.
size_type dim() const
Returns the number of elements of the VectorSliceTmpl.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Index size() const
Return the size of the range (ubound() - lbound() + 1)
Teuchos::RCP< const Vector > vec_ptr_t
Index ubound() const
Return upper bound of the range.
void commit_sub_vector(RTOpPack::MutableSubVector *sub_vec)
void apply_op(const RTOpPack::RTOp &op, const size_t num_vecs, const Vector *vecs[], const size_t num_targ_vecs, VectorMutable *targ_vecs[], RTOpPack::ReductTarget *reduct_obj, const index_type first_ele, const index_type sub_dim, const index_type global_offset) const
Teuchos::RCP< VectorMutable > vec_mut_ptr_t
VectorMutableDense(const size_type dim=0)
Calls this->initialize(dim).
value_type * raw_ptr()
Return a pointer to the address of the first memory location of underlying array. ...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
. One-based subregion index range class.
virtual void has_changed() const
Must be called by any vector subclass that modifies this vector object!
RTOpT< RTOp_value_type > RTOp
Abstract interface for objects that represent a space for mutable coordinate vectors.
Extract a constant DenseLinAlgPack::DVectorSlice view of a Vector object.
void set_sub_vector(const RTOpPack::SparseSubVector &sub_vec)
void initialize(const size_type dim)
Call this->initialize(v,v_release) with an allocated DenseLinAlgPack::DVector object.
void Vp_StMtV(VectorMutable *v_lhs, value_type alpha, const MatrixOp &M_rhs1, BLAS_Cpp::Transp trans_rhs1, const Vector &v_rhs2, value_type beta=1.0)
v_lhs = alpha * op(M_rhs1) * v_rhs2 + beta * v_lhs (BLAS xGEMV)
void bind(VectorSliceTmpl< value_type > vs)
Bind to the view of another VectorSliceTmpl.
Index lbound() const
Return lower bound of the range.
void initialize(size_type dim)
Initialize given the dimension of the vector space.
virtual VectorMutable & operator=(value_type alpha)
Assign the elements of this vector to a scalar.
void initialize(Teuchos_Ordinal globalOffset, Teuchos_Ordinal subDim, Scalar *values, ptrdiff_t stride)
DenseLinAlgPack::VectorTmpl< value_type > DVector
difference_type stride() const
Return the distance (+,-) (in units of elements) between adjacent elements in the underlying array...
Abstract interface for mutable coordinate vectors {abstract}.
void Vp_StMtV(value_type alpha, const GenPermMatrixSlice &P, BLAS_Cpp::Transp P_trans, const Vector &x, value_type beta)
SparseSubVectorT< RTOp_value_type > SparseSubVector
void apply_op_serial(const RTOpPack::RTOp &op, const size_t num_vecs, const Vector *vecs[], const size_t num_targ_vecs, VectorMutable *targ_vecs[], RTOpPack::ReductTarget *reduct_obj, const index_type first_ele, const index_type sub_dim, const index_type global_offset) const
Class for a mutable sub-vector.
void apply_op_validate_input(const char func_name[], const RTOpPack::RTOp &op, const size_t num_vecs, const Vector *vecs[], const size_t num_targ_vecs, VectorMutable *targ_vecs[], RTOpPack::ReductTarget *reduct_obj, const index_type first_ele, const index_type sub_dim, const index_type global_offset)
Validate the inputs to apply_op().
void get_sub_vector(const Range1D &rng, RTOpPack::SubVector *sub_vec) const
Transp
TRANS.
void initialize(Teuchos_Ordinal globalOffset, Teuchos_Ordinal subDim, const Scalar *values, ptrdiff_t stride)
Class for a non-mutable sub-vector.
void free_sub_vector(RTOpPack::SubVector *sub_vec) const
Concrete matrix type to represent general permutation (mapping) matrices.