RTOp_ROp_num_bounded.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 "RTOp_ROp_num_bounded.h"
00032 #include "RTOp_obj_value_vtbl.h"
00033 #include "RTOp_reduct_sum_value.h"
00034 
00035 /* Note that the reduction quantity that we are accumulating (num_bounded) */
00036 /* is an integral type and really should be delcared as RTOp_index_type. */
00037 /* However, the machinary is already there for accumulating an RTOp_value_type */
00038 /* reduction object so this implementation is just lazy and uses a double */
00039 /* for an integer.  This should not slow things down very much and does */
00040 /* not really waste any memory. */
00041 
00042 /* Implementation functions */
00043 
00044 static int RTOp_ROp_num_bounded_apply_op(
00045   const struct RTOp_RTOp_vtbl_t* vtbl,  const void* obj_data
00046   , const int num_vecs, const struct RTOp_SubVector vecs[]
00047   , const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[]
00048   , RTOp_ReductTarget targ_obj )
00049 {
00050   RTOp_value_type        inf_bnd;
00051   RTOp_index_type        sub_dim;
00052   const RTOp_value_type  *xl_val;
00053   ptrdiff_t              xl_val_s;
00054   const RTOp_value_type  *xu_val;
00055   ptrdiff_t              xu_val_s;
00056   RTOp_index_type        num_bounded = 0;
00057   register RTOp_index_type k;
00058 
00059   /* */
00060   /* Validate the input */
00061   /* */
00062   if( num_vecs != 2 )
00063     return RTOp_ERR_INVALID_NUM_VECS;
00064   assert( vecs );
00065   if( num_targ_vecs != 0 )
00066     return RTOp_ERR_INVALID_NUM_TARG_VECS;
00067   if( vecs[0].sub_dim != vecs[1].sub_dim )                 /* Same sizes */
00068     return RTOp_ERR_INCOMPATIBLE_VECS;
00069 
00070   /* */
00071   /* Get pointers to data */
00072   /* */
00073 
00074   /* inf_bnd */
00075   inf_bnd = *((RTOp_value_type*)obj_data);
00076   /* sub_dim */
00077   sub_dim        = vecs[0].sub_dim;
00078   /* xl */
00079   xl_val         = vecs[0].values;
00080   xl_val_s       = vecs[0].values_stride;
00081   /* xl */
00082   xu_val         = vecs[1].values;
00083   xu_val_s       = vecs[1].values_stride;
00084 
00085   /* */
00086   /* Count the number of bounded variables */
00087   /* */
00088   for( k = 0; k < sub_dim; ++k, xl_val += xl_val_s, xu_val += xu_val_s ) {
00089     if( *xl_val > -inf_bnd || *xu_val < +inf_bnd )
00090       ++num_bounded;
00091   }
00092 
00093   /* */
00094   /* Add this to the result */
00095   /* */
00096   *((RTOp_value_type*)targ_obj) += num_bounded;
00097 
00098   return 0; /* success? */
00099 }
00100 
00101 /* Virtual function table pointer */
00102 const struct RTOp_RTOp_vtbl_t RTOp_ROp_num_bounded_vtbl =
00103 {
00104   &RTOp_obj_value_vtbl  /* use simple scalar value type for object instance data */
00105   ,&RTOp_obj_value_vtbl /* use simple scalar value type for target object */
00106   ,"ROp_num_bounded"
00107   ,NULL
00108   ,RTOp_ROp_num_bounded_apply_op
00109   ,RTOp_reduct_sum_value
00110   ,RTOp_get_reduct_sum_value_op
00111 };
00112 
00113 /* Class specific functions */
00114 
00115 int RTOp_ROp_num_bounded_construct( RTOp_value_type inf_bnd, struct RTOp_RTOp* op )
00116 {
00117   op->vtbl = &RTOp_ROp_num_bounded_vtbl;
00118   op->vtbl->obj_data_vtbl->obj_create( NULL, NULL, &op->obj_data );
00119   *((RTOp_value_type*)op->obj_data) = inf_bnd;
00120   return 0; /* success? */
00121 }
00122 
00123 int RTOp_ROp_num_bounded_destroy( struct RTOp_RTOp* op )
00124 {
00125   op->vtbl->obj_data_vtbl->obj_free(NULL,NULL,&op->obj_data);
00126   op->vtbl      = NULL;
00127   return 0; /* success? */
00128 }
00129 
00130 int RTOp_ROp_num_bounded_set_inf_bnd( RTOp_value_type inf_bnd, struct RTOp_RTOp* op )
00131 {
00132   *((RTOp_value_type*)op->obj_data) = inf_bnd;
00133   return 0; /* success? */
00134 }
00135 
00136 RTOp_index_type RTOp_ROp_num_bounded_val(RTOp_ReductTarget targ_obj)
00137 {
00138   return (RTOp_index_type)*((RTOp_value_type*)targ_obj);
00139 }

Generated on Tue Jul 13 09:28:39 2010 for RTOpPack: Extra C/C++ Code for Vector Reduction/Transformation Operators by  doxygen 1.4.7