RTOp_ROp_max_abs_ele.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 #include <math.h>
00032 
00033 #include "RTOp_ROp_max_abs_ele.h"
00034 #include "RTOp_obj_null_vtbl.h"
00035 #include "RTOp_obj_value_vtbl.h"
00036 #include "RTOp_obj_free_free.h"
00037 #include "RTOp_get_reduct_op.hpp"
00038 
00039 /* */
00040 /* Implementation functions */
00041 /* */
00042 
00043 /* Selected functions that are used to implement exteral_reduct_op */
00044 
00045 static int CALL_API targ_extract_state(
00046   const struct RTOp_obj_type_vtbl_t* vtbl
00047   ,const void *       instance_data
00048   ,void *             obj
00049   ,int                num_values
00050   ,RTOp_value_type    value_data[]
00051   ,int                num_indexes
00052   ,RTOp_index_type    index_data[]
00053   ,int                num_chars
00054   ,RTOp_char_type     char_data[]
00055   )
00056 {
00057   struct RTOp_value_index_type* vi_obj;
00058 #ifdef RTOp_DEBUG
00059   assert( obj );
00060   assert( num_values  == 1 );
00061   assert( num_indexes == 1 );
00062   assert( num_chars   == 0 );
00063 #endif
00064   vi_obj = (struct RTOp_value_index_type*)obj;
00065   value_data[0] = vi_obj->value;
00066   index_data[0] = vi_obj->index;
00067   return 0;
00068 }
00069 
00070 static int CALL_API targ_load_state(
00071   const struct RTOp_obj_type_vtbl_t* vtbl
00072   ,const void *            instance_data
00073   ,int                     num_values
00074   ,const RTOp_value_type   value_data[]
00075   ,int                     num_indexes
00076   ,const RTOp_index_type   index_data[]
00077   ,int                     num_chars
00078   ,const RTOp_char_type    char_data[]
00079   ,void **                 obj
00080   )
00081 {
00082   struct RTOp_value_index_type* vi_obj;
00083 #ifdef RTOp_DEBUG
00084   assert( obj );
00085   assert( *obj );
00086   assert( num_values  == 1 );
00087   assert( num_indexes == 1 );
00088   assert( num_chars   == 0 );
00089 #endif
00090   vi_obj = (struct RTOp_value_index_type*)*obj;
00091   vi_obj->value = value_data[0];
00092   vi_obj->index = index_data[0];
00093   return 0;
00094 }
00095 
00096 /* Other functions */
00097 
00098 static int RTOp_ROp_max_abs_ele_apply_op(
00099   const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data
00100   ,const int num_vecs, const struct RTOp_SubVector vecs[]
00101   ,const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[]
00102   ,RTOp_ReductTarget targ_obj
00103   )
00104 {
00105   /* */
00106   /* Declare local variables */
00107   /* */
00108 
00109   /* targ */
00110   struct RTOp_value_index_type
00111     *targ = NULL;
00112   /* global_off */
00113   size_t                 global_offset;
00114   /* sub_dim */
00115   size_t                 sub_dim;
00116   /* v */
00117   const RTOp_value_type  *v_val = NULL;
00118   ptrdiff_t              v_val_s;
00119 
00120   register size_t  k;
00121   RTOp_index_type  i;
00122   RTOp_value_type  abs_v_i;
00123 
00124   /* */
00125   /* Validate the input */
00126   /* */
00127   if( num_vecs != 1 )
00128     return RTOp_ERR_INVALID_NUM_VECS;
00129   if( num_targ_vecs != 0 )
00130     return RTOp_ERR_INVALID_NUM_TARG_VECS;
00131 
00132   /* */
00133   /* Get pointers to the data */
00134   /* */
00135 
00136   /* targ */
00137   targ            = (struct RTOp_value_index_type*)targ_obj;
00138   /* global_off */
00139   global_offset   = vecs[0].global_offset;
00140   /* sub_dim */
00141   sub_dim         = vecs[0].sub_dim;
00142   /* v */
00143   v_val           = vecs[0].values;
00144   v_val_s         = vecs[0].values_stride;
00145 
00146   /* */
00147   /* Perform the reduction operation. */
00148   /* */
00149 
00150   i = global_offset + 1;
00151   for( k = 0; k < sub_dim; ++k, ++i, v_val += v_val_s ) {
00152     abs_v_i = fabs(*v_val);
00153     if( abs_v_i > targ->value || ( abs_v_i == targ->value && i < targ->index ) || targ->index == 0 ) {
00154       targ->value = *v_val;
00155       targ->index = i;
00156     }
00157   }
00158 
00159   return 0; /* success? */
00160 }
00161 
00162 static int reduce_reduct_objs(
00163   const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data /* Can be NULL! */
00164   , RTOp_ReductTarget in_reduct_obj, RTOp_ReductTarget inout_reduct_obj )
00165 {
00166   const struct RTOp_value_index_type
00167     *i_targ = (const struct RTOp_value_index_type*)in_reduct_obj;
00168   struct RTOp_value_index_type
00169     *io_targ = (struct RTOp_value_index_type*)inout_reduct_obj;
00170   RTOp_value_type
00171     i_abs_val  = fabs(i_targ->value),
00172     io_abs_val = fabs(io_targ->value);
00173   if(
00174     ( i_abs_val > io_abs_val )
00175     ||
00176     ( i_abs_val > io_abs_val && i_targ->index < io_targ->index )
00177     )
00178   {
00179     io_targ->value     = i_targ->value;
00180     io_targ->index     = i_targ->index;
00181   }
00182   return 0;
00183 }
00184 
00185 INSERT_GET_REDUCT_OP_FUNCS(
00186   1,1,0,RTOp_value_index_type,reduce_reduct_objs
00187   ,targ_load_state,targ_extract_state
00188   ,external_reduct_op,get_reduct_op)
00189 
00190 const struct RTOp_RTOp_vtbl_t RTOp_ROp_max_abs_ele_vtbl =
00191 {
00192   &RTOp_obj_null_vtbl
00193   ,&RTOp_obj_value_index_vtbl
00194   ,"ROp_max_abs_ele"
00195   ,NULL
00196   ,RTOp_ROp_max_abs_ele_apply_op
00197   ,reduce_reduct_objs
00198   ,get_reduct_op
00199 };
00200 
00201 /* Class specific functions */
00202 
00203 int RTOp_ROp_max_abs_ele_construct( struct RTOp_RTOp* op )
00204 {
00205   op->vtbl     = &RTOp_ROp_max_abs_ele_vtbl;
00206   op->obj_data = NULL;
00207   return 0; /* success? */
00208 }
00209 
00210 int RTOp_ROp_max_abs_ele_destroy( struct RTOp_RTOp* op )
00211 {
00212   op->vtbl     = NULL;
00213   op->obj_data = NULL;
00214   return 0; /* success? */
00215 }
00216 
00217 struct RTOp_value_index_type
00218 RTOp_ROp_max_abs_ele_val(RTOp_ReductTarget targ_obj)
00219 {
00220   return *(struct RTOp_value_index_type*)targ_obj;
00221 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends
Generated on Wed Apr 13 10:09:08 2011 for RTOpPack: Extra C/C++ Code for Vector Reduction/Transformation Operators by  doxygen 1.6.3