ADMB Documentation  11.5.3197
 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   size_t adjustment;
00070 public:
00071   humungous_pointer();
00072   ~humungous_pointer() {}
00073 
00074   char* ptr;
00075   void free();
00076   void adjust(const size_t);
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 size_t TOTAL_BYTES;
00108    static size_t PREVIOUS_TOTAL_BYTES;
00109    static unsigned long ARRAY_MEMBLOCK_SIZE;//js
00110    static humungous_pointer ARRAY_MEMBLOCK_BASE;
00111    static humungous_pointer ARRAY_MEMBLOCK_SAVE;
00112  public:
00113    static double *get_ARRAY_MEMBLOCK_BASE()
00114    {
00115       return (double*)ARRAY_MEMBLOCK_BASE;
00116    }
00117  private:
00118 #ifdef __BORLANDC__
00119    static long int CMPDIF_BUFFER_SIZE;
00120    static long int GRADSTACK_BUFFER_SIZE;
00121 #else
00122    static size_t CMPDIF_BUFFER_SIZE;
00123    static size_t GRADSTACK_BUFFER_SIZE;
00124 #endif
00125    static unsigned int MAX_NVAR_OFFSET;
00126    static int save_var_file_flag;
00127    static int save_var_flag;
00128 
00129    static unsigned int MAX_DLINKS;
00130    static indvar_offset_list *INDVAR_LIST;
00131    static int NUM_DEPENDENT_VARIABLES;
00132    static dependent_variables_information *DEPVARS_INFO;
00133 
00134    // this needs to be a static member function so other static
00135    // member functions can call it
00136    static void check_set_error(const char *variable_name);
00137 
00138    static int instances;
00139    int x;
00140 
00141  public:
00142    // exception class
00143    class arrmemblerr
00144    {
00145    };
00146 
00147    static int Hybrid_bounded_flag;
00148    static double *hessian_ptr;
00149    static long int get_USE_FOR_HESSIAN()
00150    {
00151       return USE_FOR_HESSIAN;
00152    }
00153    static void set_USE_FOR_HESSIAN(const long int i)
00154    {
00155       USE_FOR_HESSIAN = i;
00156    }
00157    friend class dfsdmat;
00158    gradient_structure(long int size = 100000L);// constructor
00159    ~gradient_structure(void);// destructor
00160    static void save_variables(void);
00161    static void restore_variables(void);
00162    static void save_arrays(void);
00163    static void restore_arrays(void);
00164    static size_t totalbytes(void);
00165    friend dvector restore_dvar_vector_value(
00166      const dvar_vector_position& tmp);
00167    friend void cleanup_temporary_files();
00168    //friend dvector restore_dvar_vector_value(const dvar_vector_position&);
00169    friend dvector restore_dvar_vector_derivatives(void);
00170    friend dmatrix restore_dvar_matrix_derivatives(void);
00171    friend dmatrix restore_dvar_matrix_value(void);
00172    //friend dmatrix restore_derivatives(void);
00173    friend void gradfree(dlink * v);
00174    friend double_and_int *arr_new(unsigned int sz);//js
00175    friend void arr_free(double_and_int *);
00176    friend void RETURN_ARRAYS_DECREMENT(void);
00177    friend void RETURN_ARRAYS_INCREMENT(void);
00178    friend void make_indvar_list(const dvar_vector & t);
00179    //friend void gradcalc( int , double *);
00180    friend void gradcalc(int nvar, const dvector & g);
00181    friend void slave_gradcalc(void);
00182    friend void funnel_gradcalc(void);
00183    friend void allocate_dvariable_space(void);
00184    friend void wide_funnel_gradcalc(void);
00185    friend dvar_vector_position restore_dvar_vector_position(void);
00186    static grad_stack *GRAD_STACK1;
00187    friend double_and_int *gradnew();
00188    static dlist *GRAD_LIST;
00189    static int RETURN_ARRAYS_SIZE;
00190    //static int RETURN_INDEX;
00191    static dvariable *RETURN_PTR;
00192    static dvariable *MIN_RETURN;
00193    static dvariable *MAX_RETURN;
00194    static arr_list *ARR_LIST1;
00195    static arr_list *ARR_FREE_LIST1;
00196    //static void funnel_jacobcalc(void);
00197    static void jacobcalc(int nvar, const dmatrix & jac);
00198    static void jacobcalc(int nvar, const ofstream & jac);
00199    static void jacobcalc(int nvar, const uostream & jac);
00200 
00201    friend void default_evaluation(void);
00202    //access functions
00203 
00204    friend class DF_FILE;
00205    static DF_FILE *get_fp(void)
00206    {
00207       return fp;
00208    }
00209    static void set_NUM_RETURN_ARRAYS(int i);
00210 #if defined(NO_DERIVS)
00211    static void set_NO_DERIVATIVES(void);
00212    static void set_YES_DERIVATIVES(void);
00213 #endif
00214    static void set_YES_SAVE_VARIABLES_VALUES();
00215    static void set_NO_SAVE_VARIABLES_VALUES();
00216    //static int _GRADFILE_PTR; // should be int gradfile_handle;
00217    //static int _GRADFILE_PTR1; // should be int gradfile_handle;
00218    //static int _GRADFILE_PTR2; // should be int gradfile_handle;
00219    //static int _VARSSAV_PTR; // should be int gradfile_handle;
00220    static void set_NUM_DEPENDENT_VARIABLES(int i);
00221    static void set_RETURN_ARRAYS_SIZE(int i);
00222    static void set_ARRAY_MEMBLOCK_SIZE(unsigned long i);
00223 #ifdef __BORLANDC__
00224    static void set_CMPDIF_BUFFER_SIZE(long int i);
00225    static void set_GRADSTACK_BUFFER_SIZE(long int i);
00226    static void set_GRADSTACK_BUFFER_BYTES(long int i);
00227 #else
00228    static void set_CMPDIF_BUFFER_SIZE(const size_t i);
00229    static void set_GRADSTACK_BUFFER_SIZE(const size_t i);
00230    static void set_GRADSTACK_BUFFER_BYTES(const size_t i);
00231 #endif
00232    static void set_MAX_NVAR_OFFSET(unsigned int i);
00233    static void set_MAX_DLINKS(int i);
00234    static size_t NUM_GRADSTACK_BYTES_WRITTEN(void);
00235    friend class dlist;
00236    friend class grad_stack;
00237    static void save_dependent_variable_position(const prevariable & v1);
00238    static unsigned long int max_last_offset;
00239    friend class function_minimizer;
00240    friend void funnel_derivatives(void);
00241 };
00242 
00247 class DF_FILE
00248 {
00249 public:
00250   DF_FILE(const size_t nbytes);
00251   ~DF_FILE();
00252 
00253   char* buff;
00254   size_t toffset;
00255   union
00256   {
00257     size_t offset;
00258     char fourb[sizeof(size_t)];
00259   };
00260   char cmpdif_file_name[81];
00261   int file_ptr;
00262 
00263   void fwrite(const void *s, const size_t num_bytes);
00264   void fread(void *s, const size_t num_bytes);
00265 
00266   void fwrite(const int &);
00267   void fread(const int &);
00268 
00269   void fwrite(double);
00270   void fread(const double &);
00271 
00272   void fread(void *&ptr);
00273   void fwrite(void *ptr);
00274 
00275   void write_cmpdif_stack_buffer(void);
00276   void read_cmpdif_stack_buffer(off_t & lpos);
00277 
00278 private:
00279 #ifdef _MSC_VER
00280   size_t buff_end;
00281   size_t buff_size;
00282 #else
00283   const size_t buff_end;
00284   const size_t buff_size;
00285 #endif
00286 };
00287 #endif