ADMB Documentation  11.1.2504
 All Classes Files Functions Variables Typedefs Friends Defines
gradient_structure.h
Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2014 Regents of the University of California
00006  *
00007  * ADModelbuilder and associated libraries and documentations are
00008  * provided under the general terms of the "New BSD" license
00009  *
00010  * License:
00011  *
00012  * Redistribution and use in source and binary forms, with or without
00013  * modification, are permitted provided that the following conditions are
00014  * met:
00015  *
00016  * 1. Redistributions of source code must retain the above copyright
00017  * notice, this list of conditions and the following disclaimer.
00018  *
00019  * 2.  Redistributions in binary form must reproduce the above copyright
00020  * notice, this list of conditions and the following disclaimer in the
00021  * documentation and/or other materials provided with the distribution.
00022  *
00023  * 3.  Neither the name of the  University of California, Otter Research,
00024  * nor the ADMB Foundation nor the names of its contributors may be used
00025  * to endorse or promote products derived from this software without
00026  * specific prior written permission.
00027  *
00028  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00029  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00030  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00031  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00032  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00033  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00034  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00035  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00036  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00037  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00038  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00039  *
00040  */
00041 #ifndef __ADMB_GRADIENT_STRUCTURE_H__
00042 #define __ADMB_GRADIENT_STRUCTURE_H__
00043 
00044 #include <fstream>
00045 using std::ofstream;
00046 
00047 class dvariable;
00048 class DF_FILE;
00049 class dvector;
00050 class dmatrix;
00051 class dlink;
00052 class double_and_int;
00053 class arr_list;
00054 class dvar_vector;
00055 class dvar_vector_position;
00056 class prevariable;
00057 class indvar_offset_list;
00058 class dependent_variables_information;
00059 class grad_stack;
00060 class uostream;
00061 class dlist;
00062 
00067 class humungous_pointer
00068 {
00069 public:
00070   humungous_pointer();
00071   ~humungous_pointer() {}
00072 
00073   char *ptr;
00074   int adjustment;
00075   void free(void);
00076   void adjust(int);
00077   humungous_pointer operator +(unsigned long int &offset);
00078   humungous_pointer & operator +=(unsigned long int &offset);
00079   humungous_pointer & operator =(void *p);
00080   int operator ==(void *p);
00081   int operator !=(void *p);
00082   operator char *();
00083   operator void *();
00084   operator double_and_int *();
00085   operator double *();
00086 };
00087 
00092 class gradient_structure
00093 {
00094    static char cmpdif_file_name[61];
00095    static DF_FILE *fp;
00096  public:
00097 #if defined(NO_DERIVS)
00098    static int no_derivatives;
00099 #endif
00100  private:
00101    static long int USE_FOR_HESSIAN;
00102    static long int NVAR;
00103    static int NUM_RETURN_ARRAYS;
00104    static dvariable **RETURN_ARRAYS;
00105    static int RETURN_ARRAYS_PTR;
00106    static dvariable **RETURN_PTR_CONTAINER;
00107    static long int TOTAL_BYTES;
00108    static long int PREVIOUS_TOTAL_BYTES;
00109    static unsigned long ARRAY_MEMBLOCK_SIZE;//js
00110    static humungous_pointer ARRAY_MEMBLOCK_BASE;
00111    static humungous_pointer ARRAY_MEMBLOCK_BASEA;
00112    static humungous_pointer ARRAY_MEMBLOCK_SAVE;
00113  public:
00114    static double *get_ARRAY_MEMBLOCK_BASE()
00115    {
00116       return (double*)ARRAY_MEMBLOCK_BASE;
00117    }
00118  private:
00119    static double *variables_save;
00120 #ifdef __BORLANDC__
00121    static long int CMPDIF_BUFFER_SIZE;
00122    static long int GRADSTACK_BUFFER_SIZE;
00123 #else
00124    static size_t CMPDIF_BUFFER_SIZE;
00125    static size_t GRADSTACK_BUFFER_SIZE;
00126 #endif
00127    static unsigned int MAX_NVAR_OFFSET;
00128    static int save_var_file_flag;
00129    static int save_var_flag;
00130 
00131    static unsigned int MAX_DLINKS;
00132    static indvar_offset_list *INDVAR_LIST;
00133    static int NUM_DEPENDENT_VARIABLES;
00134    static dependent_variables_information *DEPVARS_INFO;
00135 
00136    // this needs to be a static member function so other static
00137    // member functions can call it
00138    static void check_set_error(const char *variable_name);
00139 
00140    static int instances;
00141    int x;
00142 
00143  public:
00144    // exception class
00145    class arrmemblerr
00146    {
00147    };
00148 
00149    static int Hybrid_bounded_flag;
00150    static double *hessian_ptr;
00151    static long int get_USE_FOR_HESSIAN()
00152    {
00153       return USE_FOR_HESSIAN;
00154    }
00155    static void set_USE_FOR_HESSIAN(const long int i)
00156    {
00157       USE_FOR_HESSIAN = i;
00158    }
00159    friend class dfsdmat;
00160    gradient_structure(long int size = 100000L);// constructor
00161    ~gradient_structure(void);// destructor
00162    static void save_variables(void);
00163    static void restore_variables(void);
00164    static void save_arrays(void);
00165    static void restore_arrays(void);
00166    static long int totalbytes(void);
00167    friend dvector restore_dvar_vector_value(
00168      const dvar_vector_position& tmp);
00169    friend void cleanup_temporary_files();
00170    //friend dvector restore_dvar_vector_value(const dvar_vector_position&);
00171    friend dvector restore_dvar_vector_derivatives(void);
00172    friend dmatrix restore_dvar_matrix_derivatives(void);
00173    friend dmatrix restore_dvar_matrix_value(void);
00174    //friend dmatrix restore_derivatives(void);
00175    friend void gradfree(dlink * v);
00176    friend double_and_int *arr_new(unsigned int sz);//js
00177    friend void arr_free(double_and_int *);
00178    friend void RETURN_ARRAYS_DECREMENT(void);
00179    friend void RETURN_ARRAYS_INCREMENT(void);
00180    friend void make_indvar_list(const dvar_vector & t);
00181    //friend void gradcalc( int , double *);
00182    friend void gradcalc(int nvar, const dvector & g);
00183    friend void slave_gradcalc(void);
00184    friend void funnel_gradcalc(void);
00185    friend void allocate_dvariable_space(void);
00186    friend void wide_funnel_gradcalc(void);
00187    friend dvar_vector_position restore_dvar_vector_position(void);
00188    static grad_stack *GRAD_STACK1;
00189    friend double_and_int *gradnew();
00190    static dlist *GRAD_LIST;
00191    static int RETURN_ARRAYS_SIZE;
00192    //static int RETURN_INDEX;
00193    static dvariable *RETURN_PTR;
00194    static dvariable *MIN_RETURN;
00195    static dvariable *MAX_RETURN;
00196    static arr_list *ARR_LIST1;
00197    static arr_list *ARR_FREE_LIST1;
00198    //static void funnel_jacobcalc(void);
00199    static void jacobcalc(int nvar, const dmatrix & jac);
00200    static void jacobcalc(int nvar, const ofstream & jac);
00201    static void jacobcalc(int nvar, const uostream & jac);
00202 
00203    friend void default_evaluation(void);
00204    //access functions
00205 
00206    friend class DF_FILE;
00207    static DF_FILE *get_fp(void)
00208    {
00209       return fp;
00210    }
00211    static void set_NUM_RETURN_ARRAYS(int i);
00212 #if defined(NO_DERIVS)
00213    static void set_NO_DERIVATIVES(void);
00214    static void set_YES_DERIVATIVES(void);
00215 #endif
00216    static void set_YES_SAVE_VARIABLES_VALUES();
00217    static void set_NO_SAVE_VARIABLES_VALUES();
00218    //static int _GRADFILE_PTR; // should be int gradfile_handle;
00219    //static int _GRADFILE_PTR1; // should be int gradfile_handle;
00220    //static int _GRADFILE_PTR2; // should be int gradfile_handle;
00221    //static int _VARSSAV_PTR; // should be int gradfile_handle;
00222    static void set_NUM_DEPENDENT_VARIABLES(int i);
00223    static void set_RETURN_ARRAYS_SIZE(int i);
00224    static void set_ARRAY_MEMBLOCK_SIZE(unsigned long i);
00225 #ifdef __BORLANDC__
00226    static void set_CMPDIF_BUFFER_SIZE(long int i);
00227    static void set_GRADSTACK_BUFFER_SIZE(long int i);
00228    static void set_GRADSTACK_BUFFER_BYTES(long int i);
00229 #else
00230    static void set_CMPDIF_BUFFER_SIZE(const size_t i);
00231    static void set_GRADSTACK_BUFFER_SIZE(const size_t i);
00232    static void set_GRADSTACK_BUFFER_BYTES(const size_t i);
00233 #endif
00234    static void set_MAX_NVAR_OFFSET(unsigned int i);
00235    static void set_MAX_DLINKS(int i);
00236    static long int NUM_GRADSTACK_BYTES_WRITTEN(void);
00237    friend class dlist;
00238    friend class grad_stack;
00239    static void save_dependent_variable_position(const prevariable & v1);
00240    static long int max_last_offset;
00241    friend class function_minimizer;
00242    friend void funnel_derivatives(void);
00243 };
00244 
00249 class DF_FILE
00250 {
00251 public:
00252   DF_FILE(const size_t nbytes);
00253   ~DF_FILE();
00254 
00255   char* buff;
00256   size_t toffset;
00257   union
00258   {
00259     size_t offset;
00260     char fourb[sizeof(size_t)];
00261   };
00262   char cmpdif_file_name[81];
00263   int file_ptr;
00264 
00265   void fwrite(const void *s, const size_t num_bytes);
00266   void fread(void *s, const size_t num_bytes);
00267 
00268   void fwrite(const int &);
00269   void fread(const int &);
00270 
00271   void fwrite(double);
00272   void fread(const double &);
00273 
00274   void fread(void *&ptr);
00275   void fwrite(void *ptr);
00276 
00277   void write_cmpdif_stack_buffer(void);
00278   void read_cmpdif_stack_buffer(off_t & lpos);
00279 
00280 private:
00281 #ifdef _MSC_VER
00282   size_t buff_end;
00283   size_t buff_size;
00284 #else
00285   const size_t buff_end;
00286   const size_t buff_size;
00287 #endif
00288 };
00289 #endif