MOOCHO (Single Doxygen Collection) Version of the Day
RTOp_TOp_force_in_bounds.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 "RTOp_TOp_force_in_bounds.h"
00045 #include "RTOp_obj_null_vtbl.h"
00046 #include "RTOp_obj_value_value_vtbl.h"  /* vtbl for operator object instance data */
00047 
00048 #define max(a,b) ( (a) > (b) ? (a) : (b) )
00049 #define min(a,b) ( (a) < (b) ? (a) : (b) )
00050 
00051 /* Implementation functions for RTOp_RTOp TOp_force_in_bounds */
00052 
00053 static int RTOp_TOp_force_in_bounds_apply_op(
00054   const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data
00055   , const int num_vecs, const struct RTOp_SubVector vecs[]
00056   , const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[]
00057   , RTOp_ReductTarget targ_obj )
00058 {
00059   RTOp_index_type        sub_dim;
00060   RTOp_value_type        *x_val;
00061   ptrdiff_t              x_val_s;
00062   const RTOp_value_type  *xl_val;
00063   ptrdiff_t              xl_val_s;
00064   const RTOp_value_type  *xu_val;
00065   ptrdiff_t              xu_val_s;
00066   register RTOp_index_type k;
00067 
00068   /* */
00069   /* Validate the input */
00070   /* */
00071   if( num_vecs != 2 || vecs == NULL )
00072     return RTOp_ERR_INVALID_NUM_VECS;
00073   if( num_targ_vecs != 1 || targ_vecs == NULL )
00074     return RTOp_ERR_INVALID_NUM_TARG_VECS;
00075   if( targ_vecs[0].sub_dim != vecs[0].sub_dim
00076     || targ_vecs[0].sub_dim != vecs[1].sub_dim )
00077     return RTOp_ERR_INCOMPATIBLE_VECS;
00078 
00079   /* */
00080   /* Get pointers to data */
00081   /* */
00082 
00083   /* x */
00084   sub_dim       = targ_vecs[0].sub_dim;
00085   x_val         = targ_vecs[0].values;
00086   x_val_s       = targ_vecs[0].values_stride;
00087   /* xl */
00088   xl_val         = vecs[0].values;
00089   xl_val_s       = vecs[0].values_stride;
00090   /* xu */
00091   xu_val         = vecs[1].values;
00092   xu_val_s       = vecs[1].values_stride;
00093 
00094   /* */
00095   /* Force in bounds */
00096   /* */
00097 
00098   if( x_val_s == 1 && xl_val_s == 1 && xu_val_s == 1 ) {
00099     /* Slightly faster loop for unit stride vectors */
00100     for( k = 0; k < sub_dim; ++k, ++x_val, ++xl_val, ++xu_val ) {
00101       if( *x_val < *xl_val )
00102         *x_val = *xl_val;
00103       else if( *x_val > *xu_val )
00104         *x_val = *xu_val;
00105     }
00106   }
00107   else {
00108     /* More general implementation for non-unit strides */
00109     for( k = 0; k < sub_dim; ++k, x_val+=x_val_s, xl_val+=xl_val_s, xu_val+=xu_val_s ) {
00110       if( *x_val < *xl_val )
00111         *x_val = *xl_val;
00112       else if( *x_val > *xu_val )
00113         *x_val = *xu_val;
00114     }
00115   }
00116 
00117   return 0; /* success? */
00118 }
00119 
00120 /* Virtual function table */
00121 const struct RTOp_RTOp_vtbl_t RTOp_TOp_force_in_bounds_vtbl =
00122 {
00123   &RTOp_obj_null_vtbl  /* Use a null object for instance data */
00124   ,&RTOp_obj_null_vtbl /* use null type for target object */
00125   ,"TOp_force_in_bounds"
00126   ,NULL /* use default from reduct_vtbl */
00127   ,RTOp_TOp_force_in_bounds_apply_op
00128   ,NULL
00129   ,NULL
00130 };
00131 
00132 /* Class specific functions */
00133 
00134 int RTOp_TOp_force_in_bounds_construct( struct RTOp_RTOp* op )
00135 {
00136   op->obj_data  = NULL;
00137   op->vtbl      = &RTOp_TOp_force_in_bounds_vtbl;
00138   return 0; /* success? */
00139 }
00140 
00141 int RTOp_TOp_force_in_bounds_destroy( struct RTOp_RTOp* op )
00142 {
00143   op->obj_data  = NULL;
00144   op->vtbl      = NULL;
00145   return 0; /* success? */
00146 }
00147 
00148 /* Implementation functions for RTOp_RTOp for TOp_force_in_bounds_buffer */
00149 
00150 static int RTOp_TOp_force_in_bounds_buffer_apply_op(
00151   const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data
00152   , const int num_vecs, const struct RTOp_SubVector vecs[]
00153   , const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[]
00154   , RTOp_ReductTarget reduct_obj )
00155 {
00156   /* */
00157   /* Declare local variables */
00158   /* */
00159 
00160   /* Access to the operator object instance data */
00161   struct RTOp_value_value_type *data_cntr = (struct RTOp_value_value_type*)obj_data;
00162   RTOp_value_type *rel_push = &data_cntr->value1;
00163   RTOp_value_type *abs_push = &data_cntr->value2;
00164   /* Vector data */
00165   RTOp_index_type           sub_dim;
00166   /* z0 */
00167   RTOp_value_type           *z0_val;
00168   ptrdiff_t                 z0_val_s;
00169   /* v0 */
00170   const RTOp_value_type     *v0_val;
00171   ptrdiff_t                 v0_val_s;
00172   /* v1 */
00173   const RTOp_value_type     *v1_val;
00174   ptrdiff_t                 v1_val_s;
00175 
00176   /* Automatic temporary variables */
00177   register RTOp_index_type  k;
00178   /* User defined temporary variables */
00179   RTOp_value_type xl_sb;
00180   RTOp_value_type xu_sb;
00181 
00182   /* */
00183   /* Validate the input */
00184   /* */
00185   if( num_vecs != 2 || ( num_vecs && vecs == NULL ) )
00186     return RTOp_ERR_INVALID_NUM_VECS;
00187   if( num_targ_vecs != 1 || ( num_targ_vecs && targ_vecs == NULL ) )
00188     return RTOp_ERR_INVALID_NUM_TARG_VECS;
00189   if( /* Validate sub_dim */
00190     vecs[1].sub_dim != vecs[0].sub_dim
00191     || targ_vecs[0].sub_dim != vecs[0].sub_dim
00192     )
00193     return RTOp_ERR_INCOMPATIBLE_VECS;
00194   assert(obj_data);
00195 
00196 
00197   /* */
00198   /* Get pointers to data */
00199   /* */
00200   sub_dim       = vecs[0].sub_dim;
00201   /* z0 */
00202   z0_val        = targ_vecs[0].values;
00203   z0_val_s      = targ_vecs[0].values_stride;
00204   /* v0 */
00205   v0_val        = vecs[0].values;
00206   v0_val_s      = vecs[0].values_stride;
00207   /* v1 */
00208   v1_val        = vecs[1].values;
00209   v1_val_s      = vecs[1].values_stride;
00210 
00211 
00212   /* */
00213   /* Apply the operator: */
00214   /* */
00215   for( k = 0; k < sub_dim; ++k, v0_val += v0_val_s, v1_val += v1_val_s, z0_val += z0_val_s )
00216   {
00217     /* Element-wise transformation */
00218     xl_sb = min((*v0_val) + (*rel_push)*((*v1_val)-(*v0_val)), (*v0_val) + (*abs_push));
00219     xu_sb = max((*v1_val) - (*rel_push)*((*v1_val)-(*v0_val)), (*v1_val) - (*abs_push));
00220     if (xl_sb >= xu_sb)
00221         {
00222         (*z0_val) = (*v0_val) + ((*v1_val)-(*v0_val))/2.0;
00223         }
00224     else if ((*z0_val) < xl_sb)
00225         { (*z0_val) = xl_sb; }
00226     else if ((*z0_val) > xu_sb)
00227         { (*z0_val) = xu_sb; }
00228     /* Otherwise, leave it */
00229   }
00230 
00231   return 0; /* success? */
00232 }
00233 
00234 /* Virtual function table */
00235 const struct RTOp_RTOp_vtbl_t RTOp_TOp_force_in_bounds_buffer_vtbl =
00236 {
00237   &RTOp_obj_value_value_vtbl
00238   ,&RTOp_obj_null_vtbl
00239   ,"TOp_force_in_bounds_buffer"
00240   ,NULL
00241   ,RTOp_TOp_force_in_bounds_buffer_apply_op
00242   ,NULL
00243   ,NULL
00244 };
00245 
00246 /* Class specific functions */
00247 
00248 int RTOp_TOp_force_in_bounds_buffer_construct( RTOp_value_type rel_push, RTOp_value_type abs_push,  struct RTOp_RTOp* op )
00249 {
00250 #ifdef RTOp_DEBUG
00251   assert(op);
00252 #endif
00253   op->obj_data  = NULL;
00254   op->vtbl      = &RTOp_TOp_force_in_bounds_buffer_vtbl;
00255   op->vtbl->obj_data_vtbl->obj_create(NULL,NULL,&op->obj_data);
00256   return RTOp_TOp_force_in_bounds_buffer_init(rel_push,abs_push,op);
00257 }
00258 
00259 int RTOp_TOp_force_in_bounds_buffer_destroy( struct RTOp_RTOp* op )
00260 {
00261   op->vtbl->obj_data_vtbl->obj_free(NULL,NULL,&op->obj_data);
00262   op->obj_data  = NULL;
00263   op->vtbl      = NULL;
00264   return 0;
00265 }
00266 
00267 int RTOp_TOp_force_in_bounds_buffer_init( RTOp_value_type rel_push, RTOp_value_type abs_push, struct RTOp_RTOp* op )
00268 {
00269   struct RTOp_value_value_type *ptr_data_cntr = (struct RTOp_value_value_type*)op->obj_data;
00270   RTOp_value_type *ptr_rel_push = &ptr_data_cntr->value1;
00271   RTOp_value_type *ptr_abs_push = &ptr_data_cntr->value2;
00272   *ptr_rel_push = rel_push;
00273   *ptr_abs_push = abs_push;
00274   return 0;
00275 }
00276 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines