|
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 // 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_TOp_ele_wise_divide.h" 00032 #include "RTOp_obj_null_vtbl.h" 00033 #include "RTOp_obj_value_vtbl.h" 00034 00035 /* Implementation functions for RTOp_RTOp */ 00036 00037 static int RTOp_TOp_ele_wise_divide_apply_op( 00038 const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data 00039 , const int num_vecs, const struct RTOp_SubVector vecs[] 00040 , const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[] 00041 , RTOp_ReductTarget targ_obj ) 00042 { 00043 RTOp_value_type alpha; 00044 RTOp_index_type sub_dim; 00045 RTOp_value_type *z_val; 00046 ptrdiff_t z_val_s; 00047 const RTOp_value_type *v0_val; 00048 ptrdiff_t v0_val_s; 00049 const RTOp_value_type *v1_val; 00050 ptrdiff_t v1_val_s; 00051 register RTOp_index_type k; 00052 00053 /* */ 00054 /* Validate the input */ 00055 /* */ 00056 if( num_vecs != 2 || vecs == NULL ) 00057 return RTOp_ERR_INVALID_NUM_VECS; 00058 if( num_targ_vecs != 1 || targ_vecs == NULL ) 00059 return RTOp_ERR_INVALID_NUM_TARG_VECS; 00060 if( targ_vecs[0].sub_dim != vecs[0].sub_dim 00061 || targ_vecs[0].sub_dim != vecs[1].sub_dim ) 00062 return RTOp_ERR_INCOMPATIBLE_VECS; 00063 assert(obj_data); 00064 00065 00066 /* */ 00067 /* Get pointers to data */ 00068 /* */ 00069 00070 /* alpha */ 00071 alpha = *(RTOp_value_type*)obj_data; 00072 /* z */ 00073 sub_dim = targ_vecs[0].sub_dim; 00074 z_val = targ_vecs[0].values; 00075 z_val_s = targ_vecs[0].values_stride; 00076 /* v0 */ 00077 v0_val = vecs[0].values; 00078 v0_val_s = vecs[0].values_stride; 00079 /* v1 */ 00080 v1_val = vecs[1].values; 00081 v1_val_s = vecs[1].values_stride; 00082 00083 /* */ 00084 /* Force in bounds */ 00085 /* */ 00086 00087 if( z_val_s == 1 && v0_val_s == 1 && v1_val_s == 1 ) { 00088 /* Slightly faster loop for unit stride vectors */ 00089 for( k = 0; k < sub_dim; ++k ) 00090 *z_val++ = alpha*(*v0_val++)/(*v1_val++); 00091 } 00092 else { 00093 /* More general implementation for non-unit strides */ 00094 for( k = 0; k < sub_dim; ++k, z_val+=z_val_s, v0_val+=v0_val_s, v1_val+=v1_val_s ) 00095 *z_val = alpha*(*v0_val)/(*v1_val); 00096 } 00097 00098 return 0; /* success? */ 00099 } 00100 00101 /* Virtual function table */ 00102 const struct RTOp_RTOp_vtbl_t RTOp_TOp_ele_wise_divide_vtbl = 00103 { 00104 &RTOp_obj_value_vtbl /* alpha */ 00105 ,&RTOp_obj_null_vtbl /* use null type for target object */ 00106 ,"TOp_ele_wise_divide" 00107 ,NULL /* use default from reduct_vtbl */ 00108 ,RTOp_TOp_ele_wise_divide_apply_op 00109 ,NULL 00110 ,NULL 00111 }; 00112 00113 /* Class specific functions */ 00114 00115 int RTOp_TOp_ele_wise_divide_construct( RTOp_value_type alpha, struct RTOp_RTOp* op ) 00116 { 00117 #ifdef RTOp_DEBUG 00118 assert(op); 00119 #endif 00120 op->obj_data = NULL; 00121 op->vtbl = &RTOp_TOp_ele_wise_divide_vtbl; 00122 op->vtbl->obj_data_vtbl->obj_create(NULL,NULL,&op->obj_data); 00123 return RTOp_TOp_ele_wise_divide_set_alpha(alpha,op); 00124 } 00125 00126 int RTOp_TOp_ele_wise_divide_set_alpha( RTOp_value_type alpha, struct RTOp_RTOp* op ) 00127 { 00128 #ifdef RTOp_DEBUG 00129 assert(op); 00130 assert(op->obj_data); 00131 #endif 00132 *(RTOp_value_type*)op->obj_data = alpha; 00133 return 0; 00134 } 00135 00136 int RTOp_TOp_ele_wise_divide_destroy( struct RTOp_RTOp* op ) 00137 { 00138 op->obj_data = NULL; 00139 op->vtbl = NULL; 00140 return 0; /* success? */ 00141 }
1.7.4