ADMB Documentation  11.1x.2738
 All Classes Files Functions Variables Typedefs Friends Defines
df_file.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: df_file.cpp 2668 2014-11-17 18:54:35Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #include "fvar.hpp"
00012 #include <fcntl.h>
00013 
00014 #ifdef _MSC_VER
00015   #define lseek _lseek
00016   #define  read _read
00017   #define write _write
00018   #include <sys\stat.h>
00019 #else
00020   #include <iostream>
00021   using namespace std;
00022   #include <memory.h>
00023   #include <sys/stat.h>
00024   #include <sys/types.h>
00025   #include <unistd.h>
00026 #endif
00027 
00028 #if defined(__TURBOC__)
00029   #pragma hdrstop
00030   #include <iostream.h>
00031   #include <iomanip.h>
00032   #include <sys\stat.h>
00033 #endif
00034 
00035 #ifdef __ZTC__
00036   #include <iostream.hpp>
00037   #define S_IREAD 0000400
00038   #define S_IWRITE 0000200
00039 #endif
00040 
00041 #ifdef __NDPX__
00042   #define O_RDONLY 0
00043   #define O_WRONLY 1
00044   #define O_RDWR 2
00045   extern "C"
00046   {
00047     int lseek(int, int, int);
00048     int open(const char*, int);
00049     int creat(const char*, int);
00050     int close(int);
00051     int write(int, char*, int);
00052     int read(int, char*, int);
00053   };
00054 #endif
00055 
00056 #ifdef __SUN__
00057 #include <memory.h>
00058 #include <iostream.h>
00059 #include <sys/stat.h>
00060 #include <sys/types.h>
00061 #ifndef _MSC_VER
00062 #include <unistd.h>
00063 #endif
00064 #endif
00065 
00066 #include <limits.h>
00067 #include <stdlib.h>
00068 #include <stdio.h>
00069 #include <string.h>
00070 
00071 #ifdef __MINGW64__
00072   #include <cassert>
00073   #include <climits>
00074 #else
00075   #ifndef OPT_LIB
00076     #include <cassert>
00077   #endif
00078 #endif
00079 
00080 char lastchar(char*);
00081 
00082 void byte_copy(void* dest, void* source, const size_t num_bytes)
00083 {
00084 #if defined(__ADSGI__)
00085   char* pdest = (char*)dest;
00086   char* psource = (char*)source;
00087   int ii=0;
00088   while (ii < num_bytes)
00089   {
00090     //*((char *)dest)++ = *((char *)source)++;
00091     *pdest = *psource;
00092     pdest++;
00093     psource++;
00094     ii++;
00095   }
00096 #else
00097   memcpy((char*)dest, (char*)source, num_bytes);
00098 #endif
00099 }
00100 
00101 extern char ad_random_part[6];
00102 
00108 DF_FILE::DF_FILE(const size_t nbytes):
00109   buff_end(nbytes - sizeof(size_t) - 2),
00110   buff_size(nbytes)
00111 {
00112 #if defined(__BORLANDC__)
00113   if (nbytes > INT_MAX)
00114   {
00115     cout << "Error -- largest size for CMPDIF_BUFFER_SIZE is "
00116          << INT_MAX<<endl;
00117   }
00118 #endif
00119 
00120 #if defined(_M_IX86)
00121   if (nbytes > LONG_MAX)
00122   {
00123     cout << "Error -- largest size for CMPDIF_BUFFER_SIZE is "
00124          << LONG_MAX <<endl;
00125   }
00126 #endif
00127 
00128 #if defined(__GNC__) && defined(__i686__)
00129   if (nbytes > INT_MAX)
00130   {
00131     cout << "Error -- largest size for CMPDIF_BUFFER_SIZE is "
00132          << INT_MAX<<endl;
00133   }
00134 #endif
00135 
00137 /*
00138 #ifndef OPT_LIB
00139   assert(nbytes > sizeof(size_t) + 2);
00140 #endif
00141 */
00142 
00143   if ((buff = new char[buff_size]) == NULL)
00144   {
00145     cerr << "Error trying to allocate memory for DF_FILE buffer"<<endl;
00146     ad_exit(1);
00147   }
00148   offset = 0;
00149   toffset = 0;
00150 
00151   char* path = getenv("ADTMP1"); // NULL if not defined
00152 
00153 #if defined(USE_ADPVM)
00154   adstring string_path;
00155   if (path) string_path=path;
00156 
00157   adstring currdir;
00158   ad_getcd(currdir);
00159   if (ad_comm::pvm_manager)
00160   {
00161     int on = 0;
00162     int nopt = 0;
00163     if ((on = option_match(ad_comm::argc,ad_comm::argv,"-slave",nopt)) > -1)
00164     {
00165       if (nopt == 1)
00166       {
00167         int ierr=make_sub_directory(ad_comm::argv[on+1]);
00168         ad_comm::subdir=ad_comm::argv[on+1];
00169         string_path+=ad_comm::subdir;
00170         path=(char*) string_path;
00171       }
00172       else
00173       {
00174         cerr << "Wrong number of options to -slave -- must be 1"
00175           " you have " << nopt << endl;
00176         ad_exit(1);
00177       }
00178     }
00179   }
00180 #endif
00181 
00182   if (path != NULL)
00183 #if !defined (_WIN32)
00184   {
00185       sprintf(&cmpdif_file_name[0],"%s/cmpdiff.%s", path,
00186         ad_random_part);
00187   }
00188 #else
00189   {
00190     if (lastchar(path) != '\\')
00191     {
00192       sprintf(&cmpdif_file_name[0],"%s\\cmpdiff.%s", path,
00193         ad_random_part);
00194     }
00195     else
00196     {
00197       sprintf(&cmpdif_file_name[0],"%scmpdiff.%s", path,
00198         ad_random_part);
00199     }
00200   }
00201 #endif
00202   else
00203   {
00204     sprintf(&cmpdif_file_name[0],"cmpdiff.%s",ad_random_part);
00205   }
00206 #if defined (_MSC_VER) || defined (__WAT32__)
00207   file_ptr=open(cmpdif_file_name, O_RDWR | O_CREAT | O_TRUNC |
00208                      O_BINARY, S_IREAD | S_IWRITE);
00209 #elif defined (__TURBOC__)
00210   file_ptr=open(cmpdif_file_name, O_RDWR | O_CREAT | O_TRUNC |
00211                      O_BINARY, S_IREAD | S_IWRITE);
00212 #elif defined (__ZTC__)
00213   file_ptr=open(cmpdif_file_name, O_RDWR | O_CREAT | O_TRUNC ,
00214                      S_IREAD | S_IWRITE);
00215 #elif  defined (__NDPX__)
00216   file_ptr=creat(cmpdif_file_name, O_RDWR);
00217 #else
00218   file_ptr=open(cmpdif_file_name, O_RDWR | O_CREAT | O_TRUNC |
00219        O_BINARY, 0777);
00220 #endif
00221 
00222   if (file_ptr == -1)
00223   {
00224     if (ad_printf) (*ad_printf)("Error opening temporary gradient"
00225         " file %s\n", cmpdif_file_name );
00226     ad_exit(1);
00227   }
00228 }
00232 DF_FILE::~DF_FILE()
00233 {
00234   delete [] buff;
00235   buff=0;
00236 
00237   int repfs = option_match(ad_comm::argc,ad_comm::argv,"-fsize");
00238 
00239   if (ad_comm::global_logfile && repfs)
00240   {
00241     off_t pos = lseek(file_ptr, 0, SEEK_END);
00242     *ad_comm::global_logfile << "size of file " << cmpdif_file_name
00243         << " = " << pos << endl;
00244   }
00245   if (close(file_ptr))
00246   {
00247     cerr << "Error closing file " << cmpdif_file_name << "\n";
00248   }
00249 #if !defined (_WIN32)
00250   unlink(cmpdif_file_name);
00251 #else
00252   adstring currentdir;
00253   ad_getcd(currentdir);
00254   int xxx=remove(cmpdif_file_name);
00255   if (xxx)
00256   {
00257     cerr << "Error trying to delete file " << cmpdif_file_name << endl;
00258     xxx=unlink(cmpdif_file_name);
00259     cout << xxx << endl;
00260   }
00261 #endif
00262 }
00267 void DF_FILE::fread(void* s,const size_t num_bytes)
00268 {
00269   if (toffset < num_bytes)
00270   {
00271     off_t lpos = lseek(file_ptr, -((off_t)buff_size), SEEK_CUR);
00272     read_cmpdif_stack_buffer(lpos);
00273     offset -= num_bytes;
00274     toffset = offset;
00275   }
00276   else
00277   {
00278     toffset-=num_bytes; //decrement the temporary offset count
00279   }
00280   byte_copy((char*)s, buff+toffset,num_bytes);
00281   offset=toffset;
00282 }
00287 void DF_FILE::fwrite(const void* s, const size_t num_bytes)
00288 {
00289 #ifdef NO_DERIVS
00290   if (gradient_structure::no_derivatives)
00291   {
00292     return;
00293   }
00294 #endif
00295   toffset+=num_bytes; //increment the temporary offset count
00296   if (toffset>buff_end)
00297   {
00298     if (num_bytes > buff_end)
00299     {
00300       const size_t us = toffset + sizeof(size_t) + 2L;
00301       cerr << "Need to increase gradient_structure::CMPDIF_BUFFER_SIZE "
00302        "to at least" << us << endl;
00303     }
00304     write_cmpdif_stack_buffer();
00305     toffset=num_bytes;
00306     offset=0;
00307   }
00308   byte_copy(buff+offset,(char *) s,num_bytes);
00309   offset=toffset;
00310 }
00315 void DF_FILE::read_cmpdif_stack_buffer(off_t& lpos)
00316 {
00317   if (lpos == -1L)
00318   {
00319     cerr << "Error rewinding file in DF_FILE:fread"<<endl;
00320     ad_exit(1);
00321   }
00322 #ifdef __MINGW64__
00323   assert(buff_size <= UINT_MAX);
00324   if (read(file_ptr, buff, (unsigned int)buff_size) < 0)
00325 #else
00326   if (read(file_ptr, buff, buff_size) < 0)
00327 #endif
00328   {
00329     cerr << "End of file trying to read "<< cmpdif_file_name << endl;
00330     ad_exit(1);
00331   }
00332   lpos = lseek(file_ptr, -((off_t)buff_size),SEEK_CUR);
00333   for (size_t i = 0;i < sizeof(size_t); i++)
00334   {
00335     fourb[i] = *(buff+buff_end+1+i);
00336   }
00337 }
00342 void DF_FILE::write_cmpdif_stack_buffer(void)
00343 {
00344   // save the offset at the end of the used part of the buffer
00345   for (size_t i = 0; i < sizeof(size_t); i++)
00346   {
00347     *(buff+buff_end+1+i) = fourb[i];
00348   }
00349 #ifdef __MINGW64__
00350   assert(buff_size <= UINT_MAX);
00351   if (write(file_ptr, buff, (unsigned int)buff_size) < 0)
00352 #else
00353   if (write(file_ptr, buff, buff_size) < 0)
00354 #endif
00355   {
00356     cerr << "End of file trying to write to file "<< cmpdif_file_name << endl;
00357     cerr << "There is probably no more room on the TMP1 (if defined) device\n"
00358     "If possible set TMP1 environment string to a device with more room\n";
00359     ad_exit(1);
00360   }
00361 }