RTOp Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RTOpPack_ROpGetSubVector_def.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // RTOp: Interfaces and Support Software for Vector Reduction Transformation
5 // Operations
6 // Copyright (2006) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 
43 #ifndef RTOPPACK_ROP_GET_SUB_VECTOR_DEF_HPP
44 #define RTOPPACK_ROP_GET_SUB_VECTOR_DEF_HPP
45 
46 
48 
49 
50 namespace RTOpPack {
51 
52 
53 template<class Scalar>
55  const index_type u
56  )
57  :RTOpT<Scalar>("ROpGetSubVector"), l_(l), u_(u)
58 {}
59 
60 
61 template<class Scalar>
63  const index_type u
64  )
65 {
66  l_ = l;
67  u_ = u;
68 }
69 
70 
71 template<class Scalar>
74 {
75  using Teuchos::dyn_cast;
76  return dyn_cast<const DefaultReductTarget<SubVectorView< Scalar> > >(reduct_obj).get();
77 }
78 
79 
80 // Overridden from RTOpT
81 
82 
83 template<class Scalar>
85  const Ptr<int> &num_values,
86  const Ptr<int> &num_indexes,
87  const Ptr<int> &num_chars
88  ) const
89 {
91  const int num_prim_objs_per_scalar = PTT::numPrimitiveObjs();
92  *num_values = (u_-l_+1)*num_prim_objs_per_scalar;
93  *num_indexes = 0;
94  *num_chars = 0;
95 }
96 
97 
98 template<class Scalar>
101 {
102  const index_type subDim = u_ - l_ + 1;
103  const ArrayRCP<Scalar> values = Teuchos::arcp<Scalar>(subDim);
104  std::fill(values.begin(), values.end(), ScalarTraits<Scalar>::zero());
105  return defaultReductTarget(
106  SubVectorView<Scalar>( l_, subDim, values, 1 )
107  );
108 }
109 
110 
111 template<class Scalar>
113  const ReductTarget &in_reduct_obj, const Ptr<ReductTarget> &inout_reduct_obj
114  ) const
115 {
116 
117  using Teuchos::dyn_cast;
119 
120  DRTSVV &drtsvv_inout_reduct_obj = dyn_cast<DRTSVV>(*inout_reduct_obj);
121 
122  const ConstSubVectorView<Scalar> sub_vec_in =
123  dyn_cast<const DRTSVV>(in_reduct_obj).get();
124  SubVectorView<Scalar> sub_vec_inout = drtsvv_inout_reduct_obj.get();
125 
126 #ifdef TEUCHOS_DEBUG
128  sub_vec_in.subDim()!=sub_vec_inout.subDim()
129  || sub_vec_in.globalOffset()!=sub_vec_inout.globalOffset()
130  || is_null(sub_vec_in.values())
131  || is_null(sub_vec_inout.values())
132  || sub_vec_in.stride()!=1
133  || sub_vec_inout.stride()!=1
134  );
135 #endif // TEUCHOS_DEBUG
136 
137  typedef typename ArrayRCP<const Scalar>::const_iterator const_iter_t;
138  typedef typename ArrayRCP<Scalar>::iterator iter_t;
139 
140  const_iter_t in_iter = sub_vec_in.values().begin();
141  iter_t inout_iter = sub_vec_inout.values().begin();
142 
143  for( int k = 0; k < sub_vec_in.subDim(); ++k ) {
144  *inout_iter++ += *in_iter++;
145  }
146 
147  drtsvv_inout_reduct_obj.set(sub_vec_inout);
148 
149 }
150 
151 
152 template<class Scalar>
154  const Ptr<ReductTarget> &reduct_obj ) const
155 {
156  using Teuchos::dyn_cast;
158  DRTSVV &drtsvv_inout_reduct_obj = dyn_cast<DRTSVV>(*reduct_obj);
159  SubVectorView<Scalar> sub_vec = drtsvv_inout_reduct_obj.get();
160  std::fill( sub_vec.values().begin(), sub_vec.values().end(),
162 }
163 
164 
165 template<class Scalar>
167  const ReductTarget &reduct_obj,
168  const ArrayView<primitive_value_type> &value_data,
169  const ArrayView<index_type> &/* index_data */,
170  const ArrayView<char_type> &/* char_data */
171  ) const
172 {
173  using Teuchos::null;
174  using Teuchos::dyn_cast;
176  const int num_prim_objs_per_scalar = PTT::numPrimitiveObjs();
177  const ConstSubVectorView<Scalar> sub_vec =
178  dyn_cast<const DefaultReductTarget<SubVectorView<Scalar> > >(reduct_obj).get();
179  int value_data_off = 0;
180  for(
181  int k = 0;
182  k < sub_vec.subDim();
183  ++k, value_data_off += num_prim_objs_per_scalar
184  )
185  {
186  PTT::extractPrimitiveObjs( sub_vec[k],
187  value_data(value_data_off, num_prim_objs_per_scalar),
188  null, null );
189  }
190 }
191 
192 
193 template<class Scalar>
195  const ArrayView<const primitive_value_type> &value_data,
196  const ArrayView<const index_type> &/* index_data */,
197  const ArrayView<const char_type> &/* char_data */,
198  const Ptr<ReductTarget> &reduct_obj
199  ) const
200 {
201  using Teuchos::null;
202  using Teuchos::outArg;
203  using Teuchos::dyn_cast;
204  using Teuchos::arcp_const_cast;
207  const int num_prim_objs_per_scalar = PTT::numPrimitiveObjs();
208  DRTSVV &drtsvv_reduct_obj = dyn_cast<DRTSVV>(*reduct_obj);
209  const ConstSubVectorView<Scalar> const_sub_vec = drtsvv_reduct_obj.get();
210  const ArrayRCP<Scalar> sv_values =
211  arcp_const_cast<Scalar>(const_sub_vec.values());
212  int value_data_off = 0;
213  for(
214  int k = 0;
215  k < const_sub_vec.subDim();
216  ++k, value_data_off += num_prim_objs_per_scalar
217  )
218  {
219  PTT::loadPrimitiveObjs(
220  value_data(value_data_off, num_prim_objs_per_scalar), null, null,
221  outArg(sv_values[k]) );
222  }
223 }
224 
225 
226 template<class Scalar>
228 {
229  return false;
230 }
231 
232 
233 template<class Scalar>
235  const ArrayView<const ConstSubVectorView<Scalar> > &sub_vecs,
236  const ArrayView<const SubVectorView<Scalar> > &targ_sub_vecs,
237  const Ptr<ReductTarget> &reduct_obj
238  ) const
239 {
240 
241  using Teuchos::dyn_cast;
243 
244  validate_apply_op( *this, 1, 0, true,
245  sub_vecs, targ_sub_vecs, reduct_obj.getConst() );
246 
247  typedef typename Teuchos::ArrayRCP<const Scalar>::iterator const_iter_t;
248  const index_type subDim = sub_vecs[0].subDim();
249  const index_type globalOffset = sub_vecs[0].globalOffset();
250  TEUCHOS_TEST_FOR_EXCEPT(globalOffset<0);
251  const_iter_t v0_val = sub_vecs[0].values().begin();
252  const ptrdiff_t v0_s = sub_vecs[0].stride();
253 
254  if( u_ < globalOffset || globalOffset + subDim - 1 < l_ ) {
255  // None of the sub-vector elements that we are looking for is in this
256  // vector chunk!
257  return;
258  }
259 
260  index_type
261  i_l = ( l_ <= globalOffset ? 0 : l_ - globalOffset ),
262  i_u = ( u_ >= globalOffset+subDim-1 ? subDim-1 : u_ - globalOffset );
263 
264  DRTSVV &drtsvv_reduct_obj = dyn_cast<DRTSVV>(*reduct_obj);
265  SubVectorView<Scalar> sub_vec_targ = drtsvv_reduct_obj.get();
266 
267  const ArrayRCP<Scalar> svt_values = sub_vec_targ.values();
268 
269  for( index_type i = i_l; i <= i_u; ++i ) {
270  svt_values[i+(globalOffset-l_)] = v0_val[i*v0_s];
271  }
272 
273  drtsvv_reduct_obj.set(sub_vec_targ);
274 
275 }
276 
277 
278 } // namespace RTOpPack
279 
280 
281 #endif // RTOPPACK_ROP_GET_SUB_VECTOR_DEF_HPP
Simple ReductTarget subclass for simple scalar objects.
void load_reduct_obj_state_impl(const ArrayView< const primitive_value_type > &value_data, const ArrayView< const index_type > &index_data, const ArrayView< const char_type > &char_data, const Ptr< ReductTarget > &reduct_obj) const
bool is_null(const boost::shared_ptr< T > &p)
Class for a changeable sub-vector.
void reduce_reduct_objs_impl(const ReductTarget &in_reduct_obj, const Ptr< ReductTarget > &inout_reduct_obj) const
Teuchos_Ordinal index_type
void set_range(const index_type l, const index_type u)
Set the range of global indexes to extract elements for.
Class for a non-changeable sub-vector.
void extract_reduct_obj_state_impl(const ReductTarget &reduct_obj, const ArrayView< primitive_value_type > &value_data, const ArrayView< index_type > &index_data, const ArrayView< char_type > &char_data) const
Templated interface to vector reduction/transformation operators {abstract}.
const ArrayRCP< Scalar > values() const
ROpGetSubVector(const index_type l=0, const index_type u=0)
Specialization where the scalar type is the same as the concrete object type.
Teuchos::RCP< ReductTarget > reduct_obj_create_impl() const
T_To & dyn_cast(T_From &from)
Abstract base class for all reduction objects.
void apply_op_impl(const ArrayView< const ConstSubVectorView< Scalar > > &sub_vecs, const ArrayView< const SubVectorView< Scalar > > &targ_sub_vecs, const Ptr< ReductTarget > &reduct_obj) const
void get_reduct_type_num_entries_impl(const Ptr< int > &num_values, const Ptr< int > &num_indexes, const Ptr< int > &num_chars) const
void reduct_obj_reinit_impl(const Ptr< ReductTarget > &reduct_obj) const
void validate_apply_op(const RTOpT< Scalar > &op, const int allowed_num_sub_vecs, const int allowed_num_targ_sub_vecs, const bool expect_reduct_obj, const ArrayView< const ConstSubVectorView< Scalar > > &sub_vecs, const ArrayView< const SubVectorView< Scalar > > &targ_sub_vecs, const Ptr< const ReductTarget > &reduct_obj)
Validate the input to an apply_op(...) function.
iterator end() const
Ptr< const T > getConst() const
const ConstSubVectorView< Scalar > operator()(const ReductTarget &reduct_obj) const
Extract the subvector after all of the reductions are completed.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
iterator begin() const
const ArrayRCP< const Scalar > values() const