ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gst_read.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 
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <string.h>
16 
17 #ifdef __TURBOC__
18  #pragma hdrstop
19  #include <iostream.h>
20 #endif
21 
22 #ifdef __ZTC__
23  #include <iostream.hpp>
24 #endif
25 
26 #if defined (__WAT32__)
27 #endif
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 
32 #ifdef __SUN__
33  #include <iostream.h>
34  #include <fcntl.h>
35  #include <sys/stat.h>
36  #include <sys/types.h>
37  #include <unistd.h>
38 #endif
39 
40 #ifdef _MSC_VER
41  #ifdef _M_X64
42  typedef __int64 ssize_t;
43  #else
44  typedef int ssize_t;
45  #endif
46  #define read _read
47  #define write _write
48  #define open _open
49  #define close _close
50 #else
51  #include <iostream>
52  #include <fcntl.h>
53  #include <sys/stat.h>
54  #include <sys/types.h>
55  #include <unistd.h>
56 #endif
57 
58 #if defined(__MINGW64__) || (defined(_WIN64) && defined(_MSC_VER))
59  #include <cassert>
60  #include <climits>
61 #endif
62 
63 #if defined(__NDPX__ )
64  extern "C" {
65  int LSEEK(int, int, int);
66  int read(int, char*, int);
67  };
68 #endif
69 
70 #include <math.h>
71 
77  {
78  // check to see if we are past the beginning of this file
79  if (lpos < 0)
80  {
81  lpos = LSEEK(_GRADFILE_PTR, 0L,SEEK_SET);
82  // get the previous file
84  {
85  // there is no more file to read ... but perhaps the beginning of
86  // the buffer has not been used
88  {
89  // break condition
90  return 0;
91  }
92  else
93  {
96  return 1;
97  }
98  }
99  else
100  {
101  // get the end of file for the first file
102  end_pos = end_pos1;
103  // repostion the first file to end of pointer
104  LSEEK(_GRADFILE_PTR,end_pos,SEEK_SET);
105  }
106  // now back up the file one buffer size
107  lpos = LSEEK(_GRADFILE_PTR, -((OFF_T)(sizeof(grad_stack_entry)*length)),SEEK_CUR);
108  if (lpos == -1L)
109  {
110  cerr << "Error positioning temporary gradient file "
111  << get_gradfile_name()
112  << " after open.\n";
113  ad_exit(1);
114  }
115  }
116 #ifdef DIAG
117  {
118  OFF_T lpos = LSEEK(_GRADFILE_PTR,0L,SEEK_CUR);
119  cout << "Offset in file before read is " << lpos
120  << " bytes from the beginning\n";
121  }
122 #endif
123 
124 #if defined(__MINGW64__) || (defined(_WIN64) && defined(_MSC_VER))
125  size_t size = sizeof(grad_stack_entry) * length;
126  assert(size <= UINT_MAX);
127  ssize_t nread = read(_GRADFILE_PTR, ptr_first, (unsigned int)size);
128 #else
129  ssize_t nread = read(_GRADFILE_PTR, ptr_first,
130  sizeof(grad_stack_entry)*length);
131 #endif
132  ptr = ptr_first + length-1;
133 
134  if (nread == -1 )
135  {
136  perror("IO error trying to read temporary gradient file\n");
137  }
138  if (nread <((int)(sizeof(grad_stack_entry)*length)) )
139  {
140  perror("End of file encountered trying to read temporary gradient file\n");
141  cout << "Read " << nread << "bytes from temp. grad. file\n";
142  }
143  lpos = LSEEK(_GRADFILE_PTR,-((OFF_T)(sizeof(grad_stack_entry)*length)),
144  SEEK_CUR);
145  // no break condition
146  return 1;
147  }
void read(const test_smartlist &, void *, int nsize)
grad_stack_entry * true_ptr_first
Definition: fvar.hpp:923
OFF_T end_pos
Definition: fvar.hpp:952
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
char * get_gradfile_name()
Description not yet available.
Definition: gradstak.cpp:543
exitptr ad_exit
Definition: gradstrc.cpp:53
#define LSEEK
Definition: fvar.hpp:75
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
size_t true_length
Definition: fvar.hpp:931
#define OFF_T
Definition: fvar.hpp:74
grad_stack_entry * ptr_first
Definition: fvar.hpp:924
grad_stack_entry * ptr
Definition: fvar.hpp:934
int read_grad_stack_buffer(OFF_T &lpos)
Description not yet available.
Definition: gst_read.cpp:76
size_t length
Definition: fvar.hpp:930
int _GRADFILE_PTR
Definition: fvar.hpp:942