RTOp_ROp_max_abs_ele.c

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

Generated on Thu Sep 18 12:35:19 2008 for MOOCHO (Single Doxygen Collection) by doxygen 1.3.9.1