ADMB Documentation  11.5.3152
 All Classes Files Functions Variables Typedefs Friends Defines
gradstak.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #include <iostream>
00012 using namespace std;
00013 #include <fcntl.h>
00014 #include "fvar.hpp"
00015 #include <adstring.hpp>
00016 
00017 #if defined (__WAT32__)
00018   #include <io.h>
00019 #endif
00020 
00021 #ifdef _MSC_VER
00022   #define lseek _lseek
00023   #define  read _read
00024   #define write _write
00025   #define open _open
00026   #define close _close
00027   #include <sys\stat.h>
00028 #else
00029   #include <sys/stat.h>
00030   #include <sys/types.h>
00031   #include <unistd.h>
00032 #endif
00033 
00034 #if defined(__TURBOC__)
00035   #pragma hdrstop
00036   #include <iostream.h>
00037   #include <iomanip.h>
00038   #include <sys\stat.h>
00039 #endif
00040 
00041 #ifdef __ZTC__
00042   #include <iostream.hpp>
00043   #define S_IREAD 0000400
00044   #define S_IWRITE 0000200
00045 #endif
00046 
00047 #ifdef __NDPX__
00048   #define O_RDONLY 0
00049   #define O_WRONLY 1
00050   #define O_RDWR 2
00051   extern "C"
00052   {
00053     int lseek(int, int, int);
00054     int open(const char*, int);
00055     int creat(const char*, int);
00056     int close(int);
00057     int write(int, char*, int);
00058   };
00059 #endif
00060 
00061 #include <stdlib.h>
00062 #include <stdio.h>
00063 #include <string.h>
00064 #include <time.h>
00065 
00066 #if !defined(OPT_LIB)
00067   #include <cassert>
00068   #include <climits>
00069 #endif
00070 #if defined(__MINGW64__) || (defined(_MSC_VER) && defined(_M_X64))
00071   #include <cassert>
00072   #include <climits>
00073 #endif
00074 
00075 #ifdef _MSC_VER
00076   #ifdef _M_X64
00077   typedef __int64 ssize_t;
00078   #else
00079   typedef int ssize_t;
00080   #endif
00081   #ifndef SSIZE_MAX
00082     #define SSIZE_MAX INT_MAX
00083   #endif
00084 #endif
00085 
00086 char lastchar(char *);
00087 char ad_random_part[6]="tmp";
00088 
00089 void fill_ad_random_part(void)
00090 {
00091 /*
00092   time_t t,tt;
00093   time(&t);
00094   tt=t;
00095   int div=1;
00096   for (int i=0;i<6;i++)
00097   {
00098     ad_random_part[i]=(tt/div)%10+48;
00099     div*=10;
00100   }
00101 */
00102 }
00106 grad_stack::grad_stack()
00107 {
00108   gradient_structure::TOTAL_BYTES = 0;
00109   gradient_structure::PREVIOUS_TOTAL_BYTES=0;
00110   true_length = gradient_structure::GRADSTACK_BUFFER_SIZE;
00111   length = true_length;
00112 
00113 #ifndef OPT_LIB
00114   assert(length > 0);
00115 #endif
00116 
00117   true_ptr_first = new grad_stack_entry[length];
00118   if (!true_ptr_first)
00119   {
00120     cerr << "Memory allocation error in grad_stack constructor\n"
00121          << " trying to allocate grad_stack_entry[" << length << "] array.\n";
00122     ad_exit(1);
00123   }
00124 
00125   test_the_pointer();
00126 
00127   ptr_first=true_ptr_first;
00128   ptr = ptr_first;
00129   ptr_last=ptr_first+(length-1);
00130   //table=new lvector(-128,250);
00131 
00132   _GRADFILE_PTR = -1; // set to illegal value for later checking
00133   end_pos  = 0;
00134   end_pos1 = 0;
00135   end_pos2 = 0;
00136 
00137   char* path = getenv("ADTMP"); // NULL if not defined
00138 
00139 #if defined(USE_ADPVM)
00140   adstring string_path;
00141   if (path) string_path=path;
00142   int on=0;
00143   int nopt=0;
00144   adstring currdir;
00145   ad_getcd(currdir);
00146   if (ad_comm::pvm_manager)
00147   {
00148     if ((on = option_match(ad_comm::argc, ad_comm::argv, "-slave", nopt)) > -1)
00149     {
00150       if (nopt ==1)
00151       {
00152         int ierr=make_sub_directory(ad_comm::argv[on+1]);
00153         ad_comm::subdir=ad_comm::argv[on+1];
00154         string_path+=ad_comm::subdir;
00155         path=(char*) string_path;
00156       }
00157       else
00158       {
00159         cerr << "Wrong number of options to -slave -- must be 1"
00160             " you have " << nopt << endl;
00161         ad_exit(1);
00162       }
00163     }
00164   }
00165 #endif
00166 
00167   if (path != NULL && strlen(path) <= 45)
00168   {
00169 #if !defined (_WIN32)
00170     sprintf(&gradfile_name1[0],"%s/gradfil1.%s", path, ad_random_part);
00171 #else
00172     if (lastchar(path) != '\\')
00173     {
00174       sprintf(&gradfile_name1[0], "%s\\gradfil1.%s", path, ad_random_part);
00175     }
00176     else
00177     {
00178       sprintf(&gradfile_name1[0], "%sgradfil1.%s", path, ad_random_part);
00179     }
00180 #endif
00181   }
00182   else
00183   {
00184     sprintf(&gradfile_name1[0], "gradfil1.%s", ad_random_part);
00185   }
00186 
00187   path = getenv("ADTMP1"); // NULL if not defined
00188 #if defined(USE_ADPVM)
00189   adstring string_path2;
00190   if (path) string_path2=path;
00191   string_path2+=ad_comm::subdir;
00192   path=(char*) string_path2;
00193 #endif
00194   if (path != NULL && strlen(path) <= 45)
00195   {
00196 #if !defined (_WIN32)
00197     if (strlen(path) > 0)
00198     {
00199       sprintf(&var_store_file_name[0],"%s/varssave.%s",path, ad_random_part);
00200       sprintf(&gradfile_name2[0],"%s/gradfil2.%s", path, ad_random_part);
00201     }
00202     else
00203     {
00204       sprintf(&var_store_file_name[0],"varssave.tmp");
00205       sprintf(&gradfile_name2[0],"gradfil2.tmp");
00206     }
00207 #else
00208     if (lastchar(path)!='\\')
00209     {
00210       sprintf(&gradfile_name2[0], "%s\\gradfil2.%s", path, ad_random_part);
00211       sprintf(&var_store_file_name[0], "%s\\varssave.%s", path, ad_random_part);
00212     }
00213     else
00214     {
00215       sprintf(&gradfile_name2[0], "%sgradfil2.%s", path, ad_random_part);
00216       sprintf(&var_store_file_name[0], "%svarssave.%s", path, ad_random_part);
00217     }
00218 #endif
00219   }
00220   else
00221   {
00222     sprintf(&gradfile_name2[0], "gradfil2.%s", ad_random_part);
00223     sprintf(&var_store_file_name[0], "varssave.%s", ad_random_part);
00224   }
00225 
00226   create_gradfile();
00227 
00228   strcpy(gradfile_name, gradfile_name1);
00229   _GRADFILE_PTR = _GRADFILE_PTR1;
00230 }
00234 grad_stack::~grad_stack()
00235 {
00236   const int repfs = option_match(ad_comm::argc, ad_comm::argv, "-fsize");
00237   if (ad_comm::global_logfile && repfs)
00238   {
00239      off_t pos = lseek(_GRADFILE_PTR1, 0, SEEK_END);
00240      *ad_comm::global_logfile << "size of file " << gradfile_name1
00241        << " = " << pos << endl;
00242 
00243      pos = lseek(_GRADFILE_PTR2, 0, SEEK_END);
00244      *ad_comm::global_logfile << "size of file " << gradfile_name2
00245        << " = " << pos << endl;
00246 
00247      pos = lseek(_VARSSAV_PTR, 0, SEEK_END);
00248      *ad_comm::global_logfile << "size of file " << var_store_file_name
00249        << " = " << pos << endl;
00250   }
00251   if (close(_GRADFILE_PTR1))
00252   {
00253     cerr << "Error closing file " << gradfile_name1 << "\n";
00254   }
00255   if (close(_GRADFILE_PTR2))
00256   {
00257     cerr << "Error closing file " << gradfile_name2 << "\n";
00258   }
00259   if (close(_VARSSAV_PTR))
00260   {
00261     cerr << "Error closing file " << var_store_file_name << "\n";
00262   }
00263 #if !defined (_MSC_VER)
00264   unlink(gradfile_name1);
00265   unlink(gradfile_name2);
00266   unlink(var_store_file_name);
00267 #else
00268   remove(gradfile_name1);
00269   remove(gradfile_name2);
00270   remove(var_store_file_name);
00271 #endif
00272 
00273   delete [] true_ptr_first;
00274   true_ptr_first = 0;
00275 }
00276 
00281 void  grad_stack::write_grad_stack_buffer()
00282 {
00283 #ifdef GRAD_DIAG
00284   cout << "Grad_stack size exceeded\n ";
00285   cout << "Writing to temporary file -- \n";
00286 #endif
00287 
00288   ptr--;
00289 
00290 #ifdef GRAD_DIAG
00291   {
00292     off_t lpos = lseek(_GRADFILE_PTR,0L,SEEK_CUR);
00293     cout << "Offset in file before write is " << lpos
00294          << " bytes from the beginning\n";
00295   }
00296 #endif
00297 
00298   // if the buffer is really large only write the end of it
00299   set_gbuffer_pointers();
00300   size_t nbw = sizeof(grad_stack_entry) * length;
00301 
00302   //char * ttmp = (char *) ptr_first; ttmp--;
00303 
00304   // save the current end of file in case we can't write the whole buffer
00305   end_pos = lseek(_GRADFILE_PTR,0L,SEEK_CUR);
00306 #if defined(__MINGW64__) || (defined(_WIN64) && defined(_MSC_VER))
00307   assert(nbw <= UINT_MAX);
00308   ssize_t ierr = write(_GRADFILE_PTR, ptr_first, (unsigned int)nbw);
00309 #else
00310   ssize_t ierr = write(_GRADFILE_PTR, ptr_first, nbw);
00311 #endif
00312 
00313 #ifndef OPT_LIB
00314   assert(nbw <= SSIZE_MAX);
00315 #endif
00316 
00317   if  (ierr != (ssize_t)nbw)
00318   {
00319     cout << "Wrote " << ierr << " not " << nbw << endl;
00320 
00321 #ifndef OPT_LIB
00322     off_t offset = lseek(_GRADFILE_PTR, end_pos, SEEK_SET);
00323     assert(offset != -1);
00324 #else
00325     lseek(_GRADFILE_PTR, end_pos, SEEK_SET);
00326 #endif
00327 
00328     //save the end of file for this file so we can reposition later
00329     end_pos1 = end_pos;
00330     increment_current_gradfile_ptr();
00331 #if defined(__MINGW64__) || (defined(_WIN64) && defined(_MSC_VER))
00332     assert(nbw <= UINT_MAX);
00333     ierr = write(_GRADFILE_PTR, ptr_first, (unsigned int)nbw);
00334 #else
00335     ierr = write(_GRADFILE_PTR, ptr_first, nbw);
00336 #endif
00337 
00338     if  (ierr != (ssize_t)nbw)
00339     {
00340         perror("Error writing to temporary gradient stack file");
00341         cerr <<"   File name: " << gradfile_name << "\n";
00342 #if defined(_MSC_VER)
00343         cerr <<"   File length: "
00344              << filelength(_GRADFILE_PTR)
00345              << " bytes.\n";
00346 #endif
00347       cerr << "   Attempting to write " << nbw <<" bytes; wrote "
00348            << ierr << ".\n";
00349       ad_exit(1);
00350     }
00351   }
00352 #ifdef GRAD_DIAG
00353   else
00354   {
00355     cout << "Wrote " << ierr << "bytes into temp. grad. file\n";
00356   }
00357   {
00358     off_t lpos = lseek(gradient_structure::_GRADFILE_PTR,0L,SEEK_CUR);
00359     cout << "Offset in file after write is " << lpos
00360          << " bytes from the beginning\n";
00361   }
00362 #endif
00363   //keep track of the size of the grad_stack
00364   gradient_structure::TOTAL_BYTES+=nbw;
00365   ptr = ptr_first;
00366 }
00367 
00372 void grad_stack::create_gradfile()
00373 {
00374   #if defined (__TURBOC__)
00375    _GRADFILE_PTR1=open(gradfile_name1, O_RDWR | O_CREAT |
00376        O_TRUNC | O_BINARY, S_IREAD | S_IWRITE);
00377    _VARSSAV_PTR=open(var_store_file_name, O_RDWR | O_CREAT |
00378        O_TRUNC | O_BINARY, S_IREAD | S_IWRITE);
00379   #elif defined (__ZTC__)
00380     _GRADFILE_PTR1=open(gradfile_name1, O_RDWR | O_CREAT |
00381       O_TRUNC , S_IREAD | S_IWRITE);
00382     _VARSSAV_PTR=open(var_store_file_name, O_RDWR | O_CREAT
00383       | O_TRUNC,  S_IREAD | S_IWRITE);
00384   #elif defined (__NDPX__)
00385     _GRADFILE_PTR1=creat(gradfile_name1, O_RDWR);
00386      _VARSSAV_PTR=creat(var_store_file_name, O_RDWR);
00387   #elif defined (__WAT32__)
00388    _GRADFILE_PTR1=open(gradfile_name1, O_RDWR | O_CREAT |
00389        O_TRUNC | O_BINARY, S_IREAD | S_IWRITE);
00390    _VARSSAV_PTR=open(var_store_file_name, O_RDWR | O_CREAT |
00391        O_TRUNC | O_BINARY, S_IREAD | S_IWRITE);
00392   #else
00393     _GRADFILE_PTR1=open(gradfile_name1, O_RDWR | O_CREAT | O_TRUNC |
00394       O_BINARY , 0777);
00395     _VARSSAV_PTR=open(var_store_file_name, O_RDWR |
00396       O_CREAT | O_TRUNC | O_BINARY, 0777);
00397   #endif
00398 
00399   if (_GRADFILE_PTR1 == -1)
00400   {
00401     if (ad_printf) (*ad_printf)("Error opening temporary gradient file"
00402      " %s\n",gradfile_name1);
00403     ad_exit(1);
00404   }
00405 
00406   if (_VARSSAV_PTR == -1)
00407   {
00408     perror("Error opening temporary gradient file");
00409     cerr <<"  Attempting to open " << var_store_file_name <<"\n";
00410     ad_exit(1);
00411   }
00412 
00413   #if defined (__TURBOC__)
00414     _GRADFILE_PTR2=open(gradfile_name2, O_RDWR | O_CREAT | O_TRUNC |
00415       O_BINARY, S_IREAD | S_IWRITE);
00416   #elif defined (__ZTC__)
00417     _GRADFILE_PTR2=open(gradfile_name2, O_RDWR | O_CREAT | O_TRUNC ,
00418       S_IREAD | S_IWRITE);
00419   #elif defined (__NDPX__)
00420     _GRADFILE_PTR2=creat(gradfile_name2, O_RDWR);
00421   #elif defined (_MSC_VER)
00422     _GRADFILE_PTR2=open(gradfile_name2, O_RDWR | O_CREAT | O_TRUNC |
00423       O_BINARY, S_IREAD | S_IWRITE);
00424   #elif defined (__WAT32__)
00425     _GRADFILE_PTR2=open(gradfile_name2, O_RDWR | O_CREAT | O_TRUNC |
00426       O_BINARY, S_IREAD | S_IWRITE);
00427   #else
00428     _GRADFILE_PTR2=open(gradfile_name2, O_RDWR | O_CREAT | O_TRUNC |
00429       O_BINARY , 0777);
00430   #endif
00431 
00432   if (_GRADFILE_PTR2 == -1)
00433   {
00434     perror("Error opening temporary gradient file");
00435     cerr <<"  Attempting to open " << gradfile_name2 <<"\n";
00436     ad_exit(1);
00437   }
00438 }
00443 void grad_stack::increment_current_gradfile_ptr()
00444 {
00445   if (_GRADFILE_PTR == _GRADFILE_PTR2)
00446   {
00447     cerr << "Attempted to open a third gradient file -- There is\n"
00448       "probably no more room on the TMP1 (if defined) device\n"
00449       "if possible set TMP1 environment string to a device with more room\n";
00450     ad_exit(1);
00451   }
00452    strcpy(gradfile_name, gradfile_name2);
00453   _GRADFILE_PTR = _GRADFILE_PTR2;
00454 }
00459 int grad_stack::decrement_current_gradfile_ptr()
00460 {
00461   if (_GRADFILE_PTR == _GRADFILE_PTR1)
00462   {
00463     return -1;
00464   }
00465 
00466   strcpy(gradfile_name, gradfile_name1);
00467   _GRADFILE_PTR = _GRADFILE_PTR1;
00468 
00469   return 0;
00470 }
00471 #ifdef _MSC_VER
00472 int grad_stack::gradfile_handle()
00473 #else
00474 int& grad_stack::gradfile_handle()
00475 #endif
00476 {
00477   return _GRADFILE_PTR;
00478 }
00483 char* grad_stack::get_gradfile_name()
00484 {
00485   return gradfile_name;
00486 }
00491 void grad_stack::set_gbuffer_pointers()
00492 {
00493   if (length > 10000L)
00494   {
00495     //make the buffer end_buf_size
00496     unsigned int end_buf_size=5000L;
00497     ptr_first+=length-end_buf_size;
00498     length=end_buf_size;
00499   }
00500 }
00505 void grad_stack::set_gradient_stack0(void (* func)(void),double * dep_addr)
00506 {
00507 #ifdef NO_DERIVS
00508   if (!gradient_structure::no_derivatives)
00509   {
00510 #endif
00511     if (ptr > ptr_last)
00512     {
00513       // current buffer is full -- write it to disk and reset pointer
00514       // and counter
00515       this->write_grad_stack_buffer();
00516     }
00517     ptr->func = func;
00518     ptr->dep_addr = dep_addr;
00519     ptr++;
00520 #ifdef NO_DERIVS
00521   }
00522 #endif
00523 }