ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
jacobclc.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: David Fournier
5  * Copyright (c) 2008-2012 Regents of the University of California
6  */
11 #include "fvar.hpp"
12 
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <string.h>
16 
17 #ifdef __TURBOC__
18  #pragma hdrstop
19  #include <iostream.h>
20 #endif
21 
22 #ifdef __ZTC__
23  #include <iostream.hpp>
24 #endif
25 
26 #if defined (__WAT32__)
27 # include <io.h>
28 #endif
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 
33 #ifdef __SUN__
34  #include <iostream.h>
35  #include <sys/stat.h>
36  #include <sys/types.h>
37  #include <unistd.h>
38 #endif
39 
40 #ifndef _MSC_VER
41  #include <iostream>
42  #include <sys/stat.h>
43  #include <sys/types.h>
44  #include <unistd.h>
45 #endif
46 
47 #if defined(__NDPX__ )
48  extern "C" {
49  int LSEEK(int, int, int);
50  int read(int, char*, int);
51  };
52 #endif
53 
54 #if defined(__ZTC__)
55  void _far * _cdecl _farptr_norm(void _far *);
56  void _far * _cdecl _farptr_fromlong(unsigned long);
57  long _cdecl _farptr_tolong(void _far *);
58 #endif
59 
60 #include <math.h>
61 
66 void jacobcalc(int nvar, const dmatrix& jac)
67 {
69 }
70 
75 void gradient_structure::jacobcalc(int nvar, const dmatrix& _jac)
76 {
77  ADUNCONST(dmatrix,jac)
78 
79  OFF_T lpos;
80  if(!instances)
81  {
82  jac.initialize();
83  return;
84  }
85  if (jac.rowmin()!=1)
86  {
87  cerr << "Error in jacobcalc jacobian must have minimum valid"
88  " index of 1" << endl;
89  jac.initialize();
90  return;
91  }
92  int depvar_count=DEPVARS_INFO->depvar_count;
93  if (jac.rowmax()<depvar_count)
94  {
95  cerr << "Error in jacobcalc jacobian must have maximumvalid"
96  " index >= the number of dependent variables " << endl
97  << " which is " << depvar_count << endl;
98  jac.initialize();
99  return;
100  }
101 
102  int& _GRADFILE_PTR = GRAD_STACK1->_GRADFILE_PTR;
103  // check to see if anything has been written into the file
104  OFF_T last_gpos=LSEEK(_GRADFILE_PTR,0L,SEEK_CUR);
105 
106  //save current contents of the buffer so we can get them later
107  if (last_gpos)
108  {
110  }
111 
112  // check to see if anything has been written into the file
113  OFF_T last_cpos=LSEEK(fp->file_ptr,0L,SEEK_CUR);
114 
115  //save current contents of the buffer so we can get them later
116  if (last_cpos)
117  {
119  }
120 
121  for (int i=jac.rowmin();i<=jac.rowmax();i++)
122  {
123  if (jac(i).indexmin() !=1)
124  {
125  cerr << "jacobian matrix minimum column index must equal 1"
126  " for all rows" << endl;
127  return;
128  }
129  if (jac(i).indexmax() < nvar)
130  {
131  cerr << "jacobian matrix column size is less than the number of"
132  " independent variables" << endl;
133  return;
134  }
135  }
136  // save variable values if desired
137  if (save_var_flag)
138  {
139  save_arrays();
140  save_variables();
141  }
142  // now evalueate the jacobian
143  for (int ijac=1;ijac<=depvar_count;ijac++)
144  {
145  dvector& g=(dvector&)(jac(ijac));
146  //max_num_dependent_variables=ndv;
147  if (depvar_count>DEPVARS_INFO->max_num_dependent_variables)
148  {
149  cout << "maximum number of depdendent variables of "
150  << DEPVARS_INFO->max_num_dependent_variables << " exceeded "
151  << endl
152  << "use gradient_structure::set_NUM_DEPENDENT_VARIABLES(int i);"
153  << endl << "to increase the number of dependent variables"
154  << endl;
155  ad_exit(1);
156  }
157 
159  fp->toffset=fp->offset;
160  _GRADFILE_PTR=DEPVARS_INFO->grad_file_count(ijac);
162  lpos=DEPVARS_INFO->grad_file_position(ijac);
163  // position the cmpdif file correctly;
164  if (last_cpos)
165  {
166  OFF_T cmp_lpos=DEPVARS_INFO->cmpdif_file_position(ijac);
167  LSEEK(fp->file_ptr,cmp_lpos,SEEK_SET);
168  fp->read_cmpdif_stack_buffer(cmp_lpos);
169  }
171 
172  if (last_gpos)
173  {
174  // just use the end of the buffer
176 
177  // check to sere if buffer was written into the beginning of
178  // the next file
180  && (lpos == GRAD_STACK1->end_pos1) && (lpos>0) )
181  {
182  // get the next file
184  lpos=0;
185  }
186  // and position the file to the begginig of the buffer image
187  LSEEK(_GRADFILE_PTR,lpos,SEEK_SET);
188  // now fill the buffer with the appropriate stuff
190  // now reposition the grad_buffer pointer
191  }
192  GRAD_STACK1->ptr=
194 
196  {
197 #ifdef DIAG
198  cerr << "warning -- calling gradcalc when no calculations generating"
199  << endl << "derivative information have occurred" << endl;
200 #endif
201  g.initialize();
202  return;
203  } // current is one past the end so -- it
204 
205  GRAD_STACK1->ptr--;
206 
208 
210 
211  unsigned long int max_last_offset = ARR_LIST1->get_max_last_offset();
212 
213  size_t size = sizeof(double_and_int);
214 
215  for (unsigned int i = 0; i < (max_last_offset/size); i++)
216  {
217  tmp->x = 0;
218 #if defined (__ZTC__)
219  tmp = (double_and_int*)_farptr_norm((void*)(++tmp));
220 #else
221  tmp++;
222 #endif
223  }
224 
225  *GRAD_STACK1->ptr->dep_addr = 1;
226 
227  int break_flag=1;
228  do
229  {
230  GRAD_STACK1->ptr++;
231  #ifdef FAST_ASSEMBLER
232  gradloop();
233  #else
234  //int counter=0;
235  while (GRAD_STACK1->ptr-- > GRAD_STACK1->ptr_first)
236  {
237  //grad_stack_entry* grad_ptr =
238  //gradient_structure::get()->GRAD_STACK1->ptr;
239  {
240  (*GRAD_STACK1->ptr->func)();
241  }
242  }
243  #endif
244  // \todo Need test
245  // back up the file one buffer size and read forward
246  OFF_T offset = (OFF_T)(sizeof(grad_stack_entry) * GRAD_STACK1->length);
247  lpos = LSEEK(GRAD_STACK1->_GRADFILE_PTR, -offset, SEEK_CUR);
248 
249  break_flag = GRAD_STACK1->read_grad_stack_buffer(lpos);
250  } while (break_flag); // do
251 
252  double* pgj = g.get_v() + g.indexmin();
253  for (int i = 0; i < nvar; ++i)
254  {
256  ++pgj;
257  }
258 
260  }// loop over dep vars
263  {
266  }
267 }
indvar_offset_list * INDVAR_LIST
void read(const test_smartlist &, void *, int nsize)
void set_gbuffer_pointers(void)
Description not yet available.
Definition: gradstak.cpp:551
double * get_address(const int &i)
Definition: fvar.hpp:1292
void jacobcalc(int nvar, const dmatrix &jac)
Description not yet available.
Definition: jacobclc.cpp:75
dependent_variables_information * DEPVARS_INFO
#define ADUNCONST(type, obj)
Creates a shallow copy of obj that is not CONST.
Definition: fvar.hpp:140
Vector of double precision numbers.
Definition: dvector.h:50
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
void jacobcalc(int nvar, const dmatrix &g)
Description not yet available.
Definition: jacobclc.cpp:66
OFF_T end_pos1
Definition: fvar.hpp:953
Holds derivative information for arithmetic operators and math library functions. ...
Definition: fvar.hpp:894
void write_grad_stack_buffer()
Definition: fvar.hpp:976
exitptr ad_exit
Definition: gradstrc.cpp:53
void initialize()
Definition: ddlist.cpp:129
int _GRADFILE_PTR1
Definition: fvar.hpp:943
#define LSEEK
Definition: fvar.hpp:75
Holds the data for the prevariable class.
Definition: fvar.hpp:191
void write_cmpdif_stack_buffer()
Description not yet available.
Definition: df_file.cpp:350
void read_cmpdif_stack_buffer(OFF_T &lpos)
Description not yet available.
Definition: df_file.cpp:328
humungous_pointer ARRAY_MEMBLOCK_BASE
Definition: fvar.hpp:2074
prnstream & endl(prnstream &)
unsigned long int max_last_offset
unsigned long int get_max_last_offset()
Definition: fvar.hpp:2103
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
static _THREAD gradient_structure * _instance
#define OFF_T
Definition: fvar.hpp:74
Description not yet available.
Definition: fvar.hpp:2819
void initialize(void)
Initialze all elements of dvector to zero.
Definition: dvect5.cpp:10
grad_stack_entry * ptr_first
Definition: fvar.hpp:924
static _THREAD DF_FILE * fp
void save_variables()
Save variables to a buffer.
Definition: sgradclc.cpp:353
int & gradfile_handle()
Definition: gradstak.cpp:534
grad_stack_entry * ptr
Definition: fvar.hpp:934
int read_grad_stack_buffer(OFF_T &lpos)
Description not yet available.
Definition: gst_read.cpp:76
void restore_variables()
Restore variables from buffer.
Definition: sgradclc.cpp:360
size_t length
Definition: fvar.hpp:930
void save_arrays()
Compute the gradient from the data stored in the global gradient_structure.
Definition: sgradclc.cpp:223
int _GRADFILE_PTR
Definition: fvar.hpp:942
static _THREAD grad_stack * GRAD_STACK1
void(* func)(void)
&lt; Pointer to function (if any) to be used for derivative calculation
Definition: fvar.hpp:898
double *& get_v(void)
Definition: dvector.h:148
double * dep_addr
Pointer to dependent variable.
Definition: fvar.hpp:899
void increment_current_gradfile_ptr(void)
If there is another file set the handle to point to it otherwise we are out of room.
Definition: gradstak.cpp:503
double x
&lt; value of the variable
Definition: fvar.hpp:195