|
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_ROp_num_nonzeros.h" 00032 #include "RTOp_obj_null_vtbl.h" 00033 #include "RTOp_obj_value_vtbl.h" 00034 #include "RTOp_reduct_sum_value.h" 00035 00036 /* Note that the reduction quantity that we are accumulating (num_bounded) */ 00037 /* is an integral type and really should be delcared as RTOp_index_type. */ 00038 /* However, the machinary is already there for accumulating an RTOp_value_type */ 00039 /* reduction object so this implementation is just lazy and uses a double */ 00040 /* for an integer. This should not slow things down very much and does */ 00041 /* not really waste any memory. */ 00042 00043 /* Implementation functions */ 00044 00045 static int RTOp_ROp_num_nonzeros_apply_op( 00046 const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data 00047 , const int num_vecs, const struct RTOp_SubVector vecs[] 00048 , const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[] 00049 , RTOp_ReductTarget targ_obj ) 00050 { 00051 RTOp_index_type v0_sub_dim; 00052 const RTOp_value_type *v0_val; 00053 ptrdiff_t v0_val_s; 00054 register RTOp_index_type k; 00055 RTOp_index_type nz = 0; 00056 00057 /* */ 00058 /* Validate the input */ 00059 /* */ 00060 if( num_vecs != 1 ) 00061 return RTOp_ERR_INVALID_NUM_VECS; 00062 assert( vecs ); 00063 if( num_targ_vecs != 0 ) 00064 return RTOp_ERR_INVALID_NUM_TARG_VECS; 00065 00066 /* */ 00067 /* Get pointers to data */ 00068 /* */ 00069 00070 /* v0 */ 00071 v0_sub_dim = vecs[0].sub_dim; 00072 v0_val = vecs[0].values; 00073 v0_val_s = vecs[0].values_stride; 00074 00075 /* */ 00076 /* Count the number of nonzeros */ 00077 /* */ 00078 00079 for( k = 0; k < v0_sub_dim; ++k, v0_val += v0_val_s ) 00080 if(*v0_val != 0.0) 00081 ++nz; 00082 00083 /* */ 00084 /* Add this to the result */ 00085 /* */ 00086 *((RTOp_value_type*)targ_obj) += nz; 00087 00088 return 0; /* success? */ 00089 } 00090 00091 /* Virtual function table */ 00092 const struct RTOp_RTOp_vtbl_t RTOp_ROp_num_nonzeros_vtbl = 00093 { 00094 &RTOp_obj_null_vtbl /* use null type for instance data */ 00095 ,&RTOp_obj_value_vtbl /* use simple scalar type for target object */ 00096 ,"ROp_num_nonzeros" 00097 ,NULL 00098 ,RTOp_ROp_num_nonzeros_apply_op 00099 ,RTOp_reduct_sum_value 00100 ,RTOp_get_reduct_sum_value_op 00101 }; 00102 00103 /* Class specific functions */ 00104 00105 int RTOp_ROp_num_nonzeros_construct( struct RTOp_RTOp* op ) 00106 { 00107 op->obj_data = NULL; 00108 op->vtbl = &RTOp_ROp_num_nonzeros_vtbl; 00109 return 0; 00110 } 00111 00112 int RTOp_ROp_num_nonzeros_destroy( struct RTOp_RTOp* op ) 00113 { 00114 op->obj_data = NULL; 00115 op->vtbl = NULL; 00116 return 0; 00117 } 00118 00119 RTOp_index_type RTOp_ROp_num_nonzeros_val(RTOp_ReductTarget targ_obj) 00120 { 00121 return (RTOp_index_type)*((RTOp_value_type*)targ_obj); 00122 }
1.7.4