AbstractLinAlgPack: C++ Interfaces For Vectors, Matrices And Related Linear Algebra Objects  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
AbstractLinAlgPack_VectorSubView.cpp
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 <assert.h>
43 
44 #include <stdexcept>
45 
46 #include "AbstractLinAlgPack_VectorMutableSubView.hpp"
47 #include "Teuchos_Assert.hpp"
48 #include "Teuchos_Workspace.hpp"
49 #include "Teuchos_dyn_cast.hpp"
50 
51 namespace AbstractLinAlgPack {
52 
54 {
55  initialize(vec,rng);
56 }
57 
58 void VectorSubView::initialize( const vec_ptr_t& vec, const Range1D& rng )
59 {
60  namespace rcp = MemMngPack;
61 #ifdef TEUCHOS_DEBUG
63  vec.get() == NULL, std::invalid_argument
64  ,"VectorSubView::initialize(...) : Error!" );
65 #endif
66  typedef VectorSpace::space_ptr_t space_ptr_t;
67  space_.initialize( Teuchos::rcp(&vec->space(),false), rng );
68  full_vec_ = vec;
69  this->has_changed();
70 }
71 
73 {
74  space_.set_uninitialized();
75  full_vec_ = Teuchos::null;
76  this->has_changed();
77 }
78 
79 // Overridden from Vector
80 
82 {
83  return space_;
84 }
85 
86 index_type VectorSubView::dim() const
87 {
88  return space_.dim();
89 }
90 
92  const RTOpPack::RTOp& op
93  ,const size_t num_vecs, const Vector* vecs[]
94  ,const size_t num_targ_vecs, VectorMutable* targ_vecs[]
95  ,RTOpPack::ReductTarget* reduct_obj
96  ,const index_type first_ele_in, const index_type sub_dim_in, const index_type global_offset_in
97  ) const
98 {
99  using Teuchos::dyn_cast;
100  using Teuchos::Workspace;
102  // If these are in-core vectors then just let a default implementation
103  // take care of this.
104  if( this->space().is_in_core() ) {
105  this->apply_op_serial(
106  op,num_vecs,vecs,num_targ_vecs,targ_vecs,reduct_obj
107  ,first_ele_in,sub_dim_in,global_offset_in
108  );
109  return;
110  }
111  // These are not in-core vectors so ...
112  int k;
113  const index_type this_dim = this->dim();
114 #ifdef TEUCHOS_DEBUG
116  sub_dim_in < 0
117  || !(1 <= first_ele_in && first_ele_in <= this_dim)
118  || ( sub_dim_in > 0 && (sub_dim_in - (first_ele_in - 1) > this_dim) )
119  , std::logic_error
120  ,"VectorSubView::apply_op(...): Error, first_ele_in = "
121  << first_ele_in << ", global_offset_in = " << global_offset_in
122  << ", sub_dim_in = " << sub_dim_in << " and this->dim() = this_dim = "
123  << this_dim << " are not compatible." );
124 #endif
125  const index_type this_offset = space_impl().rng().lbound() - 1;
126  const index_type
127  this_sub_dim = ( sub_dim_in
128  ? sub_dim_in
129  : space_impl().rng().size() - (first_ele_in - 1)
130  );
131  Workspace<const Vector*> vecs_full(wss,num_vecs);
132  for( k = 0; k < num_vecs; ++k )
133  vecs_full[k] = dyn_cast<const VectorSubView>(*vecs[k]).full_vec().get();
134  Workspace<VectorMutable*> targ_vecs_full(wss,num_targ_vecs);
135  for( k = 0; k < num_targ_vecs; ++k )
136  targ_vecs_full[k] = dyn_cast<VectorMutableSubView>(*targ_vecs[k]).full_vec().get();
137  AbstractLinAlgPack::apply_op(
138  op
139  ,num_vecs, num_vecs ? &vecs_full[0] : NULL
140  ,num_targ_vecs, num_targ_vecs ? &targ_vecs_full[0] : NULL
141  ,reduct_obj
142  ,this_offset + first_ele_in // first_ele
143  ,this_sub_dim // sub_dim
144  ,global_offset_in // global_dim
145  );
146 }
147 
148 value_type VectorSubView::get_ele(index_type i) const
149 {
150  space_.validate_range(Range1D(i,i));
151  return full_vec_->get_ele( space_.rng().lbound() + i - 1 );
152 }
153 
155 VectorSubView::sub_view( const Range1D& rng_in ) const
156 {
157  namespace rcp = MemMngPack;
158  const index_type this_dim = this->dim();
159  const Range1D rng = RangePack::full_range(rng_in,1,this_dim);
160  space_.validate_range(rng);
161  const index_type this_offset = space_.rng().lbound() - 1;
162  return Teuchos::rcp(
163  new VectorSubView(
164  full_vec_
165  ,Range1D(
166  this_offset + rng.lbound()
167  ,this_offset + rng.ubound() )
168  ) );
169 }
170 
171 void VectorSubView::get_sub_vector( const Range1D& rng_in, RTOpPack::SubVector* sub_vec ) const
172 {
173 #ifdef TEUCHOS_DEBUG
174  TEUCHOS_TEST_FOR_EXCEPTION( !sub_vec, std::logic_error, "VectorSubView::get_sub_vector(...): Error!" );
175 #endif
176  const index_type this_dim = this->dim();
177  const Range1D rng = RangePack::full_range(rng_in,1,this_dim);
178  space_.validate_range(rng);
179  const index_type this_offset = space_.rng().lbound() - 1;
180  full_vec_->get_sub_vector( rng + this_offset, sub_vec );
181  sub_vec->setGlobalOffset( sub_vec->globalOffset() - this_offset );
182 }
183 
184 void VectorSubView::free_sub_vector( RTOpPack::SubVector* sub_vec ) const
185 {
186 #ifdef TEUCHOS_DEBUG
187  TEUCHOS_TEST_FOR_EXCEPTION( !sub_vec, std::logic_error, "VectorSubView::free_sub_vector(...): Error!" );
188 #endif
189  const index_type this_offset = space_.rng().lbound() - 1;
190  sub_vec->setGlobalOffset( sub_vec->globalOffset() + this_offset );
191  full_vec_->free_sub_vector( sub_vec );
192 }
193 
194 } // end namespace AbstractLinAlgPack
void initialize(const space_ptr_t &full_space, const Range1D &rng)
Initialize.
const VectorSpaceSubSpace & space_impl() const
Abstract interface for immutable, finite dimensional, coordinate vectors {abstract}.
void get_sub_vector(const Range1D &rng, RTOpPack::SubVector *sub_vec) const
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Index size() const
Index ubound() const
T_To & dyn_cast(T_From &from)
T * get() const
void free_sub_vector(RTOpPack::SubVector *sub_vec) const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
virtual void has_changed() const
Must be called by any vector subclass that modifies this vector object!
void validate_range(const Range1D &rng) const
Validate rng.
Abstract interface for objects that represent a space for mutable coordinate vectors.
vec_ptr_t sub_view(const Range1D &rng) const
Index lbound() const
Abstract interface for mutable coordinate vectors {abstract}.
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
void initialize(const vec_ptr_t &full_vec, const Range1D &rng)
Initialize a sub-view based on a full vector.
TEUCHOSCORE_LIB_DLL_EXPORT Teuchos::RCP< WorkspaceStore > get_default_workspace_store()
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
Calls apply_op() on the underlying full vectors.