|
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 #include <math.h> 00045 00046 #include "RTOp_ROp_norms.h" 00047 #include "RTOp_obj_null_vtbl.h" 00048 #include "RTOp_obj_value_vtbl.h" 00049 #include "RTOp_reduct_sum_value.h" 00050 #include "RTOp_reduct_max_value.h" 00051 00052 #define MY_MAX(a,b) a > b ? a : b 00053 00054 /* One norm reduction operator class. */ 00055 00056 static int RTOp_ROp_norms_apply_op_norm_1( 00057 const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data 00058 , const int num_vecs, const struct RTOp_SubVector vecs[] 00059 , const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[] 00060 , RTOp_ReductTarget targ_obj ) 00061 { 00062 RTOp_index_type sub_dim; 00063 const RTOp_value_type *v0_val; 00064 ptrdiff_t v0_val_s; 00065 register RTOp_index_type k; 00066 RTOp_value_type *norm = NULL; 00067 00068 /* */ 00069 /* Validate the input */ 00070 /* */ 00071 if( num_vecs != 1 ) 00072 return RTOp_ERR_INVALID_NUM_VECS; 00073 if( num_targ_vecs != 0 ) 00074 return RTOp_ERR_INVALID_NUM_TARG_VECS; 00075 assert(targ_obj); 00076 assert(vecs); 00077 00078 /* */ 00079 /* Get pointers to data */ 00080 /* */ 00081 00082 /* v0 */ 00083 sub_dim = vecs[0].sub_dim; 00084 v0_val = vecs[0].values; 00085 v0_val_s = vecs[0].values_stride; 00086 00087 /* */ 00088 /* Perform the reduction */ 00089 /* */ 00090 norm = (RTOp_value_type*)targ_obj; 00091 for( k = 0; k < sub_dim; ++k, v0_val += v0_val_s ) { 00092 *norm += fabs(*v0_val); /* ||v[0]||_1 */ 00093 } 00094 00095 return 0; /* success? */ 00096 } 00097 00098 const struct RTOp_RTOp_vtbl_t RTOp_ROp_norm_1_vtbl = 00099 { 00100 &RTOp_obj_null_vtbl /* use null type for instance data */ 00101 ,&RTOp_obj_value_vtbl /* use simple scalar type for target object */ 00102 ,"RTOp_ROp_norm_1" 00103 ,NULL 00104 ,RTOp_ROp_norms_apply_op_norm_1 00105 ,RTOp_reduct_sum_value 00106 ,RTOp_get_reduct_sum_value_op 00107 }; 00108 00109 int RTOp_ROp_norm_1_construct( struct RTOp_RTOp* op ) 00110 { 00111 op->vtbl = &RTOp_ROp_norm_1_vtbl; 00112 op->obj_data = NULL; 00113 return 0; 00114 } 00115 00116 RTOp_value_type RTOp_ROp_norm_1_val(RTOp_ReductTarget targ_obj) 00117 { 00118 #ifdef RTOp_DEBUG 00119 assert(targ_obj != RTOp_REDUCT_OBJ_NULL ); 00120 #endif 00121 return *(RTOp_value_type*)targ_obj; 00122 } 00123 00124 /* Two norm reduction operator class. */ 00125 00126 static int RTOp_ROp_norms_apply_op_norm_2( 00127 const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data 00128 , const int num_vecs, const struct RTOp_SubVector vecs[] 00129 , const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[] 00130 , RTOp_ReductTarget targ_obj ) 00131 { 00132 RTOp_index_type sub_dim; 00133 const RTOp_value_type *v0_val; 00134 ptrdiff_t v0_val_s; 00135 register RTOp_index_type k; 00136 RTOp_value_type *norm = NULL; 00137 00138 /* */ 00139 /* Validate the input */ 00140 /* */ 00141 if( num_vecs != 1 ) 00142 return RTOp_ERR_INVALID_NUM_VECS; 00143 if( num_targ_vecs != 0 ) 00144 return RTOp_ERR_INVALID_NUM_TARG_VECS; 00145 assert(targ_obj); 00146 assert(vecs); 00147 00148 /* */ 00149 /* Get pointers to data */ 00150 /* */ 00151 00152 /* v0 */ 00153 sub_dim = vecs[0].sub_dim; 00154 v0_val = vecs[0].values; 00155 v0_val_s = vecs[0].values_stride; 00156 00157 /* */ 00158 /* Perform the reduction */ 00159 /* */ 00160 norm = (RTOp_value_type*)targ_obj; 00161 for( k = 0; k < sub_dim; ++k, v0_val += v0_val_s ) 00162 *norm += (*v0_val)*(*v0_val); /* (||v[0]||_2)^2 */ 00163 00164 return 0; /* success? */ 00165 } 00166 00167 const struct RTOp_RTOp_vtbl_t RTOp_ROp_norm_2_vtbl = 00168 { 00169 &RTOp_obj_null_vtbl /* use null type for instance data */ 00170 ,&RTOp_obj_value_vtbl /* use simple scalar type for target object */ 00171 ,"RTOp_ROp_norm_2" 00172 ,NULL 00173 ,RTOp_ROp_norms_apply_op_norm_2 00174 ,RTOp_reduct_sum_value 00175 ,RTOp_get_reduct_sum_value_op 00176 }; 00177 00178 int RTOp_ROp_norm_2_construct( struct RTOp_RTOp* op ) 00179 { 00180 op->vtbl = &RTOp_ROp_norm_2_vtbl; 00181 op->obj_data = NULL; 00182 return 0; 00183 } 00184 00185 RTOp_value_type RTOp_ROp_norm_2_val(RTOp_ReductTarget targ_obj) 00186 { 00187 #ifdef RTOp_DEBUG 00188 assert(targ_obj != RTOp_REDUCT_OBJ_NULL ); 00189 #endif 00190 return sqrt(*(RTOp_value_type*)targ_obj); 00191 } 00192 00193 /* Infinity norm reduction operator class. */ 00194 00195 static int RTOp_ROp_norms_apply_op_norm_inf( 00196 const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data 00197 , const int num_vecs, const struct RTOp_SubVector vecs[] 00198 , const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[] 00199 , RTOp_ReductTarget targ_obj ) 00200 { 00201 RTOp_index_type sub_dim; 00202 const RTOp_value_type *v0_val; 00203 ptrdiff_t v0_val_s; 00204 register RTOp_index_type k; 00205 RTOp_value_type *norm = NULL; 00206 00207 /* */ 00208 /* Validate the input */ 00209 /* */ 00210 if( num_vecs != 1 ) 00211 return RTOp_ERR_INVALID_NUM_VECS; 00212 if( num_targ_vecs != 0 ) 00213 return RTOp_ERR_INVALID_NUM_TARG_VECS; 00214 assert(targ_obj); 00215 assert(vecs); 00216 00217 /* */ 00218 /* Get pointers to data */ 00219 /* */ 00220 00221 /* v0 */ 00222 sub_dim = vecs[0].sub_dim; 00223 v0_val = vecs[0].values; 00224 v0_val_s = vecs[0].values_stride; 00225 00226 /* */ 00227 /* Perform the reduction */ 00228 /* */ 00229 norm = (RTOp_value_type*)targ_obj; 00230 for( k = 0; k < sub_dim; ++k, v0_val += v0_val_s ) 00231 *norm = MY_MAX( fabs(*v0_val), (*(RTOp_value_type*)targ_obj) ); /* ||v[0]||_inf */ 00232 00233 return 0; /* success? */ 00234 } 00235 00236 const struct RTOp_RTOp_vtbl_t RTOp_ROp_norm_inf_vtbl = 00237 { 00238 &RTOp_obj_null_vtbl /* use null type for instance data */ 00239 ,&RTOp_obj_value_vtbl /* use simple scalar type for target object */ 00240 ,"RTOp_ROp_norm_inf" 00241 ,NULL 00242 ,RTOp_ROp_norms_apply_op_norm_inf 00243 ,RTOp_reduct_max_value 00244 ,RTOp_get_reduct_max_value_op 00245 }; 00246 00247 int RTOp_ROp_norm_inf_construct( struct RTOp_RTOp* op ) 00248 { 00249 op->vtbl = &RTOp_ROp_norm_inf_vtbl; 00250 op->obj_data = NULL; 00251 return 0; 00252 } 00253 00254 RTOp_value_type RTOp_ROp_norm_inf_val(RTOp_ReductTarget targ_obj) 00255 { 00256 #ifdef RTOp_DEBUG 00257 assert(targ_obj != RTOp_REDUCT_OBJ_NULL ); 00258 #endif 00259 return *(RTOp_value_type*)targ_obj; 00260 } 00261 00262 /* Common functions */ 00263 00264 int RTOp_ROp_norm_destroy( struct RTOp_RTOp* op ) 00265 { 00266 op->vtbl = NULL; 00267 return 0; 00268 }
1.7.6.1