RTOp_ROp_max_inequ_viol.c

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 #define max(a,b) ( (a) > (b) ? (a) : (b) )
00032 #define min(a,b) ( (a) < (b) ? (a) : (b) )
00033 
00034 #include "RTOp_ROp_max_inequ_viol.h"
00035 #include "RTOp_obj_null_vtbl.h"
00036 #include "RTOp_obj_value_vtbl.h"
00037 #include "RTOp_obj_free_free.h"
00038 #include "RTOp_get_reduct_op.hpp"
00039 
00040 /* */
00041 /* Implementation functions */
00042 /* */
00043 
00044 /* Functions for the reduction target object */
00045 
00046 static int get_targ_type_num_entries(
00047   const struct RTOp_obj_type_vtbl_t* vtbl
00048   ,const void* obj_data
00049   ,int* num_values
00050   ,int* num_indexes
00051   ,int* num_chars
00052   )
00053 {
00054   *num_values  = 3;
00055   *num_indexes = 2;
00056   *num_chars   = 0;
00057   return 0;
00058 }
00059 
00060 static int targ_obj_reinit(
00061   const struct RTOp_obj_type_vtbl_t* vtbl, const void* obj_data
00062   , RTOp_ReductTarget targ_obj )
00063 {
00064   struct RTOp_ROp_max_inequ_viol_reduct_obj_t
00065     *targ = (struct RTOp_ROp_max_inequ_viol_reduct_obj_t*)targ_obj;
00066   targ->max_viol   = 0.0; /* None violated yet! */
00067   targ->v_i        = 0.0; /* arbitrary */
00068   targ->vLU_i      = 0.0; /* arbitrary */
00069   targ->max_viol_i = 0;   /* None violated yet! */
00070   targ->bnd_type   = -2;  /* Invalid type (just in case used by accident) */
00071   return 0;
00072 }
00073 
00074 static int targ_obj_create(
00075   const struct RTOp_obj_type_vtbl_t* vtbl, const void* obj_data
00076   ,RTOp_ReductTarget* targ_obj
00077   )
00078 {
00079   const int mem_size = sizeof(struct RTOp_ROp_max_inequ_viol_reduct_obj_t);
00080   *targ_obj = malloc( mem_size );
00081   return targ_obj_reinit(vtbl,obj_data,*targ_obj);
00082 }
00083 
00084 static int targ_extract_state(
00085   const struct RTOp_obj_type_vtbl_t* vtbl
00086   ,const void *       obj_data
00087   ,void *             reduct_obj
00088   ,int                num_values
00089   ,RTOp_value_type    value_data[]
00090   ,int                num_indexes
00091   ,RTOp_index_type    index_data[]
00092   ,int                num_chars
00093   ,RTOp_char_type     char_data[]
00094   )
00095 {
00096   struct RTOp_ROp_max_inequ_viol_reduct_obj_t *targ = NULL;
00097   assert( reduct_obj );
00098   assert( num_values  == 3 );
00099   assert( num_indexes == 3 );
00100   assert( num_chars   == 0 );
00101   targ = (struct RTOp_ROp_max_inequ_viol_reduct_obj_t*)reduct_obj;
00102   value_data[0] = targ->max_viol;
00103   value_data[1] = targ->v_i;
00104   value_data[2] = targ->vLU_i;
00105   index_data[0] = targ->max_viol_i;
00106   index_data[1] = targ->bnd_type;
00107   return 0;
00108 }
00109 
00110 static int targ_load_state(
00111   const struct RTOp_obj_type_vtbl_t* vtbl
00112   ,const void*             obj_data
00113   ,int                     num_values
00114   ,const RTOp_value_type   value_data[]
00115   ,int                     num_indexes
00116   ,const RTOp_index_type   index_data[]
00117   ,int                     num_chars
00118   ,const RTOp_char_type    char_data[]
00119   ,void **                 reduct_obj
00120   )
00121 {
00122   struct RTOp_ROp_max_inequ_viol_reduct_obj_t *targ = NULL;
00123   assert( *reduct_obj );
00124   assert( num_values  == 3 );
00125   assert( num_indexes == 2 );
00126   assert( num_chars   == 0 );
00127   targ = (struct RTOp_ROp_max_inequ_viol_reduct_obj_t*)*reduct_obj;
00128   targ->max_viol   = value_data[0];
00129   targ->v_i        = value_data[1];
00130   targ->vLU_i      = value_data[2];
00131   targ->max_viol_i = index_data[0];
00132   targ->bnd_type   = index_data[1];
00133   return 0;
00134 }
00135 
00136 static const struct RTOp_obj_type_vtbl_t targ_obj_vtbl =
00137 {
00138   get_targ_type_num_entries
00139   ,targ_obj_create
00140   ,targ_obj_reinit
00141   ,RTOp_obj_free_free
00142   ,targ_extract_state
00143   ,targ_load_state
00144 };
00145 
00146 /* Other functions */
00147 
00148 static int RTOp_ROp_max_inequ_viol_apply_op(
00149   const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data
00150   ,const int num_vecs, const struct RTOp_SubVector vecs[]
00151   ,const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[]
00152   ,RTOp_ReductTarget targ_obj
00153   )
00154 {
00155   /* */
00156   /* Declare local variables */
00157   /* */
00158 
00159   /* targ */
00160   struct RTOp_ROp_max_inequ_viol_reduct_obj_t
00161     *targ = NULL;
00162   /* global_off */
00163   size_t                 global_offset;
00164   /* sub_dim */
00165   size_t                 sub_dim;
00166   /* v */
00167   const RTOp_value_type  *v_val = NULL;
00168   ptrdiff_t              v_val_s;
00169   /* vL */
00170   const RTOp_value_type  *vL_val = NULL;
00171   ptrdiff_t              vL_val_s;
00172   /* vU */
00173   const RTOp_value_type  *vU_val = NULL;
00174   ptrdiff_t              vU_val_s;
00175 
00176   register size_t  k;
00177   RTOp_index_type  i;
00178   RTOp_value_type  v_scale;
00179   RTOp_value_type  violL;
00180   RTOp_value_type  violU;
00181 
00182   /* */
00183   /* Validate the input */
00184   /* */
00185   if( num_vecs != 3 )
00186     return RTOp_ERR_INVALID_NUM_VECS;
00187   if( num_targ_vecs != 0 )
00188     return RTOp_ERR_INVALID_NUM_TARG_VECS;
00189   if(    vecs[0].global_offset != vecs[1].global_offset
00190     || vecs[0].sub_dim       != vecs[1].sub_dim
00191     || vecs[0].global_offset != vecs[2].global_offset
00192     || vecs[0].sub_dim       != vecs[2].sub_dim )
00193     return RTOp_ERR_INCOMPATIBLE_VECS;
00194 
00195   /* */
00196   /* Get pointers to the data */
00197   /* */
00198 
00199   /* targ */
00200   targ            = (struct RTOp_ROp_max_inequ_viol_reduct_obj_t*)targ_obj;
00201   /* global_off */
00202   global_offset   = vecs[0].global_offset;
00203   /* sub_dim */
00204   sub_dim         = vecs[0].sub_dim;
00205   /* v */
00206   v_val           = vecs[0].values;
00207   v_val_s         = vecs[0].values_stride;
00208   /* vL */
00209   vL_val          = vecs[1].values;
00210   vL_val_s        = vecs[1].values_stride;
00211   /* vU */
00212   vU_val          = vecs[2].values;
00213   vU_val_s        = vecs[2].values_stride;
00214 
00215   /* */
00216   /* Perform the reduction operation. */
00217   /* */
00218 
00219   i = global_offset + 1;
00220   for( k = 0; k < sub_dim; ++k, ++i, v_val += v_val_s, vL_val += vL_val_s, vU_val += vU_val_s ) {
00221     v_scale = 1.0 / (1.0 + fabs(*v_val));
00222     /* (vL - v)*v_scale */
00223     violL = (*vL_val - *v_val) * v_scale;
00224     /* (v - vU)*v_scale */
00225     violU = (*v_val - *vU_val) * v_scale;
00226     /* Perform the reduction */
00227     if(
00228       ( max(violL,violU) > targ->max_viol )
00229       ||
00230       ( max(violL,violU) == targ->max_viol && i < targ->max_viol_i )
00231       )
00232      {
00233       targ->bnd_type = ( violL > 0.0
00234                 ? ( *vL_val == *vU_val
00235                   ? 0  /* EQUALITY */
00236                   : -1 /* LOWER */
00237                   )
00238                 : +1 /* UPPER */
00239                 );
00240       targ->max_viol   = ( targ->bnd_type <= 0 ? violL : violU );
00241       targ->v_i        = *v_val;
00242       targ->vLU_i      = ( targ->bnd_type <= 0 ? *vL_val : *vU_val );
00243       targ->max_viol_i = i;
00244     }
00245   }
00246 
00247   return 0; /* success? */
00248 }
00249 
00250 static int reduce_reduct_objs(
00251   const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data /* Can be NULL! */
00252   , RTOp_ReductTarget in_reduct_obj, RTOp_ReductTarget inout_reduct_obj )
00253 {
00254   const struct RTOp_ROp_max_inequ_viol_reduct_obj_t
00255     *i_targ = (const struct RTOp_ROp_max_inequ_viol_reduct_obj_t*)in_reduct_obj;
00256   struct RTOp_ROp_max_inequ_viol_reduct_obj_t
00257     *io_targ = (struct RTOp_ROp_max_inequ_viol_reduct_obj_t*)inout_reduct_obj;
00258 
00259   if(
00260     ( i_targ->max_viol > io_targ->max_viol )
00261     ||
00262     ( i_targ->max_viol == io_targ->max_viol && i_targ->max_viol_i < io_targ->max_viol_i )
00263     )
00264   {
00265     io_targ->max_viol     = i_targ->max_viol;
00266     io_targ->v_i          = i_targ->v_i;
00267     io_targ->vLU_i        = i_targ->vLU_i;
00268     io_targ->max_viol_i   = i_targ->max_viol_i;
00269     io_targ->bnd_type     = i_targ->bnd_type;
00270   }
00271   return 0;
00272 }
00273 
00274 INSERT_GET_REDUCT_OP_FUNCS(
00275   3,2,0,RTOp_ROp_max_inequ_viol_reduct_obj_t,reduce_reduct_objs
00276   ,targ_load_state,targ_extract_state
00277   ,external_reduct_op,get_reduct_op)
00278 
00279 const struct RTOp_RTOp_vtbl_t RTOp_ROp_max_inequ_viol_vtbl =
00280 {
00281   &RTOp_obj_null_vtbl
00282   ,&targ_obj_vtbl
00283   ,"ROp_max_inequ_viol"
00284   ,NULL
00285   ,RTOp_ROp_max_inequ_viol_apply_op
00286   ,reduce_reduct_objs
00287   ,get_reduct_op
00288 };
00289 
00290 /* Class specific functions */
00291 
00292 int RTOp_ROp_max_inequ_viol_construct( struct RTOp_RTOp* op )
00293 {
00294   op->vtbl     = &RTOp_ROp_max_inequ_viol_vtbl;
00295   op->obj_data = NULL;
00296   return 0; /* success? */
00297 }
00298 
00299 int RTOp_ROp_max_inequ_viol_destroy( struct RTOp_RTOp* op )
00300 {
00301   op->vtbl     = NULL;
00302   op->obj_data = NULL;
00303   return 0; /* success? */
00304 }
00305 
00306 struct RTOp_ROp_max_inequ_viol_reduct_obj_t
00307 RTOp_ROp_max_inequ_viol_val(RTOp_ReductTarget targ_obj)
00308 {
00309   return *(struct RTOp_ROp_max_inequ_viol_reduct_obj_t*)targ_obj;
00310 }

Generated on Thu Sep 18 12:33:38 2008 for RTOpPack: Extra C/C++ Code for Vector Reduction/Transformation Operators by doxygen 1.3.9.1