ADMB Documentation  11.1x.2711
 All Classes Files Functions Variables Typedefs Friends Defines
dfsdmat.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: dfsdmat.cpp 2676 2014-11-17 22:13:49Z 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 #if defined (__WAT32__)
00015   #include <io.h>
00016 #endif
00017 
00018 #ifdef _MSC_VER
00019   #include <io.h>
00020   #define lseek _lseek
00021   #define  read _read
00022   #define write _write
00023   #define open _open
00024   #define close _close
00025   #ifdef _M_X64
00026   typedef __int64 ssize_t;
00027   #else
00028   typedef int ssize_t;
00029   #endif
00030 #else
00031   #include <sys/stat.h>
00032   #include <sys/types.h>
00033   #include <unistd.h>
00034 #endif
00035 
00036 #if defined(__TURBOC__)
00037   #pragma hdrstop
00038   #include <iostream.h>
00039   #include <iomanip.h>
00040   #include <sys\stat.h>
00041 #endif
00042 
00043 #ifdef __ZTC__
00044   #include <iostream.hpp>
00045   #define S_IREAD 0000400
00046   #define S_IWRITE 0000200
00047 #endif
00048 
00049 #ifdef __MINGW64__
00050   #include <cassert>
00051   #include <climits>
00052 #else
00053   #ifndef OPT_LIB
00054   #include <cassert>
00055   #endif
00056 #endif
00057 
00058 #ifdef __NDPX__
00059   #define O_RDONLY 0
00060   #define O_WRONLY 1
00061   #define O_RDWR 2
00062   extern "C"
00063   {
00064     int lseek(int, int, int);
00065     int open(const char*, int);
00066     int creat(const char*, int);
00067     int close(int);
00068     int write(int, char*, int);
00069   };
00070 #endif
00071 
00072 #ifdef __SUN__
00073   #include <iostream.h>
00074   #include <sys/stat.h>
00075   #include <sys/types.h>
00076   #ifdef _MSC_VER
00077     #define lseek _lseek
00078     #define  read _read
00079     #define write _write
00080     #define open _open
00081     #define close _close
00082   #endif
00083   #include <unistd.h>
00084 #endif
00085 
00086 #include <stdlib.h>
00087 #include <stdio.h>
00088 #include <string.h>
00089 
00090 
00091 /*class dfsdmat
00092 {
00093   double * ptr;
00094   double ** m;
00095   double* minp;
00096   double* maxp;
00097   int n;
00098 public:
00099   double * getminp(void){ return minp;}
00100   int size(void) {return n;}
00101   dfsdmat(int n);
00102   dfsdmat();
00103   allocate(n);
00104   ~dfsdmat();
00105   deallocate(void);
00106   double& elem(int i,int j);
00107   double& oprator ()(int i,int j);
00108 };
00109 */
00110 
00115 dfsdmat::dfsdmat(void)
00116 {
00117   tmp_file=0;
00118   allocate();
00119 }
00120 
00125 void dfsdmat::allocate(void)
00126 {
00127   shared_memory=0;
00128   ptr=NULL;
00129   m=NULL;
00130   minp=NULL;
00131   maxp=NULL;
00132   n=0;
00133 }
00134 
00139 dfsdmat::dfsdmat(int _n)
00140 {
00141   tmp_file=0;
00142   allocate(_n);
00143 }
00144 
00149 void dfsdmat::allocate(int _n)
00150 {
00151   n=_n;
00152   if (gradient_structure::get_USE_FOR_HESSIAN())
00153   {
00154     ptr=gradient_structure::hessian_ptr;
00155     shared_memory=1;
00156     disk_save_flag=1;
00157   }
00158   else
00159   {
00160     ptr= new double [(n*(n+1))/2];
00161     shared_memory=0;
00162     disk_save_flag=0;
00163   }
00164   minp=ptr;
00165   maxp=ptr+((n*(n+1))/2-1);
00166   m=new double * [n];
00167   m-=1;
00168   double * tmp =ptr;
00169   for (int i=1;i<=n;i++)
00170   {
00171     m[i]=tmp-1;
00172     tmp+=i;
00173   }
00174   tmp_file = 0;
00175   /*
00176   if (!tmp_file)
00177   {
00178     tmp_file=open("fmm_tmp.tmp", O_RDWR | O_CREAT | O_TRUNC |
00179       O_BINARY , 0777);
00180     if (tmp_file == -1)
00181     {
00182       cerr << "error trying to open temporary hessian file\n";
00183       ad_exit(1);
00184     }
00185   }
00186    */
00187 }
00188 
00193 dfsdmat::dfsdmat(int _n, const gradient_structure& gs)
00194 {
00195   tmp_file=0;
00196   disk_save_flag=1;
00197   allocate(_n,gs);
00198 }
00199 
00204 void dfsdmat::allocate(int _n, const gradient_structure& gs)
00205 {
00206   n=_n;
00207   ptr= (double *) gs.ARRAY_MEMBLOCK_BASE;
00208   shared_memory=1;
00209   minp=ptr;
00210   maxp=ptr+((n*(n+1))/2-1);
00211   m=new double * [n];
00212   m-=1;
00213   double * tmp =ptr;
00214   for (int i=1;i<=n;i++)
00215   {
00216     m[i]=tmp-1;
00217     tmp+=i;
00218   }
00219 }
00220 
00224 dfsdmat::~dfsdmat()
00225 {
00226   deallocate();
00227 }
00228 
00233 void dfsdmat::deallocate()
00234 {
00235   if (ptr && !shared_memory)
00236   {
00237     delete [] ptr;
00238     ptr=NULL;
00239   }
00240   if (m)
00241   {
00242     m+=1;
00243     delete [] m;
00244     m=NULL;
00245   }
00246   n=0;
00247   if (tmp_file)
00248   {
00249     close(tmp_file);
00250     tmp_file=0;
00251   }
00252 }
00253 
00254 #if !defined(OPT_LIB) || defined(__INTEL_COMPILER)
00255 
00260 double& dfsdmat::elem(int i,int j)
00261 {
00262   double * tmp= m[i]+j;
00263   if (tmp < minp || tmp > maxp)
00264   {
00265     cerr << "Index out of bounds in double& dfsdmat::elem(int i,int j)"
00266          << endl;
00267     ad_exit(1);
00268   }
00269   return *tmp;
00270 }
00271 
00276 double& dfsdmat::operator () (int i,int j)
00277 {
00278   double * tmp= m[i]+j;
00279   if (tmp < minp || tmp > maxp)
00280   {
00281     cerr << "Index out of bounds in double& dfsdmat::elem(int i,int j)"
00282          << endl;
00283     ad_exit(1);
00284   }
00285   return *tmp;
00286 }
00287 #endif
00288 
00293 uostream& operator<<(const uostream& ofs, const dfsdmat& m)
00294 {
00295   double * p=((dfsdmat&)m).getminp();
00296   int nn=((dfsdmat&)m).size();
00297   nn=(nn*(nn+1))/2;
00298   for (int i=1;i<=nn;i++)
00299   {
00300     (uostream&)ofs << *p++;
00301   }
00302 
00303   return (uostream&)ofs;
00304 }
00305 
00310 uistream& operator>>(const uistream& _ifs, const dfsdmat& _m)
00311 {
00312   uistream& ifs= (uistream&) _ifs;
00313   dfsdmat& m=(dfsdmat&) _m;
00314   double * p=m.getminp();
00315   int nn=m.size();
00316   nn=(nn*(nn+1))/2;
00317   for (int i=1;i<=nn;i++)
00318   {
00319     ifs >> *p++;
00320   }
00321   return ifs;
00322 }
00323 
00328 void dfsdmat::save()
00329 {
00330   if (!tmp_file)
00331   {
00332     tmp_file=open("fmm_tmp.tmp", O_RDWR | O_CREAT | O_TRUNC |
00333       O_BINARY , 0777);
00334     if (tmp_file == -1)
00335     {
00336       cerr << "error trying to open temporary hessian file\n";
00337       ad_exit(1);
00338     }
00339   }
00340   unsigned int _n = (unsigned int)size();
00341   unsigned int nn = (_n*(_n+1))/2;
00342   lseek(tmp_file,0L,SEEK_SET);
00343 #ifdef OPT_LIB
00344   write(tmp_file,&_n,sizeof(int));
00345 #else
00346   ssize_t ret = write(tmp_file,&_n,sizeof(int));
00347   assert(ret != -1);
00348 #endif
00349 #ifdef __MINGW64__
00350   size_t size = nn * sizeof(double);
00351   assert(size <= UINT_MAX);
00352   ssize_t num_bytes = write(tmp_file, ptr, (unsigned int)size);
00353 #else
00354   ssize_t num_bytes=write(tmp_file,ptr,nn*sizeof(double));
00355 #endif
00356   if (num_bytes <= 0)
00357   {
00358     cerr << "Error writing to temporary hess file in dfsdmat::save()"
00359          << endl;
00360   }
00361   /*
00362   if (const ptr && !shared_memory)
00363   {
00364     delete [] ptr;
00365     ptr=NULL;
00366   }
00367   if (m)
00368   {
00369     m+=1;
00370     delete [] m;
00371     m=NULL;
00372   }
00373   n=0;
00374  */
00375 }
00376 
00381 void dfsdmat::restore()
00382 {
00383   int _n=0;
00384   lseek(tmp_file,0L,SEEK_SET);
00385 #ifdef OPT_LIB
00386   read(tmp_file,&_n,sizeof(int));
00387 #else
00388   ssize_t ret = read(tmp_file,&_n,sizeof(int));
00389   assert(ret != -1);
00390   assert(_n > 0);
00391 #endif
00392   unsigned int nn = (unsigned int)((_n*(_n+1))/2);
00393   //if (!shared_memory) allocate(_n);
00394 #ifdef __MINGW64__
00395   size_t size = nn * sizeof(double);
00396   assert(size <= UINT_MAX);
00397   ssize_t num_bytes=read(tmp_file, ptr, (unsigned int)size);
00398 #else
00399   ssize_t num_bytes=read(tmp_file,ptr,nn*sizeof(double));
00400 #endif
00401   if (num_bytes <= 0)
00402   {
00403     cerr << "Error reading from temporary hess file in dfsdmat::save()"
00404          << endl;
00405   }
00406   if (tmp_file) close(tmp_file);
00407   tmp_file = 0;
00408 }