ADMB Documentation  11.1.1920
 All Classes Files Functions Variables Typedefs Friends Defines
dfsdmat.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: dfsdmat.cpp 1919 2014-04-22 22:02:01Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #define SAFE_ARRAYS
00012 #include <fvar.hpp>
00013 #include <fcntl.h>
00014 
00015 #if defined (__WAT32__)
00016   #include <io.h>
00017 #endif
00018 
00019 #ifdef _MSC_VER
00020   #include <io.h>
00021   #define lseek _lseek
00022   #define  read _read
00023   #define write _write
00024   #define open _open
00025   #define close _close
00026 #else
00027   #include <sys/stat.h>
00028   #include <sys/types.h>
00029   #include <unistd.h>
00030 #endif
00031 
00032 #if defined(__TURBOC__)
00033   #pragma hdrstop
00034   #include <iostream.h>
00035   #include <iomanip.h>
00036   #include <sys\stat.h>
00037 #endif
00038 
00039 #ifdef __ZTC__
00040   #include <iostream.hpp>
00041   #define S_IREAD 0000400
00042   #define S_IWRITE 0000200
00043 #endif
00044 
00045 #ifdef __NDPX__
00046   #define O_RDONLY 0
00047   #define O_WRONLY 1
00048   #define O_RDWR 2
00049   extern "C"
00050   {
00051     int lseek(int, int, int);
00052     int open(const char*, int);
00053     int creat(const char*, int);
00054     int close(int);
00055     int write(int, char*, int);
00056   };
00057 #endif
00058 
00059 #ifdef __SUN__
00060   #include <iostream.h>
00061   #include <sys/stat.h>
00062   #include <sys/types.h>
00063   #ifdef _MSC_VER
00064     #define lseek _lseek
00065     #define  read _read
00066     #define write _write
00067     #define open _open
00068     #define close _close
00069   #endif
00070   #include <unistd.h>
00071 #endif
00072 
00073 #include <stdlib.h>
00074 #include <stdio.h>
00075 #include <string.h>
00076 
00077 
00078 /*class dfsdmat
00079 {
00080   double * ptr;
00081   double ** m;
00082   double* minp;
00083   double* maxp;
00084   int n;
00085 public:
00086   double * getminp(void){ return minp;}
00087   int size(void) {return n;}
00088   dfsdmat(int n);
00089   dfsdmat();
00090   allocate(n);
00091   ~dfsdmat();
00092   deallocate(void);
00093   double& elem(int i,int j);
00094   double& oprator ()(int i,int j);
00095 };
00096 */
00097 
00102 dfsdmat::dfsdmat(void)
00103 {
00104   tmp_file=0;
00105   allocate();
00106 }
00107 
00112 void dfsdmat::allocate(void)
00113 {
00114   shared_memory=0;
00115   ptr=NULL;
00116   m=NULL;
00117   minp=NULL;
00118   maxp=NULL;
00119   n=0;
00120 }
00121 
00126 dfsdmat::dfsdmat(int _n)
00127 {
00128   tmp_file=0;
00129   allocate(_n);
00130 }
00131 
00136 void dfsdmat::allocate(int _n)
00137 {
00138   n=_n;
00139   if (gradient_structure::get_USE_FOR_HESSIAN())
00140   {
00141     ptr=gradient_structure::hessian_ptr;
00142     shared_memory=1;
00143     disk_save_flag=1;
00144   }
00145   else
00146   {
00147     ptr= new double [(n*(n+1))/2];
00148     shared_memory=0;
00149     disk_save_flag=0;
00150   }
00151   minp=ptr;
00152   maxp=ptr+((n*(n+1))/2-1);
00153   m=new double * [n];
00154   m-=1;
00155   double * tmp =ptr;
00156   for (int i=1;i<=n;i++)
00157   {
00158     m[i]=tmp-1;
00159     tmp+=i;
00160   }
00161   tmp_file = 0;
00162   /*
00163   if (!tmp_file)
00164   {
00165     tmp_file=open("fmm_tmp.tmp", O_RDWR | O_CREAT | O_TRUNC |
00166       O_BINARY , 0777);
00167     if (tmp_file == -1)
00168     {
00169       cerr << "error trying to open temporary hessian file\n";
00170       ad_exit(1);
00171     }
00172   }
00173    */
00174 }
00175 
00180 dfsdmat::dfsdmat(int _n, const gradient_structure& gs)
00181 {
00182   tmp_file=0;
00183   disk_save_flag=1;
00184   allocate(_n,gs);
00185 }
00186 
00191 void dfsdmat::allocate(int _n, const gradient_structure& gs)
00192 {
00193   n=_n;
00194   ptr= (double *) gs.ARRAY_MEMBLOCK_BASE;
00195   shared_memory=1;
00196   minp=ptr;
00197   maxp=ptr+((n*(n+1))/2-1);
00198   m=new double * [n];
00199   m-=1;
00200   double * tmp =ptr;
00201   for (int i=1;i<=n;i++)
00202   {
00203     m[i]=tmp-1;
00204     tmp+=i;
00205   }
00206 }
00207 
00211 dfsdmat::~dfsdmat()
00212 {
00213   deallocate();
00214 }
00215 
00220 void dfsdmat::deallocate()
00221 {
00222   if (ptr && !shared_memory)
00223   {
00224     delete [] ptr;
00225     ptr=NULL;
00226   }
00227   if (m)
00228   {
00229     m+=1;
00230     delete [] m;
00231     m=NULL;
00232   }
00233   n=0;
00234   if (tmp_file)
00235   {
00236     close(tmp_file);
00237     tmp_file=0;
00238   }
00239 }
00240 
00241 #if !defined(OPT_LIB) || defined(__INTEL_COMPILER)
00242 
00247 double& dfsdmat::elem(int i,int j)
00248 {
00249   double * tmp= m[i]+j;
00250   if (tmp < minp || tmp > maxp)
00251   {
00252     cerr << "Index out of bounds in double& dfsdmat::elem(int i,int j)"
00253          << endl;
00254     ad_exit(1);
00255   }
00256   return *tmp;
00257 }
00258 
00263 double& dfsdmat::operator () (int i,int j)
00264 {
00265   double * tmp= m[i]+j;
00266   if (tmp < minp || tmp > maxp)
00267   {
00268     cerr << "Index out of bounds in double& dfsdmat::elem(int i,int j)"
00269          << endl;
00270     ad_exit(1);
00271   }
00272   return *tmp;
00273 }
00274 #endif
00275 
00280 uostream& operator<<(const uostream& ofs, const dfsdmat& m)
00281 {
00282   double * p=((dfsdmat&)m).getminp();
00283   int nn=((dfsdmat&)m).size();
00284   nn=(nn*(nn+1))/2;
00285   for (int i=1;i<=nn;i++)
00286   {
00287     (uostream&)ofs << *p++;
00288   }
00289 
00290   return (uostream&)ofs;
00291 }
00292 
00297 uistream& operator>>(const uistream& _ifs, const dfsdmat& _m)
00298 {
00299   uistream& ifs= (uistream&) _ifs;
00300   dfsdmat& m=(dfsdmat&) _m;
00301   double * p=m.getminp();
00302   int nn=m.size();
00303   nn=(nn*(nn+1))/2;
00304   for (int i=1;i<=nn;i++)
00305   {
00306     ifs >> *p++;
00307   }
00308   return ifs;
00309 }
00310 
00315 void dfsdmat::save()
00316 {
00317   if (!tmp_file)
00318   {
00319     tmp_file=open("fmm_tmp.tmp", O_RDWR | O_CREAT | O_TRUNC |
00320       O_BINARY , 0777);
00321     if (tmp_file == -1)
00322     {
00323       cerr << "error trying to open temporary hessian file\n";
00324       ad_exit(1);
00325     }
00326   }
00327   int _n=size();
00328   int nn=(_n*(_n+1))/2;
00329   lseek(tmp_file,0L,SEEK_SET);
00330   write(tmp_file,&_n,sizeof(int));
00331   int num_bytes=write(tmp_file,ptr,nn*sizeof(double));
00332   if (num_bytes < nn)
00333   {
00334     cerr << "Error writing to temporary hess file in dfsdmat::save()"
00335          << endl;
00336   }
00337   /*
00338   if (const ptr && !shared_memory)
00339   {
00340     delete [] ptr;
00341     ptr=NULL;
00342   }
00343   if (m)
00344   {
00345     m+=1;
00346     delete [] m;
00347     m=NULL;
00348   }
00349   n=0;
00350  */
00351 }
00352 
00357 void dfsdmat::restore()
00358 {
00359   int _n=0;
00360   lseek(tmp_file,0L,SEEK_SET);
00361   read(tmp_file,&_n,sizeof(int));
00362   int nn=(_n*(_n+1))/2;
00363   //if (!shared_memory) allocate(_n);
00364   int num_bytes=read(tmp_file,ptr,nn*sizeof(double));
00365   if (num_bytes < nn)
00366   {
00367     cerr << "Error reading from temporary hess file in dfsdmat::save()"
00368          << endl;
00369   }
00370   if (tmp_file) close(tmp_file);
00371   tmp_file = 0;
00372 }