ADMB Documentation  11.1.2192
 All Classes Files Functions Variables Typedefs Friends Defines
gradstrc.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: gradstrc.cpp 1909 2014-04-17 23:00:42Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #include "fvar.hpp"
00012 
00013 //#define THREAD_SAFE
00014 
00015 #if defined(THREAD_SAFE)
00016   pthread_mutex_t mutex_return_arrays = PTHREAD_MUTEX_INITIALIZER;
00017 #endif
00018 
00019 void null_ptr_err_message(void);
00020 
00021 #include <string.h>
00022 
00023 #if defined(__TURBOC__)
00024   #pragma hdrstop
00025   #include <iostream.h>
00026   #include <alloc.h>
00027 #endif
00028 
00029 #include <stdlib.h>
00030 
00031 #ifdef __ZTC__
00032   #include <iostream.hpp>
00033   int _cdecl farfree(void _far *ptr);
00034   void _far * _cdecl farmalloc(unsigned long size);
00035 #endif
00036 
00037 extern ofstream clogf;
00038 #ifndef __SUNPRO_CC
00039 typedef int (* fptr) (const char * format, ...) ;
00040 #endif
00041 
00042 //void adwait(double);
00043 fptr ad_printf = printf;
00044 extern "C"{
00045   exitptr ad_exit=&exit;
00046 
00047   void spdll_exit(int ierr)
00048   {
00049     if (ad_printf) (*ad_printf) (" Exception -- error code %d\n",ierr);
00050     if (ad_printf) (*ad_printf) (" Pause");
00051     //adwait(4.0);
00052 #if defined(USE_EXCEPTIONS)
00053     throw spdll_exception(ierr);
00054 #endif
00055   }
00056 }
00057 
00058 //char * MY_BUF=NULL;
00059 // *************************************************************
00060 // *************************************************************
00061 int ctlc_flag = 0;
00062 int gradient_structure::Hybrid_bounded_flag=0;
00063 DF_FILE * gradient_structure::fp=NULL;
00064 char gradient_structure::cmpdif_file_name[61];
00065 //char gradient_structure::var_store_file_name[61];
00066 int gradient_structure::NUM_RETURN_ARRAYS = 25;
00067 double * gradient_structure::hessian_ptr=NULL;
00068 int gradient_structure::NUM_DEPENDENT_VARIABLES = 2000;
00069 #if (defined(NO_DERIVS))
00070   int gradient_structure::no_derivatives = 0;
00071 #endif
00072 long int gradient_structure::max_last_offset = 0;
00073 long int gradient_structure::NVAR = 0;
00074 long int gradient_structure::TOTAL_BYTES = 0;
00075 long int gradient_structure::PREVIOUS_TOTAL_BYTES = 0;
00076 long int gradient_structure::USE_FOR_HESSIAN = 0;
00077 dvariable** gradient_structure::RETURN_ARRAYS = NULL;
00078 int gradient_structure::RETURN_ARRAYS_PTR;
00079 dvariable ** gradient_structure::RETURN_PTR_CONTAINER = NULL;
00080 int gradient_structure::RETURN_ARRAYS_SIZE = 70;
00081 int gradient_structure::instances = 0;
00082 //int gradient_structure::RETURN_INDEX = 0;
00083 //dvariable * gradient_structure::FRETURN = NULL;
00084 dvariable * gradient_structure::MAX_RETURN = NULL;
00085 dvariable * gradient_structure::MIN_RETURN = NULL;
00086 dvariable * gradient_structure::RETURN_PTR = NULL;
00087 #ifdef __BORLANDC__
00088 long int gradient_structure::GRADSTACK_BUFFER_SIZE = 4000000L;
00089 long int gradient_structure::CMPDIF_BUFFER_SIZE=140000000L;
00090 #else
00091 long long int gradient_structure::GRADSTACK_BUFFER_SIZE = 4000000L;
00092 long long int gradient_structure::CMPDIF_BUFFER_SIZE=140000000L;
00093 #endif
00094 
00095 dependent_variables_information * gradient_structure::DEPVARS_INFO=NULL;
00096 
00097 int gradient_structure::save_var_flag=0;
00098 int gradient_structure::save_var_file_flag=0;
00099 
00100 // should be int gradfile_handle;
00101 //int gradient_structure::_GRADFILE_PTR = NULL;
00102 
00103 // should be int gradfile_handle;
00104 //int gradient_structure::_GRADFILE_PTR1 = NULL;
00105 
00106 // should be int gradfile_handle;
00107 //int gradient_structure::_GRADFILE_PTR2 = NULL;
00108 
00109 // should be int gradfile_handle;
00110 //int gradient_structure::_VARSSAV_PTR = 0;
00111 
00112 unsigned int gradient_structure::MAX_NVAR_OFFSET = 5000;
00113 unsigned long gradient_structure::ARRAY_MEMBLOCK_SIZE = 0L; //js
00114 dlist * gradient_structure::GRAD_LIST;
00115 grad_stack * gradient_structure::GRAD_STACK1;
00116 indvar_offset_list * gradient_structure::INDVAR_LIST = NULL;
00117 arr_list * gradient_structure::ARR_LIST1 = NULL;
00118 arr_list * gradient_structure::ARR_FREE_LIST1 = NULL;
00119 unsigned int gradient_structure::MAX_DLINKS = 5000;
00120 
00121 // note: ARRAY_MEMBLOCK stuff is set by tpl2cpp for historical reasons
00122 //       those settings could be moved into this file in the future
00123 //       - Ian Taylor 5/3/2012
00124 
00125 //unsigned long int gradient_structure::ARRAY_MEMBLOCK_BASE = 0L;
00126 humungous_pointer gradient_structure::ARRAY_MEMBLOCK_BASE;
00127 humungous_pointer gradient_structure::ARRAY_MEMBLOCK_BASEA;
00128 humungous_pointer gradient_structure::ARRAY_MEMBLOCK_SAVE;
00129 double * gradient_structure::variables_save=NULL;
00130 void * farptr_norm(void *);
00131 long int farptr_tolong(void *) ;
00132 void memory_allocate_error(const char * s, void * ptr);
00133 
00138 long int gradient_structure::NUM_GRADSTACK_BYTES_WRITTEN(void)
00139 {
00140   long int tmp = TOTAL_BYTES - PREVIOUS_TOTAL_BYTES;
00141   PREVIOUS_TOTAL_BYTES = TOTAL_BYTES;
00142   return tmp;
00143 }
00144 
00149  char lastchar(char * s)
00150  {
00151    int k=strlen(s);
00152    return s[k-1];
00153  }
00154 
00159  long int gradient_structure::totalbytes(void)
00160  {
00161    return TOTAL_BYTES;
00162  }
00163 
00164  void fill_ad_random_part(void);
00165  extern char ad_random_part[6];
00166 
00170 void cleanup_temporary_files()
00171 {
00172    if (gradient_structure::fp)
00173    {
00174      delete gradient_structure::fp;
00175      gradient_structure::fp=NULL;
00176    }
00177    if (gradient_structure::GRAD_STACK1)
00178    {
00179      if (close(gradient_structure::GRAD_STACK1->_GRADFILE_PTR1))
00180      {
00181        cerr << "Error closing file "
00182        << gradient_structure::GRAD_STACK1->gradfile_name1 << "\n";
00183      }
00184      if (close(gradient_structure::GRAD_STACK1->_GRADFILE_PTR2))
00185      {
00186        cerr << "Error closing file "
00187        << gradient_structure::GRAD_STACK1->gradfile_name2 << "\n";
00188      }
00189      if (close( gradient_structure::GRAD_STACK1->_VARSSAV_PTR))
00190      {
00191        cerr << "Error closing file "
00192        << gradient_structure::GRAD_STACK1->var_store_file_name << "\n";
00193      }
00194    }
00195 #if !defined (_MSC_VER)
00196    if (gradient_structure::GRAD_STACK1)
00197    {
00198      unlink(gradient_structure::GRAD_STACK1->gradfile_name1);
00199      unlink(gradient_structure::GRAD_STACK1->gradfile_name2);
00200      unlink(gradient_structure::GRAD_STACK1->var_store_file_name);
00201      //unlink(gradient_structure::cmpdif_file_name);
00202    }
00203 #else
00204    if (gradient_structure::GRAD_STACK1)
00205    {
00206      remove(gradient_structure::GRAD_STACK1->gradfile_name1);
00207      remove(gradient_structure::GRAD_STACK1->gradfile_name2);
00208      remove(gradient_structure::GRAD_STACK1->var_store_file_name);
00209      //cout << remove(gradient_structure::cmpdif_file_name);
00210    }
00211 #endif
00212 }
00213 
00218 void allocate_dvariable_space(void)
00219 {
00220   int on,nopt = 0;
00221   if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mdl",nopt))>-1)
00222   {
00223     if (nopt ==1)
00224     {
00225       gradient_structure::MAX_DLINKS=atoi(ad_comm::argv[on+1]);
00226     }
00227     else
00228     {
00229       cerr << "Wrong number of options to -mdl -- must be 1"
00230         " you have " << nopt << endl;
00231       ad_exit(1);
00232     }
00233   }
00234   unsigned int numlinks=gradient_structure::MAX_DLINKS;
00235   //cout << sizeof(dlink) << endl;
00236 
00237   if (sizeof(char)!=1)
00238   {
00239     cerr << "sizeof(char) is not equal 1) --"
00240        " need to modify allocate_dvariable_space in gradstrc.cpp" << endl;
00241   }
00242   if (sizeof(dlink)>2*sizeof(double))
00243   {
00244     cerr << "sizeof(dlink) is greater than 2*sizeof(double) --"
00245        " need to modify allocate_dvariable_space in gradstrc.cpp" << endl;
00246   }
00247   char * tmp= (char*) malloc(2.0*sizeof(double)*(numlinks+1));
00248   char * tmp1=tmp;
00249 
00250   dlink * dl=(dlink*)tmp1;
00251   tmp1+=2*sizeof(double);
00252   dl->prev=NULL;
00253   dlink * prev=dl;
00254   int& nlinks=(int&)gradient_structure::GRAD_LIST->nlinks;
00255   gradient_structure::GRAD_LIST->dlink_addresses[nlinks++]=dl;
00256   for (unsigned int i=1;i<=numlinks;i++)
00257   {
00258     dl=(dlink*)tmp1;
00259     dl->prev=prev;
00260     prev=dl;
00261     tmp1+=2*sizeof(double);
00262 
00263     gradient_structure::GRAD_LIST->dlink_addresses[nlinks++]=dl;
00264       // keep track of the links so you can
00265   }                               // zero them out
00266   gradient_structure::GRAD_LIST->last=dl;
00267 }
00268 
00273  gradient_structure::gradient_structure(long int _size)
00274  {
00275    gradient_structure::NVAR=0;
00276    atexit(cleanup_temporary_files);
00277    fill_ad_random_part();
00278    long int size;
00279    size=_size;
00280 
00281    if (instances++ > 0)
00282    {
00283      cerr << "More than one gradient_structure object has been declared.\n"
00284           << "  Only one gradient_structure object can exist. Check the scope\n"
00285           << "  of the objects declared.\n";
00286      ad_exit(1);
00287    }
00288    gradient_structure::ARRAY_MEMBLOCK_SIZE=size; //js
00289 
00290    char * path = getenv("ADTMP1"); // NULL if not defined
00291    if (path != NULL)
00292    {
00293      #ifdef __SUN__
00294      sprintf(&cmpdif_file_name[0],"%s/cmpdiff.%s", path,
00295           ad_random_part);
00296      #else
00297         if (lastchar(path)!='\\')
00298         {
00299           sprintf(&cmpdif_file_name[0],"%s\\cmpdiff.%s", path,
00300             ad_random_part);
00301         }
00302         else
00303         {
00304           sprintf(&cmpdif_file_name[0],"%scmpdiff.%s", path,
00305             ad_random_part);
00306         }
00307      #endif
00308    }
00309    else
00310    {
00311       sprintf(&cmpdif_file_name[0],"cmpdiff.%s",ad_random_part);
00312    }
00313 
00314    if (DEPVARS_INFO!= NULL)
00315    {
00316       cerr << "  0 Trying to allocate to a non NULL pointer in gradient"
00317               "_structure" << endl;
00318    }
00319    else
00320    {
00321      int on,nopt = 0;
00322      if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-ndv",nopt))>-1)
00323      {
00324        if (!nopt)
00325        {
00326          cerr << "Usage -ndv option needs integer  -- ignored" << endl;
00327        }
00328        else
00329        {
00330          int jj=atoi(ad_comm::argv[on+1]);
00331          if (jj<=0)
00332          {
00333            cerr << "Usage -ndv option needs positive integer"
00334               "  -- ignored" << endl;
00335          }
00336          else
00337          {
00338            NUM_DEPENDENT_VARIABLES=jj;
00339          }
00340        }
00341      }
00342      DEPVARS_INFO=new dependent_variables_information(NUM_DEPENDENT_VARIABLES);
00343      memory_allocate_error("DEPVARS_INFO", (void *) DEPVARS_INFO);
00344    }
00345 
00346    if (fp!= NULL)
00347    {
00348       cerr << "  0 Trying to allocate to a non NULL pointer in gradient"
00349               "_structure" << endl;
00350    }
00351    else
00352    {
00353      fp=new DF_FILE(CMPDIF_BUFFER_SIZE);
00354      memory_allocate_error("fp", (void *) fp);
00355    }
00356 
00357    void * temp_ptr;
00358   // double_and_int * tmp;
00359    int i;
00360    #ifdef DIAG
00361      cerr <<" In gradient_structure::gradient_structure()\n";
00362      cerr <<"  ARRAY_MEMBLOCK_SIZE = " << ARRAY_MEMBLOCK_SIZE << "\n";
00363    #endif
00364 
00365    if ( GRAD_LIST!= NULL)
00366    {
00367     cerr << "Trying to allocate to a non NULL pointer in gradient structure\n";
00368    }
00369    else
00370    {
00371       GRAD_LIST = new dlist;
00372       memory_allocate_error("GRAD_LIST", (void *) GRAD_LIST);
00373    }
00374 
00375    if ( ARR_LIST1!= NULL)
00376    {
00377      cerr << "Trying to allocate to a non NULL pointer in gradient structure\n";
00378    }
00379    else
00380    {
00381       ARR_LIST1 = new arr_list;
00382       memory_allocate_error("ARR_LIST1", (void *) ARR_LIST1);
00383    }
00384 
00385  /*
00386    if ( ARR_FREE_LIST1!= NULL)
00387    {
00388 cerr << "  2 Trying to allocate to a non NULL pointer in gradient structure \n";
00389    }
00390    else
00391    {
00392       ARR_FREE_LIST1 = new arr_list;
00393       memory_allocate_error("ARR_FREE_LIST1", (void *) ARR_FREE_LIST1);
00394    }
00395  */
00396 
00397 #ifdef __ZTC__
00398    if ((temp_ptr = farmalloc(ARRAY_MEMBLOCK_SIZE)) == 0)
00399 #else
00400    if ((temp_ptr = (void*)malloc(ARRAY_MEMBLOCK_SIZE)) == 0)
00401 #endif
00402    {
00403      cerr << "insufficient memory to allocate space for ARRAY_MEMBLOCKa\n";
00404      ad_exit(1);
00405    }
00406 
00407   /*
00408    if (ARRAY_MEMBLOCK_BASE != NULL)
00409    {
00410 cerr << "Trying to allocate to a non NULL pointer in gradient structure \n";
00411    }
00412  */
00413 
00414    ARRAY_MEMBLOCK_BASE = temp_ptr;
00415 
00416    //cout << (void*) ARRAY_MEMBLOCK_BASE.ptr  << "   ";
00417    //cout << (int) ARRAY_MEMBLOCK_BASE.ptr  << endl;
00418 #if defined(__x86_64)
00419    intptr_t adjustment=(8-((intptr_t)ARRAY_MEMBLOCK_BASE.ptr)%8)%8;
00420 #else
00421    int adjustment=(8-((int) ARRAY_MEMBLOCK_BASE.ptr)%8)%8;
00422 #endif
00423    //cout << ((int) ARRAY_MEMBLOCK_BASE.ptr)%8  << endl;
00424    ARRAY_MEMBLOCK_BASE.adjust(adjustment);
00425    //cout << ((int) ARRAY_MEMBLOCK_BASE.ptr)%8  << endl;
00426 
00427    if (GRAD_STACK1 != NULL)
00428    {
00429       cerr << " 3 Trying to allocate to a non NULL pointer\n";
00430    }
00431    else
00432    {
00433      GRAD_STACK1 = new grad_stack;
00434      memory_allocate_error("GRAD_STACK1",GRAD_STACK1);
00435      gradient_structure::hessian_ptr= (double*) GRAD_STACK1->true_ptr_first;
00436    }
00437     #ifdef DIAG
00438         cout << "GRAD_STACK1= "<< farptr_tolong(GRAD_STACK1)<<"\n";
00439     #endif
00440 
00441    if ( INDVAR_LIST!= NULL)
00442    {
00443 cerr << "Trying to allocate to a non NULL pointer in gradient structure \n";
00444       ad_exit(1);
00445    }
00446    else
00447    {
00448      INDVAR_LIST = new indvar_offset_list;
00449      memory_allocate_error("INDVAR_LIST",INDVAR_LIST);
00450  // ****************************************************************
00451  // ****************************************************************
00452       int nopt=0;
00453       int on=0;
00454 
00455       if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mno",nopt))>-1)
00456       {
00457         if (nopt ==1)
00458         {
00459           MAX_NVAR_OFFSET=atoi(ad_comm::argv[on+1]);
00460         }
00461         else
00462         {
00463           cerr << "Wrong number of options to -mno -- must be 1"
00464             " you have " << nopt << endl;
00465           ad_exit(1);
00466         }
00467       }
00468 
00469  // ****************************************************************
00470  // ****************************************************************
00471 
00472      INDVAR_LIST->address = new double * [ (size_t) MAX_NVAR_OFFSET];
00473      memory_allocate_error("INDVAR_LIST->address",INDVAR_LIST->address);
00474    }
00475 
00476    //allocate_dvariable_space();
00477 
00478    if ( RETURN_ARRAYS!= NULL)
00479    {
00480 cerr << "Trying to allocate to a non NULL pointer in gradient structure \n";
00481       ad_exit(1);
00482    }
00483    else
00484    {
00485       RETURN_ARRAYS = new dvariable*[NUM_RETURN_ARRAYS];
00486       memory_allocate_error("RETURN_ARRAYS",RETURN_ARRAYS);
00487 
00488       //allocate_dvariable_space();
00489       for (i=0; i< NUM_RETURN_ARRAYS; i++)
00490       {
00491         RETURN_ARRAYS[i]=new dvariable[RETURN_ARRAYS_SIZE];
00492         memory_allocate_error("RETURN_ARRAYS[i]",RETURN_ARRAYS[i]);
00493       }
00494       RETURN_ARRAYS_PTR=0;
00495       MIN_RETURN = RETURN_ARRAYS[RETURN_ARRAYS_PTR];
00496       MAX_RETURN = RETURN_ARRAYS[RETURN_ARRAYS_PTR]+RETURN_ARRAYS_SIZE-1;
00497       RETURN_PTR = MIN_RETURN;
00498    }
00499    //RETURN_INDEX = 0;
00500 
00501    RETURN_PTR_CONTAINER=new dvariable*[NUM_RETURN_ARRAYS];
00502    memory_allocate_error("RETURN_INDICES_CONTAINER",RETURN_PTR_CONTAINER);
00503 
00504    for (i=0; i< NUM_RETURN_ARRAYS; i++)
00505    {
00506      RETURN_PTR_CONTAINER[i]=0;
00507    }
00508  }
00509 
00516 void RETURN_ARRAYS_INCREMENT(void)
00517 {
00518 #if defined(THREAD_SAFE)
00519   pthread_mutex_lock(&mutex_return_arrays);
00520 #endif
00521   gradient_structure::RETURN_PTR_CONTAINER[
00522     gradient_structure::RETURN_ARRAYS_PTR]=gradient_structure::RETURN_PTR;
00523   if (++gradient_structure::RETURN_ARRAYS_PTR ==
00524     gradient_structure::NUM_RETURN_ARRAYS)
00525   {
00526     cerr << " Overflow in RETURN_ARRAYS stack -- Increase NUM_RETURN_ARRAYS\n";
00527     cerr << " There may be a RETURN_ARRAYS_INCREMENT()\n";
00528     cerr << " which is not matched by a RETURN_ARRAYS_DECREMENT()\n";
00529     ad_exit(24);
00530   }
00531   gradient_structure::MIN_RETURN =
00532     gradient_structure::RETURN_ARRAYS[gradient_structure::RETURN_ARRAYS_PTR];
00533   gradient_structure::MAX_RETURN =
00534     gradient_structure::RETURN_ARRAYS[gradient_structure::RETURN_ARRAYS_PTR]+
00535     gradient_structure::RETURN_ARRAYS_SIZE-1;
00536   gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00537 #if defined(THREAD_SAFE)
00538   pthread_mutex_unlock(&mutex_return_arrays);
00539 #endif
00540 }
00541 
00548 void RETURN_ARRAYS_DECREMENT(void)
00549 {
00550 #if defined(THREAD_SAFE)
00551   pthread_mutex_lock(&mutex_return_arrays);
00552 #endif
00553   if (--gradient_structure::RETURN_ARRAYS_PTR< 0)
00554   {
00555     cerr << " Error -- RETURN_ARRAYS_PTR < 0  \n";
00556     cerr << " There must be a RETURN_ARRAYS_DECREMENT()\n";
00557     cerr << " which is not matched by a RETURN_ARRAYS_INCREMENT()\n";
00558     ad_exit(24);
00559   }
00560   gradient_structure::MIN_RETURN =
00561     gradient_structure::RETURN_ARRAYS[gradient_structure::RETURN_ARRAYS_PTR];
00562   gradient_structure::MAX_RETURN =
00563     gradient_structure::RETURN_ARRAYS[gradient_structure::RETURN_ARRAYS_PTR]+
00564     gradient_structure::RETURN_ARRAYS_SIZE-1;
00565   gradient_structure::RETURN_PTR =
00566     gradient_structure::RETURN_PTR_CONTAINER[
00567       gradient_structure::RETURN_ARRAYS_PTR];
00568 #if defined(THREAD_SAFE)
00569   pthread_mutex_unlock(&mutex_return_arrays);
00570 #endif
00571 }
00572 
00577 gradient_structure::~gradient_structure()
00578 {
00579    gradient_structure::NVAR=0;
00580    if (RETURN_ARRAYS == NULL)
00581    {
00582      null_ptr_err_message();
00583      ad_exit(1);
00584    }
00585    else
00586    {
00587      for (int i=0; i< NUM_RETURN_ARRAYS; i++)
00588      {
00589        delete [] RETURN_ARRAYS[i];
00590        RETURN_ARRAYS[i]=NULL;
00591      }
00592      delete [] RETURN_ARRAYS;
00593      RETURN_ARRAYS = NULL;
00594      delete [] RETURN_PTR_CONTAINER;
00595      RETURN_PTR_CONTAINER = NULL;
00596    }
00597    if (INDVAR_LIST == NULL)
00598    {
00599      null_ptr_err_message();
00600      ad_exit(1);
00601    }
00602    else
00603    {
00604      delete [] INDVAR_LIST->address;
00605      delete INDVAR_LIST;
00606      INDVAR_LIST = NULL;
00607    }
00608 
00609    if (GRAD_STACK1 == NULL)
00610    {
00611      null_ptr_err_message();
00612      ad_exit(1);
00613    }
00614    else
00615    {
00616      delete GRAD_STACK1;
00617      GRAD_STACK1 = NULL;
00618    }
00619 
00620    if (ARRAY_MEMBLOCK_BASE == NULL)
00621    {
00622      cerr << "Trying to farfree a NULL pointer in ~gradient_structure\n";
00623      ad_exit(1);
00624    }
00625    else
00626    {
00627      ARRAY_MEMBLOCK_BASE.free();
00628    }
00629 
00630    if (ARR_LIST1 == NULL)
00631    {
00632      null_ptr_err_message();
00633      ad_exit(1);
00634    }
00635    else
00636    {
00637       delete ARR_LIST1;
00638       ARR_LIST1 = NULL;
00639    }
00640 
00641    if (GRAD_LIST == NULL)
00642    {
00643      null_ptr_err_message();
00644      ad_exit(1);
00645    }
00646    else
00647    {
00648       delete GRAD_LIST;
00649       GRAD_LIST = NULL;
00650    }
00651 
00652    instances--;
00653 
00654    if (DEPVARS_INFO==NULL)
00655    {
00656      null_ptr_err_message();
00657      ad_exit(1);
00658    }
00659    delete DEPVARS_INFO;
00660    DEPVARS_INFO=NULL;
00661 
00662    if (fp == NULL)
00663    {
00664      cerr << "Trying to close stream referenced by a NULL pointer\n"
00665        " in ~gradient_structure\n";
00666      ad_exit(1);
00667    }
00668    delete fp;
00669    fp = NULL;
00670 }
00671 
00676 void null_ptr_err_message(void)
00677 {
00678    cerr << "Trying to delete a NULL pointer in ~gradient_structure" << endl;
00679 }
00680 
00685 void memory_allocate_error(const char * s, void * ptr)
00686 {
00687   if (ptr == NULL)
00688   {
00689     cerr << " Error trying to allocate " << s << "\n";
00690     ad_exit(21);
00691   }
00692 }
00693 
00694  #if defined(NO_DERIVS)
00695 
00700     void gradient_structure::set_NO_DERIVATIVES(void)
00701     {
00702       no_derivatives=1;
00703     }
00704 
00709     void gradient_structure::set_YES_DERIVATIVES(void)
00710     {
00711       no_derivatives=0;
00712     }
00713  #endif
00714 
00719     void  gradient_structure::set_YES_SAVE_VARIABLES_VALUES(void)
00720     {
00721       save_var_flag=1;
00722     }
00723 
00728     void  gradient_structure::set_NO_SAVE_VARIABLES_VALUES(void)
00729     {
00730       save_var_flag=0;
00731     }
00732 
00737     void gradient_structure::set_NUM_DEPENDENT_VARIABLES(int i)
00738     {
00739       if (i<1)
00740       {
00741         cerr << " Error in "
00742                 "gradient_structure::set_NUM_DEPENDENT_VARIABLES(int i)"
00743              << endl << " value of i must be >= 1" << endl;
00744         i=1;
00745       }
00746       NUM_DEPENDENT_VARIABLES=i;
00747     }