MOOCHO (Single Doxygen Collection) Version of the Day
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 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00039 // 
00040 // ***********************************************************************
00041 // @HEADER
00042 */
00043 
00044 #include <math.h>
00045 
00046 #include "RTOp_ROp_max_abs_ele.h"
00047 #include "RTOp_obj_null_vtbl.h"
00048 #include "RTOp_obj_value_vtbl.h"
00049 #include "RTOp_obj_free_free.h"
00050 #include "RTOp_get_reduct_op.hpp"
00051 
00052 /* */
00053 /* Implementation functions */
00054 /* */
00055 
00056 /* Selected functions that are used to implement exteral_reduct_op */
00057 
00058 static int CALL_API targ_extract_state(
00059   const struct RTOp_obj_type_vtbl_t* vtbl
00060   ,const void *       instance_data
00061   ,void *             obj
00062   ,int                num_values
00063   ,RTOp_value_type    value_data[]
00064   ,int                num_indexes
00065   ,RTOp_index_type    index_data[]
00066   ,int                num_chars
00067   ,RTOp_char_type     char_data[]
00068   )
00069 {
00070   struct RTOp_value_index_type* vi_obj;
00071 #ifdef RTOp_DEBUG
00072   assert( obj );
00073   assert( num_values  == 1 );
00074   assert( num_indexes == 1 );
00075   assert( num_chars   == 0 );
00076 #endif
00077   vi_obj = (struct RTOp_value_index_type*)obj;
00078   value_data[0] = vi_obj->value;
00079   index_data[0] = vi_obj->index;
00080   return 0;
00081 }
00082 
00083 static int CALL_API targ_load_state(
00084   const struct RTOp_obj_type_vtbl_t* vtbl
00085   ,const void *            instance_data
00086   ,int                     num_values
00087   ,const RTOp_value_type   value_data[]
00088   ,int                     num_indexes
00089   ,const RTOp_index_type   index_data[]
00090   ,int                     num_chars
00091   ,const RTOp_char_type    char_data[]
00092   ,void **                 obj
00093   )
00094 {
00095   struct RTOp_value_index_type* vi_obj;
00096 #ifdef RTOp_DEBUG
00097   assert( obj );
00098   assert( *obj );
00099   assert( num_values  == 1 );
00100   assert( num_indexes == 1 );
00101   assert( num_chars   == 0 );
00102 #endif
00103   vi_obj = (struct RTOp_value_index_type*)*obj;
00104   vi_obj->value = value_data[0];
00105   vi_obj->index = index_data[0];
00106   return 0;
00107 }
00108 
00109 /* Other functions */
00110 
00111 static int RTOp_ROp_max_abs_ele_apply_op(
00112   const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data
00113   ,const int num_vecs, const struct RTOp_SubVector vecs[]
00114   ,const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[]
00115   ,RTOp_ReductTarget targ_obj
00116   )
00117 {
00118   /* */
00119   /* Declare local variables */
00120   /* */
00121 
00122   /* targ */
00123   struct RTOp_value_index_type
00124     *targ = NULL;
00125   /* global_off */
00126   size_t                 global_offset;
00127   /* sub_dim */
00128   size_t                 sub_dim;
00129   /* v */
00130   const RTOp_value_type  *v_val = NULL;
00131   ptrdiff_t              v_val_s;
00132 
00133   register size_t  k;
00134   RTOp_index_type  i;
00135   RTOp_value_type  abs_v_i;
00136 
00137   /* */
00138   /* Validate the input */
00139   /* */
00140   if( num_vecs != 1 )
00141     return RTOp_ERR_INVALID_NUM_VECS;
00142   if( num_targ_vecs != 0 )
00143     return RTOp_ERR_INVALID_NUM_TARG_VECS;
00144 
00145   /* */
00146   /* Get pointers to the data */
00147   /* */
00148 
00149   /* targ */
00150   targ            = (struct RTOp_value_index_type*)targ_obj;
00151   /* global_off */
00152   global_offset   = vecs[0].global_offset;
00153   /* sub_dim */
00154   sub_dim         = vecs[0].sub_dim;
00155   /* v */
00156   v_val           = vecs[0].values;
00157   v_val_s         = vecs[0].values_stride;
00158 
00159   /* */
00160   /* Perform the reduction operation. */
00161   /* */
00162 
00163   i = global_offset + 1;
00164   for( k = 0; k < sub_dim; ++k, ++i, v_val += v_val_s ) {
00165     abs_v_i = fabs(*v_val);
00166     if( abs_v_i > targ->value || ( abs_v_i == targ->value && i < targ->index ) || targ->index == 0 ) {
00167       targ->value = *v_val;
00168       targ->index = i;
00169     }
00170   }
00171 
00172   return 0; /* success? */
00173 }
00174 
00175 static int reduce_reduct_objs(
00176   const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data /* Can be NULL! */
00177   , RTOp_ReductTarget in_reduct_obj, RTOp_ReductTarget inout_reduct_obj )
00178 {
00179   const struct RTOp_value_index_type
00180     *i_targ = (const struct RTOp_value_index_type*)in_reduct_obj;
00181   struct RTOp_value_index_type
00182     *io_targ = (struct RTOp_value_index_type*)inout_reduct_obj;
00183   RTOp_value_type
00184     i_abs_val  = fabs(i_targ->value),
00185     io_abs_val = fabs(io_targ->value);
00186   if(
00187     ( i_abs_val > io_abs_val )
00188     ||
00189     ( i_abs_val > io_abs_val && i_targ->index < io_targ->index )
00190     )
00191   {
00192     io_targ->value     = i_targ->value;
00193     io_targ->index     = i_targ->index;
00194   }
00195   return 0;
00196 }
00197 
00198 INSERT_GET_REDUCT_OP_FUNCS(
00199   1,1,0,RTOp_value_index_type,reduce_reduct_objs
00200   ,targ_load_state,targ_extract_state
00201   ,external_reduct_op,get_reduct_op)
00202 
00203 const struct RTOp_RTOp_vtbl_t RTOp_ROp_max_abs_ele_vtbl =
00204 {
00205   &RTOp_obj_null_vtbl
00206   ,&RTOp_obj_value_index_vtbl
00207   ,"ROp_max_abs_ele"
00208   ,NULL
00209   ,RTOp_ROp_max_abs_ele_apply_op
00210   ,reduce_reduct_objs
00211   ,get_reduct_op
00212 };
00213 
00214 /* Class specific functions */
00215 
00216 int RTOp_ROp_max_abs_ele_construct( struct RTOp_RTOp* op )
00217 {
00218   op->vtbl     = &RTOp_ROp_max_abs_ele_vtbl;
00219   op->obj_data = NULL;
00220   return 0; /* success? */
00221 }
00222 
00223 int RTOp_ROp_max_abs_ele_destroy( struct RTOp_RTOp* op )
00224 {
00225   op->vtbl     = NULL;
00226   op->obj_data = NULL;
00227   return 0; /* success? */
00228 }
00229 
00230 struct RTOp_value_index_type
00231 RTOp_ROp_max_abs_ele_val(RTOp_ReductTarget targ_obj)
00232 {
00233   return *(struct RTOp_value_index_type*)targ_obj;
00234 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines