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