ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sgradclc.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 #if !defined(DOS386)
12  #define DOS386
13 #endif
14 
15 #include "fvar.hpp"
16 
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <string.h>
20 
21 #ifdef __TURBOC__
22  #pragma hdrstop
23  #include <iostream.h>
24 #endif
25 
26 #ifdef __ZTC__
27  #include <iostream.hpp>
28 #endif
29 
30 #if defined (__WAT32__)
31 # include <io.h>
32 #endif
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 
37 #ifdef _MSC_VER
38  #ifdef _M_X64
39  typedef __int64 ssize_t;
40  #else
41  typedef int ssize_t;
42  #endif
43  #define read _read
44  #define write _write
45 #else
46  #include <iostream>
47  #include <sys/stat.h>
48  #include <sys/types.h>
49  #include <unistd.h>
50 #endif
51 
52 #ifdef __SUN__
53  #include <iostream.h>
54  #include <sys/stat.h>
55  #include <sys/types.h>
56  #include <unistd.h>
57 #endif
58 
59 #if defined(__NDPX__ )
60  extern "C" {
61  int LSEEK(int, int, int);
62  int read(int, char*, int);
63  };
64 #endif
65 
66 #include <math.h>
67 
68 #if defined(__ZTC__)
69  void _far * _cdecl _farptr_norm(void _far *);
70  void _far * _cdecl _farptr_fromlong(unsigned long);
71  long _cdecl _farptr_tolong(void _far *);
72 #endif
73 
74 #if !defined(OPT_LIB) || defined(_MSC_VER)
75  #include <cassert>
76 #endif
77 void gradcalc(int nvar, const dvector& g)
78 {
80  if (!gs)
81  {
82  cerr << "Error: No instance of gradient data structure.\n";
83  ad_exit(1);
84  }
85  gs->gradcalc(nvar, g);
86 }
87 
94 void gradient_structure::gradcalc(int nvar, const dvector& _g)
95 {
96  if (nvar!=0)
97  {
98  if (nvar != NVAR)
99  {
100  cerr << "nvar != gradient_structure::NVAR in gradcalc" << endl;
101  cerr << " nvar = " << nvar << endl;
102  cerr << " gradient_structure::NVAR = " << NVAR
103  << endl;
104  cerr << " in " __FILE__ << endl;
105  ad_exit(1);
106  }
107  }
108  dvector& g= (dvector&) _g;
109  TOTAL_BYTES = 0;
112  {
113  g.initialize();
114  return;
115  }
116 
117  if (g.size() < static_cast<unsigned int>(nvar))
118  {
119  cerr << "gradient vector size is less than the number of variables.\n";
120  ad_exit(1);
121  }
122 
124 
125  int& _GRADFILE_PTR = GRAD_STACK1->_GRADFILE_PTR;
126 
127  LSEEK(_GRADFILE_PTR,0L,SEEK_CUR);
128 
130  {
131 #ifdef DIAG
132  cerr << "warning -- calling gradcalc when no calculations generating"
133  << endl << "derivative information have occurred" << endl;
134 #endif
135  g.initialize();
136  return;
137  } // current is one past the end so -- it
138 
140  {
141  save_arrays();
142  save_variables();
143  }
144 
145  GRAD_STACK1->ptr--;
146 
148 
149  memset(ARR_LIST1->ARRAY_MEMBLOCK_BASE, 0,
151 
152  *GRAD_STACK1->ptr->dep_addr = 1;
153 
154  constexpr size_t sizeofgrad_stack_entry = sizeof(grad_stack_entry);
155 
156  //int icount=0;
157  int break_flag=1;
158  do
159  {
160  GRAD_STACK1->ptr++;
161 #ifdef FAST_ASSEMBLER
162  gradloop();
163 #else
164  grad_stack_entry* grad_ptr_first = GRAD_STACK1->ptr_first;
165  while (GRAD_STACK1->ptr-- > grad_ptr_first)
166  {
167  (*(GRAD_STACK1->ptr->func))();
168  }
169 #endif
170 
171  // back up the file one buffer size and read forward
172  OFF_T offset = (OFF_T)(sizeofgrad_stack_entry * GRAD_STACK1->length);
174  -offset,SEEK_CUR);
175 
176  break_flag = GRAD_STACK1->read_grad_stack_buffer(lpos);
177  } while (break_flag);
178 
179  {
180 #ifdef GRAD_DIAG
181  long int ttmp =
182 #endif
183  LSEEK(GRAD_STACK1->_GRADFILE_PTR, 0, SEEK_CUR);
184 #ifdef GRAD_DIAG
185  cout << "Offset in file at end of gradcalc is " << ttmp
186  << " bytes from the beginning\n";
187 #endif
188  }
189 
190  double* pgi = g.get_v() + g.indexmin();
191  double** paddressi = gradient_structure::INDVAR_LIST->address;
192  for (int i = 0; i < nvar; ++i)
193  {
194  *pgi = *(*paddressi);
195  ++pgi;
196  ++paddressi;
197  }
198 
200 
202  {
203  restore_arrays();
205  }
206 }
224 {
225  void* temp_ptr = nullptr;
226  unsigned long bytes_needed =
229 #ifdef __ZTC__
230  if ( (temp_ptr = farmalloc(bytes_needed) ) == 0)
231 #else
232  //if ( (temp_ptr = malloc(bytes_needed) ) == 0)
233  if ((temp_ptr = (void *)malloc(bytes_needed)) == 0)
234  #define __USE_IOSTREAM__
235 #endif
236  {
237  save_var_file_flag=1;
238  cerr << "insufficient memory to allocate space for ARRAY_MEMBLOCK"
239  << " save buffer " << endl;
240  }
241  if (save_var_file_flag==0)
242  {
243  ARR_LIST1->ARRAY_MEMBLOCK_SAVE = temp_ptr;
244 #if defined(DOS386)
245  #ifndef USE_ASSEMBLER
247  bytes_needed);
248  #else
249  dw_block_move((double*)ARR_LIST1->ARRAY_MEMBLOCK_SAVE,
250  (double*)ARR_LIST1->ARRAY_MEMBLOCK_BASE,bytes_needed/8);
251  #endif
252 #else
253  unsigned long int max_move=50000;
254  unsigned long int left_to_move=bytes_needed;
257  while(left_to_move > max_move)
258  {
259  memcpy((char*)dest,(char*)src,max_move);
260  left_to_move-=max_move;
261  src+=max_move;
262  dest+=max_move;
263  }
264  memcpy((char*)dest,(char*)src,left_to_move);
265 #endif
266  }
267  else
268  {
270  LSEEK(GRAD_STACK1->_VARSSAV_PTR,0L,SEEK_SET);
271 #if defined(DOS386)
272  #ifdef OPT_LIB
273  write(GRAD_STACK1->_VARSSAV_PTR, (char*)src, bytes_needed);
274  #else
275  ssize_t ret = write(GRAD_STACK1->_VARSSAV_PTR, (char*)src, bytes_needed);
276  assert(ret != -1);
277  #endif
278 #else
279  unsigned long int max_move=500;
280  unsigned long int left_to_move=bytes_needed;
281  while(left_to_move > max_move)
282  {
283  write(_VARSSAV_PTR,(char*)src,max_move);
284  left_to_move-=max_move;
285  src+=max_move;
286  }
287  write(GRAD_STACK1->_VARSSAV_PTR,(char*)src, left_to_move);
288 #endif
289  }
290 }
291 
295 {
296  unsigned long bytes_needed =
298  if (save_var_file_flag==0)
299  {
300 #if defined(DOS386)
301  #ifndef USE_ASSEMBLER
303  bytes_needed);
304  #else
305  dw_block_move((double*)ARR_LIST1->ARRAY_MEMBLOCK_BASE,
306  (double*)ARR_LIST1->ARRAY_MEMBLOCK_SAVE,bytes_needed/8);
307  #endif
308 #else
309  unsigned long max_move=50000;
310 
311  long int left_to_move=bytes_needed;
314  while(left_to_move > max_move)
315  {
316  memcpy((char*)dest,(char*)src,max_move);
317  left_to_move-=max_move;
318  src+=max_move;
319  dest+=max_move;
320  }
321  memcpy((char*)dest,(char*)src,left_to_move);
322 #endif
324  }
325  else
326  {
328  LSEEK(GRAD_STACK1->_VARSSAV_PTR,0L,SEEK_SET);
329 #if defined(DOS386)
330  #if defined(OPT_LIB) && !defined(_MSC_VER)
331  read(GRAD_STACK1->_VARSSAV_PTR, (char*)dest,bytes_needed);
332  #else
333  ssize_t ret = read(GRAD_STACK1->_VARSSAV_PTR, (char*)dest,bytes_needed);
334  assert(ret != -1);
335  #endif
336 #else
337  unsigned long int max_move=50000;
338 
339  long int left_to_move=bytes_needed;
340  while(left_to_move > max_move)
341  {
342  read(GRAD_STACK1->_VARSSAV_PTR, (char*)dest, max_move);
343  left_to_move-=max_move;
344  dest+=max_move;
345  }
346  read(GRAD_STACK1->_VARSSAV_PTR, (char*)dest, left_to_move);
347 #endif
348  }
349 }
354 {
356 }
361 {
363 }
368 {
371 
373 
374  LSEEK(_GRADFILE_PTR,0L,SEEK_SET);
375 }
385 void grad_stack::set_gradient_stack1(void (* func)(void),
386  double* dep_addr, double* ind_addr1)
387 {
388 #ifdef NO_DERIVS
390  {
391 #endif
392  if (ptr > ptr_last)
393  {
394  // current buffer is full -- write it to disk and reset pointer
395  // and counter
396  this->write_grad_stack_buffer();
397  }
398  //test_the_pointer();
399  ptr->func = func;
400  ptr->dep_addr = dep_addr;
401  ptr->ind_addr1 = ind_addr1;
402  ptr++;
403 #ifdef NO_DERIVS
404  }
405 #endif
406 }
407 #ifdef DIAG
408 void test_the_pointer(void)
409 {
410 /*
411  static int inner_count = 0;
412  static grad_stack_entry * pgse = (grad_stack_entry*) (0x1498fffc);
413  inner_count++;
414  if (inner_count == 404849)
415  {
416  cout << ptr << endl;
417  cout << ptr->func << endl;
418  cout << ptr->dep_addr << endl;
419  cout << (int)(ptr->dep_addr)%8 << endl;
420  }
421  pgse->func = (void (*)())(100);
422  pgse->dep_addr = (double*) 100;
423  pgse->ind_addr1 = (double*) 100;
424 */
425 }
426 #endif
indvar_offset_list * INDVAR_LIST
void set_gradient_stack1(void(*func)(void), double *dep_addr, double *ind_addr1)
Sets the gradient stack entry for a function or operator with a single independent variable...
Definition: sgradclc.cpp:385
void read(const test_smartlist &, void *, int nsize)
double * ind_addr1
Pointer to first independent variable.
Definition: fvar.hpp:900
void memcpy(test_smartlist &dest, void *source, const size_t nsize)
memcpy for test_smartlist
Definition: df1b2f10.cpp:367
void gradcalc(int nvar, const dvector &g)
Compute the gradient from the data stored in the global gradient_structure.
Definition: sgradclc.cpp:94
double ** address
Definition: fvar.hpp:1288
Vector of double precision numbers.
Definition: dvector.h:50
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
void save_variables()
Save variables to a buffer.
Definition: ddlist.cpp:141
static unsigned long ARRAY_MEMBLOCK_SIZE
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
#define __USE_IOSTREAM__
void initialize()
Definition: ddlist.cpp:129
void gradcalc(int nvar, const dvector &g)
Definition: sgradclc.cpp:77
#define LSEEK
Definition: fvar.hpp:75
unsigned long int get_last_offset()
Definition: fvar.hpp:2095
humungous_pointer ARRAY_MEMBLOCK_BASE
Definition: fvar.hpp:2074
prnstream & endl(prnstream &)
unsigned long int get_max_last_offset()
Definition: fvar.hpp:2103
#define min(a, b)
Definition: cbivnorm.cpp:188
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
humungous_pointer ARRAY_MEMBLOCK_SAVE
Definition: fvar.hpp:2075
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
void restore_variables()
Restore variables from buffer.
Definition: ddlist.cpp:155
unsigned int size() const
Get number of elements in array.
Definition: dvector.h:209
void save_variables()
Save variables to a buffer.
Definition: sgradclc.cpp:353
void write(const test_smartlist &, void *, int nsize)
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 reset_gradient_stack(void)
Rewind buffer.
Definition: sgradclc.cpp:367
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
class for things related to the gradient structures, including dimension of arrays, size of buffers, etc.
void free()
Description not yet available.
Definition: humung.cpp:34
grad_stack_entry * ptr_last
Definition: fvar.hpp:925
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
Description not yet available.
double * dep_addr
Pointer to dependent variable.
Definition: fvar.hpp:899
int _VARSSAV_PTR
Definition: fvar.hpp:945