MOOCHO (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ExampleNLPDirectRTOps.c
Go to the documentation of this file.
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 "Teuchos_ConfigDefs.hpp"
45 #include "ExampleNLPDirectRTOps.h"
46 #include "RTOp_obj_null_vtbl.h"
47 #include "RTOp_obj_index_vtbl.h"
48 
49 /* Implementation for RTOp_TOp_explnlp2_c_eval */
50 
52  const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data
53  , const int num_vecs, const struct RTOp_SubVector vecs[]
54  , const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[]
55  , RTOp_ReductTarget targ_obj )
56 {
57  /* c */
58  size_t sub_dim;
59  RTOp_value_type *c_val;
60  ptrdiff_t c_val_s;
61  /* xD */
62  const RTOp_value_type *xD_val;
63  ptrdiff_t xD_val_s;
64  /* xI */
65  const RTOp_value_type *xI_val;
66  ptrdiff_t xI_val_s;
67 
68  register RTOp_index_type k;
69 
70  /*
71  *Validate the input
72  */
73  if( num_vecs != 2 || vecs == NULL )
75  if( num_targ_vecs != 1 || targ_vecs == NULL )
77  if( targ_vecs[0].sub_dim != vecs[0].sub_dim
78  || targ_vecs[0].sub_dim != vecs[1].sub_dim
79  || targ_vecs[0].global_offset != vecs[0].global_offset
80  || targ_vecs[0].global_offset != vecs[1].global_offset )
82 
83  /*
84  * Get pointers to data
85  */
86 
87  /* c */
88  sub_dim = targ_vecs[0].sub_dim;
89  c_val = targ_vecs[0].values;
90  c_val_s = targ_vecs[0].values_stride;
91  /* xD */
92  xD_val = vecs[0].values;
93  xD_val_s = vecs[0].values_stride;
94  /* xI */
95  xI_val = vecs[1].values;
96  xI_val_s = vecs[1].values_stride;
97 
98  /*
99  * Compute c(j) = xI(i) * ( xD(i) - 1 ) - 10 * xD(i)
100  */
101 
102  if( c_val_s == 1 && xD_val_s == 1 && xI_val_s == 1 ) {
103  /* Slightly faster loop for unit stride vectors */
104  for( k = 0; k < sub_dim; ++k, ++xI_val )
105  *c_val++ = (*xD_val++) * (*xI_val - 1.0) - 10.0 * (*xI_val);
106  }
107  else {
108  /* More general implementation for non-unit strides */
109  for( k = 0; k < sub_dim; ++k, c_val+=c_val_s, xD_val+=xD_val_s, xI_val+=xI_val_s )
110  *c_val = (*xD_val) * (*xI_val - 1.0) - 10.0 * (*xI_val);
111  }
112 
113  return 0; /* success? */
114 }
115 
117 {
120  ,"TOp_explnlp2_c_eval"
121  ,NULL
123  ,NULL
124  ,NULL
125 };
126 
128 {
129  op->obj_data = NULL;
131  return 0;
132 }
133 
135 {
136  op->obj_data = NULL;
137  op->vtbl = NULL;
138  return 0;
139 }
140 
141 /* Implementation for RTOp_TOp_explnlp2_calc_py_D */
142 
144  const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data
145  , const int num_vecs, const struct RTOp_SubVector vecs[]
146  , const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[]
147  , RTOp_ReductTarget targ_obj )
148 {
149  size_t sub_dim;
150  /* xD */
151  const RTOp_value_type *xD_val;
152  ptrdiff_t xD_val_s;
153  /* xI */
154  const RTOp_value_type *xI_val;
155  ptrdiff_t xI_val_s;
156  /* c */
157  const RTOp_value_type *c_val;
158  ptrdiff_t c_val_s;
159  /* d */
160  RTOp_value_type *d_val;
161  ptrdiff_t d_val_s;
162  /* py */
163  RTOp_value_type *py_val;
164  ptrdiff_t py_val_s;
165 
166  register RTOp_index_type k;
167  int all_unit_stride = 0;
168  RTOp_value_type denom;
169 
170  /* task */
171  int task;
172  assert(obj_data);
173  task = *(int*)obj_data;
174  assert(0 <= task && task <= 2);
175 
176  /*
177  * Validate the input
178  */
179  if( ( (task == 0 || task == 1) && num_vecs != 2 )
180  || ( (task == 2) && num_vecs != 3 )
181  || vecs == NULL )
183  if( ( (task == 0 || task == 1) && num_targ_vecs != 1 )
184  || ( (task == 2) && num_targ_vecs != 2 )
185  || targ_vecs == NULL )
187  if( targ_vecs[0].sub_dim != vecs[0].sub_dim
188  || targ_vecs[0].sub_dim != vecs[1].sub_dim
189  || ( task == 2 && ( targ_vecs[0].sub_dim != vecs[2].sub_dim ) )
190  || ( task == 2 && ( targ_vecs[0].sub_dim != targ_vecs[1].sub_dim ) )
191  || targ_vecs[0].global_offset != vecs[0].global_offset
192  || targ_vecs[0].global_offset != vecs[1].global_offset
193  || ( task == 2 && (targ_vecs[0].global_offset != vecs[2].global_offset ) )
194  || ( task == 2 && ( targ_vecs[0].global_offset != targ_vecs[1].global_offset ) ) )
196 
197  /*
198  * Get pointers to data
199  */
200 
201  sub_dim = vecs[0].sub_dim;
202 
203  k = 0;
204  /* xD */
205  xD_val = vecs[k].values;
206  xD_val_s = vecs[k].values_stride;
207  ++k;
208  if( task == 1 || task == 2 ) {
209  /* xI */
210  xI_val = vecs[k].values;
211  xI_val_s = vecs[k].values_stride;
212  ++k;
213  }
214  if( task == 0 || task == 2 ) {
215  /* c */
216  c_val = vecs[k].values;
217  c_val_s = vecs[k].values_stride;
218  ++k;
219  }
220  k = 0;
221  if( task == 1 || task == 2 ) {
222  /* d */
223  d_val = targ_vecs[k].values;
224  d_val_s = targ_vecs[k].values_stride;
225  ++k;
226  }
227  if( task == 0 || task == 2 ) {
228  /* py */
229  py_val = targ_vecs[k].values;
230  py_val_s = targ_vecs[k].values_stride;
231  ++k;
232  }
233 
234  /* Determine if all the vectors have unit stride! */
235  all_unit_stride = 1;
236  for( k = 0; k < num_vecs && !all_unit_stride; ++k )
237  if( vecs[k].values_stride != 1 )
238  all_unit_stride = 0;
239  for( k = 0; k < num_targ_vecs && !all_unit_stride; ++k )
240  if( targ_vecs[k].values_stride != 1 )
241  all_unit_stride = 0;
242 
243  /*
244  * Compute py and/or D
245  */
246 
247  if( all_unit_stride) {
248  if(task == 0) {
249  /* Compute py only */
250  for( k = 0; k < sub_dim; ++k )
251  *py_val++ = *c_val++ / ( 1.0 - *xI_val++ );
252  }
253  if(task == 1) {
254  /* Compute D only */
255  for( k = 0; k < sub_dim; ++k )
256  *d_val++ = ( *xD_val++ - 10.0 ) / ( 1.0 - *xI_val++ );
257  }
258  if(task == 2) {
259  /* Compute py and D */
260  for( k = 0; k < sub_dim; ++k ) {
261  denom = ( 1.0 - *xI_val++ );
262  *d_val++ = ( *xD_val++ - 10.0 ) / denom;
263  *py_val++ = *c_val++ / denom;
264  }
265  }
266  }
267  else {
268  assert(0); /* ToDo: Implement if needed! */
269  }
270 
271  return 0;
272 }
273 
275 {
278  ,"TOp_explnlp2_calc_py_D"
279  ,NULL
281  ,NULL
282  ,NULL
283 };
284 
286 {
287  int result;
288 #ifdef RTOp_DEBUG
289  assert( 0 <= task && task <= 2 );
290 #endif
291  op->obj_data = NULL;
293  result = op->vtbl->obj_data_vtbl->obj_create( NULL, NULL, &op->obj_data );
294  if(result != 0) return result;
295 #ifdef RTOp_DEBUG
296  assert(op->obj_data);
297 #endif
298  *((int*)op->obj_data) = task;
299  return 0;
300 }
301 
303 {
304 #ifdef RTOp_DEBUG
305  assert( 0 <= task && task <= 2 );
306  assert(op->obj_data);
307 #endif
308  *((int*)op->obj_data) = task;
309  return 0;
310 }
311 
313 {
314  int result;
315  result = op->vtbl->reduct_vtbl->obj_free( NULL, NULL, &op->obj_data );
316  if(result != 0) return result;
317  op->obj_data = NULL;
318  op->vtbl = NULL;
319  return 0;
320 }
ptrdiff_t values_stride
Definition: RTOp.h:305
double RTOp_value_type
Definition: RTOp.h:69
const struct RTOp_RTOp_vtbl_t RTOp_TOp_explnlp2_c_eval_vtbl
ptrdiff_t values_stride
Definition: RTOp.h:338
void * RTOp_ReductTarget
Definition: RTOp.h:191
int(* obj_create)(const struct RTOp_obj_type_vtbl_t *vtbl, const void *instance_data, void **obj)
Definition: RTOp.h:941
const struct RTOp_obj_type_vtbl_t * obj_data_vtbl
Definition: RTOp.h:819
int RTOp_TOp_explnlp2_calc_py_D_set_task(int task, struct RTOp_RTOp *op)
int RTOp_TOp_explnlp2_calc_py_D_destroy(struct RTOp_RTOp *op)
static int explnlp2_calc_py_D_apply_op(const struct RTOp_RTOp_vtbl_t *vtbl, const void *obj_data, const int num_vecs, const struct RTOp_SubVector vecs[], const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[], RTOp_ReductTarget targ_obj)
#define RTOp_ERR_INCOMPATIBLE_VECS
Definition: RTOp.h:270
const struct RTOp_obj_type_vtbl_t * reduct_vtbl
Definition: RTOp.h:821
static int explnlp2_c_eval_apply_op(const struct RTOp_RTOp_vtbl_t *vtbl, const void *obj_data, const int num_vecs, const struct RTOp_SubVector vecs[], const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[], RTOp_ReductTarget targ_obj)
int RTOp_TOp_explnlp2_c_eval_destroy(struct RTOp_RTOp *op)
const RTOp_value_type * values
Definition: RTOp.h:303
const struct RTOp_RTOp_vtbl_t RTOp_TOp_explnlp2_calc_py_D_vtbl
int RTOp_TOp_explnlp2_c_eval_construct(struct RTOp_RTOp *op)
RTOp_index_type sub_dim
Definition: RTOp.h:334
#define RTOp_ERR_INVALID_NUM_VECS
Definition: RTOp.h:266
int RTOp_TOp_explnlp2_calc_py_D_construct(int task, struct RTOp_RTOp *op)
int(* obj_free)(const struct RTOp_obj_type_vtbl_t *vtbl, const void *instance_data, void **obj)
Definition: RTOp.h:1019
RTOp_value_type * values
Definition: RTOp.h:336
const struct RTOp_obj_type_vtbl_t RTOp_obj_null_vtbl
void * obj_data
Definition: RTOp.h:800
const struct RTOp_RTOp_vtbl_t * vtbl
Definition: RTOp.h:802
const struct RTOp_obj_type_vtbl_t RTOp_obj_index_vtbl
RTOp_index_type sub_dim
Definition: RTOp.h:301
#define RTOp_ERR_INVALID_NUM_TARG_VECS
Definition: RTOp.h:268
Teuchos_Ordinal RTOp_index_type
Definition: RTOp.h:68