ADMB Documentation  11.2.2828
 All Classes Files Functions Variables Typedefs Friends Defines
dfsdmat.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: dfsdmat.cpp 2723 2014-11-25 20:24:34Z 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 #if !defined(OPT_LIB) || defined(__MINGW64__) || defined(_MSC_VER)
00050   #include <cassert>
00051   #include <climits>
00052 #endif
00053 
00054 #ifdef __NDPX__
00055   #define O_RDONLY 0
00056   #define O_WRONLY 1
00057   #define O_RDWR 2
00058   extern "C"
00059   {
00060     int lseek(int, int, int);
00061     int open(const char*, int);
00062     int creat(const char*, int);
00063     int close(int);
00064     int write(int, char*, int);
00065   };
00066 #endif
00067 
00068 #ifdef __SUN__
00069   #include <iostream.h>
00070   #include <sys/stat.h>
00071   #include <sys/types.h>
00072   #ifdef _MSC_VER
00073     #define lseek _lseek
00074     #define  read _read
00075     #define write _write
00076     #define open _open
00077     #define close _close
00078   #endif
00079   #include <unistd.h>
00080 #endif
00081 
00082 #include <stdlib.h>
00083 #include <stdio.h>
00084 #include <string.h>
00085 
00086 
00087 /*class dfsdmat
00088 {
00089   double * ptr;
00090   double ** m;
00091   double* minp;
00092   double* maxp;
00093   int n;
00094 public:
00095   double * getminp(void){ return minp;}
00096   int size(void) {return n;}
00097   dfsdmat(int n);
00098   dfsdmat();
00099   allocate(n);
00100   ~dfsdmat();
00101   deallocate(void);
00102   double& elem(int i,int j);
00103   double& oprator ()(int i,int j);
00104 };
00105 */
00106 
00111 dfsdmat::dfsdmat(void)
00112 {
00113   tmp_file=0;
00114   allocate();
00115 }
00116 
00121 void dfsdmat::allocate(void)
00122 {
00123   shared_memory=0;
00124   ptr=NULL;
00125   m=NULL;
00126   minp=NULL;
00127   maxp=NULL;
00128   n=0;
00129 }
00130 
00135 dfsdmat::dfsdmat(int _n)
00136 {
00137   tmp_file=0;
00138   allocate(_n);
00139 }
00140 
00145 void dfsdmat::allocate(int _n)
00146 {
00147   n=_n;
00148   if (gradient_structure::get_USE_FOR_HESSIAN())
00149   {
00150     ptr=gradient_structure::hessian_ptr;
00151     shared_memory=1;
00152     disk_save_flag=1;
00153   }
00154   else
00155   {
00156     ptr= new double [(n*(n+1))/2];
00157     shared_memory=0;
00158     disk_save_flag=0;
00159   }
00160   minp=ptr;
00161   maxp=ptr+((n*(n+1))/2-1);
00162   m=new double * [n];
00163   m-=1;
00164   double * tmp =ptr;
00165   for (int i=1;i<=n;i++)
00166   {
00167     m[i]=tmp-1;
00168     tmp+=i;
00169   }
00170   tmp_file = 0;
00171   /*
00172   if (!tmp_file)
00173   {
00174     tmp_file=open("fmm_tmp.tmp", O_RDWR | O_CREAT | O_TRUNC |
00175       O_BINARY , 0777);
00176     if (tmp_file == -1)
00177     {
00178       cerr << "error trying to open temporary hessian file\n";
00179       ad_exit(1);
00180     }
00181   }
00182    */
00183 }
00184 
00189 dfsdmat::dfsdmat(int _n, const gradient_structure& gs)
00190 {
00191   tmp_file=0;
00192   disk_save_flag=1;
00193   allocate(_n,gs);
00194 }
00195 
00200 void dfsdmat::allocate(int _n, const gradient_structure& gs)
00201 {
00202   n=_n;
00203   ptr= (double *) gs.ARRAY_MEMBLOCK_BASE;
00204   shared_memory=1;
00205   minp=ptr;
00206   maxp=ptr+((n*(n+1))/2-1);
00207   m=new double * [n];
00208   m-=1;
00209   double * tmp =ptr;
00210   for (int i=1;i<=n;i++)
00211   {
00212     m[i]=tmp-1;
00213     tmp+=i;
00214   }
00215 }
00216 
00220 dfsdmat::~dfsdmat()
00221 {
00222   deallocate();
00223 }
00224 
00229 void dfsdmat::deallocate()
00230 {
00231   if (ptr && !shared_memory)
00232   {
00233     delete [] ptr;
00234     ptr=NULL;
00235   }
00236   if (m)
00237   {
00238     m+=1;
00239     delete [] m;
00240     m=NULL;
00241   }
00242   n=0;
00243   if (tmp_file)
00244   {
00245     close(tmp_file);
00246     tmp_file=0;
00247   }
00248 }
00249 
00250 #if !defined(OPT_LIB) || defined(__INTEL_COMPILER)
00251 
00256 double& dfsdmat::elem(int i,int j)
00257 {
00258   double * tmp= m[i]+j;
00259   if (tmp < minp || tmp > maxp)
00260   {
00261     cerr << "Index out of bounds in double& dfsdmat::elem(int i,int j)"
00262          << endl;
00263     ad_exit(1);
00264   }
00265   return *tmp;
00266 }
00267 
00272 double& dfsdmat::operator () (int i,int j)
00273 {
00274   double * tmp= m[i]+j;
00275   if (tmp < minp || tmp > maxp)
00276   {
00277     cerr << "Index out of bounds in double& dfsdmat::elem(int i,int j)"
00278          << endl;
00279     ad_exit(1);
00280   }
00281   return *tmp;
00282 }
00283 #endif
00284 
00289 uostream& operator<<(const uostream& ofs, const dfsdmat& m)
00290 {
00291   double * p=((dfsdmat&)m).getminp();
00292   int nn=((dfsdmat&)m).size();
00293   nn=(nn*(nn+1))/2;
00294   for (int i=1;i<=nn;i++)
00295   {
00296     (uostream&)ofs << *p++;
00297   }
00298 
00299   return (uostream&)ofs;
00300 }
00301 
00306 uistream& operator>>(const uistream& _ifs, const dfsdmat& _m)
00307 {
00308   uistream& ifs= (uistream&) _ifs;
00309   dfsdmat& m=(dfsdmat&) _m;
00310   double * p=m.getminp();
00311   int nn=m.size();
00312   nn=(nn*(nn+1))/2;
00313   for (int i=1;i<=nn;i++)
00314   {
00315     ifs >> *p++;
00316   }
00317   return ifs;
00318 }
00319 
00324 void dfsdmat::save()
00325 {
00326   if (!tmp_file)
00327   {
00328     tmp_file=open("fmm_tmp.tmp", O_RDWR | O_CREAT | O_TRUNC |
00329       O_BINARY , 0777);
00330     if (tmp_file == -1)
00331     {
00332       cerr << "error trying to open temporary hessian file\n";
00333       ad_exit(1);
00334     }
00335   }
00336   unsigned int _n = (unsigned int)size();
00337   unsigned int nn = (_n*(_n+1))/2;
00338   lseek(tmp_file,0L,SEEK_SET);
00339 #ifdef OPT_LIB
00340   write(tmp_file,&_n,sizeof(int));
00341 #else
00342   ssize_t ret = write(tmp_file,&_n,sizeof(int));
00343   assert(ret != -1);
00344 #endif
00345 #ifdef __MINGW64__
00346   size_t size = nn * sizeof(double);
00347   assert(size <= UINT_MAX);
00348   ssize_t num_bytes = write(tmp_file, ptr, (unsigned int)size);
00349 #else
00350   ssize_t num_bytes=write(tmp_file,ptr,nn*sizeof(double));
00351 #endif
00352   if (num_bytes <= 0)
00353   {
00354     cerr << "Error writing to temporary hess file in dfsdmat::save()"
00355          << endl;
00356   }
00357   /*
00358   if (const ptr && !shared_memory)
00359   {
00360     delete [] ptr;
00361     ptr=NULL;
00362   }
00363   if (m)
00364   {
00365     m+=1;
00366     delete [] m;
00367     m=NULL;
00368   }
00369   n=0;
00370  */
00371 }
00372 
00377 void dfsdmat::restore()
00378 {
00379   int _n=0;
00380   lseek(tmp_file,0L,SEEK_SET);
00381 #if defined(OPT_LIB) && !defined(_MSC_VER)
00382   read(tmp_file,&_n,sizeof(int));
00383 #else
00384   ssize_t ret = read(tmp_file,&_n,sizeof(int));
00385   assert(ret != -1);
00386   assert(_n > 0);
00387 #endif
00388   unsigned int nn = (unsigned int)((_n*(_n+1))/2);
00389   //if (!shared_memory) allocate(_n);
00390 #ifdef __MINGW64__
00391   size_t size = nn * sizeof(double);
00392   assert(size <= UINT_MAX);
00393   ssize_t num_bytes=read(tmp_file, ptr, (unsigned int)size);
00394 #else
00395   ssize_t num_bytes=read(tmp_file,ptr,nn*sizeof(double));
00396 #endif
00397   if (num_bytes <= 0)
00398   {
00399     cerr << "Error reading from temporary hess file in dfsdmat::save()"
00400          << endl;
00401   }
00402   if (tmp_file) close(tmp_file);
00403   tmp_file = 0;
00404 }