ADMB Documentation  11.1.2397
 All Classes Files Functions Variables Typedefs Friends Defines
dfsdmat.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: dfsdmat.cpp 1921 2014-04-23 18:47:05Z 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 #ifdef __NDPX__
00045   #define O_RDONLY 0
00046   #define O_WRONLY 1
00047   #define O_RDWR 2
00048   extern "C"
00049   {
00050     int lseek(int, int, int);
00051     int open(const char*, int);
00052     int creat(const char*, int);
00053     int close(int);
00054     int write(int, char*, int);
00055   };
00056 #endif
00057 
00058 #ifdef __SUN__
00059   #include <iostream.h>
00060   #include <sys/stat.h>
00061   #include <sys/types.h>
00062   #ifdef _MSC_VER
00063     #define lseek _lseek
00064     #define  read _read
00065     #define write _write
00066     #define open _open
00067     #define close _close
00068   #endif
00069   #include <unistd.h>
00070 #endif
00071 
00072 #include <stdlib.h>
00073 #include <stdio.h>
00074 #include <string.h>
00075 
00076 
00077 /*class dfsdmat
00078 {
00079   double * ptr;
00080   double ** m;
00081   double* minp;
00082   double* maxp;
00083   int n;
00084 public:
00085   double * getminp(void){ return minp;}
00086   int size(void) {return n;}
00087   dfsdmat(int n);
00088   dfsdmat();
00089   allocate(n);
00090   ~dfsdmat();
00091   deallocate(void);
00092   double& elem(int i,int j);
00093   double& oprator ()(int i,int j);
00094 };
00095 */
00096 
00101 dfsdmat::dfsdmat(void)
00102 {
00103   tmp_file=0;
00104   allocate();
00105 }
00106 
00111 void dfsdmat::allocate(void)
00112 {
00113   shared_memory=0;
00114   ptr=NULL;
00115   m=NULL;
00116   minp=NULL;
00117   maxp=NULL;
00118   n=0;
00119 }
00120 
00125 dfsdmat::dfsdmat(int _n)
00126 {
00127   tmp_file=0;
00128   allocate(_n);
00129 }
00130 
00135 void dfsdmat::allocate(int _n)
00136 {
00137   n=_n;
00138   if (gradient_structure::get_USE_FOR_HESSIAN())
00139   {
00140     ptr=gradient_structure::hessian_ptr;
00141     shared_memory=1;
00142     disk_save_flag=1;
00143   }
00144   else
00145   {
00146     ptr= new double [(n*(n+1))/2];
00147     shared_memory=0;
00148     disk_save_flag=0;
00149   }
00150   minp=ptr;
00151   maxp=ptr+((n*(n+1))/2-1);
00152   m=new double * [n];
00153   m-=1;
00154   double * tmp =ptr;
00155   for (int i=1;i<=n;i++)
00156   {
00157     m[i]=tmp-1;
00158     tmp+=i;
00159   }
00160   tmp_file = 0;
00161   /*
00162   if (!tmp_file)
00163   {
00164     tmp_file=open("fmm_tmp.tmp", O_RDWR | O_CREAT | O_TRUNC |
00165       O_BINARY , 0777);
00166     if (tmp_file == -1)
00167     {
00168       cerr << "error trying to open temporary hessian file\n";
00169       ad_exit(1);
00170     }
00171   }
00172    */
00173 }
00174 
00179 dfsdmat::dfsdmat(int _n, const gradient_structure& gs)
00180 {
00181   tmp_file=0;
00182   disk_save_flag=1;
00183   allocate(_n,gs);
00184 }
00185 
00190 void dfsdmat::allocate(int _n, const gradient_structure& gs)
00191 {
00192   n=_n;
00193   ptr= (double *) gs.ARRAY_MEMBLOCK_BASE;
00194   shared_memory=1;
00195   minp=ptr;
00196   maxp=ptr+((n*(n+1))/2-1);
00197   m=new double * [n];
00198   m-=1;
00199   double * tmp =ptr;
00200   for (int i=1;i<=n;i++)
00201   {
00202     m[i]=tmp-1;
00203     tmp+=i;
00204   }
00205 }
00206 
00210 dfsdmat::~dfsdmat()
00211 {
00212   deallocate();
00213 }
00214 
00219 void dfsdmat::deallocate()
00220 {
00221   if (ptr && !shared_memory)
00222   {
00223     delete [] ptr;
00224     ptr=NULL;
00225   }
00226   if (m)
00227   {
00228     m+=1;
00229     delete [] m;
00230     m=NULL;
00231   }
00232   n=0;
00233   if (tmp_file)
00234   {
00235     close(tmp_file);
00236     tmp_file=0;
00237   }
00238 }
00239 
00240 #if !defined(OPT_LIB) || defined(__INTEL_COMPILER)
00241 
00246 double& dfsdmat::elem(int i,int j)
00247 {
00248   double * tmp= m[i]+j;
00249   if (tmp < minp || tmp > maxp)
00250   {
00251     cerr << "Index out of bounds in double& dfsdmat::elem(int i,int j)"
00252          << endl;
00253     ad_exit(1);
00254   }
00255   return *tmp;
00256 }
00257 
00262 double& dfsdmat::operator () (int i,int j)
00263 {
00264   double * tmp= m[i]+j;
00265   if (tmp < minp || tmp > maxp)
00266   {
00267     cerr << "Index out of bounds in double& dfsdmat::elem(int i,int j)"
00268          << endl;
00269     ad_exit(1);
00270   }
00271   return *tmp;
00272 }
00273 #endif
00274 
00279 uostream& operator<<(const uostream& ofs, const dfsdmat& m)
00280 {
00281   double * p=((dfsdmat&)m).getminp();
00282   int nn=((dfsdmat&)m).size();
00283   nn=(nn*(nn+1))/2;
00284   for (int i=1;i<=nn;i++)
00285   {
00286     (uostream&)ofs << *p++;
00287   }
00288 
00289   return (uostream&)ofs;
00290 }
00291 
00296 uistream& operator>>(const uistream& _ifs, const dfsdmat& _m)
00297 {
00298   uistream& ifs= (uistream&) _ifs;
00299   dfsdmat& m=(dfsdmat&) _m;
00300   double * p=m.getminp();
00301   int nn=m.size();
00302   nn=(nn*(nn+1))/2;
00303   for (int i=1;i<=nn;i++)
00304   {
00305     ifs >> *p++;
00306   }
00307   return ifs;
00308 }
00309 
00314 void dfsdmat::save()
00315 {
00316   if (!tmp_file)
00317   {
00318     tmp_file=open("fmm_tmp.tmp", O_RDWR | O_CREAT | O_TRUNC |
00319       O_BINARY , 0777);
00320     if (tmp_file == -1)
00321     {
00322       cerr << "error trying to open temporary hessian file\n";
00323       ad_exit(1);
00324     }
00325   }
00326   int _n=size();
00327   int nn=(_n*(_n+1))/2;
00328   lseek(tmp_file,0L,SEEK_SET);
00329   write(tmp_file,&_n,sizeof(int));
00330   int num_bytes=write(tmp_file,ptr,nn*sizeof(double));
00331   if (num_bytes < nn)
00332   {
00333     cerr << "Error writing to temporary hess file in dfsdmat::save()"
00334          << endl;
00335   }
00336   /*
00337   if (const ptr && !shared_memory)
00338   {
00339     delete [] ptr;
00340     ptr=NULL;
00341   }
00342   if (m)
00343   {
00344     m+=1;
00345     delete [] m;
00346     m=NULL;
00347   }
00348   n=0;
00349  */
00350 }
00351 
00356 void dfsdmat::restore()
00357 {
00358   int _n=0;
00359   lseek(tmp_file,0L,SEEK_SET);
00360   read(tmp_file,&_n,sizeof(int));
00361   int nn=(_n*(_n+1))/2;
00362   //if (!shared_memory) allocate(_n);
00363   int num_bytes=read(tmp_file,ptr,nn*sizeof(double));
00364   if (num_bytes < nn)
00365   {
00366     cerr << "Error reading from temporary hess file in dfsdmat::save()"
00367          << endl;
00368   }
00369   if (tmp_file) close(tmp_file);
00370   tmp_file = 0;
00371 }