RTOp_TOp_random_vector.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 /* */
00032 /* RAB: 1/22/01: Warning, this implementation based */
00033 /* on the struct may not be portable in a heterogeneous */
00034 /* environment!  The user has been warned! */
00035 /* */
00036 
00037 #include <stdlib.h>
00038 
00039 #include "RTOp_TOp_random_vector.h"
00040 #include "RTOp_obj_null_vtbl.h"
00041 #include "RTOp_obj_free_free.h"
00042 
00043 /* Functions for the reduction/transformation object */
00044 
00045 struct RTOp_TOp_random_vector_bnd_t {
00046   RTOp_value_type  l;
00047   RTOp_value_type  u;
00048 };
00049 
00050 static int get_op_type_num_entries(
00051   const struct RTOp_obj_type_vtbl_t*   vtbl
00052   ,const void* obj_data
00053   ,int* num_values
00054   ,int* num_indexes
00055   ,int* num_chars
00056   )
00057 {
00058   *num_values  = 2;
00059   *num_indexes = 0;
00060   *num_chars   = 0;
00061   return 0;
00062 }
00063 
00064 static int op_create(
00065   const struct RTOp_obj_type_vtbl_t* vtbl, const void* instance_data
00066   , RTOp_ReductTarget* obj )
00067 {
00068   *obj = malloc(sizeof(struct RTOp_TOp_random_vector_bnd_t));
00069   return 0;
00070 }
00071 
00072 static int extract_op_state(
00073   const struct RTOp_obj_type_vtbl_t*   vtbl
00074   ,const void *       dummy
00075   ,void *             obj_data
00076   ,int                num_values
00077   ,RTOp_value_type    value_data[]
00078   ,int                num_indexes
00079   ,RTOp_index_type    index_data[]
00080   ,int                num_chars
00081   ,RTOp_char_type     char_data[]
00082   )
00083 {
00084   const struct RTOp_TOp_random_vector_bnd_t *bnd = NULL;
00085   assert(obj_data);
00086   assert( num_values  == 2 );
00087   assert( num_indexes == 0 );
00088   assert( num_chars   == 0 );
00089   bnd = (const struct RTOp_TOp_random_vector_bnd_t*)obj_data;
00090   value_data[0] = bnd->l;
00091   value_data[1] = bnd->u;
00092   return 0;
00093 }
00094 
00095 static int load_op_state(
00096   const struct RTOp_obj_type_vtbl_t*   vtbl
00097   ,const void *            dummy
00098   ,int                     num_values
00099   ,const RTOp_value_type   value_data[]
00100   ,int                     num_indexes
00101   ,const RTOp_index_type   index_data[]
00102   ,int                     num_chars
00103   ,const RTOp_char_type    char_data[]
00104   ,void **                 obj_data
00105   )
00106 {
00107   struct RTOp_TOp_random_vector_bnd_t *bnd = NULL;
00108   assert( obj_data );
00109   assert( num_values  == 2 );
00110   assert( num_indexes == 0 );
00111   assert( num_chars   == 0 );
00112   if(*obj_data == NULL)
00113     *obj_data = malloc(sizeof(struct RTOp_TOp_random_vector_bnd_t));
00114   bnd = (struct RTOp_TOp_random_vector_bnd_t*)*obj_data;
00115   bnd->l = value_data[0];
00116   bnd->u = value_data[1];
00117   return 0;
00118 }
00119 
00120 static struct RTOp_obj_type_vtbl_t  instance_obj_vtbl =
00121 {
00122   get_op_type_num_entries
00123   ,op_create
00124   ,NULL
00125   ,RTOp_obj_free_free
00126   ,extract_op_state
00127   ,load_op_state
00128 };
00129 
00130 /* Implementation functions for RTOp_RTOp */
00131 
00132 static int RTOp_TOp_random_vector_apply_op(
00133   const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data
00134   , const int num_vecs, const struct RTOp_SubVector vecs[]
00135   , const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[]
00136   , RTOp_ReductTarget targ_obj )
00137 {
00138   /* */
00139   /* Get pointers to data */
00140   /* */
00141 
00142   /* lbnd, ubnd */
00143   const struct RTOp_TOp_random_vector_bnd_t *bnd = (const struct RTOp_TOp_random_vector_bnd_t*)obj_data;
00144 
00145   /* z */
00146   RTOp_index_type  z_sub_dim;
00147   RTOp_value_type  *z_val      = NULL;
00148   ptrdiff_t        z_val_s;
00149 
00150   register RTOp_index_type k;
00151 
00152   /* */
00153   /* Validate the input */
00154   /* */
00155   if( num_vecs != 0 || vecs != NULL )
00156     return RTOp_ERR_INVALID_NUM_VECS;
00157   if( num_targ_vecs != 1 || targ_vecs == NULL )
00158     return RTOp_ERR_INVALID_NUM_TARG_VECS;
00159 
00160   /* z */
00161   z_sub_dim     = targ_vecs[0].sub_dim;
00162   z_val         = targ_vecs[0].values;
00163   z_val_s       = targ_vecs[0].values_stride;
00164 
00165   /* */
00166   /* Assign the elements to random values */
00167   /* */
00168 
00169   for( k = 0; k < z_sub_dim; ++k, z_val += z_val_s )
00170     *z_val = bnd->l + ((RTOp_value_type)rand())/RAND_MAX * (bnd->u - bnd->l);
00171 
00172   return 0; /* success? */
00173 }
00174 
00175 /* Virtual function table */
00176 const struct RTOp_RTOp_vtbl_t RTOp_TOp_random_vector_vtbl =
00177 {
00178   &instance_obj_vtbl
00179   ,&RTOp_obj_null_vtbl /* use null type for target object */
00180   ,"TOp_random_vector"
00181   ,NULL /* use default from reduct_vtbl */
00182   ,RTOp_TOp_random_vector_apply_op
00183   ,NULL
00184   ,NULL
00185 };
00186 
00187 /* Class specific functions */
00188 
00189 int RTOp_TOp_random_vector_construct( RTOp_value_type lbnd, RTOp_value_type ubnd
00190   , struct RTOp_RTOp* op )
00191 {
00192   struct RTOp_TOp_random_vector_bnd_t *bnd = NULL;
00193   op->vtbl      = &RTOp_TOp_random_vector_vtbl;
00194   op->vtbl->obj_data_vtbl->obj_create(NULL,NULL,&op->obj_data);
00195   bnd = (struct RTOp_TOp_random_vector_bnd_t*)op->obj_data;
00196   bnd->l = lbnd;
00197   bnd->u = ubnd;
00198   return 0; /* success? */
00199 }
00200 
00201 int RTOp_TOp_random_vector_destroy( struct RTOp_RTOp* op )
00202 {
00203   free( op->obj_data );
00204   op->obj_data  = NULL;
00205   op->vtbl      = NULL;
00206   return 0; /* success? */
00207 }
00208 
00209 int RTOp_TOp_random_vector_set_bounds( RTOp_value_type lbnd, RTOp_value_type ubnd
00210   , struct RTOp_RTOp* op )
00211 {
00212   struct RTOp_TOp_random_vector_bnd_t *bnd = (struct RTOp_TOp_random_vector_bnd_t*)op->obj_data;
00213   bnd->l = lbnd;
00214   bnd->u = ubnd;
00215   return 0; /* success? */
00216 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Wed Apr 13 10:11:01 2011 for MOOCHO (Single Doxygen Collection) by  doxygen 1.6.3