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 // This library is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Lesser General Public License as
00013 // published by the Free Software Foundation; either version 2.1 of the
00014 // License, or (at your option) any later version.
00015 //  
00016 // This library is distributed in the hope that it will be useful, but
00017 // WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 // Lesser General Public License for more details.
00020 //  
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License along with this library; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00024 // USA
00025 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00026 // 
00027 // ***********************************************************************
00028 // @HEADER
00029 
00030 #ifndef RTOPPACK_ROP_GET_SUB_VECTOR_DEF_HPP
00031 #define RTOPPACK_ROP_GET_SUB_VECTOR_DEF_HPP
00032 
00033 
00034 #include "RTOpPack_ROpGetSubVector_decl.hpp"
00035 
00036 
00037 namespace RTOpPack {
00038 
00039 
00040 template<class Scalar>
00041 ROpGetSubVector<Scalar>::ROpGetSubVector( const index_type l,
00042   const index_type u
00043   )
00044   :RTOpT<Scalar>("ROpGetSubVector"), l_(l), u_(u)
00045 {}
00046 
00047 
00048 template<class Scalar>
00049 void ROpGetSubVector<Scalar>::set_range( const index_type l,
00050   const index_type u
00051   )
00052 {
00053   l_ = l;
00054   u_ = u;
00055 }
00056 
00057 
00058 template<class Scalar>
00059 const ConstSubVectorView<Scalar>
00060 ROpGetSubVector<Scalar>::operator()( const ReductTarget& reduct_obj ) const
00061 {
00062   using Teuchos::dyn_cast;
00063   return dyn_cast<const DefaultReductTarget<SubVectorView< Scalar> > >(reduct_obj).get();
00064 }
00065 
00066 
00067 // Overridden from RTOpT
00068 
00069 
00070 template<class Scalar>
00071 void ROpGetSubVector<Scalar>::get_reduct_type_num_entries_impl(
00072   const Ptr<int> &num_values,
00073   const Ptr<int> &num_indexes,
00074   const Ptr<int> &num_chars
00075   ) const
00076 {
00077   typedef PrimitiveTypeTraits<Scalar,Scalar> PTT;
00078   const int num_prim_objs_per_scalar = PTT::numPrimitiveObjs();
00079   *num_values = (u_-l_+1)*num_prim_objs_per_scalar;
00080   *num_indexes = 0;
00081   *num_chars = 0;
00082 }
00083 
00084 
00085 template<class Scalar>
00086 Teuchos::RCP<ReductTarget>
00087 ROpGetSubVector<Scalar>::reduct_obj_create_impl() const
00088 {
00089   const index_type subDim = u_ - l_ + 1;
00090   const ArrayRCP<Scalar> values = Teuchos::arcp<Scalar>(subDim);
00091   std::fill(values.begin(), values.end(), ScalarTraits<Scalar>::zero());
00092   return defaultReductTarget(
00093     SubVectorView<Scalar>( l_, subDim, values, 1 )
00094     );
00095 }
00096 
00097 
00098 template<class Scalar>
00099 void ROpGetSubVector<Scalar>::reduce_reduct_objs_impl(
00100   const ReductTarget &in_reduct_obj, const Ptr<ReductTarget> &inout_reduct_obj
00101   ) const
00102 {
00103 
00104   using Teuchos::dyn_cast;
00105   typedef DefaultReductTarget<SubVectorView<Scalar> > DRTSVV;
00106 
00107   DRTSVV &drtsvv_inout_reduct_obj = dyn_cast<DRTSVV>(*inout_reduct_obj);
00108 
00109   const ConstSubVectorView<Scalar> sub_vec_in =
00110     dyn_cast<const DRTSVV>(in_reduct_obj).get();
00111   SubVectorView<Scalar> sub_vec_inout = drtsvv_inout_reduct_obj.get();
00112 
00113 #ifdef TEUCHOS_DEBUG
00114   TEST_FOR_EXCEPT(
00115     sub_vec_in.subDim()!=sub_vec_inout.subDim()
00116     || sub_vec_in.globalOffset()!=sub_vec_inout.globalOffset()
00117     || is_null(sub_vec_in.values())
00118     || is_null(sub_vec_inout.values())
00119     || sub_vec_in.stride()!=1
00120     || sub_vec_inout.stride()!=1
00121     );
00122 #endif // TEUCHOS_DEBUG
00123 
00124   typedef typename ArrayRCP<const Scalar>::const_iterator const_iter_t;
00125   typedef typename ArrayRCP<Scalar>::iterator iter_t;
00126 
00127   const_iter_t in_iter = sub_vec_in.values().begin();
00128   iter_t inout_iter = sub_vec_inout.values().begin();
00129 
00130   for( int k = 0; k < sub_vec_in.subDim(); ++k ) {
00131     *inout_iter++ += *in_iter++;
00132   }
00133 
00134   drtsvv_inout_reduct_obj.set(sub_vec_inout);
00135 
00136 }
00137 
00138 
00139 template<class Scalar>
00140 void ROpGetSubVector<Scalar>::reduct_obj_reinit_impl(
00141   const Ptr<ReductTarget> &reduct_obj ) const
00142 {
00143   using Teuchos::dyn_cast;
00144   typedef typename ArrayRCP<Scalar>::iterator iter_t;
00145   typedef DefaultReductTarget<SubVectorView<Scalar> > DRTSVV;
00146   DRTSVV &drtsvv_inout_reduct_obj = dyn_cast<DRTSVV>(*reduct_obj);
00147   SubVectorView<Scalar> sub_vec = drtsvv_inout_reduct_obj.get();
00148   std::fill( sub_vec.values().begin(), sub_vec.values().end(),
00149     ScalarTraits<Scalar>::zero() );
00150 }
00151 
00152 
00153 template<class Scalar>
00154 void ROpGetSubVector<Scalar>::extract_reduct_obj_state_impl(
00155   const ReductTarget &reduct_obj,
00156   const ArrayView<primitive_value_type> &value_data,
00157   const ArrayView<index_type> &index_data,
00158   const ArrayView<char_type> &char_data
00159   ) const
00160 {
00161   using Teuchos::null;
00162   using Teuchos::dyn_cast;
00163   typedef PrimitiveTypeTraits<Scalar,Scalar> PTT;
00164   const int num_prim_objs_per_scalar = PTT::numPrimitiveObjs();
00165   const ConstSubVectorView<Scalar> sub_vec =
00166     dyn_cast<const DefaultReductTarget<SubVectorView<Scalar> > >(reduct_obj).get();
00167   int value_data_off = 0;
00168   for(
00169     int k = 0;
00170     k < sub_vec.subDim();
00171     ++k, value_data_off += num_prim_objs_per_scalar
00172     )
00173   {
00174     PTT::extractPrimitiveObjs( sub_vec[k],
00175       value_data(value_data_off, num_prim_objs_per_scalar),
00176       null, null );
00177   }
00178 }
00179 
00180 
00181 template<class Scalar>
00182 void ROpGetSubVector<Scalar>::load_reduct_obj_state_impl(
00183   const ArrayView<const primitive_value_type> &value_data,
00184   const ArrayView<const index_type> &index_data,
00185   const ArrayView<const char_type> &char_data,
00186   const Ptr<ReductTarget> &reduct_obj
00187   ) const
00188 {
00189   using Teuchos::null;
00190   using Teuchos::outArg;
00191   using Teuchos::dyn_cast;
00192   using Teuchos::arcp_const_cast;
00193   typedef PrimitiveTypeTraits<Scalar,Scalar> PTT;
00194   typedef DefaultReductTarget<SubVectorView<Scalar> > DRTSVV;
00195   const int num_prim_objs_per_scalar = PTT::numPrimitiveObjs();
00196   DRTSVV &drtsvv_reduct_obj = dyn_cast<DRTSVV>(*reduct_obj);
00197   const ConstSubVectorView<Scalar> const_sub_vec = drtsvv_reduct_obj.get();
00198   const ArrayRCP<Scalar> sv_values =
00199     arcp_const_cast<Scalar>(const_sub_vec.values());
00200   int value_data_off = 0;
00201   for(
00202     int k = 0;
00203     k < const_sub_vec.subDim();
00204     ++k, value_data_off += num_prim_objs_per_scalar
00205     )
00206   {
00207     PTT::loadPrimitiveObjs(
00208       value_data(value_data_off, num_prim_objs_per_scalar), null, null,
00209       outArg(sv_values[k]) );
00210   }
00211 }
00212 
00213 
00214 template<class Scalar>
00215 bool ROpGetSubVector<Scalar>::coord_invariant_impl() const
00216 {
00217   return false;
00218 }
00219 
00220 
00221 template<class Scalar>
00222 void ROpGetSubVector<Scalar>::apply_op_impl(
00223   const ArrayView<const ConstSubVectorView<Scalar> > &sub_vecs,
00224   const ArrayView<const SubVectorView<Scalar> > &targ_sub_vecs,
00225   const Ptr<ReductTarget> &reduct_obj
00226   ) const
00227 {
00228 
00229   using Teuchos::dyn_cast;
00230   typedef DefaultReductTarget<SubVectorView<Scalar> > DRTSVV;
00231 
00232   validate_apply_op( *this, 1, 0, true,
00233     sub_vecs, targ_sub_vecs, reduct_obj.getConst() );
00234 
00235   typedef typename Teuchos::ArrayRCP<const Scalar>::iterator const_iter_t;
00236   const index_type subDim  = sub_vecs[0].subDim();
00237   const index_type globalOffset = sub_vecs[0].globalOffset();
00238   TEST_FOR_EXCEPT(globalOffset<0);
00239   const_iter_t v0_val = sub_vecs[0].values().begin();
00240   const ptrdiff_t v0_s = sub_vecs[0].stride();
00241   
00242   if( u_ < globalOffset || globalOffset + subDim - 1 < l_ ) {
00243     // None of the sub-vector elements that we are looking for is in this
00244     // vector chunk!
00245     return;
00246   }
00247  
00248   index_type
00249     i_l = ( l_ <= globalOffset ? 0 : l_ - globalOffset ),
00250     i_u = ( u_ >= globalOffset+subDim-1 ? subDim-1 : u_ - globalOffset );
00251 
00252   DRTSVV &drtsvv_reduct_obj = dyn_cast<DRTSVV>(*reduct_obj);
00253   SubVectorView<Scalar> sub_vec_targ = drtsvv_reduct_obj.get();
00254 
00255   const ArrayRCP<Scalar> svt_values = sub_vec_targ.values();
00256 
00257   for( index_type i = i_l; i <= i_u; ++i ) {
00258     svt_values[i+(globalOffset-l_)] = v0_val[i*v0_s];
00259   }
00260 
00261   drtsvv_reduct_obj.set(sub_vec_targ);
00262 
00263 }
00264 
00265 
00266 } // namespace RTOpPack
00267 
00268 
00269 #endif // RTOPPACK_ROP_GET_SUB_VECTOR_DEF_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Wed Apr 13 09:59:06 2011 for RTOp Package Browser (Single Doxygen Collection) by  doxygen 1.6.3