ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
xgradclc.cpp
Go to the documentation of this file.
1 /*
2  * Author: David Fournier
3  * Copyright (c) 2008-2012 Regents of the University of California
4  */
9 #include "fvar.hpp"
10 
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <string.h>
14 
15 #ifdef __TURBOC__
16  #pragma hdrstop
17  #include <iostream.h>
18 #endif
19 
20 #ifdef __ZTC__
21  #include <iostream.hpp>
22 #endif
23 
24 #if defined (__WAT32__)
25 # include <io.h>
26 #endif
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 
31 #ifndef _MSC_VER
32  #include <iostream>
33  #include <sys/stat.h>
34  #include <sys/types.h>
35  #include <unistd.h>
36 #endif
37 
38 #ifdef __SUN__
39  #include <iostream.h>
40  #include <sys/stat.h>
41  #include <sys/types.h>
42  #include <unistd.h>
43 #endif
44 
45 #if defined(__NDPX__ )
46  extern "C" {
47  int LSEEK(int, int, int);
48  int read(int, char*, int);
49  };
50 #endif
51 
52 #include <math.h>
53 
54 #ifdef DEBUG
55  #include <cassert>
56  #include <climits>
57 #endif
58 
59 #ifdef ISZERO
60  #undef ISZERO
61 #endif
62 #define ISZERO(d) ((d)==0.0)
63 
64 void funnel_derivatives(void);
65 
66 #if defined(__ZTC__)
67  void _far * _cdecl _farptr_norm(void _far *);
68  void _far * _cdecl _farptr_fromlong(unsigned long);
69  long _cdecl _farptr_tolong(void _far *);
70 #endif
71 
77 {
79  if (!gs)
80  {
81  cerr << "Error: No instance of gradient data structure.\n";
82  ad_exit(1);
83  }
84  gs->funnel_gradcalc();
85 }
87 {
88 # ifdef NO_DERIVS
90  {
91  return;
92  }
93 # endif
94 
95  TOTAL_BYTES = 0;
97 
99  {
100  return;
101  }
102 
104 
105  int& _GRADFILE_PTR = GRAD_STACK1->_GRADFILE_PTR;
106 
107  OFF_T lpos = LSEEK(_GRADFILE_PTR,0L,SEEK_CUR);
108 
110  {
111 #ifdef DIAG
112  cerr <<
113  "warning -- calling funnel_gradcalc when no calculations generating"
114  << endl << "derivative information have occurred" << endl;
115 #endif
116  return;
117  } // current is one past the end so -- it
118 
119  //if (gradient_structure::save_var_flag)
120  {
123  }
124 
125  GRAD_STACK1->ptr--;
126 
128 
130 
131  unsigned long int max_last_offset = ARR_LIST1->get_max_last_offset();
132 
133  constexpr size_t size = sizeof(double_and_int);
134 
135  for (unsigned int i = 0; i < (max_last_offset/size); i++)
136  {
137  tmp->x = 0;
138 #if defined (__ZTC__)
139  tmp = (double_and_int*)_farptr_norm((void*)(++tmp));
140 #else
141  tmp++;
142 #endif
143  }
144 
145  *GRAD_STACK1->ptr->dep_addr = 1;
146  double* zptr = GRAD_STACK1->ptr->dep_addr;
147 
148 int break_flag=1;
149 int funnel_flag=0;
150 
151 do
152 {
153  GRAD_STACK1->ptr++;
154  #ifdef FAST_ASSEMBLER
155  gradloop();
156  #else
157  grad_stack_entry * grad_ptr_first = GRAD_STACK1->ptr_first;
158  while (GRAD_STACK1->ptr-- > grad_ptr_first)
159  {
160  if (!GRAD_STACK1->ptr->func)
161  {
162  funnel_flag=1;
163  break;
164  }
165  else
166  (*(GRAD_STACK1->ptr->func))();
167  }
168 
169  #endif
170  if (funnel_flag) break;
171 
172  // back up the file one buffer size and read forward
173  OFF_T offset = (OFF_T)(sizeof(grad_stack_entry) * GRAD_STACK1->length);
174  lpos = LSEEK(GRAD_STACK1->_GRADFILE_PTR, -offset, SEEK_CUR);
175 
176  break_flag = GRAD_STACK1->read_grad_stack_buffer(lpos);
177 } while (break_flag); // do
178 
179  {
180  if (lpos<0)
181  {
182  #ifdef GRAD_DIAG
183  OFF_T ttmp =
184  #endif
185  LSEEK(GRAD_STACK1->_GRADFILE_PTR, 0,SEEK_CUR);
186 
187  #ifdef GRAD_DIAG
188  cout << "Offset in file at end of gradcalc is " << ttmp
189  << " bytes from the beginning\n";
190  #endif
191  }
192  }
193 
194  constexpr size_t sizeofdouble = sizeof(double);
195  //if (gradient_structure::save_var_flag)
196  {
197  unsigned long bytes_needed = min(
198  gradient_structure::ARR_LIST1->get_last_offset() + 1,
200  size_t _dsize = bytes_needed / sizeofdouble;
201 #ifdef DEBUG
202  assert(_dsize <= INT_MAX);
203 #endif
204  int dsize = (int)_dsize;
205 
206  //dvector dtmp(0,dsize-1);
207  //memcpy((char*)&(dtmp(0)),(char*)ARR_LIST1->ARRAY_MEMBLOCK_BASE,
208  //dsize*sizeof(double));
209 
210  double* dptr=(double*)ARR_LIST1->ARRAY_MEMBLOCK_BASE;
211  dptr-=1;
212  int ii=0;
213  int nzero=0;
214  int nnzero=0;
215  int dcount=0;
216  int zero_flag=0;
217  ivector offset(0,dsize-1);
219  if (!ISZERO(*(++dptr)))
220  {
221  fp->save_double_value(*dptr);
222  dcount++;
223  zero_flag=0;
224  offset(ii++)=0;
225  nnzero++;
226  }
227  else
228  {
229  zero_flag=1;
230  nzero++;
231  }
232 
233  for (int i1=1;i1<dsize;i1++)
234  {
235  if (!ISZERO(*(++dptr)))
236  {
237  fp->save_double_value(*dptr);
238  dcount++;
239  nnzero++;
240  if (zero_flag)
241  {
242  offset(ii++)=nzero;
243  nzero=0;
244  zero_flag=0;
245  }
246  }
247  else
248  {
249  nzero++;
250  if (!zero_flag)
251  {
252  offset(ii++)=nnzero;
253  nnzero=0;
254  zero_flag=1;
255  }
256  }
257  }
258  fp->save_int_value(dcount);
259 
260  for (int i=0;i<ii;i++)
261  {
262  fp->save_int_value(offset(i));
263  }
264  fp->save_int_value(ii);
265 
266  int nlinks = static_cast<int>(GRAD_LIST->nlinks);
267 #ifdef DEBUG
268  assert(GRAD_LIST->nlinks <= INT_MAX);
269  assert(nlinks > 0);
270 #endif
271  dvector stmp(0, nlinks - 1);
272 
273  double* pstmpi = stmp.get_v();
274  dlink** dlink_addresses = GRAD_LIST->dlink_addresses;
275  for (int i=0; i < nlinks; ++i)
276  {
277  memcpy((char*)pstmpi, *dlink_addresses, sizeofdouble);
278 
279  ++pstmpi;
280  ++dlink_addresses;
281  }
282  //dtmp.save_dvector_value();
283  //dtmp.save_dvector_position();
284  fp->save_dvector_value(stmp);
285  fp->save_dvector_position(stmp);
286 
287  // save the address of the dependent variable for the funnel
288  size_t wsize=sizeof(double_and_int*);
289  fp->fwrite(&zptr, wsize);
291 
295  }
296 }
297 
303 {
306 
310  dvector stmp=fp->restore_dvector_value(stmp_pos);
311  //dvector_position dtmp_pos=fp->restore_dvector_position();
312  //dvector dtmp=restore_dvector_value(dtmp_pos);
313  int ii=fp->restore_int_value();
314  int i;
315  int ip=ii;
316  if (!ip) ip=1;
317  ivector offset(0,ip);
318  offset(ip)=0;
319  //ivector offset(0,ip-1);
320  for (i=ii-1;i>=0;i--)
321  {
322  offset(i)=fp->restore_int_value();
323  }
324  int dcount=fp->restore_int_value();
325  int dc=dcount;
326  if (!dc) dc=1;
327  dvector dx(0,dc-1);
328  for (i=dcount-1;i>=0;i--)
329  {
330  dx(i)=fp->restore_double_value();
331  }
332 
334 
335  double df = restore_prevariable_derivative(deppos);
336  double* dptr = (double*)(gs->ARR_LIST1->ARRAY_MEMBLOCK_BASE);
337 
338  //double * dd = &(dx(1));
339  ii=0;
340  int ic=0;
341  dptr+=offset(ii++);
342  for (i=0;i<dcount;i++)
343  {
344  *(dptr++)+=dx(i)*df;
345  if (++ic==offset(ii))
346  {
347  if (i==dcount-1)
348  {
349  break;
350  }
351  dptr+=offset(ii+1);
352  ii+=2;
353  ic=0;
354  }
355  }
356 
357  int smax=stmp.indexmax();
358  double* pstmpi = stmp.get_v();
359  dlink** dlink_addresses = gs->GRAD_LIST->dlink_addresses;
360  for (i=0;i<smax;i++)
361  {
362  if (!ISZERO(*pstmpi))
363  {
364  *((double*)(*dlink_addresses)) += *pstmpi * df;
365  }
366  ++pstmpi;
367  ++dlink_addresses;
368  }
369 }
370 
376 {
378  funnel_gradcalc();
379  return *this;
380 }
381 
386 void ad_begin_funnel(void)
387 {
389 }
390 
double restore_prevariable_derivative(const prevariable_position &_pos)
Description not yet available.
Definition: cmpdif8.cpp:131
Base class for dvariable.
Definition: fvar.hpp:1315
void read(const test_smartlist &, void *, int nsize)
void memcpy(test_smartlist &dest, void *source, const size_t nsize)
memcpy for test_smartlist
Definition: df1b2f10.cpp:367
Description not yet available.
Definition: fvar.hpp:4440
friend void funnel_derivatives(void)
Description not yet available.
Vector of double precision numbers.
Definition: dvector.h:50
static unsigned long ARRAY_MEMBLOCK_SIZE
Holds derivative information for arithmetic operators and math library functions. ...
Definition: fvar.hpp:894
dvariable & operator=(const prevariable &)
Description not yet available.
Definition: xgradclc.cpp:375
exitptr ad_exit
Definition: gradstrc.cpp:53
Description not yet available.
Definition: fvar.hpp:4923
void verify_identifier_string(const char *)
Verifies gradient stack string.
Definition: cmpdif3.cpp:149
void initialize()
Definition: ddlist.cpp:129
void save_int_value(int x)
Definition: cmpdif8.cpp:108
#define ISZERO(d)
Definition: xgradclc.cpp:62
void funnel_derivatives(void)
Description not yet available.
Definition: xgradclc.cpp:302
dvector restore_dvector_value(const dvector_position &tmp)
Definition: cmpdif4.cpp:178
#define LSEEK
Definition: fvar.hpp:75
Holds the data for the prevariable class.
Definition: fvar.hpp:191
void set_gradient_stack(void(*func)(void), double *dep_addr, double *ind_addr1=NULL, double mult1=0, double *ind_addr2=NULL, double mult2=0)
Description not yet available.
Definition: fvar.hpp:1045
void funnel_gradcalc()
Description not yet available.
Definition: xgradclc.cpp:76
humungous_pointer ARRAY_MEMBLOCK_BASE
Definition: fvar.hpp:2074
void save_dvector_position(const dvector &v)
Definition: cmpdif4.cpp:32
prnstream & endl(prnstream &)
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
unsigned long int max_last_offset
prevariable_position restore_prevariable_position()
Definition: cmpdif8.cpp:43
void save_dvector_value(const dvector &v)
Definition: cmpdif4.cpp:130
unsigned long int get_max_last_offset()
Definition: fvar.hpp:2103
#define min(a, b)
Definition: cbivnorm.cpp:188
unsigned int nlinks
Definition: fvar.hpp:837
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
dlink ** dlink_addresses
Definition: fvar.hpp:838
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
double restore_double_value()
Definition: cmpdif8.cpp:186
static _THREAD gradient_structure * _instance
#define OFF_T
Definition: fvar.hpp:74
int restore_int_value()
Definition: cmpdif8.cpp:201
grad_stack_entry * ptr_first
Definition: fvar.hpp:924
int save_identifier_string(const char *)
Writes a gradient stack verification string.
Definition: cmpdif2.cpp:315
void ad_begin_funnel(void)
Description not yet available.
Definition: xgradclc.cpp:386
dvector_position restore_dvector_position()
Definition: cmpdif4.cpp:88
dvariable & operator=(const double x)
Assigns a value to a dvariable object.
Definition: fvar.hpp:1595
static _THREAD DF_FILE * fp
void save_variables()
Save variables to a buffer.
Definition: sgradclc.cpp:353
int & gradfile_handle()
Definition: gradstak.cpp:534
void funnel_gradcalc()
Definition: xgradclc.cpp:86
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
Stores the adjoint gradient data that will be processed by gradcalc.
void restore_variables()
Restore variables from buffer.
Definition: sgradclc.cpp:360
void fwrite(const void *s, const size_t num_bytes)
Reads num_bytes from s and writes to buffer.
Definition: df_file.cpp:296
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
void save_double_value(double x)
Definition: cmpdif8.cpp:94
static _THREAD grad_stack * GRAD_STACK1
class for things related to the gradient structures, including dimension of arrays, size of buffers, etc.
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
Fundamental data type for reverse mode automatic differentiation.
Definition: fvar.hpp:1518
double x
&lt; value of the variable
Definition: fvar.hpp:195