RTOpPack: Extra C/C++ Code for Vector Reduction/Transformation Operators  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
RTOp_ROp_find_nan_inf.c
1 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
6 // Copyright (2003) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 */
43 
44 #include "RTOp_ROp_find_nan_inf.h"
45 #include "RTOp_obj_null_vtbl.h"
46 #include "RTOp_obj_value_index_vtbl.h"
47 #include "check_nan_inf.h"
48 #include "RTOp_get_reduct_op.hpp"
49 
50 /* Implementation functions */
51 
52 static int RTOp_ROp_find_nan_inf_apply_op(
53  const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data
54  , const int num_vecs, const struct RTOp_SubVector vecs[]
55  , const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[]
56  , RTOp_ReductTarget targ_obj )
57 {
58  struct RTOp_value_index_type
59  *targ;
60  RTOp_index_type global_offset;
61  RTOp_index_type sub_dim;
62  const RTOp_value_type *v0_val;
63  ptrdiff_t v0_val_s;
64  RTOp_index_type i;
65 
66  /* */
67  /* Validate the input */
68  /* */
69  if( num_vecs != 1 )
70  return RTOp_ERR_INVALID_NUM_VECS;
71  if( num_targ_vecs != 0 )
72  return RTOp_ERR_INVALID_NUM_TARG_VECS;
73  assert(targ_obj);
74  assert(vecs);
75 
76  /* */
77  /* Get pointers to data */
78  /* */
79 
80  /* {value,index} */
81  targ = (struct RTOp_value_index_type*)targ_obj;
82 
83  /* v0 */
84  global_offset = vecs[0].global_offset;
85  sub_dim = vecs[0].sub_dim;
86  v0_val = vecs[0].values;
87  v0_val_s = vecs[0].values_stride;
88 
89  /* */
90  /* Find a NaN or Inf element! */
91  /* */
92 
93  for( i = global_offset + 1; i <= global_offset + sub_dim; ++i, v0_val += v0_val_s ) {
94  if( RTOp_is_nan_inf(*v0_val) && ( targ->index == 0 || i < targ->index ) ) {
95  targ->value = *v0_val;
96  targ->index = i;
97  break;
98  }
99  }
100 
101  return 0; /* success? */
102 }
103 
104 static int reduce_reduct_objs(
105  const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data /* Can be NULL! */
106  , RTOp_ReductTarget in_reduct_obj, RTOp_ReductTarget inout_reduct_obj )
107 {
108  const struct RTOp_value_index_type
109  *i_targ = (const struct RTOp_value_index_type*)in_reduct_obj;
110  struct RTOp_value_index_type
111  *io_targ = (struct RTOp_value_index_type*)inout_reduct_obj;
112  if( io_targ->index == 0 || ( i_targ->index != 0 && i_targ->index < io_targ->index ) ) {
113  io_targ->index = i_targ->index;
114  io_targ->value = i_targ->value;
115  }
116  return 0;
117 }
118 
119 INSERT_GET_REDUCT_OP_FUNCS(
120  1,1,0,RTOp_value_index_type,reduce_reduct_objs
121  ,RTOp_obj_value_index_vtbl.load_state
122  ,RTOp_obj_value_index_vtbl.extract_state
123  ,external_reduct_op,get_reduct_op)
124 
125 /* Virtual function table */
126 const struct RTOp_RTOp_vtbl_t RTOp_ROp_find_nan_inf_vtbl =
127 {
128  &RTOp_obj_null_vtbl /* use null type for instance data */
129  ,&RTOp_obj_value_index_vtbl /* use {value,index} type for target object */
130  ,"ROp_find_nan_inf"
131  ,NULL
132  ,RTOp_ROp_find_nan_inf_apply_op
133  ,reduce_reduct_objs
134  ,get_reduct_op
135 };
136 
137 /* Class specific functions */
138 
139 int RTOp_ROp_find_nan_inf_construct( struct RTOp_RTOp* op )
140 {
141  op->obj_data = NULL;
142  op->vtbl = &RTOp_ROp_find_nan_inf_vtbl;
143  return 0;
144 }
145 
146 int RTOp_ROp_find_nan_inf_destroy( struct RTOp_RTOp* op )
147 {
148  op->obj_data = NULL;
149  op->vtbl = NULL;
150  return 0;
151 }
152 
153 struct RTOp_ROp_find_nan_inf_reduct_obj_t
154 RTOp_ROp_find_nan_inf_val(RTOp_ReductTarget targ_obj)
155 {
156  const struct RTOp_value_index_type *value_index;
157  struct RTOp_ROp_find_nan_inf_reduct_obj_t return_obj;
158 #ifdef RTOp_DEBUG
159  assert(targ_obj != RTOp_REDUCT_OBJ_NULL);
160 #endif
161  value_index = (const struct RTOp_value_index_type*)targ_obj;
162  return_obj.v0_i = value_index->value;
163  return_obj.i = value_index->index;
164  return return_obj;
165 }
int(* extract_state)(const struct RTOp_obj_type_vtbl_t *vtbl, const void *instance_data, void *obj, int num_values, RTOp_value_type value_data[], int num_indexes, RTOp_index_type index_data[], int num_chars, RTOp_char_type char_data[])
Definition: RTOp.h:1052
int(* load_state)(const struct RTOp_obj_type_vtbl_t *vtbl, const void *instance_data, int num_values, const RTOp_value_type value_data[], int num_indexes, const RTOp_index_type index_data[], int num_chars, const RTOp_char_type char_data[], void **obj)
Definition: RTOp.h:1097
struct RTOp_ROp_find_nan_inf_reduct_obj_t RTOp_ROp_find_nan_inf_val(RTOp_ReductTarget targ_obj)