ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gradient_structure.h
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: David Fournier
5  * Copyright (c) 2008-2014 Regents of the University of California
6  *
7  * ADModelbuilder and associated libraries and documentations are
8  * provided under the general terms of the "New BSD" license
9  *
10  * License:
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are
14  * met:
15  *
16  * 1. Redistributions of source code must retain the above copyright
17  * notice, this list of conditions and the following disclaimer.
18  *
19  * 2. Redistributions in binary form must reproduce the above copyright
20  * notice, this list of conditions and the following disclaimer in the
21  * documentation and/or other materials provided with the distribution.
22  *
23  * 3. Neither the name of the University of California, Otter Research,
24  * nor the ADMB Foundation nor the names of its contributors may be used
25  * to endorse or promote products derived from this software without
26  * specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39  *
40  */
41 #ifndef __ADMB_GRADIENT_STRUCTURE_H__
42 #define __ADMB_GRADIENT_STRUCTURE_H__
43 
44 #include <thread>
45 #include <fstream>
46 using std::ofstream;
47 
48 #ifdef __BORLANDC__
49  #define size_t long int
50 #endif
51 
52 class dvariable;
53 class DF_FILE;
54 class dvector;
55 class dmatrix;
56 class dlink;
57 class double_and_int;
58 class arr_list;
59 class dvar_vector;
61 class prevariable;
62 class indvar_offset_list;
64 class grad_stack;
65 class uostream;
66 class dlist;
67 class imatrix_position;
69 
75 {
76  size_t adjustment;
77 public:
80 
81  char* ptr;
82  void free();
83  void adjust(const size_t);
84  humungous_pointer operator+(unsigned long int& offset);
85  humungous_pointer& operator+=(unsigned long int& offset);
86  humungous_pointer& operator=(void* p);
87  int operator==(void* p);
88  int operator!=(void* p);
89  operator char*();
90  operator void*();
91  operator double_and_int*();
92  operator double*();
93 };
94 
95 #ifdef USE_THREAD
96 #define _THREAD __thread
97  #ifdef _MSC_VER
98  #define __thread __declspec(thread)
99  #endif
100 #else
101 #define _THREAD
102 #endif
103 
108 {
109 public:
112 
113  static _THREAD DF_FILE* fp;
116 
117  static gradient_structure* get() { return _instance; }
118  static DF_FILE* get_fp() { return fp; }
119  static grad_stack* get_GRAD_STACK1() { return GRAD_STACK1; }
120 
122 
126  static dvariable* next_RETURN_PTR();
127 
129  unsigned int gradients_size;
130  void create(const unsigned int size);
131  gradient_structure* set(const unsigned int id);
132  void clean();
133 
134  void gradcalc(int nvar, const dvector& g);
135  void funnel_gradcalc();
136 
137 #if defined(NO_DERIVS)
138  static int no_derivatives;
139 #endif
140 
141  static long int USE_FOR_HESSIAN;
142  long int NVAR;
143  static unsigned int NUM_RETURN_ARRAYS;
145  unsigned int RETURN_ARRAYS_PTR;
147  size_t TOTAL_BYTES;
149  static unsigned long ARRAY_MEMBLOCK_SIZE;//js
150 public:
151  friend class arr_list;
152 
153  static size_t CMPDIF_BUFFER_SIZE;
154  static size_t GRADSTACK_BUFFER_SIZE;
155  static unsigned int MAX_NVAR_OFFSET;
157  static int save_var_flag;
158 
159  static unsigned int MAX_DLINKS;
163 
164  // this needs to be a static member function so other static
165  // member functions can call it
166  static void check_set_error(const char *variable_name);
167 
168  public:
169  static int instances;
170  unsigned int x;
171 
172  // exception class
174  {
175  };
176 
178  double* hessian_ptr;
179  static long int get_USE_FOR_HESSIAN()
180  {
181  return USE_FOR_HESSIAN;
182  }
183  static void set_USE_FOR_HESSIAN(const long int i)
184  {
185  USE_FOR_HESSIAN = i;
186  }
187  friend class dfsdmat;
188 
190  gradient_structure(const long int size): gradient_structure(size, 0) {}
191  gradient_structure(const long int size, const unsigned int id);
192  gradient_structure(const gradient_structure&) = delete;
194  virtual ~gradient_structure();
195 
198 
199  void save_variables();
200  void restore_variables();
201  void save_arrays();
202  void restore_arrays();
203 
204  size_t totalbytes(void);
205 
207  const dvar_vector_position& tmp);
208  friend void cleanup_temporary_files();
209  //friend dvector restore_dvar_vector_value(const dvar_vector_position&);
212  friend dmatrix restore_dvar_matrix_value(void);
213  //friend dmatrix restore_derivatives(void);
214  friend void gradfree(dlink * v);
215 
216  void make_indvar_list(const dvar_vector& t);
217 
218  //friend void gradcalc( int , double *);
219  friend void gradcalc(int nvar, const dvector & g);
220  friend void slave_gradcalc(void);
221  friend void funnel_gradcalc(void);
222  friend void allocate_dvariable_space(void);
223  friend void wide_funnel_gradcalc(void);
225  friend double_and_int *gradnew();
227  static unsigned int RETURN_ARRAYS_SIZE;
228  //static int RETURN_INDEX;
230  //static arr_list *ARR_FREE_LIST1;
231  //static void funnel_jacobcalc(void);
232  void jacobcalc(int nvar, const dmatrix& jac);
233  void jacobcalc(int nvar, const ofstream& jac);
234  void jacobcalc(int nvar, const uostream& jac);
235 
236  friend void default_evaluation(void);
237  //access functions
238 
239  friend class DF_FILE;
240  static void set_NUM_RETURN_ARRAYS(unsigned int i);
241 #if defined(NO_DERIVS)
242  static void set_NO_DERIVATIVES(void);
243  static void set_YES_DERIVATIVES(void);
244 #endif
245  static void set_YES_SAVE_VARIABLES_VALUES();
246  static void set_NO_SAVE_VARIABLES_VALUES();
247  //static int _GRADFILE_PTR; // should be int gradfile_handle;
248  //static int _GRADFILE_PTR1; // should be int gradfile_handle;
249  //static int _GRADFILE_PTR2; // should be int gradfile_handle;
250  //static int _VARSSAV_PTR; // should be int gradfile_handle;
251  static void set_NUM_DEPENDENT_VARIABLES(int i);
252  static void set_RETURN_ARRAYS_SIZE(unsigned int i);
253  static void set_ARRAY_MEMBLOCK_SIZE(unsigned long i);
254  static unsigned long get_ARRAY_MEMBLOCK_SIZE()
255  { return ARRAY_MEMBLOCK_SIZE; }
256  static void set_CMPDIF_BUFFER_SIZE(const size_t i);
257  static void set_GRADSTACK_BUFFER_SIZE(const size_t i);
259  { return GRADSTACK_BUFFER_SIZE; }
260  static void set_GRADSTACK_BUFFER_BYTES(const size_t i);
261  static void set_MAX_NVAR_OFFSET(unsigned int i);
262  static void set_MAX_DLINKS(int i);
263 
265 
266  static unsigned int get_MAX_DLINKS() { return MAX_DLINKS; }
268  unsigned long int max_last_offset;
269 
272 
273  friend class dlist;
274  friend class grad_stack;
275  friend class function_minimizer;
276  friend void funnel_derivatives(void);
277 };
278 
283 class DF_FILE
284 {
285 public:
287  DF_FILE(): DF_FILE(gradient_structure::CMPDIF_BUFFER_SIZE) {}
289  DF_FILE(const DF_FILE&) = delete;
291  DF_FILE(const size_t nbytes): DF_FILE(nbytes, 0) {}
292  DF_FILE(const size_t nbytes, const unsigned int id);
293  ~DF_FILE();
294 
296  DF_FILE& operator=(const DF_FILE&) = delete;
297 
298  char* buff;
300  union
301  {
303  char fourb[sizeof(OFF_T)];
304  };
306  int file_ptr;
307 
308  void fwrite(const void *s, const size_t num_bytes);
309  void fread(void *s, const size_t num_bytes);
310 
311  void fwrite(const int &);
312  void fread(const int &);
313 
314  void fwrite(double);
315  void fread(const double &);
316 
317  void fread(void *&ptr);
318  void fwrite(void *ptr);
319 
321  void read_cmpdif_stack_buffer(OFF_T & lpos);
322 
323  void save_double_value(double x);
324  double restore_double_value();
325 
326  void save_int_value(int x);
327  int restore_int_value();
328 
329  void save_pointer_value(void *ptr);
330  void* restore_pointer_value();
331 
332  double restore_prevariable_value();
333 
335 
336  void save_dvar_vector_value(const dvar_vector& v);
337 
345 
346  void save_dvector_value(const dvector& v);
347  void save_dvector_position(const dvector& v);
348 
349  void save_dvar_vector_position(const dvar_vector& v);
351 
358  void save_dmatrix_value(const dmatrix& m);
359  void save_d3_array_value(const d3_array& a);
360  void save_dmatrix_position(const dmatrix& m);
361 
362  void save_ivector_position(const ivector& v);
364 
365  void save_ivector_value(const ivector& v);
366  void save_imatrix_value(const imatrix& m);
367 
368  void save_imatrix_position(const imatrix& m);
369  void save_dvar_matrix_position(const dvar_matrix& m);
370  void save_dvar_matrix_value(const dvar_matrix& m);
371 
372  void save_prevariable_position(const prevariable& v);
373  void save_prevariable_value(const prevariable& v);
374 
375  void save_d3_array_position(const d3_array& a);
376 
377 private:
379 #if defined(_MSC_VER) || defined(__MINGW64__)
380  unsigned int buff_size;
381 #else
382  size_t buff_size;
383 #endif
384 };
385 #endif
Description not yet available.
Definition: fvar.hpp:4883
void save_d3_array_value(const d3_array &a)
Definition: cmpdif5.cpp:50
d3_array restore_d3_array_value(const d3_array_position &mpos)
Definition: cmpdif5.cpp:127
indvar_offset_list * INDVAR_LIST
static int NUM_DEPENDENT_VARIABLES
Description not yet available.
Definition: fvar.hpp:920
Base class for dvariable.
Definition: fvar.hpp:1315
static void set_ARRAY_MEMBLOCK_SIZE(unsigned long i)
Set the the maximum amount of memory (in bytes) available for the autodif variable type container cla...
Definition: gs_set.cpp:57
#define _THREAD
void gradcalc(int nvar, const dvector &g)
Compute the gradient from the data stored in the global gradient_structure.
Definition: sgradclc.cpp:94
friend void default_evaluation(void)
Description not yet available.
Definition: def_eval.cpp:61
void jacobcalc(int nvar, const dmatrix &jac)
Description not yet available.
Definition: jacobclc.cpp:75
Description not yet available.
Definition: fvar.hpp:4440
Description not yet available.
Definition: imatrix.h:69
static void set_NO_DERIVATIVES(void)
Disable accumulation of derivative information.
Definition: gradstrc.cpp:641
double restore_prevariable_derivative()
Definition: cmpdif8.cpp:147
friend dmatrix restore_dvar_matrix_value(void)
Description not yet available.
Definition: imatrix.h:48
void save_ivector_position(const ivector &v)
Definition: cmpdif3.cpp:198
static void set_GRADSTACK_BUFFER_SIZE(const size_t i)
Set the number of entries contained in the buffer that, in turn, contains the information necessary f...
Definition: gs_set.cpp:119
virtual ~gradient_structure()
Destructor.
Definition: gradstrc.cpp:537
dependent_variables_information * DEPVARS_INFO
void create(const unsigned int size)
Allocate array of gradient_structure instances with size elements.
Definition: gradstrc.cpp:106
friend void funnel_derivatives(void)
Description not yet available.
static void set_USE_FOR_HESSIAN(const long int i)
DF_FILE()
Default uses gradient_structure::CMPDIF_BUFFER_SIZE.
friend dvector restore_dvar_vector_value(const dvar_vector_position &tmp)
Restores the size, address, and value information for a dvar_vector.
Definition: cmpdif4.cpp:227
#define x
void save_prevariable_value(const prevariable &v)
Definition: cmpdif8.cpp:76
void save_imatrix_position(const imatrix &m)
Definition: cmpdif9.cpp:39
Vector of double precision numbers.
Definition: dvector.h:50
ivector_position restore_ivector_position()
Definition: cmpdif4.cpp:49
void save_dmatrix_position(const dmatrix &m)
Definition: cmpdif6.cpp:31
static unsigned long ARRAY_MEMBLOCK_SIZE
Description not yet available.
Definition: fvar.hpp:3232
static long int USE_FOR_HESSIAN
char cmpdif_file_name[81]
Description not yet available.
Definition: fvar.hpp:4814
dmatrix_position restore_dmatrix_position()
Definition: cmpdif6.cpp:147
friend void cleanup_temporary_files()
Close gradient and variable files and free gradient structure memory.
Definition: gradstrc.cpp:192
size_t buff_size
imatrix restore_imatrix_value(const imatrix_position &mpos)
Definition: cmpdif9.cpp:89
static unsigned int NUM_RETURN_ARRAYS
Description not yet available.
Definition: fvar.hpp:4923
void save_dmatrix_value(const dmatrix &m)
Definition: cmpdif5.cpp:26
static void check_set_error(const char *variable_name)
Produce error if gradient structure hasn&#39;t been set.
Definition: gs_set.cpp:21
void save_dependent_variable_position(const prevariable &)
Description not yet available.
Definition: depvars.cpp:108
int operator==(void *p)
Description not yet available.
Definition: humung.cpp:60
ADMB variable vector.
Definition: fvar.hpp:2172
friend dvar_vector_position restore_dvar_vector_position(void)
Description not yet available.
Definition: cmpdif4.cpp:65
static void set_NUM_RETURN_ARRAYS(unsigned int i)
Set the maximum allowable depth of nesting of functions that return autodif variable types...
Definition: gs_set.cpp:46
gradient_structure(const long int size)
static unsigned int get_MAX_DLINKS()
void save_int_value(int x)
Definition: cmpdif8.cpp:108
static unsigned int MAX_NVAR_OFFSET
humungous_pointer & operator=(void *p)
Description not yet available.
Definition: humung.cpp:141
dvector restore_dvector_value(const dvector_position &tmp)
Definition: cmpdif4.cpp:178
dmatrix restore_dmatrix_value(const dmatrix_position &mpos)
Definition: cmpdif5.cpp:100
friend void slave_gradcalc(void)
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
friend dmatrix restore_dvar_matrix_derivatives(void)
void read_cmpdif_stack_buffer(OFF_T &lpos)
Description not yet available.
Definition: df_file.cpp:328
DF_FILE(const size_t nbytes)
User defined size with default id.
dmatrix restore_dvar_matrix_value(const dvar_matrix_position &mpos)
Definition: cmpdif5.cpp:74
static unsigned int RETURN_ARRAYS_SIZE
static void set_MAX_NVAR_OFFSET(unsigned int i)
Set the the maximum number of independent variables that can be used.
Definition: gs_set.cpp:218
size_t totalbytes(void)
Description not yet available.
Definition: gradstrc.cpp:183
void save_dvector_position(const dvector &v)
Definition: cmpdif4.cpp:32
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
Description not yet available.
Definition: fvar.hpp:4902
unsigned long int max_last_offset
prevariable_position restore_prevariable_position()
Definition: cmpdif8.cpp:43
static dvariable * next_RETURN_PTR()
Definition: gradstrc.cpp:457
static unsigned long get_ARRAY_MEMBLOCK_SIZE()
void save_prevariable_position(const prevariable &v)
Definition: cmpdif8.cpp:60
static DF_FILE * get_fp()
void save_dvector_value(const dvector &v)
Definition: cmpdif4.cpp:130
dvector restore_dvar_vector_value(const dvar_vector_position &tmp)
Definition: cmpdif4.cpp:231
void save_dvar_vector_value(const dvar_vector &v)
Definition: cmpdif4.cpp:106
dvar_vector_position restore_dvar_vector_position()
Definition: cmpdif4.cpp:69
void RETURN_ARRAYS_INCREMENT()
Definition: gradstrc.cpp:478
friend void wide_funnel_gradcalc(void)
Description not yet available.
Definition: fvar.hpp:3398
size_t NUM_GRADSTACK_BYTES_WRITTEN()
Description not yet available.
Definition: gradstrc.cpp:162
void clean()
Delete gradient_structure instances.
Definition: gradstrc.cpp:135
~DF_FILE()
Destructor.
Definition: df_file.cpp:233
Description not yet available.
Definition: fvar.hpp:4947
void make_indvar_list(const dvar_vector &t)
Definition: fvar_arr.cpp:163
char fourb[sizeof(OFF_T)]
static gradient_structure * reset(gradient_structure *)
Definition: gradstrc.cpp:120
double restore_double_value()
Definition: cmpdif8.cpp:186
humungous_pointer operator+(unsigned long int &offset)
Note that pointer addition is in bytes not the size of the object pointed to.
Definition: humung.cpp:113
static _THREAD gradient_structure * _instance
dvariable ** RETURN_PTR_CONTAINER
#define OFF_T
Definition: fvar.hpp:74
void adjust(const size_t)
Description not yet available.
Definition: humung.cpp:50
int restore_int_value()
Definition: cmpdif8.cpp:201
Description not yet available.
Definition: fvar.hpp:2819
Description not yet available.
Definition: fvar.hpp:1284
gradient_structure * set(const unsigned int id)
Set _instance of gradient_structure from instances with id.
Definition: gradstrc.cpp:126
void save_d3_array_position(const d3_array &a)
Definition: cmpdif6.cpp:63
static void set_CMPDIF_BUFFER_SIZE(const size_t i)
Set the size in bytes of the buffer used to contain the information generated by the &quot;precompiled&quot; de...
Definition: gs_set.cpp:75
static long int get_USE_FOR_HESSIAN()
friend void allocate_dvariable_space(void)
Description not yet available.
Definition: gradstrc.cpp:210
humungous_pointer & operator+=(unsigned long int &offset)
Note that pointer addition is in bytes not the size of the object pointed to.
Definition: humung.cpp:127
dvector_position restore_dvector_position()
Definition: cmpdif4.cpp:88
static void set_GRADSTACK_BUFFER_BYTES(const size_t i)
Set (in bytes) the number of entries contained in the buffer used for calculating derivatives...
Definition: gs_set.cpp:174
void save_dvar_vector_position(const dvar_vector &v)
Definition: cmpdif3.cpp:214
dvar_matrix_position restore_dvar_matrix_position()
Definition: cmpdif6.cpp:114
static _THREAD DF_FILE * fp
static void set_YES_SAVE_VARIABLES_VALUES()
Description not yet available.
Definition: gradstrc.cpp:660
static void set_NUM_DEPENDENT_VARIABLES(int i)
Description not yet available.
Definition: gradstrc.cpp:678
dvariable ** RETURN_ARRAYS
static grad_stack * get_GRAD_STACK1()
void save_variables()
Save variables to a buffer.
Definition: sgradclc.cpp:353
void save_dvar_matrix_position(const dvar_matrix &m)
Definition: cmpdif5.cpp:345
static int Hybrid_bounded_flag
void funnel_gradcalc()
Definition: xgradclc.cpp:86
static void set_RETURN_ARRAYS_SIZE(unsigned int i)
Set the return arrays size controlling the amount of complexity that one line of arithmetic can have...
Definition: gs_set.cpp:36
Link list.
Definition: fvar.hpp:834
static void set_NO_SAVE_VARIABLES_VALUES()
Description not yet available.
Definition: gradstrc.cpp:669
Class definition of matrix with derivitive information .
Definition: fvar.hpp:2480
unsigned int RETURN_ARRAYS_PTR
Stores the adjoint gradient data that will be processed by gradcalc.
void * restore_pointer_value()
Definition: cmpdif8.cpp:216
static size_t get_GRADSTACK_BUFFER_SIZE()
void restore_variables()
Restore variables from buffer.
Definition: sgradclc.cpp:360
int operator!=(void *p)
Description not yet available.
Definition: humung.cpp:69
ivector restore_ivector_value(const ivector_position &tmp)
Definition: cmpdif4.cpp:202
d3_array_position restore_d3_array_position()
Definition: cmpdif6.cpp:93
void fwrite(const void *s, const size_t num_bytes)
Reads num_bytes from s and writes to buffer.
Definition: df_file.cpp:296
double restore_prevariable_value()
Definition: cmpdif8.cpp:168
void save_dvar_matrix_value(const dvar_matrix &m)
Definition: cmpdif4.cpp:252
void fread(void *s, const size_t num_bytes)
Reads num_bytes from buffer and copies to s.
Definition: df_file.cpp:270
Description not yet available.
Definition: admodel.h:1850
static void set_YES_DERIVATIVES(void)
Enable accumulation of derivative information.
Definition: gradstrc.cpp:650
void save_arrays()
Compute the gradient from the data stored in the global gradient_structure.
Definition: sgradclc.cpp:223
void RETURN_ARRAYS_DECREMENT()
Definition: gradstrc.cpp:511
static unsigned int MAX_DLINKS
friend void gradfree(dlink *v)
Description not yet available.
Definition: fvar2.cpp:65
void save_pointer_value(void *ptr)
Definition: cmpdif8.cpp:122
void save_double_value(double x)
Definition: cmpdif8.cpp:94
humungous_pointer()
Default constructor.
Definition: humung.cpp:24
void save_imatrix_value(const imatrix &m)
Definition: cmpdif9.cpp:24
static size_t GRADSTACK_BUFFER_SIZE
Description not yet available.
Definition: fvar.hpp:2064
static _THREAD grad_stack * GRAD_STACK1
Description not yet available.
Definition: fvar.hpp:766
class for things related to the gradient structures, including dimension of arrays, size of buffers, etc.
Description not yet available.
Definition: fvar.hpp:3727
DF_FILE & operator=(const DF_FILE &)=delete
Do not allow assignment operator.
void free()
Description not yet available.
Definition: humung.cpp:34
friend dvector restore_dvar_vector_derivatives(void)
imatrix_position restore_imatrix_position()
Definition: cmpdif9.cpp:63
friend double_and_int * gradnew()
Creates an entry in the gradient structure linked list.
Definition: fvar2.cpp:36
Description not yet available.
gradient_structure & operator=(const gradient_structure &)=delete
Fundamental data type for reverse mode automatic differentiation.
Definition: fvar.hpp:1518
void save_ivector_value(const ivector &v)
Definition: cmpdif4.cpp:152
gradient_structure ** gradients
static void set_MAX_DLINKS(int i)
Set the maximum number of dvariable objects that can be in scope at one time.
Definition: gs_set.cpp:228
unsigned int gradients_size
int size(void)
Definition: fvar.hpp:3257
Description not yet available.
Definition: fvar.hpp:4843
static size_t CMPDIF_BUFFER_SIZE