RTOpPack_RTOpTHelpers.hpp

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_RTOP_NEW_T_HELPERS_HPP
00031 #define RTOPPACK_RTOP_NEW_T_HELPERS_HPP
00032 
00033 //#define RTOPPACK_RTOPT_HELPER_DUMP_OUTPUT
00034 
00035 #include "RTOpPack_RTOpT.hpp"
00036 #include "Teuchos_StandardMemberCompositionMacros.hpp"
00037 #include "Teuchos_ScalarTraits.hpp"
00038 #include "Teuchos_dyn_cast.hpp"
00039 
00040 #ifdef RTOPPACK_RTOPT_HELPER_DUMP_OUTPUT
00041 #  include "Teuchos_VerboseObject.hpp"
00042 namespace RTOpPack { extern bool rtop_helpers_dump_all; }
00043 #endif // RTOPPACK_RTOPT_HELPER_DUMP_OUTPUT
00044 
00045 namespace RTOpPack {
00046 
00051 template<class Scalar>
00052 struct ScalarIndex {
00053   Scalar scalar;
00054   Index  index;
00055   ScalarIndex( const Scalar &_scalar, const Index &_index ) : scalar(_scalar), index(_index) {}
00056 };
00057 
00062 template<class Scalar>
00063 class ReductTargetScalar : public ReductTarget {
00064 public:
00065   ReductTargetScalar( const Scalar &scalar = Teuchos::ScalarTraits<Scalar>::zero() ) : scalar_(scalar) {}
00066   void set( const Scalar &scalar ) { scalar_ = scalar; }
00067   const Scalar& get() const { return scalar_; }
00068   std::string description() const
00069     {
00070       std::ostringstream oss;
00071       oss << "RTOpPack::ReductTargetScalar<"<<Teuchos::ScalarTraits<Scalar>::name()<<">{scalar="<<scalar_<<"}";
00072       return oss.str();
00073     }
00074 private:
00075   Scalar scalar_;
00076 };
00077 
00082 template<class Scalar>
00083 class ReductTargetScalarIndex : public ReductTarget {
00084 public:
00085   ReductTargetScalarIndex(
00086     const Scalar &scalar = Teuchos::ScalarTraits<Scalar>::zero()
00087     ,const Index &index  = Teuchos::ScalarTraits<Index>::zero()
00088     )
00089     :scalarIndex_(scalar,index)
00090     {}
00091   ReductTargetScalarIndex(
00092     const ScalarIndex<Scalar> &scalarIndex = ScalarIndex<Scalar>(Teuchos::ScalarTraits<Scalar>::zero(),Teuchos::ScalarTraits<Index>::zero())
00093     )
00094     :scalarIndex_(scalarIndex)
00095     {}
00096   void set( const ScalarIndex<Scalar> &scalarIndex ) { scalarIndex_ = scalarIndex; }
00097   const ScalarIndex<Scalar>& get() const { return scalarIndex_; }
00098 private:
00099   ScalarIndex<Scalar> scalarIndex_;
00100 };
00101 
00102 template<bool isComplex, bool isScalarReductScalar, class Scalar, class ReductScalar>
00103 class ROpScalarReductionBaseRawValSetter;
00104 
00114 template<class Scalar, class ReductScalar = Scalar>
00115 class ROpScalarReductionBase : virtual public RTOpT<Scalar> {
00116 public:
00117   typedef typename RTOpT<Scalar>::primitive_value_type        primitive_value_type;
00119   ROpScalarReductionBase(
00120     const ReductScalar &initReductObjValue = Teuchos::ScalarTraits<ReductScalar>::zero()
00121     )
00122     :RTOpT<Scalar>(""), initReductObjValue_(initReductObjValue) 
00123     {}
00125   const ReductScalar& getRawVal( const ReductTarget &reduct_obj ) const
00126     {
00127       using Teuchos::dyn_cast;
00128       return dyn_cast<const ReductTargetScalar<ReductScalar> >(reduct_obj).get();
00129     }
00131   void setRawVal( const ReductScalar &rawVal, ReductTarget *reduct_obj ) const
00132     {
00133 #ifdef TEUCHOS_DEBUG
00134       TEST_FOR_EXCEPTION( reduct_obj==NULL, std::invalid_argument, "Error!" );
00135 #endif
00136       using Teuchos::dyn_cast;
00137       dyn_cast<ReductTargetScalar<ReductScalar> >(*reduct_obj).set(rawVal);
00138     }
00142   void get_reduct_type_num_entries(
00143     int*   num_values
00144     ,int*  num_indexes
00145     ,int*  num_chars
00146     ) const
00147     {
00148       *num_values = Teuchos::PrimitiveTypeTraits<Scalar>::numPrimitiveObjs();
00149       *num_indexes = 0;
00150       *num_chars = 0;
00151     }
00153   Teuchos::RefCountPtr<ReductTarget> reduct_obj_create() const
00154     {
00155       return Teuchos::rcp(new ReductTargetScalar<ReductScalar>(initReductObjValue()));
00156     }
00158   void reduce_reduct_objs(
00159     const ReductTarget& in_reduct_obj, ReductTarget* inout_reduct_obj
00160     ) const
00161     {
00162       const ReductScalar in_val    = getRawVal(in_reduct_obj);
00163       const ReductScalar inout_val = getRawVal(*inout_reduct_obj);
00164 #ifdef RTOPPACK_RTOPT_HELPER_DUMP_OUTPUT
00165       Teuchos::RefCountPtr<Teuchos::FancyOStream>
00166         out = Teuchos::VerboseObjectBase::getDefaultOStream();
00167       Teuchos::OSTab tab(out);
00168       if(rtop_helpers_dump_all) {
00169         *out << "\nEntering RTOpPack::ROpScalarReductionBase::reduce_reduct_objs(...) ...\n";
00170         *out
00171           << "\nop = " << this->description() << "\n"
00172           << "in_reduct_obj = " << Teuchos::describe(in_reduct_obj,Teuchos::VERB_EXTREME)
00173           << "in_val = " << in_val << "\n"
00174           << "inout_reduct_obj (before reduction) = "
00175           <<     Teuchos::describe(*inout_reduct_obj,Teuchos::VERB_EXTREME)
00176           << "inout_val (before reduction) = " << inout_val << "\n";
00177       }
00178 #endif // RTOPPACK_RTOPT_HELPER_DUMP_OUTPUT
00179       setRawVal( in_val + inout_val, inout_reduct_obj );
00180 #ifdef RTOPPACK_RTOPT_HELPER_DUMP_OUTPUT
00181       if(rtop_helpers_dump_all) {
00182         *out
00183           << "\ninout_reduct_obj (after reduction) = "
00184           << Teuchos::describe(*inout_reduct_obj,Teuchos::VERB_EXTREME);
00185       }
00186 #endif // RTOPPACK_RTOPT_HELPER_DUMP_OUTPUT
00187     }
00189   void reduct_obj_reinit( ReductTarget* reduct_obj ) const
00190     {
00191       setRawVal( initReductObjValue(), reduct_obj );
00192     }
00194   void extract_reduct_obj_state(
00195     const ReductTarget        &reduct_obj
00196     ,int                      num_values
00197     ,primitive_value_type     value_data[]
00198     ,int                      num_indexes
00199     ,index_type               index_data[]
00200     ,int                      num_chars
00201     ,char_type                char_data[]
00202     ) const
00203     {
00204 #ifdef TEUCHOS_DEBUG
00205       TEST_FOR_EXCEPTION(
00206         num_values==0 || value_data==NULL || num_indexes!=0 || index_data!=NULL || num_chars!=0 || char_data!=NULL
00207         ,std::invalid_argument, "Error!"
00208         );
00209 #endif
00210       Teuchos::PrimitiveTypeTraits<Scalar>::extractPrimitiveObjs( getRawVal(reduct_obj), num_values, value_data );
00211     }
00213   void load_reduct_obj_state(
00214     int                            num_values
00215     ,const primitive_value_type    value_data[]
00216     ,int                           num_indexes
00217     ,const index_type              index_data[]
00218     ,int                           num_chars
00219     ,const char_type               char_data[]
00220     ,ReductTarget                  *reduct_obj
00221     ) const
00222     {
00223       typedef Teuchos::ScalarTraits<Scalar> ST;
00224 #ifdef TEUCHOS_DEBUG
00225       TEST_FOR_EXCEPTION(
00226         num_values==0 || value_data==NULL || num_indexes!=0 || index_data!=NULL || num_chars!=0 || char_data!=NULL
00227         ,std::invalid_argument, "Error!"
00228         );
00229 #endif
00230 #ifdef RTOPPACK_RTOPT_HELPER_DUMP_OUTPUT
00231       Teuchos::RefCountPtr<Teuchos::FancyOStream>
00232         out = Teuchos::VerboseObjectBase::getDefaultOStream();
00233       Teuchos::OSTab tab(out);
00234       if(rtop_helpers_dump_all) {
00235         *out << "\nEntering ROpScalarReductionBase::load_reduct_obj_state(...) ...\n"
00236              << "\nOn input:\n";
00237         Teuchos::OSTab tab(out);
00238         *out << "op = " << this->description() << "\n";
00239         *out << "num_values = " << num_values << "\n";
00240         if(num_values) {
00241           *out <<"value_data[] = { ";
00242           for( int i = 0; i < num_values-1; ++i )
00243             *out << value_data[i] << ", ";
00244           *out << value_data[num_values-1] << " }\n";
00245         }
00246         *out << "num_indexes = " << num_indexes << "\n";
00247         if(num_indexes) {
00248           *out <<"index_data[] = { ";
00249           for( int i = 0; i < num_indexes-1; ++i )
00250             *out << index_data[i] << ", ";
00251           *out << index_data[num_indexes-1] << " }\n";
00252         }
00253         *out << "num_chars = " << num_chars << "\n";
00254         if(num_chars) {
00255           *out <<"char_data[] = { ";
00256           for( int i = 0; i < num_chars-1; ++i )
00257             *out << char_data[i] << ", ";
00258           *out << char_data[num_chars-1] << " }\n";
00259         }
00260       }
00261 #endif // RTOPPACK_RTOPT_HELPER_DUMP_OUTPUT
00262       Scalar val = ST::nan();
00263       Teuchos::PrimitiveTypeTraits<Scalar>::loadPrimitiveObjs( num_values, value_data, &val );
00264       ROpScalarReductionBaseRawValSetter<ST::isComplex,sizeof(Scalar)==sizeof(ReductScalar),Scalar,ReductScalar>::setRawVal( *this, val, reduct_obj );
00265 #ifdef RTOPPACK_RTOPT_HELPER_DUMP_OUTPUT
00266       if(rtop_helpers_dump_all) {
00267         *out << "\nOn output:\n";
00268         Teuchos::OSTab tab(out);
00269         *out << "val = " << val << "\n";
00270         *out << "reduct_op = " << Teuchos::describe(*reduct_obj,Teuchos::VERB_EXTREME);
00271       }
00272 #endif // RTOPPACK_RTOPT_HELPER_DUMP_OUTPUT
00273     }
00275 protected:
00277   STANDARD_MEMBER_COMPOSITION_MEMBERS( ReductScalar, initReductObjValue )
00278 };
00279 
00280 template<class Scalar, class ReductScalar>
00281 class ROpScalarReductionBaseRawValSetter<true,false,Scalar,ReductScalar> {
00282 public:
00283   static void setRawVal(
00284     const ROpScalarReductionBase<Scalar,ReductScalar> &rtop
00285     ,const Scalar &rawVal, ReductTarget *reduct_obj
00286     )
00287     { rtop.setRawVal(Teuchos::ScalarTraits<Scalar>::real(rawVal),reduct_obj); }
00288 };
00289 
00290 template<class Scalar, class ReductScalar>
00291 class ROpScalarReductionBaseRawValSetter<true,true,Scalar,ReductScalar> {
00292 public:
00293   static void setRawVal(
00294     const ROpScalarReductionBase<Scalar,ReductScalar> &rtop
00295     ,const Scalar &rawVal, ReductTarget *reduct_obj
00296     )
00297     { rtop.setRawVal(rawVal,reduct_obj); }
00298 };
00299 
00300 template<bool isScalarReductScalar, class Scalar, class ReductScalar>
00301 class ROpScalarReductionBaseRawValSetter<false,isScalarReductScalar,Scalar,ReductScalar> {
00302 public:
00303   static void setRawVal(
00304     const ROpScalarReductionBase<Scalar,ReductScalar> &rtop
00305     ,const Scalar &rawVal, ReductTarget *reduct_obj
00306     )
00307     { rtop.setRawVal(rawVal,reduct_obj); }
00308 };
00309 
00318 template<class Scalar>
00319 class ROpScalarIndexReductionBase : virtual public RTOpT<Scalar> {
00320 public:
00321   typedef typename RTOpT<Scalar>::primitive_value_type primitive_value_type;
00323   ROpScalarIndexReductionBase(
00324     const Scalar &initScalarReductObjValue = Teuchos::ScalarTraits<Scalar>::zero()
00325     ,const Index  &initIndexReductObjValue = Teuchos::ScalarTraits<Index>::zero()
00326     )
00327     :RTOpT<Scalar>("")
00328     ,initScalarReductObjValue_(initScalarReductObjValue)
00329     ,initIndexReductObjValue_(initIndexReductObjValue)
00330     {}
00332   const ScalarIndex<Scalar>& getRawVal( const ReductTarget &reduct_obj ) const
00333     {
00334       using Teuchos::dyn_cast;
00335       return dyn_cast<const ReductTargetScalarIndex<Scalar> >(reduct_obj).get();
00336     }
00338   void setRawVal( const ScalarIndex<Scalar> &rawVal, ReductTarget *reduct_obj ) const
00339     {
00340 #ifdef TEUCHOS_DEBUG
00341       TEST_FOR_EXCEPTION( reduct_obj==NULL, std::invalid_argument, "Error!" );
00342 #endif
00343       using Teuchos::dyn_cast;
00344       dyn_cast<ReductTargetScalarIndex<Scalar> >(*reduct_obj).set(rawVal);
00345     }
00349   void get_reduct_type_num_entries(
00350     int*   num_values
00351     ,int*  num_indexes
00352     ,int*  num_chars
00353     ) const
00354     {
00355       *num_values = num_values_;
00356       *num_indexes = 1;
00357       *num_chars = 0;
00358     }
00360   Teuchos::RefCountPtr<ReductTarget> reduct_obj_create() const
00361     {
00362       return Teuchos::rcp(
00363         new ReductTargetScalarIndex<Scalar>(
00364           ScalarIndex<Scalar>(initScalarReductObjValue(),initIndexReductObjValue())
00365           )
00366         );
00367     }
00369   void reduct_obj_reinit( ReductTarget* reduct_obj ) const
00370     {
00371       setRawVal( ScalarIndex<Scalar>(initScalarReductObjValue(),initIndexReductObjValue()), reduct_obj );
00372     }
00374   void extract_reduct_obj_state(
00375     const ReductTarget        &reduct_obj
00376     ,int                      num_values
00377     ,primitive_value_type     value_data[]
00378     ,int                      num_indexes
00379     ,index_type               index_data[]
00380     ,int                      num_chars
00381     ,char_type                char_data[]
00382     ) const
00383     {
00384 #ifdef TEUCHOS_DEBUG
00385       TEST_FOR_EXCEPTION(
00386         num_values!=num_values_ || value_data==NULL || num_indexes!=1 || index_data==NULL || num_chars!=0 || char_data!=NULL
00387         ,std::invalid_argument, "Error!"
00388         );
00389 #endif
00390       const ScalarIndex<Scalar> &scalarIndex = getRawVal(reduct_obj);
00391       Teuchos::PrimitiveTypeTraits<Scalar>::extractPrimitiveObjs( scalarIndex.scalar, num_values, value_data );
00392       index_data[0] = scalarIndex.index;
00393     }
00395   void load_reduct_obj_state(
00396     int                            num_values
00397     ,const primitive_value_type    value_data[]
00398     ,int                           num_indexes
00399     ,const index_type              index_data[]
00400     ,int                           num_chars
00401     ,const char_type               char_data[]
00402     ,ReductTarget                  *reduct_obj
00403     ) const
00404     {
00405 #ifdef TEUCHOS_DEBUG
00406       TEST_FOR_EXCEPTION(
00407         num_values!=num_values_ || value_data==NULL || num_indexes!=1 || index_data==NULL || num_chars!=0 || char_data!=NULL
00408         ,std::invalid_argument, "Error!"
00409         );
00410 #endif
00411       Scalar val = Teuchos::ScalarTraits<Scalar>::nan();
00412       Teuchos::PrimitiveTypeTraits<Scalar>::loadPrimitiveObjs( num_values, value_data, &val );
00413       setRawVal( ScalarIndex<Scalar>(val,index_data[0]), reduct_obj );
00414     }
00416 protected:
00418   STANDARD_MEMBER_COMPOSITION_MEMBERS( Scalar, initScalarReductObjValue )
00419   STANDARD_MEMBER_COMPOSITION_MEMBERS( Index, initIndexReductObjValue )
00420 private:
00421   static const int num_values_;
00422 };
00423 
00424 template<class Scalar>
00425 const int ROpScalarIndexReductionBase<Scalar>::num_values_=Teuchos::PrimitiveTypeTraits<Scalar>::numPrimitiveObjs();
00426 
00436 template<class Scalar>
00437 class ROpIndexReductionBase : virtual public RTOpT<Scalar> {
00438 public:
00439   typedef typename RTOpT<Scalar>::primitive_value_type primitive_value_type;
00441   ROpIndexReductionBase( const index_type &initReductObjValue = Teuchos::ScalarTraits<index_type>::zero() )
00442     :RTOpT<Scalar>(""), initReductObjValue_(initReductObjValue) 
00443     {}
00445   index_type getRawVal( const ReductTarget &reduct_obj ) const
00446     {
00447       using Teuchos::dyn_cast;
00448       return dyn_cast<const ReductTargetScalar<index_type> >(reduct_obj).get();
00449     }
00451   void setRawVal( const index_type &rawVal, ReductTarget *reduct_obj ) const
00452     {
00453 #ifdef TEUCHOS_DEBUG
00454       TEST_FOR_EXCEPTION( reduct_obj==NULL, std::invalid_argument, "Error!" );
00455 #endif
00456       using Teuchos::dyn_cast;
00457       dyn_cast<ReductTargetScalar<index_type> >(*reduct_obj).set(rawVal);
00458     }
00462   void get_reduct_type_num_entries(
00463     int*   num_values
00464     ,int*  num_indexes
00465     ,int*  num_chars
00466     ) const
00467     {
00468       *num_values = 0;
00469       *num_indexes = 1;
00470       *num_chars = 0;
00471     }
00473   Teuchos::RefCountPtr<ReductTarget> reduct_obj_create() const
00474     {
00475       return Teuchos::rcp(new ReductTargetScalar<index_type>(initReductObjValue()));
00476     }
00478   void reduce_reduct_objs(
00479     const ReductTarget& _in_reduct_obj, ReductTarget* _inout_reduct_obj
00480     ) const
00481     {
00482       using Teuchos::dyn_cast;
00483       const ReductTargetScalar<index_type> &in_reduct_obj    = dyn_cast<const ReductTargetScalar<index_type> >(_in_reduct_obj); 
00484       ReductTargetScalar<index_type>       &inout_reduct_obj = dyn_cast<ReductTargetScalar<index_type> >(*_inout_reduct_obj); 
00485       inout_reduct_obj.set( inout_reduct_obj.get() + in_reduct_obj.get() );
00486     }
00488   void reduct_obj_reinit( ReductTarget* reduct_obj ) const
00489     {
00490       setRawVal( initReductObjValue(), reduct_obj );
00491     }
00493   void extract_reduct_obj_state(
00494     const ReductTarget        &reduct_obj
00495     ,int                      num_values
00496     ,primitive_value_type     value_data[]
00497     ,int                      num_indexes
00498     ,index_type               index_data[]
00499     ,int                      num_chars
00500     ,char_type                char_data[]
00501     ) const
00502     {
00503 #ifdef TEUCHOS_DEBUG
00504       TEST_FOR_EXCEPTION(
00505         num_values!=0 || value_data!=NULL || num_indexes!=1 || index_data==NULL || num_chars!=0 || char_data!=NULL
00506         ,std::invalid_argument, "Error!"
00507         );
00508 #endif
00509       index_data[0] = getRawVal(reduct_obj);
00510     }
00512   void load_reduct_obj_state(
00513     int                            num_values
00514     ,const primitive_value_type    value_data[]
00515     ,int                           num_indexes
00516     ,const index_type              index_data[]
00517     ,int                           num_chars
00518     ,const char_type               char_data[]
00519     ,ReductTarget                  *reduct_obj
00520     ) const
00521     {
00522       using Teuchos::dyn_cast;
00523 #ifdef TEUCHOS_DEBUG
00524       TEST_FOR_EXCEPTION(
00525         num_values!=0 || value_data!=NULL || num_indexes!=1 || index_data==NULL || num_chars!=0 || char_data!=NULL
00526         ,std::invalid_argument, "Error!"
00527         );
00528 #endif
00529       setRawVal( index_data[0], reduct_obj );
00530     }
00532 protected:
00534   STANDARD_MEMBER_COMPOSITION_MEMBERS( index_type, initReductObjValue )
00535 };
00536 
00546 template<class Scalar>
00547 class ROpScalarTransformationBase : virtual public RTOpT<Scalar> {
00548 public:
00549   typedef typename RTOpT<Scalar>::primitive_value_type primitive_value_type;
00551   ROpScalarTransformationBase( const Scalar &scalarData = Teuchos::ScalarTraits<Scalar>::zero() )
00552     :RTOpT<Scalar>(""), scalarData_(scalarData)
00553     {}
00557   void get_op_type_num_entries(
00558     int*  num_values
00559     ,int* num_indexes
00560     ,int* num_chars
00561     ) const
00562     {
00563       *num_values = num_values_;
00564       *num_indexes = 0;
00565       *num_chars = 0;
00566     }
00568   void extract_op_state(
00569     int                             num_values
00570     ,primitive_value_type           value_data[]
00571     ,int                            num_indexes
00572     ,index_type                     index_data[]
00573     ,int                            num_chars
00574     ,char_type                      char_data[]
00575     ) const
00576     {
00577 #ifdef TEUCHOS_DEBUG
00578       TEST_FOR_EXCEPTION(
00579         num_values!=num_values_ || value_data==NULL || num_indexes!=0 || index_data!=NULL || num_chars!=0 || char_data!=NULL
00580         ,std::invalid_argument, "Error!"
00581         );
00582 #endif
00583       Teuchos::PrimitiveTypeTraits<Scalar>::extractPrimitiveObjs( scalarData_, num_values, value_data );
00584     }
00586   void load_op_state(
00587     int                           num_values
00588     ,const primitive_value_type   value_data[]
00589     ,int                          num_indexes
00590     ,const index_type             index_data[]
00591     ,int                          num_chars
00592     ,const char_type              char_data[]
00593     )
00594     {
00595 #ifdef TEUCHOS_DEBUG
00596       TEST_FOR_EXCEPTION(
00597         num_values!=num_values_ || value_data==NULL || num_indexes!=0 || index_data!=NULL || num_chars!=0 || char_data!=NULL
00598         ,std::invalid_argument, "Error!"
00599         );
00600 #endif
00601       Teuchos::PrimitiveTypeTraits<Scalar>::loadPrimitiveObjs( num_values, value_data, &scalarData_ );
00602     }
00604 protected:
00606   STANDARD_MEMBER_COMPOSITION_MEMBERS( Scalar, scalarData )
00607 private:
00608   static const int num_values_;
00609 };
00610 
00611 template<class Scalar>
00612 const int ROpScalarTransformationBase<Scalar>::num_values_=Teuchos::PrimitiveTypeTraits<Scalar>::numPrimitiveObjs();
00613 
00623 template<class Scalar>
00624 class ROpScalarScalarTransformationBase : virtual public RTOpT<Scalar> {
00625 public:
00626   typedef typename RTOpT<Scalar>::primitive_value_type primitive_value_type;
00628   ROpScalarScalarTransformationBase(
00629     const Scalar &scalarData1 = Teuchos::ScalarTraits<Scalar>::zero()
00630     ,const Scalar &scalarData2 = Teuchos::ScalarTraits<Scalar>::zero()
00631     )
00632     :RTOpT<Scalar>(""), scalarData1_(scalarData1), scalarData2_(scalarData2)
00633     {}
00637   void get_op_type_num_entries(
00638     int*  num_values
00639     ,int* num_indexes
00640     ,int* num_chars
00641     ) const
00642     {
00643       *num_values = num_values_;
00644       *num_indexes = 0;
00645       *num_chars = 0;
00646     }
00648   void extract_op_state(
00649     int                             num_values
00650     ,primitive_value_type           value_data[]
00651     ,int                            num_indexes
00652     ,index_type                     index_data[]
00653     ,int                            num_chars
00654     ,char_type                      char_data[]
00655     ) const
00656     {
00657 #ifdef TEUCHOS_DEBUG
00658       TEST_FOR_EXCEPTION(
00659         num_values!=num_values_ || value_data==NULL || num_indexes!=0 || index_data!=NULL || num_chars!=0 || char_data!=NULL
00660         ,std::invalid_argument, "Error!"
00661         );
00662 #endif
00663       Teuchos::PrimitiveTypeTraits<Scalar>::extractPrimitiveObjs( scalarData1_, num_values/2, value_data );
00664       Teuchos::PrimitiveTypeTraits<Scalar>::extractPrimitiveObjs( scalarData2_, num_values/2, value_data + num_values/2 );
00665     }
00667   void load_op_state(
00668     int                           num_values
00669     ,const primitive_value_type   value_data[]
00670     ,int                          num_indexes
00671     ,const index_type             index_data[]
00672     ,int                          num_chars
00673     ,const char_type              char_data[]
00674     )
00675     {
00676 #ifdef TEUCHOS_DEBUG
00677       TEST_FOR_EXCEPTION(
00678         num_values!=num_values_ || value_data==NULL || num_indexes!=0 || index_data!=NULL || num_chars!=0 || char_data!=NULL
00679         ,std::invalid_argument, "Error!"
00680         );
00681 #endif
00682       Teuchos::PrimitiveTypeTraits<Scalar>::loadPrimitiveObjs( num_values/2, value_data, &scalarData1_ );
00683       Teuchos::PrimitiveTypeTraits<Scalar>::loadPrimitiveObjs( num_values/2, value_data+num_values/2, &scalarData2_ );
00684     }
00686 protected:
00688   STANDARD_MEMBER_COMPOSITION_MEMBERS( Scalar, scalarData1 )
00690   STANDARD_MEMBER_COMPOSITION_MEMBERS( Scalar, scalarData2 )
00691 private:
00692   static const int num_values_;
00693 };
00694 
00695 template<class Scalar>
00696 const int ROpScalarScalarTransformationBase<Scalar>::num_values_=2*Teuchos::PrimitiveTypeTraits<Scalar>::numPrimitiveObjs();
00697 
00703 template<class Scalar>
00704 class RTOpBoolReduceAndTransform 
00705   : public ROpIndexReductionBase<Scalar>,
00706     public ROpScalarTransformationBase<Scalar>
00707 {
00708 public:
00710   typedef typename RTOpT<Scalar>::primitive_value_type primitive_value_type;
00712   RTOpBoolReduceAndTransform()
00713     : RTOpT<Scalar>(""), 
00714       ROpIndexReductionBase<Scalar>(1),
00715       ROpScalarTransformationBase<Scalar>() 
00716     {;}
00717   
00719   virtual ~RTOpBoolReduceAndTransform(){;}
00720   
00722   index_type operator()(const ReductTarget& reduct_obj ) const 
00723     { return this->getRawVal(reduct_obj); }
00725   void reduce_reduct_objs(const ReductTarget& in_reduct_obj, 
00726                           ReductTarget* inout_reduct_obj) const
00727     {
00728       const index_type in_val    = this->getRawVal(in_reduct_obj);
00729       const index_type inout_val = this->getRawVal(*inout_reduct_obj);
00730       this->setRawVal( in_val && inout_val, inout_reduct_obj );
00731     }
00732 };
00733 
00734 } // namespace RTOpPack
00735 
00740 #define RTOP_APPLY_OP_1_0( NUM_VECS, SUB_VECS, NUM_TARG_VECS, TARG_SUB_VECS ) \
00741   TEST_FOR_EXCEPTION( \
00742     (NUM_VECS)!=1 || (SUB_VECS)==NULL \
00743     ,RTOpPack::InvalidNumVecs \
00744     ,"Error, num_vecs="<<(NUM_VECS)<<" not allowed, only num_vecs==1, sub_vecs!=NULL" \
00745     ); \
00746   TEST_FOR_EXCEPTION( \
00747     (NUM_TARG_VECS)!=0 || (TARG_SUB_VECS)!=NULL \
00748     ,RTOpPack::InvalidNumTargVecs \
00749     ,"Error, num_targ_vecs="<<(NUM_TARG_VECS)<<" not allowed, only num_targ_vecs==0, targ_sub_vecs==NULL" \
00750     ); \
00751   const RTOpPack::index_type   subDim  = (SUB_VECS)[0].subDim(); \
00752   const RTOpPack::index_type   globalOffset = (SUB_VECS)[0].globalOffset(); \
00753   TEST_FOR_EXCEPT(globalOffset<0); \
00754   const Scalar                 *v0_val = (SUB_VECS)[0].values(); \
00755   const ptrdiff_t              v0_s    = (SUB_VECS)[0].stride()
00756 
00761 #define RTOP_APPLY_OP_2_0( NUM_VECS, SUB_VECS, NUM_TARG_VECS, TARG_SUB_VECS ) \
00762   TEST_FOR_EXCEPTION( \
00763     (NUM_VECS)!=2 || (SUB_VECS)==NULL \
00764     ,RTOpPack::InvalidNumVecs \
00765     ,"Error, num_vecs="<<(NUM_VECS)<<" not allowed, only num_vecs==1, sub_vecs!=NULL" \
00766     ); \
00767   TEST_FOR_EXCEPTION( \
00768     (NUM_TARG_VECS)!=0 || (TARG_SUB_VECS)!=NULL \
00769     ,RTOpPack::InvalidNumTargVecs \
00770     ,"Error, num_targ_vecs="<<(NUM_TARG_VECS)<<" not allowed, only num_targ_vecs==0, targ_sub_vecs==NULL" \
00771     ); \
00772   TEST_FOR_EXCEPTION( \
00773     (SUB_VECS)[0].subDim() != (SUB_VECS)[1].subDim() || \
00774     (SUB_VECS)[0].globalOffset() != (SUB_VECS)[1].globalOffset() \
00775     ,IncompatibleVecs \
00776     ,"Error, sub_vec[0] (subDim="<<(SUB_VECS)[0].subDim()<<",globalOffset="<<(SUB_VECS)[0].globalOffset()<<")" \
00777     " is not compatible with sub_vec[1] (subDim="<<(SUB_VECS)[1].subDim()<<",globalOffset="<<(SUB_VECS)[1].globalOffset()<<")" \
00778     ); \
00779   const RTOpPack::index_type   subDim  = (SUB_VECS)[0].subDim(); \
00780   const RTOpPack::index_type   globalOffset = (SUB_VECS)[0].globalOffset(); \
00781   TEST_FOR_EXCEPT(globalOffset<0); \
00782   const Scalar                 *v0_val = (SUB_VECS)[0].values(); \
00783   const ptrdiff_t              v0_s    = (SUB_VECS)[0].stride(); \
00784   const Scalar                 *v1_val = (SUB_VECS)[1].values(); \
00785   const ptrdiff_t              v1_s    = (SUB_VECS)[1].stride()
00786 
00791 #define RTOP_APPLY_OP_0_1( NUM_VECS, SUB_VECS, NUM_TARG_VECS, TARG_SUB_VECS ) \
00792   TEST_FOR_EXCEPTION( \
00793     (NUM_VECS)!=0 || (SUB_VECS)!=NULL \
00794     ,RTOpPack::InvalidNumVecs \
00795     ,"Error, num_vecs="<<(NUM_VECS)<<" not allowed, only num_vecs==0, sub_vecs==NULL" \
00796     ); \
00797   TEST_FOR_EXCEPTION( \
00798     (NUM_TARG_VECS)!=1 || (TARG_SUB_VECS)==NULL \
00799     ,RTOpPack::InvalidNumTargVecs \
00800     ,"Error, num_targ_vecs="<<(NUM_TARG_VECS)<<" not allowed, only num_targ_vecs==1, targ_sub_vecs!=NULL" \
00801     ); \
00802   const RTOpPack::index_type   subDim  = (TARG_SUB_VECS)[0].subDim(); \
00803   const RTOpPack::index_type   globalOffset = (TARG_SUB_VECS)[0].globalOffset(); \
00804   TEST_FOR_EXCEPT(globalOffset<0); \
00805   Scalar                       *z0_val = (TARG_SUB_VECS)[0].values(); \
00806   const ptrdiff_t              z0_s    = (TARG_SUB_VECS)[0].stride()
00807 
00810 #define RTOP_APPLY_OP_1_1( NUM_VECS, SUB_VECS, NUM_TARG_VECS, TARG_SUB_VECS ) \
00811   TEST_FOR_EXCEPTION( \
00812     (NUM_VECS)!=1 || (SUB_VECS)==NULL \
00813     ,RTOpPack::InvalidNumVecs \
00814     ,"Error, num_vecs="<<(NUM_VECS)<<" not allowed, only num_vecs==1, sub_vecs!=NULL" \
00815     ); \
00816   TEST_FOR_EXCEPTION( \
00817     (NUM_TARG_VECS)!=1 || (TARG_SUB_VECS)==NULL \
00818     ,RTOpPack::InvalidNumTargVecs \
00819     ,"Error, num_targ_vecs="<<(NUM_TARG_VECS)<<" not allowed, only num_targ_vecs==1, targ_sub_vecs!=NULL" \
00820     ); \
00821   TEST_FOR_EXCEPTION( \
00822     (SUB_VECS)[0].subDim() != (TARG_SUB_VECS)[0].subDim() || \
00823     (SUB_VECS)[0].globalOffset() != (TARG_SUB_VECS)[0].globalOffset() \
00824     ,IncompatibleVecs \
00825     ,"Error, sub_vec[0] (subDim="<<(SUB_VECS)[0].subDim()<<",globalOffset="<<(SUB_VECS)[0].globalOffset()<<")" \
00826     " is not compatible with targ_sub_vec[0] (subDim="<<(TARG_SUB_VECS)[0].subDim()<<",globalOffset="<<(TARG_SUB_VECS)[0].globalOffset()<<")" \
00827     ); \
00828   const RTOpPack::index_type   subDim  = (SUB_VECS)[0].subDim(); \
00829   const RTOpPack::index_type   globalOffset = (SUB_VECS)[0].globalOffset(); \
00830   TEST_FOR_EXCEPT(globalOffset<0); \
00831   const Scalar                 *v0_val = (SUB_VECS)[0].values(); \
00832   const ptrdiff_t              v0_s    = (SUB_VECS)[0].stride(); \
00833   Scalar                       *z0_val = (TARG_SUB_VECS)[0].values(); \
00834   const ptrdiff_t              z0_s    = (TARG_SUB_VECS)[0].stride()
00835 
00840 #define RTOP_APPLY_OP_2_1( NUM_VECS, SUB_VECS, NUM_TARG_VECS, TARG_SUB_VECS ) \
00841   TEST_FOR_EXCEPTION( \
00842     (NUM_VECS)!=2 || (SUB_VECS)==NULL \
00843     ,RTOpPack::InvalidNumVecs \
00844     ,"Error, num_vecs="<<(NUM_VECS)<<" not allowed, only num_vecs==2, sub_vecs!=NULL" \
00845     ); \
00846   TEST_FOR_EXCEPTION( \
00847     (NUM_TARG_VECS)!=1 || (TARG_SUB_VECS)==NULL \
00848     ,RTOpPack::InvalidNumTargVecs \
00849     ,"Error, num_targ_vecs="<<(NUM_TARG_VECS)<<" not allowed, only num_targ_vecs==1, targ_sub_vecs!=NULL" \
00850     ); \
00851   TEST_FOR_EXCEPTION( \
00852     (SUB_VECS)[0].subDim() != (SUB_VECS)[1].subDim() || \
00853     (SUB_VECS)[0].subDim() != (TARG_SUB_VECS)[0].subDim() || \
00854     (SUB_VECS)[0].globalOffset() != (SUB_VECS)[1].globalOffset() || \
00855     (SUB_VECS)[0].globalOffset() != (TARG_SUB_VECS)[0].globalOffset() \
00856     ,IncompatibleVecs \
00857     ,"Error, sub_vec[0] (subDim="<<(SUB_VECS)[0].subDim()<<",globalOffset="<<(SUB_VECS)[0].globalOffset()<<")," \
00858     " sub_vec[1] (subDim="<<(SUB_VECS)[1].subDim()<<",globalOffset="<<(SUB_VECS)[1].globalOffset()<<")," \
00859     " and targ_sub_vec[0] (subDim="<<(TARG_SUB_VECS)[0].subDim()<<",globalOffset="<<(TARG_SUB_VECS)[0].globalOffset()<<")" \
00860     " are not compatible." \
00861     ); \
00862   const RTOpPack::index_type   subDim  = (SUB_VECS)[0].subDim(); \
00863   const RTOpPack::index_type   globalOffset = (SUB_VECS)[0].globalOffset(); \
00864   TEST_FOR_EXCEPT(globalOffset<0); \
00865   const Scalar                 *v0_val = (SUB_VECS)[0].values(); \
00866   const ptrdiff_t              v0_s    = (SUB_VECS)[0].stride(); \
00867   const Scalar                 *v1_val = (SUB_VECS)[1].values(); \
00868   const ptrdiff_t              v1_s    = (SUB_VECS)[1].stride(); \
00869   Scalar                       *z0_val = (TARG_SUB_VECS)[0].values(); \
00870   const ptrdiff_t              z0_s    = (TARG_SUB_VECS)[0].stride()
00871 
00872 
00877 #define RTOP_APPLY_OP_3_0( NUM_VECS, SUB_VECS, NUM_TARG_VECS, TARG_SUB_VECS ) \
00878   TEST_FOR_EXCEPTION(                                                   \
00879                      (NUM_VECS)!=3 || (SUB_VECS)==NULL                  \
00880                      ,RTOpPack::InvalidNumVecs                          \
00881                      ,"Error, num_vecs="<<(NUM_VECS)<<" not allowed, only num_vecs==3, sub_vecs!=NULL" \
00882                      );                                                 \
00883   TEST_FOR_EXCEPTION(                                                   \
00884                      (NUM_TARG_VECS)!=0 || (TARG_SUB_VECS)!=NULL        \
00885                      ,RTOpPack::InvalidNumTargVecs                      \
00886                      ,"Error, num_targ_vecs="<<(NUM_TARG_VECS)<<" not allowed, only num_targ_vecs==0, targ_sub_vecs==NULL" \
00887                      );                                                 \
00888   TEST_FOR_EXCEPTION(                                                   \
00889                      (SUB_VECS)[0].subDim() != (SUB_VECS)[1].subDim()   \
00890                      || (SUB_VECS)[0].subDim() != (SUB_VECS)[2].subDim() \
00891                      ||(SUB_VECS)[0].globalOffset() != (SUB_VECS)[1].globalOffset() \
00892                      ||(SUB_VECS)[0].globalOffset() != (SUB_VECS)[1].globalOffset() \
00893                      ,IncompatibleVecs                                  \
00894                      ,"Error, num_targ_vecs="<<(NUM_TARG_VECS)<<" not allowed, only num_targ_vecs==0, targ_sub_vecs==NULL" \
00895                      );                                                 \
00896   const RTOpPack::index_type   subDim  = (SUB_VECS)[0].subDim();        \
00897   const Scalar                 *v0_val = (SUB_VECS)[0].values();        \
00898   const ptrdiff_t              v0_s    = (SUB_VECS)[0].stride();        \
00899   const Scalar                 *v1_val = (SUB_VECS)[1].values();        \
00900   const ptrdiff_t              v1_s    = (SUB_VECS)[1].stride();        \
00901   const Scalar                 *v2_val = (SUB_VECS)[2].values();        \
00902   const ptrdiff_t              v2_s    = (SUB_VECS)[2].stride();
00903 
00904 
00905 
00906 #endif // RTOPPACK_RTOP_NEW_T_HELPERS_HPP

Generated on Thu Sep 18 12:30:39 2008 for Support Software for Vector Reduction/Transformation Operators by doxygen 1.3.9.1