|
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_find_nan_inf.h" 00032 #include "RTOp_obj_null_vtbl.h" 00033 #include "RTOp_obj_value_index_vtbl.h" 00034 #include "check_nan_inf.h" 00035 #include "RTOp_get_reduct_op.hpp" 00036 00037 /* Implementation functions */ 00038 00039 static int RTOp_ROp_find_nan_inf_apply_op( 00040 const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data 00041 , const int num_vecs, const struct RTOp_SubVector vecs[] 00042 , const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[] 00043 , RTOp_ReductTarget targ_obj ) 00044 { 00045 struct RTOp_value_index_type 00046 *targ; 00047 RTOp_index_type global_offset; 00048 RTOp_index_type sub_dim; 00049 const RTOp_value_type *v0_val; 00050 ptrdiff_t v0_val_s; 00051 RTOp_index_type i; 00052 00053 /* */ 00054 /* Validate the input */ 00055 /* */ 00056 if( num_vecs != 1 ) 00057 return RTOp_ERR_INVALID_NUM_VECS; 00058 if( num_targ_vecs != 0 ) 00059 return RTOp_ERR_INVALID_NUM_TARG_VECS; 00060 assert(targ_obj); 00061 assert(vecs); 00062 00063 /* */ 00064 /* Get pointers to data */ 00065 /* */ 00066 00067 /* {value,index} */ 00068 targ = (struct RTOp_value_index_type*)targ_obj; 00069 00070 /* v0 */ 00071 global_offset = vecs[0].global_offset; 00072 sub_dim = vecs[0].sub_dim; 00073 v0_val = vecs[0].values; 00074 v0_val_s = vecs[0].values_stride; 00075 00076 /* */ 00077 /* Find a NaN or Inf element! */ 00078 /* */ 00079 00080 for( i = global_offset + 1; i <= global_offset + sub_dim; ++i, v0_val += v0_val_s ) { 00081 if( RTOp_is_nan_inf(*v0_val) && ( targ->index == 0 || i < targ->index ) ) { 00082 targ->value = *v0_val; 00083 targ->index = i; 00084 break; 00085 } 00086 } 00087 00088 return 0; /* success? */ 00089 } 00090 00091 static int reduce_reduct_objs( 00092 const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data /* Can be NULL! */ 00093 , RTOp_ReductTarget in_reduct_obj, RTOp_ReductTarget inout_reduct_obj ) 00094 { 00095 const struct RTOp_value_index_type 00096 *i_targ = (const struct RTOp_value_index_type*)in_reduct_obj; 00097 struct RTOp_value_index_type 00098 *io_targ = (struct RTOp_value_index_type*)inout_reduct_obj; 00099 if( io_targ->index == 0 || ( i_targ->index != 0 && i_targ->index < io_targ->index ) ) { 00100 io_targ->index = i_targ->index; 00101 io_targ->value = i_targ->value; 00102 } 00103 return 0; 00104 } 00105 00106 INSERT_GET_REDUCT_OP_FUNCS( 00107 1,1,0,RTOp_value_index_type,reduce_reduct_objs 00108 ,RTOp_obj_value_index_vtbl.load_state 00109 ,RTOp_obj_value_index_vtbl.extract_state 00110 ,external_reduct_op,get_reduct_op) 00111 00112 /* Virtual function table */ 00113 const struct RTOp_RTOp_vtbl_t RTOp_ROp_find_nan_inf_vtbl = 00114 { 00115 &RTOp_obj_null_vtbl /* use null type for instance data */ 00116 ,&RTOp_obj_value_index_vtbl /* use {value,index} type for target object */ 00117 ,"ROp_find_nan_inf" 00118 ,NULL 00119 ,RTOp_ROp_find_nan_inf_apply_op 00120 ,reduce_reduct_objs 00121 ,get_reduct_op 00122 }; 00123 00124 /* Class specific functions */ 00125 00126 int RTOp_ROp_find_nan_inf_construct( struct RTOp_RTOp* op ) 00127 { 00128 op->obj_data = NULL; 00129 op->vtbl = &RTOp_ROp_find_nan_inf_vtbl; 00130 return 0; 00131 } 00132 00133 int RTOp_ROp_find_nan_inf_destroy( struct RTOp_RTOp* op ) 00134 { 00135 op->obj_data = NULL; 00136 op->vtbl = NULL; 00137 return 0; 00138 } 00139 00140 struct RTOp_ROp_find_nan_inf_reduct_obj_t 00141 RTOp_ROp_find_nan_inf_val(RTOp_ReductTarget targ_obj) 00142 { 00143 const struct RTOp_value_index_type *value_index; 00144 struct RTOp_ROp_find_nan_inf_reduct_obj_t return_obj; 00145 #ifdef RTOp_DEBUG 00146 assert(targ_obj != RTOp_REDUCT_OBJ_NULL); 00147 #endif 00148 value_index = (const struct RTOp_value_index_type*)targ_obj; 00149 return_obj.v0_i = value_index->value; 00150 return_obj.i = value_index->index; 00151 return return_obj; 00152 }
1.7.4