RTOpPack: Extra C/C++ Code for Vector Reduction/Transformation Operators Version of the Day
RTOpPack_RTOpC.cpp
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
00005 //                  Copyright (2003) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 // ///////////////////////////////
00030 // RTOpPack_RTOpC.cpp
00031 
00032 #include "RTOpPack_RTOpC.hpp"
00033 #include "Teuchos_Workspace.hpp"
00034 
00035 
00036 namespace RTOpPack {
00037 
00038 
00039 RTOpC::RTOpC()
00040   :RTOpT<RTOp_value_type>("RTOpC") // Should be unused since op_name() if overridden here!
00041 {
00042   op_.vtbl     = NULL;
00043   op_.obj_data = NULL;
00044 }
00045 
00046 
00047 RTOpC::~RTOpC()
00048 {
00049   if(op_.obj_data)
00050     RTOp_free_op( &op_ );
00051 }
00052 
00053 
00054 // Overridden from RTOpT
00055 
00056 
00057 void RTOpC::get_reduct_type_num_entries_impl(
00058   const Teuchos::Ptr<int> &num_values,
00059   const Teuchos::Ptr<int> &num_indexes,
00060   const Teuchos::Ptr<int> &num_chars
00061   ) const
00062 {
00063   TEST_FOR_EXCEPTION(
00064     0!=RTOp_get_reduct_type_num_entries(&op_,&*num_values,&*num_indexes,&*num_chars)
00065     ,UnknownError
00066     ,"RTOpC::get_reduct_type_num_entries(...): Error, "
00067     "RTOp_get_reduct_type_num_entries(...) returned != 0"
00068     );
00069 }
00070 
00071 
00072 Teuchos::RCP<ReductTarget>
00073 RTOpC::reduct_obj_create_impl() const
00074 {
00075   RTOp_ReductTarget reduct_obj_raw = RTOp_REDUCT_OBJ_NULL;
00076   TEST_FOR_EXCEPTION(
00077     0!=RTOp_reduct_obj_create(&op_,&reduct_obj_raw)
00078     ,UnknownError
00079     ,"RTOpC::reduct_obj_create(...): Error, "
00080     "RTOp_reduct_obj_create(...) returned != 0"
00081     );
00082   return Teuchos::rcp(new ReductTargetC(op_,reduct_obj_raw));
00083 }
00084 
00085 
00086 void RTOpC::reduce_reduct_objs_impl(
00087   const ReductTarget &in_reduct_obj,
00088   const Teuchos::Ptr<ReductTarget> &inout_reduct_obj
00089   ) const
00090 {
00091   TEST_FOR_EXCEPTION(
00092     0!=RTOp_reduce_reduct_objs( &op_, (*this)(in_reduct_obj), (*this)(*inout_reduct_obj) )
00093     ,UnknownError
00094     ,"RTOpC::reduce_reduct_objs(...): Error, "
00095     "RTOp_reduce_reduct_objs(...) returned != 0"
00096     );
00097 }
00098 
00099 
00100 void RTOpC::reduct_obj_reinit_impl(
00101   const Teuchos::Ptr<ReductTarget> &reduct_obj ) const
00102 {
00103   TEST_FOR_EXCEPTION(
00104     0!=RTOp_reduct_obj_reinit( &op_, (*this)(*reduct_obj) )
00105     ,UnknownError
00106     ,"RTOpC::reduct_obj_reinit(...): Error, "
00107     "RTOp_reduct_obj_reinit(...) returned != 0"
00108     );
00109 }
00110 
00111 
00112 void RTOpC::extract_reduct_obj_state_impl(
00113   const ReductTarget &reduct_obj,
00114   const Teuchos::ArrayView<primitive_value_type> &value_data,
00115   const Teuchos::ArrayView<index_type> &index_data,
00116   const Teuchos::ArrayView<char_type> &char_data
00117   ) const
00118 {
00119   TEST_FOR_EXCEPTION(
00120     0!=RTOp_extract_reduct_obj_state(
00121       &op_, (*this)(reduct_obj),
00122       value_data.size(), value_data.getRawPtr(),
00123       index_data.size(), index_data.getRawPtr(),
00124       char_data.size(), char_data.getRawPtr()
00125       ),
00126     UnknownError,
00127     "RTOpC::extract_reduct_obj_state(...): Error, "
00128     "RTOp_extract_reduct_obj_state(...) returned != 0"
00129     );
00130 }
00131 
00132 
00133 void RTOpC::load_reduct_obj_state_impl(
00134   const Teuchos::ArrayView<const primitive_value_type> &value_data,
00135   const Teuchos::ArrayView<const index_type> &index_data,
00136   const Teuchos::ArrayView<const char_type> &char_data,
00137   const Teuchos::Ptr<ReductTarget> &reduct_obj
00138   ) const
00139 {
00140   TEST_FOR_EXCEPTION(
00141     0!=RTOp_load_reduct_obj_state(
00142       &op_,
00143       value_data.size(), value_data.getRawPtr(),
00144       index_data.size(), index_data.getRawPtr(),
00145       char_data.size(), char_data.getRawPtr(),
00146       (*this)(*reduct_obj)
00147       ),
00148     UnknownError,
00149     "RTOpC::load_reduct_obj_state(...): Error, "
00150     "RTOp_load_reduct_obj_state(...) returned != 0"
00151     );
00152 }
00153 
00154 
00155 bool RTOpC::coord_invariant_impl() const
00156 {
00157   return false; // We have to assume this to be safe!
00158 }
00159 
00160 
00161 std::string RTOpC::op_name_impl() const
00162 {
00163   const char* op_name = NULL;
00164   TEST_FOR_EXCEPTION(
00165     0!=RTOp_get_op_name(&op_,&op_name)
00166     ,UnknownError
00167     ,"RTOpC::get_op_name(...): Error, "
00168     "RTOp_op_name(...) returned != 0"
00169     );
00170   return op_name;
00171 }
00172 
00173 
00174 void RTOpC::apply_op_impl(
00175     const Teuchos::ArrayView<const ConstSubVectorView<Scalar> > &sub_vecs,
00176     const Teuchos::ArrayView<const SubVectorView<Scalar> > &targ_sub_vecs,
00177     const Teuchos::Ptr<ReductTarget> &_reduct_obj
00178   ) const
00179 {
00180 
00181   using Teuchos::Workspace;
00182   Teuchos::WorkspaceStore* wss =
00183     Teuchos::get_default_workspace_store().get();
00184 
00185   const int num_vecs = sub_vecs.size();
00186   const int num_targ_vecs = targ_sub_vecs.size();
00187 
00188   RTOp_ReductTarget reduct_obj = RTOp_REDUCT_OBJ_NULL;
00189   if(!is_null(_reduct_obj))
00190     reduct_obj = (*this)(*_reduct_obj);
00191 
00192   int k;
00193   Workspace<RTOp_SubVector>        c_sub_vecs(wss,num_vecs,false);
00194   for( k = 0; k < num_vecs; ++k ) {
00195     const SubVector& v = sub_vecs[k];
00196     RTOp_sub_vector(v.globalOffset(),v.subDim(),v.values(),v.stride(),&c_sub_vecs[k]);
00197   }
00198   Workspace<RTOp_MutableSubVector>  c_targ_sub_vecs(wss,num_targ_vecs,false);
00199   for( k = 0; k < num_targ_vecs; ++k ) {
00200     const MutableSubVector& v = targ_sub_vecs[k];
00201     RTOp_mutable_sub_vector(v.globalOffset(),v.subDim(),v.values(),v.stride(),&c_targ_sub_vecs[k]);
00202   }
00203 
00204   const int err = RTOp_apply_op(
00205     &op_
00206     ,num_vecs,       num_vecs       ? &c_sub_vecs[0]      : (RTOp_SubVector*)NULL
00207     ,num_targ_vecs,  num_targ_vecs  ? &c_targ_sub_vecs[0] : (RTOp_MutableSubVector*)NULL
00208     ,reduct_obj
00209     );
00210   TEST_FOR_EXCEPTION(
00211     err==RTOp_ERR_INVALID_NUM_VECS, InvalidNumVecs
00212     ,"RTOpC::apply_op(...): Error, "
00213     "RTOp_apply_op(...) returned RTOp_ERR_INVALID_NUM_VECS" );
00214   TEST_FOR_EXCEPTION(
00215     err==RTOp_ERR_INVALID_NUM_TARG_VECS, InvalidNumTargVecs
00216     ,"RTOpC::apply_op(...): Error, "
00217     "RTOp_apply_op(...) returned RTOp_ERR_INVALID_NUM_TARG_VECS" );
00218   TEST_FOR_EXCEPTION(
00219     err!=0, UnknownError
00220     ,"RTOpC::apply_op(...): Error, "
00221     "RTOp_apply_op(...) returned != 0 with unknown meaning" );
00222 
00223 }
00224 
00225 
00226 } // namespace RTOpPack
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends