ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
jacob3.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 #include <stdio.h>
23 #include <stdlib.h>
24 
25 #ifdef __SUN__
26  #include <iostream.h>
27  #include <sys/stat.h>
28  #include <sys/types.h>
29  #include <unistd.h>
30 #endif
31 
32 #if !defined(_MSC_VER)
33  #include <iostream>
34  #include <sys/stat.h>
35  #include <sys/types.h>
36  #include <unistd.h>
37 #endif
38 
39 #if defined(__NDPX__ )
40  extern "C" {
41  int LSEEK(int, int, int);
42  int read(int, char*, int);
43  };
44 #endif
45 
46 #include <math.h>
47 
52 void jacobcalc(int nvar, const uostream& ofs)
53 {
55 }
56 
61 void gradient_structure::jacobcalc(int nvar, const uostream& ofs)
62 {
63  dvector jac(1,nvar);
64  OFF_T lpos;
65  int depvar_count=DEPVARS_INFO->depvar_count;
66 
67  int& _GRADFILE_PTR = GRAD_STACK1->_GRADFILE_PTR;
68  // check to see if anything has been written into the file
69  OFF_T last_gpos=LSEEK(_GRADFILE_PTR,0L,SEEK_CUR);
70 
71  //save current contents of the buffer so we can get them later
72  if (last_gpos)
73  {
75  }
76 
77  // check to see if anything has been written into the file
78  OFF_T last_cpos=LSEEK(fp->file_ptr,0L,SEEK_CUR);
79 
80  //save current contents of the buffer so we can get them later
81  if (last_cpos)
82  {
84  }
85 
86  // save variable values if desired
87  if (save_var_flag)
88  {
89  save_arrays();
91  }
92  // now evalueate the jacobian
93  for (int ijac=1;ijac<=depvar_count;ijac++)
94  {
95  dvector& g=jac;
96  //max_num_dependent_variables=ndv;
97  if (depvar_count>DEPVARS_INFO->max_num_dependent_variables)
98  {
99  cout << "maximum number of depdendent variables of "
100  << DEPVARS_INFO->max_num_dependent_variables << " exceeded "
101  << endl
102  << "use gradient_structure::set_NUM_DEPENDENT_VARIABLES(int i);"
103  << endl << "to increase the number of dependent variables"
104  << endl;
105  ad_exit(1);
106  }
107 
109  fp->toffset=fp->offset;
110  _GRADFILE_PTR=DEPVARS_INFO->grad_file_count(ijac);
112  lpos=DEPVARS_INFO->grad_file_position(ijac);
113  // position the cmpdif file correctly;
114  if (last_cpos)
115  {
116  OFF_T cmp_lpos=DEPVARS_INFO->cmpdif_file_position(ijac);
117  LSEEK(fp->file_ptr,cmp_lpos,SEEK_SET);
118  fp->read_cmpdif_stack_buffer(cmp_lpos);
119  }
121 
122  if (last_gpos)
123  {
124  // just use the end of the buffer
126 
127  // check to sere if buffer was written into the beginning of
128  // the next file
130  && (lpos == GRAD_STACK1->end_pos1) && (lpos>0) )
131  {
132  // get the next file
134  lpos=0;
135  }
136  // and position the file to the begginig of the buffer image
137  LSEEK(_GRADFILE_PTR,lpos,SEEK_SET);
138  // now fill the buffer with the appropriate stuff
140  // now reposition the grad_buffer pointer
141  }
143 
145  {
146 #ifdef DIAG
147  cerr << "warning -- calling gradcalc when no calculations generating"
148  << endl << "derivative information have occurred" << endl;
149 #endif
150  g.initialize();
151  return;
152  } // current is one past the end so -- it
153 
154  GRAD_STACK1->ptr--;
155 
157 
159 
160  unsigned long int max_last_offset = ARR_LIST1->get_max_last_offset();
161 
162  size_t size = sizeof(double_and_int);
163 
164  for (unsigned int i = 0; i < (max_last_offset/size); i++)
165  {
166  tmp->x = 0;
167 #if defined (__ZTC__)
168  tmp = (double_and_int*)_farptr_norm((void*)(++tmp));
169 #else
170  tmp++;
171 #endif
172  }
173 
174  *GRAD_STACK1->ptr->dep_addr = 1;
175  //double* zptr = gradient_structure::get()->GRAD_STACK1->ptr->dep_addr;
176 
177  int break_flag=1;
178  do
179  {
180  GRAD_STACK1->ptr++;
181  #ifdef FAST_ASSEMBLER
182  gradloop();
183  #else
184  //int counter=0;
185  while (GRAD_STACK1->ptr-- > GRAD_STACK1->ptr_first)
186  {
187  //grad_stack_entry* grad_ptr =
188  //gradient_structure::get()->GRAD_STACK1->ptr;
189  {
190  (*GRAD_STACK1->ptr->func)();
191  }
192  }
193  #endif
194 
195  // \todo Need test
196  // back up the file one buffer size and read forward
197  OFF_T offset = (OFF_T)(sizeof(grad_stack_entry) * GRAD_STACK1->length);
198  lpos = LSEEK(GRAD_STACK1->_GRADFILE_PTR, -offset, SEEK_CUR);
199 
200  break_flag = GRAD_STACK1->read_grad_stack_buffer(lpos);
201  } while (break_flag); // do
202 
203  double* pgj = g.get_v() + g.indexmin();
204  for (int i = 0; i < nvar; ++i)
205  {
207  ++pgj;
208  }
209 
211  //ofs << setprecision(10) << g << endl;
212  ofs << g ;
213  }// loop over dep vars
216  {
217  restore_arrays();
219  }
220 }
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
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
Description not yet available.
Definition: fvar.hpp:3398
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
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