ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dfsdmat.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: David Fournier
5  * Copyright (c) 2008-2012 Regents of the University of California
6  */
11 #include <fvar.hpp>
12 #include <fcntl.h>
13 
14 #if defined (__WAT32__)
15  #include <io.h>
16 #endif
17 
18 #ifdef _MSC_VER
19  #include <io.h>
20  #define read _read
21  #define write _write
22  #define open _open
23  #define close _close
24  #ifdef _M_X64
25  typedef __int64 ssize_t;
26  #else
27  typedef int ssize_t;
28  #endif
29 #else
30  #include <sys/stat.h>
31  #include <sys/types.h>
32  #include <unistd.h>
33 #endif
34 
35 #if defined(__TURBOC__)
36  #pragma hdrstop
37  #include <iostream.h>
38  #include <iomanip.h>
39  #include <sys\stat.h>
40 #endif
41 
42 #ifdef __ZTC__
43  #include <iostream.hpp>
44  #define S_IREAD 0000400
45  #define S_IWRITE 0000200
46 #endif
47 
48 #if !defined(OPT_LIB) || defined(__MINGW64__) || defined(_MSC_VER)
49  #include <cassert>
50  #include <climits>
51 #endif
52 
53 #ifdef __NDPX__
54  #define O_RDONLY 0
55  #define O_WRONLY 1
56  #define O_RDWR 2
57  extern "C"
58  {
59  int LSEEK(int, int, int);
60  int open(const char*, int);
61  int creat(const char*, int);
62  int close(int);
63  int write(int, char*, int);
64  };
65 #endif
66 
67 #ifdef __SUN__
68  #include <iostream.h>
69  #include <sys/stat.h>
70  #include <sys/types.h>
71  #ifdef _MSC_VER
72  #define LSEEK _lseek
73  #define read _read
74  #define write _write
75  #define open _open
76  #define close _close
77  #endif
78  #include <unistd.h>
79 #endif
80 
81 #include <stdlib.h>
82 #include <stdio.h>
83 #include <string.h>
84 
85 
86 /*class dfsdmat
87 {
88  double * ptr;
89  double ** m;
90  double* minp;
91  double* maxp;
92  int n;
93 public:
94  double * getminp(void){ return minp;}
95  int size(void) {return n;}
96  dfsdmat(int n);
97  dfsdmat();
98  allocate(n);
99  ~dfsdmat();
100  deallocate(void);
101  double& elem(int i,int j);
102  double& oprator ()(int i,int j);
103 };
104 */
105 
110  disk_save_flag(1)
111 {
112  tmp_file = 0;
113  allocate();
114 }
119 {
120  shared_memory=0;
121  ptr=NULL;
122  m=NULL;
123  minp=NULL;
124  maxp=NULL;
125  n=0;
126 }
127 
133 {
134  tmp_file=0;
135  allocate(_n);
136 }
137 
142 void dfsdmat::allocate(int _n)
143 {
144  n=_n;
146  {
148  shared_memory=1;
149  disk_save_flag=1;
150  }
151  else
152  {
153  ptr= new double [(n*(n+1))/2];
154  shared_memory=0;
155  disk_save_flag=0;
156  }
157  minp=ptr;
158  maxp=ptr+((n*(n+1))/2-1);
159  m=new double * [n];
160  m-=1;
161  double * tmp =ptr;
162  for (int i=1;i<=n;i++)
163  {
164  m[i]=tmp-1;
165  tmp+=i;
166  }
167  tmp_file = 0;
168  /*
169  if (!tmp_file)
170  {
171  tmp_file=open("fmm_tmp.tmp", O_RDWR | O_CREAT | O_TRUNC |
172  O_BINARY , 0777);
173  if (tmp_file == -1)
174  {
175  cerr << "error trying to open temporary hessian file\n";
176  ad_exit(1);
177  }
178  }
179  */
180 }
181 
187 {
188  tmp_file=0;
189  disk_save_flag=1;
190  allocate(_n,gs);
191 }
192 
197 void dfsdmat::allocate(int _n, const gradient_structure& gs)
198 {
199  n=_n;
200  ptr = (double*)(const_cast<gradient_structure&>(gs).ARR_LIST1->ARRAY_MEMBLOCK_BASE);
201  shared_memory=1;
202  minp=ptr;
203  maxp=ptr+((n*(n+1))/2-1);
204  m=new double*[n];
205  m-=1;
206  double * tmp =ptr;
207  for (int i=1;i<=n;i++)
208  {
209  m[i]=tmp-1;
210  tmp+=i;
211  }
212 }
215 {
216  deallocate();
217 }
218 
224 {
225  if (ptr && !shared_memory)
226  {
227  delete [] ptr;
228  ptr=NULL;
229  }
230  if (m)
231  {
232  m+=1;
233  delete [] m;
234  m=NULL;
235  }
236  n=0;
237  if (tmp_file)
238  {
239  close(tmp_file);
240  tmp_file=0;
241  }
242 }
243 
244 #if !defined(OPT_LIB) || defined(__INTEL_COMPILER)
245 
250 double& dfsdmat::elem(int i,int j)
251 {
252  double * tmp= m[i]+j;
253  if (tmp < minp || tmp > maxp)
254  {
255  cerr << "Index out of bounds in double& dfsdmat::elem(int i,int j)"
256  << endl;
257  ad_exit(1);
258  }
259  return *tmp;
260 }
261 
266 double& dfsdmat::operator () (int i,int j)
267 {
268  double * tmp= m[i]+j;
269  if (tmp < minp || tmp > maxp)
270  {
271  cerr << "Index out of bounds in double& dfsdmat::elem(int i,int j)"
272  << endl;
273  ad_exit(1);
274  }
275  return *tmp;
276 }
277 #endif
278 
283 uostream& operator<<(const uostream& ofs, const dfsdmat& m)
284 {
285  double * p=((dfsdmat&)m).getminp();
286  int nn=((dfsdmat&)m).size();
287  nn=(nn*(nn+1))/2;
288  for (int i=1;i<=nn;i++)
289  {
290  (uostream&)ofs << *p++;
291  }
292 
293  return (uostream&)ofs;
294 }
295 
300 uistream& operator>>(const uistream& _ifs, const dfsdmat& _m)
301 {
302  uistream& ifs= (uistream&) _ifs;
303  dfsdmat& m=(dfsdmat&) _m;
304  double * p=m.getminp();
305  int nn=m.size();
306  nn=(nn*(nn+1))/2;
307  for (int i=1;i<=nn;i++)
308  {
309  ifs >> *p++;
310  }
311  return ifs;
312 }
317 {
318  if (tmp_file < 0)
319  {
320  tmp_file=open("fmm_tmp.tmp", O_RDWR | O_CREAT | O_TRUNC |
321  O_BINARY , 0777);
322  if (tmp_file < 0)
323  {
324  cerr << "error trying to open temporary hessian file\n";
325  ad_exit(1);
326  }
327  }
328  LSEEK(tmp_file,0L,SEEK_SET);
329  unsigned int _n = (unsigned int)size();
330  unsigned int nn = (_n*(_n+1))/2;
331 #ifdef OPT_LIB
332  write(tmp_file,&_n,sizeof(int));
333 #else
334  ssize_t ret = write(tmp_file,&_n,sizeof(int));
335  assert(ret != -1);
336 #endif
337 #ifdef __MINGW64__
338  size_t size = nn * sizeof(double);
339  assert(size <= UINT_MAX);
340  ssize_t num_bytes = write(tmp_file, ptr, (unsigned int)size);
341 #else
342  ssize_t num_bytes=write(tmp_file,ptr,nn*sizeof(double));
343 #endif
344  if (num_bytes <= 0)
345  {
346  cerr << "Error writing to temporary hess file in dfsdmat::save()"
347  << endl;
348  }
349  /*
350  if (const ptr && !shared_memory)
351  {
352  delete [] ptr;
353  ptr=NULL;
354  }
355  if (m)
356  {
357  m+=1;
358  delete [] m;
359  m=NULL;
360  }
361  n=0;
362  */
363 }
368 {
369  int _n=0;
370  LSEEK(tmp_file,0L,SEEK_SET);
371 #if defined(OPT_LIB) && !defined(_MSC_VER)
372  read(tmp_file,&_n,sizeof(int));
373 #else
374  ssize_t ret = read(tmp_file,&_n,sizeof(int));
375  assert(ret != -1);
376  assert(_n > 0);
377 #endif
378 #ifndef OPT_LIB
379  #ifndef __SUNPRO_CC
380  assert(_n < sqrt(INT_MAX));
381  #endif
382 #endif
383  size_t nn = (size_t)((_n * (_n + 1))/2);
384  //if (!shared_memory) allocate(_n);
385 #ifdef __MINGW64__
386  size_t size = nn * sizeof(double);
387  assert(size <= UINT_MAX);
388  ssize_t num_bytes=read(tmp_file, ptr, (unsigned int)size);
389 #else
390  ssize_t num_bytes=read(tmp_file,ptr,nn*sizeof(double));
391 #endif
392  if (num_bytes <= 0)
393  {
394  cerr << "Error reading from temporary hess file in dfsdmat::save()"
395  << endl;
396  }
397  if (tmp_file) close(tmp_file);
398  tmp_file = 0;
399 }
void read(const test_smartlist &, void *, int nsize)
void save(void)
Save values to file.
Definition: dfsdmat.cpp:316
double * minp
Definition: fvar.hpp:3238
double & operator()(int i, int j)
Definition: fvar.hpp:3281
Description not yet available.
Definition: fvar.hpp:3232
exitptr ad_exit
Definition: gradstrc.cpp:53
void allocate(void)
Initialize members but does not allocate.
Definition: dfsdmat.cpp:118
#define LSEEK
Definition: fvar.hpp:75
double ** m
Definition: fvar.hpp:3237
void deallocate(void)
Description not yet available.
Definition: dfsdmat.cpp:223
prnstream & endl(prnstream &)
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2c.cpp:11
#define O_BINARY
Definition: fvar.hpp:163
Description not yet available.
Definition: fvar.hpp:3398
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
static _THREAD gradient_structure * _instance
int shared_memory
Definition: fvar.hpp:3240
double * getminp(void)
Definition: fvar.hpp:3249
int disk_save_flag
Definition: fvar.hpp:3235
dfsdmat()
Default constructor.
Definition: dfsdmat.cpp:109
void restore(void)
Restore values to file.
Definition: dfsdmat.cpp:367
static long int get_USE_FOR_HESSIAN()
~dfsdmat()
Destructor.
Definition: dfsdmat.cpp:214
void write(const test_smartlist &, void *, int nsize)
int n
Definition: fvar.hpp:3241
istream & operator>>(const istream &input, const d3_array &arr3)
Read values from input stream into arr3.
Definition: d3_io.cpp:60
ostream & operator<<(const ostream &_s, preshowpoint p)
Description not yet available.
Definition: admanip.cpp:48
double & elem(int i, int j)
Definition: fvar.hpp:3277
int tmp_file
Definition: fvar.hpp:3234
double * maxp
Definition: fvar.hpp:3239
Description not yet available.
Definition: fvar.hpp:3516
double * ptr
Definition: fvar.hpp:3236
class for things related to the gradient structures, including dimension of arrays, size of buffers, etc.
int size(void)
Definition: fvar.hpp:3257