ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gradstak.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 <iostream>
12 #include <fcntl.h>
13 #include "fvar.hpp"
14 #include <adstring.hpp>
15 
16 #if defined (__WAT32__)
17  #include <io.h>
18 #endif
19 
20 #ifdef _MSC_VER
21  #define read _read
22  #define write _write
23  #define open _open
24  #define close _close
25  #include <sys\stat.h>
26 #else
27  #include <sys/stat.h>
28  #include <sys/types.h>
29  #include <unistd.h>
30 #endif
31 
32 #if defined(__TURBOC__)
33  #pragma hdrstop
34  #include <iostream.h>
35  #include <iomanip.h>
36  #include <sys\stat.h>
37 #endif
38 
39 #ifdef __ZTC__
40  #include <iostream.hpp>
41  #define S_IREAD 0000400
42  #define S_IWRITE 0000200
43 #endif
44 
45 #ifdef __NDPX__
46  #define O_RDONLY 0
47  #define O_WRONLY 1
48  #define O_RDWR 2
49  extern "C"
50  {
51  int LSEEK(int, int, int);
52  int open(const char*, int);
53  int creat(const char*, int);
54  int close(int);
55  int write(int, char*, int);
56  };
57 #endif
58 
59 #include <stdlib.h>
60 #include <stdio.h>
61 #include <string.h>
62 #include <time.h>
63 
64 #if !defined(OPT_LIB)
65  #include <cassert>
66  #include <climits>
67 #endif
68 #if defined(__MINGW64__) || (defined(_MSC_VER) && defined(_M_X64))
69  #include <cassert>
70  #include <climits>
71 #endif
72 
73 #ifdef _MSC_VER
74  #ifdef _M_X64
75  typedef __int64 ssize_t;
76  #else
77  typedef int ssize_t;
78  #endif
79  #ifndef SSIZE_MAX
80  #define SSIZE_MAX INT_MAX
81  #endif
82 #endif
83 
84 char lastchar(char *);
85 char ad_random_part[6]="tmp";
86 
88 {
89 /*
90  time_t t,tt;
91  time(&t);
92  tt=t;
93  int div=1;
94  for (int i=0;i<6;i++)
95  {
96  ad_random_part[i]=(tt/div)%10+48;
97  div*=10;
98  }
99 */
100 }
107 grad_stack::grad_stack(const size_t size, const unsigned int id)
108 {
109  true_length = size;
111 
112 #ifndef OPT_LIB
113  assert(length > 0);
114 #endif
115 
117  if (!true_ptr_first)
118  {
119  cerr << "Memory allocation error in grad_stack constructor\n"
120  << " trying to allocate grad_stack_entry[" << length << "] array.\n";
121  ad_exit(1);
122  }
123 
124 #ifdef DIAG
125  test_the_pointer();
126 #endif
127 
129  ptr = ptr_first;
131  //table=new lvector(-128,250);
132 
133  _GRADFILE_PTR = -1; // set to illegal value for later checking
134  end_pos = 0;
135  end_pos1 = 0;
136  end_pos2 = 0;
137 
138  char* path = getenv("ADTMP"); // NULL if not defined
139 
140 #if defined(USE_ADPVM)
141  adstring string_path;
142  if (path) string_path=path;
143  int on=0;
144  int nopt=0;
145  adstring currdir;
146  ad_getcd(currdir);
148  {
149  if ((on = option_match(ad_comm::argc, ad_comm::argv, "-slave", nopt)) > -1)
150  {
151  if (nopt ==1)
152  {
153  int ierr=make_sub_directory(ad_comm::argv[on+1]);
155  string_path+=ad_comm::subdir;
156  path=(char*) string_path;
157  }
158  else
159  {
160  cerr << "Wrong number of options to -slave -- must be 1"
161  " you have " << nopt << endl;
162  ad_exit(1);
163  }
164  }
165  }
166 #endif
167 
168  if (path != NULL && strlen(path) <= 45)
169  {
170 #if !defined (_WIN32)
171  if (id > 0)
172  sprintf(&gradfile_name1[0],"%s/gradfil1%u.tmp", path, id);
173  else
174  sprintf(&gradfile_name1[0],"%s/gradfil1.tmp", path);
175 #else
176  if (lastchar(path) != '\\')
177  {
178  if (id > 0)
179  sprintf(&gradfile_name1[0], "%s\\gradfil1%u.tmp", path, id);
180  else
181  sprintf(&gradfile_name1[0], "%s\\gradfil1.tmp", path);
182  }
183  else
184  {
185  if (id > 0)
186  sprintf(&gradfile_name1[0], "%sgradfil1%u.tmp", path, id);
187  else
188  sprintf(&gradfile_name1[0], "%sgradfil1.tmp", path);
189  }
190 #endif
191  }
192  else
193  {
194  if (id > 0)
195  sprintf(&gradfile_name1[0], "gradfil1%u.tmp", id);
196  else
197  sprintf(&gradfile_name1[0], "gradfil1.tmp");
198  }
199 
200  path = getenv("ADTMP1"); // NULL if not defined
201 #if defined(USE_ADPVM)
202  adstring string_path2;
203  if (path) string_path2=path;
204  string_path2+=ad_comm::subdir;
205  path=(char*) string_path2;
206 #endif
207  if (path != NULL && strlen(path) <= 45)
208  {
209 #if !defined (_WIN32)
210  if (strlen(path) > 0)
211  {
212  if (id > 0)
213  {
214  sprintf(&var_store_file_name[0],"%s/varssave%u.tmp",path, id);
215  sprintf(&gradfile_name2[0],"%s/gradfil2%u.tmp", path, id);
216  }
217  else
218  {
219  sprintf(&var_store_file_name[0],"%s/varssave.tmp",path);
220  sprintf(&gradfile_name2[0],"%s/gradfil2.tmp", path);
221  }
222  }
223  else
224  {
225  if (id > 0)
226  {
227  sprintf(&var_store_file_name[0],"varssave%u.tmp", id);
228  sprintf(&gradfile_name2[0],"gradfil2%u.tmp", id);
229  }
230  else
231  {
232  sprintf(&var_store_file_name[0],"varssave.tmp");
233  sprintf(&gradfile_name2[0],"gradfil2.tmp");
234  }
235  }
236 #else
237  if (lastchar(path)!='\\')
238  {
239  if (id > 0)
240  {
241  sprintf(&gradfile_name2[0], "%s\\gradfil2%u.tmp", path, id);
242  sprintf(&var_store_file_name[0], "%s\\varssave%u.tmp", path, id);
243  }
244  else
245  {
246  sprintf(&gradfile_name2[0], "%s\\gradfil2.tmp", path);
247  sprintf(&var_store_file_name[0], "%s\\varssave.tmp", path);
248  }
249  }
250  else
251  {
252  if (id > 0)
253  {
254  sprintf(&gradfile_name2[0], "%sgradfil2%u.tmp", path, id);
255  sprintf(&var_store_file_name[0], "%svarssave%u.tmp", path, id);
256  }
257  else
258  {
259  sprintf(&gradfile_name2[0], "%sgradfil2.tmp", path);
260  sprintf(&var_store_file_name[0], "%svarssave.tmp", path);
261  }
262  }
263 #endif
264  }
265  else
266  {
267  if (id > 0)
268  {
269  sprintf(&gradfile_name2[0], "gradfil2%u.tmp", id);
270  sprintf(&var_store_file_name[0], "varssave%u.tmp", id);
271  }
272  else
273  {
274  sprintf(&gradfile_name2[0], "gradfil2.tmp");
275  sprintf(&var_store_file_name[0], "varssave.tmp");
276  }
277  }
278 
279  create_gradfile();
280 
281  strcpy(gradfile_name, gradfile_name1);
282  _GRADFILE_PTR = _GRADFILE_PTR1;
283 }
286 {
287  const int repfs = option_match(ad_comm::argc, ad_comm::argv, "-fsize");
288  if (ad_comm::global_logfile && repfs)
289  {
290  OFF_T pos = LSEEK(_GRADFILE_PTR1, 0, SEEK_END);
291  *ad_comm::global_logfile << "size of file " << gradfile_name1
292  << " = " << pos << endl;
293 
294  pos = LSEEK(_GRADFILE_PTR2, 0, SEEK_END);
295  *ad_comm::global_logfile << "size of file " << gradfile_name2
296  << " = " << pos << endl;
297 
298  pos = LSEEK(_VARSSAV_PTR, 0, SEEK_END);
299  *ad_comm::global_logfile << "size of file " << var_store_file_name
300  << " = " << pos << endl;
301  }
302  if (close(_GRADFILE_PTR1))
303  {
304  cerr << "Error closing file " << gradfile_name1 << "\n"
305  << "in grad_stack::~grad_stack().\n";
306  }
307  _GRADFILE_PTR1 = -1;
308  if (close(_GRADFILE_PTR2))
309  {
310  cerr << "Error closing file " << gradfile_name2 << "\n"
311  << "in grad_stack::~grad_stack().\n";
312  }
313  _GRADFILE_PTR2 = -1;
314  if (close(_VARSSAV_PTR))
315  {
316  cerr << "Error closing file " << var_store_file_name << "\n"
317  << "in grad_stack::~grad_stack().\n";
318  }
319  _VARSSAV_PTR = -1;
320 #if !defined (_MSC_VER)
321  unlink(gradfile_name1);
322  unlink(gradfile_name2);
323  unlink(var_store_file_name);
324 #else
325  remove(gradfile_name1);
326  remove(gradfile_name2);
327  remove(var_store_file_name);
328 #endif
329  if (true_ptr_first)
330  {
331  delete [] true_ptr_first;
332  true_ptr_first = NULL;
333  }
334 }
340 {
341 #ifdef GRAD_DIAG
342  cout << "Grad_stack size exceeded\n ";
343  cout << "Writing to temporary file -- \n";
344 #endif
345 
346  ptr--;
347 
348 #ifdef GRAD_DIAG
349  {
350  OFF_T lpos = lseek(_GRADFILE_PTR,0L,SEEK_CUR);
351  cout << "Offset in file before write is " << lpos
352  << " bytes from the beginning\n";
353  }
354 #endif
355 
356  // if the buffer is really large only write the end of it
358  size_t nbw = sizeof(grad_stack_entry) * length;
359 
360  //char * ttmp = (char *) ptr_first; ttmp--;
361 
362  // save the current end of file in case we can't write the whole buffer
363  end_pos = lseek(_GRADFILE_PTR,0L,SEEK_CUR);
364 #if defined(__MINGW64__) || (defined(_WIN64) && defined(_MSC_VER))
365  assert(nbw <= UINT_MAX);
366  ssize_t ierr = write(_GRADFILE_PTR, ptr_first, (unsigned int)nbw);
367 #else
368  ssize_t ierr = write(_GRADFILE_PTR, ptr_first, nbw);
369 #endif
370 
371 #ifndef OPT_LIB
372  #ifdef _MSC_VER
373  assert(nbw <= SSIZE_MAX);
374  #endif
375 #endif
376 
377  if (ierr != (ssize_t)nbw)
378  {
379  cout << "Wrote " << ierr << " not " << nbw << endl;
380 
381 #ifndef OPT_LIB
382  OFF_T offset = LSEEK(_GRADFILE_PTR, end_pos, SEEK_SET);
383  assert(offset != -1);
384 #else
385  LSEEK(_GRADFILE_PTR, end_pos, SEEK_SET);
386 #endif
387 
388  //save the end of file for this file so we can reposition later
389  end_pos1 = end_pos;
391 #if defined(__MINGW64__) || (defined(_WIN64) && defined(_MSC_VER))
392  assert(nbw <= UINT_MAX);
393  ierr = write(_GRADFILE_PTR, ptr_first, (unsigned int)nbw);
394 #else
395  ierr = write(_GRADFILE_PTR, ptr_first, nbw);
396 #endif
397 
398  if (ierr != (ssize_t)nbw)
399  {
400  perror("Error writing to temporary gradient stack file");
401  cerr <<" File name: " << gradfile_name << "\n";
402 #if defined(_MSC_VER)
403  cerr <<" File length: "
404  << filelength(_GRADFILE_PTR)
405  << " bytes.\n";
406 #endif
407  cerr << " Attempting to write " << nbw <<" bytes; wrote "
408  << ierr << ".\n";
409  ad_exit(1);
410  }
411  }
412 #ifdef GRAD_DIAG
413  else
414  {
415  cout << "Wrote " << ierr << "bytes into temp. grad. file\n";
416  }
417  {
418  OFF_T lpos = LSEEK(gradient_structure::_GRADFILE_PTR,0L,SEEK_CUR);
419  cout << "Offset in file after write is " << lpos
420  << " bytes from the beginning\n";
421  }
422 #endif
423  //keep track of the size of the grad_stack
424  gs->TOTAL_BYTES += nbw;
425  ptr = ptr_first;
426 }
427 
433 {
434  #if defined (__TURBOC__)
435  _GRADFILE_PTR1=open(gradfile_name1, O_RDWR | O_CREAT |
436  O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR);
437  _VARSSAV_PTR=open(var_store_file_name, O_RDWR | O_CREAT |
438  O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR);
439  #elif defined (__ZTC__)
440  _GRADFILE_PTR1=open(gradfile_name1, O_RDWR | O_CREAT |
441  O_TRUNC , S_IRUSR | S_IWUSR);
442  _VARSSAV_PTR=open(var_store_file_name, O_RDWR | O_CREAT
443  | O_TRUNC, S_IRUSR | S_IWUSR);
444  #elif defined (__NDPX__)
445  _GRADFILE_PTR1=creat(gradfile_name1, O_RDWR);
446  _VARSSAV_PTR=creat(var_store_file_name, O_RDWR);
447  #elif defined (__WAT32__)
448  _GRADFILE_PTR1=open(gradfile_name1, O_RDWR | O_CREAT |
449  O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR);
450  _VARSSAV_PTR=open(var_store_file_name, O_RDWR | O_CREAT |
451  O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR);
452  #else
453  _GRADFILE_PTR1=open(gradfile_name1, O_RDWR | O_CREAT | O_TRUNC |
454  O_BINARY , 0777);
455  _VARSSAV_PTR=open(var_store_file_name, O_RDWR |
456  O_CREAT | O_TRUNC | O_BINARY, 0777);
457  #endif
458 
459  if (_GRADFILE_PTR1 == -1)
460  {
461  ad_printf("Error opening temporary gradient file"
462  " %s\n",gradfile_name1);
463  ad_exit(1);
464  }
465 
466  if (_VARSSAV_PTR == -1)
467  {
468  perror("Error opening temporary gradient file");
469  cerr <<" Attempting to open " << var_store_file_name <<"\n";
470  ad_exit(1);
471  }
472 
473  #if defined (__TURBOC__)
474  _GRADFILE_PTR2=open(gradfile_name2, O_RDWR | O_CREAT | O_TRUNC |
475  O_BINARY, S_IRUSR | S_IWUSR);
476  #elif defined (__ZTC__)
477  _GRADFILE_PTR2=open(gradfile_name2, O_RDWR | O_CREAT | O_TRUNC ,
478  S_IRUSR | S_IWUSR);
479  #elif defined (__NDPX__)
480  _GRADFILE_PTR2=creat(gradfile_name2, O_RDWR);
481  #elif defined (_MSC_VER)
482  _GRADFILE_PTR2=open(gradfile_name2, O_RDWR | O_CREAT | O_TRUNC |
483  O_BINARY, S_IREAD | S_IWRITE);
484  #elif defined (__WAT32__)
485  _GRADFILE_PTR2=open(gradfile_name2, O_RDWR | O_CREAT | O_TRUNC |
486  O_BINARY, S_IRUSR | S_IWUSR);
487  #else
488  _GRADFILE_PTR2=open(gradfile_name2, O_RDWR | O_CREAT | O_TRUNC |
489  O_BINARY , 0777);
490  #endif
491 
492  if (_GRADFILE_PTR2 == -1)
493  {
494  perror("Error opening temporary gradient file");
495  cerr <<" Attempting to open " << gradfile_name2 <<"\n";
496  ad_exit(1);
497  }
498 }
504 {
506  {
507  cerr << "Attempted to open a third gradient file -- There is\n"
508  "probably no more room on the TMP1 (if defined) device\n"
509  "if possible set TMP1 environment string to a device with more room\n";
510  ad_exit(1);
511  }
512  strcpy(gradfile_name, gradfile_name2);
514 }
520 {
522  {
523  return -1;
524  }
525 
526  strcpy(gradfile_name, gradfile_name1);
528 
529  return 0;
530 }
531 #ifdef _MSC_VER
533 #else
535 #endif
536 {
537  return _GRADFILE_PTR;
538 }
544 {
545  return gradfile_name;
546 }
552 {
553  if (length > 10000L)
554  {
555  //make the buffer end_buf_size
556  unsigned int end_buf_size=5000L;
557  ptr_first+=length-end_buf_size;
558  length=end_buf_size;
559  }
560 }
565 void grad_stack::set_gradient_stack0(void (* func)(void),double * dep_addr)
566 {
567 #ifdef NO_DERIVS
569  {
570 #endif
571  if (ptr > ptr_last)
572  {
573  // current buffer is full -- write it to disk and reset pointer
574  // and counter
575  this->write_grad_stack_buffer();
576  }
577  ptr->func = func;
578  ptr->dep_addr = dep_addr;
579  ptr++;
580 #ifdef NO_DERIVS
581  }
582 #endif
583 }
~grad_stack()
Destructor.
Definition: gradstak.cpp:285
static adpvm_manager * pvm_manager
Definition: fvar.hpp:8849
void set_gbuffer_pointers(void)
Description not yet available.
Definition: gradstak.cpp:551
OFF_T end_pos2
Definition: fvar.hpp:954
grad_stack_entry * true_ptr_first
Definition: fvar.hpp:923
grad_stack()
Definition: fvar.hpp:970
char gradfile_name2[61]
Definition: fvar.hpp:937
static ofstream * global_logfile
Definition: fvar.hpp:8858
OFF_T end_pos
Definition: fvar.hpp:952
char gradfile_name1[61]
Definition: fvar.hpp:936
int _GRADFILE_PTR2
Definition: fvar.hpp:944
static adstring subdir
Definition: fvar.hpp:8851
static char ** argv
Definition: fvar.hpp:8866
OFF_T end_pos1
Definition: fvar.hpp:953
Holds derivative information for arithmetic operators and math library functions. ...
Definition: fvar.hpp:894
int decrement_current_gradfile_ptr(void)
If there is a previous file set the handle to point to it otherwise return a -1.
Definition: gradstak.cpp:519
void write_grad_stack_buffer()
Definition: fvar.hpp:976
char * get_gradfile_name()
Description not yet available.
Definition: gradstak.cpp:543
exitptr ad_exit
Definition: gradstrc.cpp:53
char lastchar(char *)
Description not yet available.
Definition: gradstrc.cpp:173
int _GRADFILE_PTR1
Definition: fvar.hpp:943
#define LSEEK
Definition: fvar.hpp:75
prnstream & endl(prnstream &)
#define O_BINARY
Definition: fvar.hpp:163
static int argc
Definition: fvar.hpp:8863
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
size_t true_length
Definition: fvar.hpp:931
void create_gradfile()
Description not yet available.
Definition: gradstak.cpp:432
#define OFF_T
Definition: fvar.hpp:74
char ad_random_part[6]
Definition: gradstak.cpp:85
void ad_getcd(const adstring &s)
Description not yet available.
Definition: makesub.cpp:56
grad_stack_entry * ptr_first
Definition: fvar.hpp:924
int option_match(int argc, char *argv[], const char *string)
Checks if the program has been invoked with a particular command line argument (&quot;string&quot;).
Definition: optmatch.cpp:25
void write(const test_smartlist &, void *, int nsize)
int & gradfile_handle()
Definition: gradstak.cpp:534
grad_stack_entry * ptr
Definition: fvar.hpp:934
char var_store_file_name[61]
Definition: fvar.hpp:938
size_t pos(const adstring &substr, const adstring &s)
Definition: string3.cpp:56
char gradfile_name[61]
Definition: fvar.hpp:935
void fill_ad_random_part(void)
Definition: gradstak.cpp:87
size_t length
Definition: fvar.hpp:930
void set_gradient_stack0(void(*func)(void), double *dep_addr)
Description not yet available.
Definition: gradstak.cpp:565
int _GRADFILE_PTR
Definition: fvar.hpp:942
int make_sub_directory(const char *s)
Create a sub directory s.
Definition: makesub.cpp:95
class for things related to the gradient structures, including dimension of arrays, size of buffers, etc.
grad_stack_entry * ptr_last
Definition: fvar.hpp:925
void(* func)(void)
&lt; Pointer to function (if any) to be used for derivative calculation
Definition: fvar.hpp:898
double * dep_addr
Pointer to dependent variable.
Definition: fvar.hpp:899
void increment_current_gradfile_ptr(void)
If there is another file set the handle to point to it otherwise we are out of room.
Definition: gradstak.cpp:503
int _VARSSAV_PTR
Definition: fvar.hpp:945
int ad_printf(FILE *stream, const char *format, Args...args)
Definition: fvar.hpp:9487