RTOp Package Browser (Single Doxygen Collection) Version of the Day
RTOpPack_ROpGetSubVector_def.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 // RTOp: Interfaces and Support Software for Vector Reduction Transformation
00005 //       Operations
00006 //                Copyright (2006) Sandia Corporation
00007 // 
00008 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00009 // license for use of this work by or on behalf of the U.S. Government.
00010 // 
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00039 // 
00040 // ***********************************************************************
00041 // @HEADER
00042 
00043 #ifndef RTOPPACK_ROP_GET_SUB_VECTOR_DEF_HPP
00044 #define RTOPPACK_ROP_GET_SUB_VECTOR_DEF_HPP
00045 
00046 
00047 #include "RTOpPack_ROpGetSubVector_decl.hpp"
00048 
00049 
00050 namespace RTOpPack {
00051 
00052 
00053 template<class Scalar>
00054 ROpGetSubVector<Scalar>::ROpGetSubVector( const index_type l,
00055   const index_type u
00056   )
00057   :RTOpT<Scalar>("ROpGetSubVector"), l_(l), u_(u)
00058 {}
00059 
00060 
00061 template<class Scalar>
00062 void ROpGetSubVector<Scalar>::set_range( const index_type l,
00063   const index_type u
00064   )
00065 {
00066   l_ = l;
00067   u_ = u;
00068 }
00069 
00070 
00071 template<class Scalar>
00072 const ConstSubVectorView<Scalar>
00073 ROpGetSubVector<Scalar>::operator()( const ReductTarget& reduct_obj ) const
00074 {
00075   using Teuchos::dyn_cast;
00076   return dyn_cast<const DefaultReductTarget<SubVectorView< Scalar> > >(reduct_obj).get();
00077 }
00078 
00079 
00080 // Overridden from RTOpT
00081 
00082 
00083 template<class Scalar>
00084 void ROpGetSubVector<Scalar>::get_reduct_type_num_entries_impl(
00085   const Ptr<int> &num_values,
00086   const Ptr<int> &num_indexes,
00087   const Ptr<int> &num_chars
00088   ) const
00089 {
00090   typedef PrimitiveTypeTraits<Scalar,Scalar> PTT;
00091   const int num_prim_objs_per_scalar = PTT::numPrimitiveObjs();
00092   *num_values = (u_-l_+1)*num_prim_objs_per_scalar;
00093   *num_indexes = 0;
00094   *num_chars = 0;
00095 }
00096 
00097 
00098 template<class Scalar>
00099 Teuchos::RCP<ReductTarget>
00100 ROpGetSubVector<Scalar>::reduct_obj_create_impl() const
00101 {
00102   const index_type subDim = u_ - l_ + 1;
00103   const ArrayRCP<Scalar> values = Teuchos::arcp<Scalar>(subDim);
00104   std::fill(values.begin(), values.end(), ScalarTraits<Scalar>::zero());
00105   return defaultReductTarget(
00106     SubVectorView<Scalar>( l_, subDim, values, 1 )
00107     );
00108 }
00109 
00110 
00111 template<class Scalar>
00112 void ROpGetSubVector<Scalar>::reduce_reduct_objs_impl(
00113   const ReductTarget &in_reduct_obj, const Ptr<ReductTarget> &inout_reduct_obj
00114   ) const
00115 {
00116 
00117   using Teuchos::dyn_cast;
00118   typedef DefaultReductTarget<SubVectorView<Scalar> > DRTSVV;
00119 
00120   DRTSVV &drtsvv_inout_reduct_obj = dyn_cast<DRTSVV>(*inout_reduct_obj);
00121 
00122   const ConstSubVectorView<Scalar> sub_vec_in =
00123     dyn_cast<const DRTSVV>(in_reduct_obj).get();
00124   SubVectorView<Scalar> sub_vec_inout = drtsvv_inout_reduct_obj.get();
00125 
00126 #ifdef TEUCHOS_DEBUG
00127   TEUCHOS_TEST_FOR_EXCEPT(
00128     sub_vec_in.subDim()!=sub_vec_inout.subDim()
00129     || sub_vec_in.globalOffset()!=sub_vec_inout.globalOffset()
00130     || is_null(sub_vec_in.values())
00131     || is_null(sub_vec_inout.values())
00132     || sub_vec_in.stride()!=1
00133     || sub_vec_inout.stride()!=1
00134     );
00135 #endif // TEUCHOS_DEBUG
00136 
00137   typedef typename ArrayRCP<const Scalar>::const_iterator const_iter_t;
00138   typedef typename ArrayRCP<Scalar>::iterator iter_t;
00139 
00140   const_iter_t in_iter = sub_vec_in.values().begin();
00141   iter_t inout_iter = sub_vec_inout.values().begin();
00142 
00143   for( int k = 0; k < sub_vec_in.subDim(); ++k ) {
00144     *inout_iter++ += *in_iter++;
00145   }
00146 
00147   drtsvv_inout_reduct_obj.set(sub_vec_inout);
00148 
00149 }
00150 
00151 
00152 template<class Scalar>
00153 void ROpGetSubVector<Scalar>::reduct_obj_reinit_impl(
00154   const Ptr<ReductTarget> &reduct_obj ) const
00155 {
00156   using Teuchos::dyn_cast;
00157   typedef typename ArrayRCP<Scalar>::iterator iter_t;
00158   typedef DefaultReductTarget<SubVectorView<Scalar> > DRTSVV;
00159   DRTSVV &drtsvv_inout_reduct_obj = dyn_cast<DRTSVV>(*reduct_obj);
00160   SubVectorView<Scalar> sub_vec = drtsvv_inout_reduct_obj.get();
00161   std::fill( sub_vec.values().begin(), sub_vec.values().end(),
00162     ScalarTraits<Scalar>::zero() );
00163 }
00164 
00165 
00166 template<class Scalar>
00167 void ROpGetSubVector<Scalar>::extract_reduct_obj_state_impl(
00168   const ReductTarget &reduct_obj,
00169   const ArrayView<primitive_value_type> &value_data,
00170   const ArrayView<index_type> &index_data,
00171   const ArrayView<char_type> &char_data
00172   ) const
00173 {
00174   using Teuchos::null;
00175   using Teuchos::dyn_cast;
00176   typedef PrimitiveTypeTraits<Scalar,Scalar> PTT;
00177   const int num_prim_objs_per_scalar = PTT::numPrimitiveObjs();
00178   const ConstSubVectorView<Scalar> sub_vec =
00179     dyn_cast<const DefaultReductTarget<SubVectorView<Scalar> > >(reduct_obj).get();
00180   int value_data_off = 0;
00181   for(
00182     int k = 0;
00183     k < sub_vec.subDim();
00184     ++k, value_data_off += num_prim_objs_per_scalar
00185     )
00186   {
00187     PTT::extractPrimitiveObjs( sub_vec[k],
00188       value_data(value_data_off, num_prim_objs_per_scalar),
00189       null, null );
00190   }
00191 }
00192 
00193 
00194 template<class Scalar>
00195 void ROpGetSubVector<Scalar>::load_reduct_obj_state_impl(
00196   const ArrayView<const primitive_value_type> &value_data,
00197   const ArrayView<const index_type> &index_data,
00198   const ArrayView<const char_type> &char_data,
00199   const Ptr<ReductTarget> &reduct_obj
00200   ) const
00201 {
00202   using Teuchos::null;
00203   using Teuchos::outArg;
00204   using Teuchos::dyn_cast;
00205   using Teuchos::arcp_const_cast;
00206   typedef PrimitiveTypeTraits<Scalar,Scalar> PTT;
00207   typedef DefaultReductTarget<SubVectorView<Scalar> > DRTSVV;
00208   const int num_prim_objs_per_scalar = PTT::numPrimitiveObjs();
00209   DRTSVV &drtsvv_reduct_obj = dyn_cast<DRTSVV>(*reduct_obj);
00210   const ConstSubVectorView<Scalar> const_sub_vec = drtsvv_reduct_obj.get();
00211   const ArrayRCP<Scalar> sv_values =
00212     arcp_const_cast<Scalar>(const_sub_vec.values());
00213   int value_data_off = 0;
00214   for(
00215     int k = 0;
00216     k < const_sub_vec.subDim();
00217     ++k, value_data_off += num_prim_objs_per_scalar
00218     )
00219   {
00220     PTT::loadPrimitiveObjs(
00221       value_data(value_data_off, num_prim_objs_per_scalar), null, null,
00222       outArg(sv_values[k]) );
00223   }
00224 }
00225 
00226 
00227 template<class Scalar>
00228 bool ROpGetSubVector<Scalar>::coord_invariant_impl() const
00229 {
00230   return false;
00231 }
00232 
00233 
00234 template<class Scalar>
00235 void ROpGetSubVector<Scalar>::apply_op_impl(
00236   const ArrayView<const ConstSubVectorView<Scalar> > &sub_vecs,
00237   const ArrayView<const SubVectorView<Scalar> > &targ_sub_vecs,
00238   const Ptr<ReductTarget> &reduct_obj
00239   ) const
00240 {
00241 
00242   using Teuchos::dyn_cast;
00243   typedef DefaultReductTarget<SubVectorView<Scalar> > DRTSVV;
00244 
00245   validate_apply_op( *this, 1, 0, true,
00246     sub_vecs, targ_sub_vecs, reduct_obj.getConst() );
00247 
00248   typedef typename Teuchos::ArrayRCP<const Scalar>::iterator const_iter_t;
00249   const index_type subDim  = sub_vecs[0].subDim();
00250   const index_type globalOffset = sub_vecs[0].globalOffset();
00251   TEUCHOS_TEST_FOR_EXCEPT(globalOffset<0);
00252   const_iter_t v0_val = sub_vecs[0].values().begin();
00253   const ptrdiff_t v0_s = sub_vecs[0].stride();
00254   
00255   if( u_ < globalOffset || globalOffset + subDim - 1 < l_ ) {
00256     // None of the sub-vector elements that we are looking for is in this
00257     // vector chunk!
00258     return;
00259   }
00260  
00261   index_type
00262     i_l = ( l_ <= globalOffset ? 0 : l_ - globalOffset ),
00263     i_u = ( u_ >= globalOffset+subDim-1 ? subDim-1 : u_ - globalOffset );
00264 
00265   DRTSVV &drtsvv_reduct_obj = dyn_cast<DRTSVV>(*reduct_obj);
00266   SubVectorView<Scalar> sub_vec_targ = drtsvv_reduct_obj.get();
00267 
00268   const ArrayRCP<Scalar> svt_values = sub_vec_targ.values();
00269 
00270   for( index_type i = i_l; i <= i_u; ++i ) {
00271     svt_values[i+(globalOffset-l_)] = v0_val[i*v0_s];
00272   }
00273 
00274   drtsvv_reduct_obj.set(sub_vec_targ);
00275 
00276 }
00277 
00278 
00279 } // namespace RTOpPack
00280 
00281 
00282 #endif // RTOPPACK_ROP_GET_SUB_VECTOR_DEF_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines