ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
dfsdmat.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 <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 
00110 dfsdmat::dfsdmat():
00111   disk_save_flag(1)
00112 {
00113   tmp_file = 0;
00114   allocate();
00115 }
00119 void dfsdmat::allocate()
00120 {
00121   shared_memory=0;
00122   ptr=NULL;
00123   m=NULL;
00124   minp=NULL;
00125   maxp=NULL;
00126   n=0;
00127 }
00128 
00133 dfsdmat::dfsdmat(int _n)
00134 {
00135   tmp_file=0;
00136   allocate(_n);
00137 }
00138 
00143 void dfsdmat::allocate(int _n)
00144 {
00145   n=_n;
00146   if (gradient_structure::get_USE_FOR_HESSIAN())
00147   {
00148     ptr=gradient_structure::hessian_ptr;
00149     shared_memory=1;
00150     disk_save_flag=1;
00151   }
00152   else
00153   {
00154     ptr= new double [(n*(n+1))/2];
00155     shared_memory=0;
00156     disk_save_flag=0;
00157   }
00158   minp=ptr;
00159   maxp=ptr+((n*(n+1))/2-1);
00160   m=new double * [n];
00161   m-=1;
00162   double * tmp =ptr;
00163   for (int i=1;i<=n;i++)
00164   {
00165     m[i]=tmp-1;
00166     tmp+=i;
00167   }
00168   tmp_file = 0;
00169   /*
00170   if (!tmp_file)
00171   {
00172     tmp_file=open("fmm_tmp.tmp", O_RDWR | O_CREAT | O_TRUNC |
00173       O_BINARY , 0777);
00174     if (tmp_file == -1)
00175     {
00176       cerr << "error trying to open temporary hessian file\n";
00177       ad_exit(1);
00178     }
00179   }
00180    */
00181 }
00182 
00187 dfsdmat::dfsdmat(int _n, const gradient_structure& gs)
00188 {
00189   tmp_file=0;
00190   disk_save_flag=1;
00191   allocate(_n,gs);
00192 }
00193 
00198 void dfsdmat::allocate(int _n, const gradient_structure& gs)
00199 {
00200   n=_n;
00201   ptr= (double *) gs.ARRAY_MEMBLOCK_BASE;
00202   shared_memory=1;
00203   minp=ptr;
00204   maxp=ptr+((n*(n+1))/2-1);
00205   m=new double * [n];
00206   m-=1;
00207   double * tmp =ptr;
00208   for (int i=1;i<=n;i++)
00209   {
00210     m[i]=tmp-1;
00211     tmp+=i;
00212   }
00213 }
00214 
00218 dfsdmat::~dfsdmat()
00219 {
00220   deallocate();
00221 }
00222 
00227 void dfsdmat::deallocate()
00228 {
00229   if (ptr && !shared_memory)
00230   {
00231     delete [] ptr;
00232     ptr=NULL;
00233   }
00234   if (m)
00235   {
00236     m+=1;
00237     delete [] m;
00238     m=NULL;
00239   }
00240   n=0;
00241   if (tmp_file)
00242   {
00243     close(tmp_file);
00244     tmp_file=0;
00245   }
00246 }
00247 
00248 #if !defined(OPT_LIB) || defined(__INTEL_COMPILER)
00249 
00254 double& dfsdmat::elem(int i,int j)
00255 {
00256   double * tmp= m[i]+j;
00257   if (tmp < minp || tmp > maxp)
00258   {
00259     cerr << "Index out of bounds in double& dfsdmat::elem(int i,int j)"
00260          << endl;
00261     ad_exit(1);
00262   }
00263   return *tmp;
00264 }
00265 
00270 double& dfsdmat::operator () (int i,int j)
00271 {
00272   double * tmp= m[i]+j;
00273   if (tmp < minp || tmp > maxp)
00274   {
00275     cerr << "Index out of bounds in double& dfsdmat::elem(int i,int j)"
00276          << endl;
00277     ad_exit(1);
00278   }
00279   return *tmp;
00280 }
00281 #endif
00282 
00287 uostream& operator<<(const uostream& ofs, const dfsdmat& m)
00288 {
00289   double * p=((dfsdmat&)m).getminp();
00290   int nn=((dfsdmat&)m).size();
00291   nn=(nn*(nn+1))/2;
00292   for (int i=1;i<=nn;i++)
00293   {
00294     (uostream&)ofs << *p++;
00295   }
00296 
00297   return (uostream&)ofs;
00298 }
00299 
00304 uistream& operator>>(const uistream& _ifs, const dfsdmat& _m)
00305 {
00306   uistream& ifs= (uistream&) _ifs;
00307   dfsdmat& m=(dfsdmat&) _m;
00308   double * p=m.getminp();
00309   int nn=m.size();
00310   nn=(nn*(nn+1))/2;
00311   for (int i=1;i<=nn;i++)
00312   {
00313     ifs >> *p++;
00314   }
00315   return ifs;
00316 }
00320 void dfsdmat::save()
00321 {
00322   if (tmp_file < 0)
00323   {
00324     tmp_file=open("fmm_tmp.tmp", O_RDWR | O_CREAT | O_TRUNC |
00325       O_BINARY , 0777);
00326     if (tmp_file < 0)
00327     {
00328       cerr << "error trying to open temporary hessian file\n";
00329       ad_exit(1);
00330     }
00331   }
00332   lseek(tmp_file, 0L, SEEK_SET);
00333 
00334   unsigned int _n = (unsigned int)size();
00335   unsigned int nn = (_n*(_n+1))/2;
00336 #ifdef OPT_LIB
00337   write(tmp_file,&_n,sizeof(int));
00338 #else
00339   ssize_t ret = write(tmp_file,&_n,sizeof(int));
00340   assert(ret != -1);
00341 #endif
00342 #ifdef __MINGW64__
00343   size_t size = nn * sizeof(double);
00344   assert(size <= UINT_MAX);
00345   ssize_t num_bytes = write(tmp_file, ptr, (unsigned int)size);
00346 #else
00347   ssize_t num_bytes=write(tmp_file,ptr,nn*sizeof(double));
00348 #endif
00349   if (num_bytes <= 0)
00350   {
00351     cerr << "Error writing to temporary hess file in dfsdmat::save()"
00352          << endl;
00353   }
00354   /*
00355   if (const ptr && !shared_memory)
00356   {
00357     delete [] ptr;
00358     ptr=NULL;
00359   }
00360   if (m)
00361   {
00362     m+=1;
00363     delete [] m;
00364     m=NULL;
00365   }
00366   n=0;
00367  */
00368 }
00372 void dfsdmat::restore()
00373 {
00374   int _n=0;
00375   lseek(tmp_file,0L,SEEK_SET);
00376 #if defined(OPT_LIB) && !defined(_MSC_VER)
00377   read(tmp_file,&_n,sizeof(int));
00378 #else
00379   ssize_t ret = read(tmp_file,&_n,sizeof(int));
00380   assert(ret != -1);
00381   assert(_n > 0);
00382 #endif
00383 #ifndef OPT_LIB
00384   #ifndef __SUNPRO_CC
00385   assert(_n < sqrt(INT_MAX));
00386   #endif
00387 #endif
00388   size_t nn = (size_t)((_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 }