ADMB Documentation  11.1.2453
 All Classes Files Functions Variables Typedefs Friends Defines
dfsdmat.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: dfsdmat.cpp 2452 2014-10-01 23:10:01Z 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 #else
00026   #include <sys/stat.h>
00027   #include <sys/types.h>
00028   #include <unistd.h>
00029 #endif
00030 
00031 #if defined(__TURBOC__)
00032   #pragma hdrstop
00033   #include <iostream.h>
00034   #include <iomanip.h>
00035   #include <sys\stat.h>
00036 #endif
00037 
00038 #ifdef __ZTC__
00039   #include <iostream.hpp>
00040   #define S_IREAD 0000400
00041   #define S_IWRITE 0000200
00042 #endif
00043 
00044 #ifndef OPT_LIB
00045   #include <cassert>
00046   #ifdef _MSC_VER
00047   typedef int ssize_t;
00048   #endif
00049 #endif
00050 
00051 #ifdef __NDPX__
00052   #define O_RDONLY 0
00053   #define O_WRONLY 1
00054   #define O_RDWR 2
00055   extern "C"
00056   {
00057     int lseek(int, int, int);
00058     int open(const char*, int);
00059     int creat(const char*, int);
00060     int close(int);
00061     int write(int, char*, int);
00062   };
00063 #endif
00064 
00065 #ifdef __SUN__
00066   #include <iostream.h>
00067   #include <sys/stat.h>
00068   #include <sys/types.h>
00069   #ifdef _MSC_VER
00070     #define lseek _lseek
00071     #define  read _read
00072     #define write _write
00073     #define open _open
00074     #define close _close
00075   #endif
00076   #include <unistd.h>
00077 #endif
00078 
00079 #include <stdlib.h>
00080 #include <stdio.h>
00081 #include <string.h>
00082 
00083 
00084 /*class dfsdmat
00085 {
00086   double * ptr;
00087   double ** m;
00088   double* minp;
00089   double* maxp;
00090   int n;
00091 public:
00092   double * getminp(void){ return minp;}
00093   int size(void) {return n;}
00094   dfsdmat(int n);
00095   dfsdmat();
00096   allocate(n);
00097   ~dfsdmat();
00098   deallocate(void);
00099   double& elem(int i,int j);
00100   double& oprator ()(int i,int j);
00101 };
00102 */
00103 
00108 dfsdmat::dfsdmat(void)
00109 {
00110   tmp_file=0;
00111   allocate();
00112 }
00113 
00118 void dfsdmat::allocate(void)
00119 {
00120   shared_memory=0;
00121   ptr=NULL;
00122   m=NULL;
00123   minp=NULL;
00124   maxp=NULL;
00125   n=0;
00126 }
00127 
00132 dfsdmat::dfsdmat(int _n)
00133 {
00134   tmp_file=0;
00135   allocate(_n);
00136 }
00137 
00142 void dfsdmat::allocate(int _n)
00143 {
00144   n=_n;
00145   if (gradient_structure::get_USE_FOR_HESSIAN())
00146   {
00147     ptr=gradient_structure::hessian_ptr;
00148     shared_memory=1;
00149     disk_save_flag=1;
00150   }
00151   else
00152   {
00153     ptr= new double [(n*(n+1))/2];
00154     shared_memory=0;
00155     disk_save_flag=0;
00156   }
00157   minp=ptr;
00158   maxp=ptr+((n*(n+1))/2-1);
00159   m=new double * [n];
00160   m-=1;
00161   double * tmp =ptr;
00162   for (int i=1;i<=n;i++)
00163   {
00164     m[i]=tmp-1;
00165     tmp+=i;
00166   }
00167   tmp_file = 0;
00168   /*
00169   if (!tmp_file)
00170   {
00171     tmp_file=open("fmm_tmp.tmp", O_RDWR | O_CREAT | O_TRUNC |
00172       O_BINARY , 0777);
00173     if (tmp_file == -1)
00174     {
00175       cerr << "error trying to open temporary hessian file\n";
00176       ad_exit(1);
00177     }
00178   }
00179    */
00180 }
00181 
00186 dfsdmat::dfsdmat(int _n, const gradient_structure& gs)
00187 {
00188   tmp_file=0;
00189   disk_save_flag=1;
00190   allocate(_n,gs);
00191 }
00192 
00197 void dfsdmat::allocate(int _n, const gradient_structure& gs)
00198 {
00199   n=_n;
00200   ptr= (double *) gs.ARRAY_MEMBLOCK_BASE;
00201   shared_memory=1;
00202   minp=ptr;
00203   maxp=ptr+((n*(n+1))/2-1);
00204   m=new double * [n];
00205   m-=1;
00206   double * tmp =ptr;
00207   for (int i=1;i<=n;i++)
00208   {
00209     m[i]=tmp-1;
00210     tmp+=i;
00211   }
00212 }
00213 
00217 dfsdmat::~dfsdmat()
00218 {
00219   deallocate();
00220 }
00221 
00226 void dfsdmat::deallocate()
00227 {
00228   if (ptr && !shared_memory)
00229   {
00230     delete [] ptr;
00231     ptr=NULL;
00232   }
00233   if (m)
00234   {
00235     m+=1;
00236     delete [] m;
00237     m=NULL;
00238   }
00239   n=0;
00240   if (tmp_file)
00241   {
00242     close(tmp_file);
00243     tmp_file=0;
00244   }
00245 }
00246 
00247 #if !defined(OPT_LIB) || defined(__INTEL_COMPILER)
00248 
00253 double& dfsdmat::elem(int i,int j)
00254 {
00255   double * tmp= m[i]+j;
00256   if (tmp < minp || tmp > maxp)
00257   {
00258     cerr << "Index out of bounds in double& dfsdmat::elem(int i,int j)"
00259          << endl;
00260     ad_exit(1);
00261   }
00262   return *tmp;
00263 }
00264 
00269 double& dfsdmat::operator () (int i,int j)
00270 {
00271   double * tmp= m[i]+j;
00272   if (tmp < minp || tmp > maxp)
00273   {
00274     cerr << "Index out of bounds in double& dfsdmat::elem(int i,int j)"
00275          << endl;
00276     ad_exit(1);
00277   }
00278   return *tmp;
00279 }
00280 #endif
00281 
00286 uostream& operator<<(const uostream& ofs, const dfsdmat& m)
00287 {
00288   double * p=((dfsdmat&)m).getminp();
00289   int nn=((dfsdmat&)m).size();
00290   nn=(nn*(nn+1))/2;
00291   for (int i=1;i<=nn;i++)
00292   {
00293     (uostream&)ofs << *p++;
00294   }
00295 
00296   return (uostream&)ofs;
00297 }
00298 
00303 uistream& operator>>(const uistream& _ifs, const dfsdmat& _m)
00304 {
00305   uistream& ifs= (uistream&) _ifs;
00306   dfsdmat& m=(dfsdmat&) _m;
00307   double * p=m.getminp();
00308   int nn=m.size();
00309   nn=(nn*(nn+1))/2;
00310   for (int i=1;i<=nn;i++)
00311   {
00312     ifs >> *p++;
00313   }
00314   return ifs;
00315 }
00316 
00321 void dfsdmat::save()
00322 {
00323   if (!tmp_file)
00324   {
00325     tmp_file=open("fmm_tmp.tmp", O_RDWR | O_CREAT | O_TRUNC |
00326       O_BINARY , 0777);
00327     if (tmp_file == -1)
00328     {
00329       cerr << "error trying to open temporary hessian file\n";
00330       ad_exit(1);
00331     }
00332   }
00333   int _n=size();
00334   int nn=(_n*(_n+1))/2;
00335   lseek(tmp_file,0L,SEEK_SET);
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   size_t num_bytes=write(tmp_file,ptr,nn*sizeof(double));
00343   if (num_bytes < (size_t)nn)
00344   {
00345     cerr << "Error writing to temporary hess file in dfsdmat::save()"
00346          << endl;
00347   }
00348   /*
00349   if (const ptr && !shared_memory)
00350   {
00351     delete [] ptr;
00352     ptr=NULL;
00353   }
00354   if (m)
00355   {
00356     m+=1;
00357     delete [] m;
00358     m=NULL;
00359   }
00360   n=0;
00361  */
00362 }
00363 
00368 void dfsdmat::restore()
00369 {
00370   int _n=0;
00371   lseek(tmp_file,0L,SEEK_SET);
00372   read(tmp_file,&_n,sizeof(int));
00373   int nn=(_n*(_n+1))/2;
00374   //if (!shared_memory) allocate(_n);
00375   size_t num_bytes=read(tmp_file,ptr,nn*sizeof(double));
00376   if (num_bytes < (size_t)nn)
00377   {
00378     cerr << "Error reading from temporary hess file in dfsdmat::save()"
00379          << endl;
00380   }
00381   if (tmp_file) close(tmp_file);
00382   tmp_file = 0;
00383 }