|
RTOpPack: Extra C/C++ Code for Vector Reduction/Transformation Operators Version of the Day
|
00001 /* 00002 // @HEADER 00003 // *********************************************************************** 00004 // 00005 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization 00006 // Copyright (2003) 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 00031 #include "RTOp_ROp_max_step.h" 00032 #include "RTOp_obj_value_vtbl.h" 00033 #include "RTOp_obj_free_free.h" 00034 #include "RTOp_get_reduct_op.hpp" 00035 #include "RTOp_reduct_min_value.h" 00036 00037 static int RTOp_ROp_max_step_reduct_obj_reinit( 00038 const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data 00039 , RTOp_ReductTarget reduct_obj ) 00040 { 00041 *((RTOp_value_type*)reduct_obj) = RTOp_ROp_max_step_inf; 00042 return 0; 00043 } 00044 00045 static int ROp_max_step_apply_op( 00046 const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data 00047 , const int num_vecs, const struct RTOp_SubVector vecs[] 00048 , const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[] 00049 , RTOp_ReductTarget reduct_obj ) 00050 { 00051 /* Declare locals */ 00052 const RTOp_value_type beta = *(RTOp_value_type*)obj_data; 00053 RTOp_value_type *alpha = (RTOp_value_type*)reduct_obj; 00054 RTOp_index_type sub_dim = 0; 00055 const RTOp_value_type *v0_val = NULL, *v1_val = NULL; 00056 ptrdiff_t v0_val_s = 0, v1_val_s = 0; 00057 register RTOp_index_type k; 00058 RTOp_value_type alpha_tmp; 00059 /* Validate the input */ 00060 if( num_vecs != 2 ) return RTOp_ERR_INVALID_NUM_VECS; 00061 if( num_targ_vecs != 0 ) return RTOp_ERR_INVALID_NUM_TARG_VECS; 00062 if( vecs[0].sub_dim != vecs[1].sub_dim ) return RTOp_ERR_INCOMPATIBLE_VECS; 00063 /* Get local variables to vector data */ 00064 sub_dim = vecs[0].sub_dim; 00065 v0_val = vecs[0].values; v0_val_s = vecs[0].values_stride; 00066 v1_val = vecs[1].values; v1_val_s = vecs[1].values_stride; 00067 /* Perform the reduction operation: */ 00068 /* max alpha s.t. v[0] + alpha * v[1] >= beta */ 00069 for( k = 0; k < sub_dim; ++k, v0_val += v0_val_s, v1_val += v1_val_s ) { 00070 alpha_tmp = (beta - (*v0_val))/(*v1_val); 00071 *alpha = ( (0 <= alpha_tmp && alpha_tmp < *alpha) ? alpha_tmp : *alpha ); 00072 } 00073 return 0; /* success! */ 00074 } 00075 00076 /* Virtual function table */ 00077 const struct RTOp_RTOp_vtbl_t RTOp_ROp_max_step_vtbl = 00078 { 00079 &RTOp_obj_value_vtbl 00080 ,&RTOp_obj_value_vtbl 00081 ,"ROp_max_step" 00082 ,RTOp_ROp_max_step_reduct_obj_reinit 00083 ,ROp_max_step_apply_op 00084 ,RTOp_reduct_min_value 00085 ,RTOp_get_reduct_min_value_op 00086 }; 00087 00088 /* Class specific functions */ 00089 00090 int RTOp_ROp_max_step_construct( RTOp_value_type beta, struct RTOp_RTOp* op ) 00091 { 00092 op->vtbl = &RTOp_ROp_max_step_vtbl; 00093 op->vtbl->obj_data_vtbl->obj_create(NULL,NULL,&op->obj_data); 00094 *((RTOp_value_type*)op->obj_data) = beta; 00095 return 0; /* success? */ 00096 } 00097 00098 int RTOp_ROp_max_step_destroy( struct RTOp_RTOp* op ) 00099 { 00100 op->vtbl->obj_data_vtbl->obj_free(NULL,NULL,&op->obj_data); 00101 op->vtbl = NULL; 00102 return 0; /* success? */ 00103 } 00104 00105 int RTOp_ROp_max_step_set_beta( RTOp_value_type beta, struct RTOp_RTOp* op ) 00106 { 00107 *((RTOp_value_type*)op->obj_data) = beta; 00108 return 0; /* success? */ 00109 } 00110 00111 RTOp_value_type RTOp_ROp_max_step_inf = +1e+50; 00112 00113 RTOp_value_type RTOp_ROp_max_step_val(RTOp_ReductTarget reduct_obj) 00114 { 00115 return *(RTOp_value_type*)reduct_obj; 00116 }
1.7.4