ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df_file.cpp
Go to the documentation of this file.
1 /*
2 Author: David Fournier
3 Copyright (c) 2008-2016 ADMB Foundation and
4  Regents of the University of California
5 */
6 #include "fvar.hpp"
7 #include <fcntl.h>
8 #include <limits>
9 
10 #ifdef _MSC_VER
11  #include <sys\stat.h>
12 #else
13  #include <iostream>
14  #include <memory.h>
15  #include <sys/stat.h>
16  #include <sys/types.h>
17  #include <unistd.h>
18 #endif
19 
20 #if defined(__TURBOC__)
21  #pragma hdrstop
22  #include <iostream.h>
23  #include <iomanip.h>
24  #include <sys\stat.h>
25 #endif
26 
27 #ifdef __ZTC__
28  #include <iostream.hpp>
29  #define S_IREAD 0000400
30  #define S_IWRITE 0000200
31 #endif
32 
33 #ifdef __NDPX__
34  #define O_RDONLY 0
35  #define O_WRONLY 1
36  #define O_RDWR 2
37  extern "C"
38  {
39  int LSEEK(int, int, int);
40  int open(const char*, int);
41  int creat(const char*, int);
42  int close(int);
43  int write(int, char*, int);
44  int read(int, char*, int);
45  };
46 #endif
47 
48 #ifdef __SUN__
49 #include <memory.h>
50 #include <iostream.h>
51 #include <sys/stat.h>
52 #include <sys/types.h>
53 #ifndef _MSC_VER
54 #include <unistd.h>
55 #endif
56 #endif
57 
58 #include <limits.h>
59 #include <stdlib.h>
60 #include <stdio.h>
61 #include <string.h>
62 
63 char lastchar(char*);
64 
65 #if defined(__ADSGI__)
66 
74 void byte_copy(void* dest, void* source, const size_t num_bytes)
75 {
76  char* pdest = (char*)dest;
77  char* psource = (char*)source;
78  int ii=0;
79  while (ii < num_bytes)
80  {
81  //*((char *)dest)++ = *((char *)source)++;
82  *pdest = *psource;
83  pdest++;
84  psource++;
85  ii++;
86  }
87  //memcpy((char*)dest, (char*)source, num_bytes);
88 }
89 #endif
90 
97 DF_FILE::DF_FILE(const size_t nbytes, const unsigned int id)
98 {
99 #if defined(_MSC_VER) || defined(__MINGW64__)
101 #elif defined(__OPENCC__)
102  size_t max = static_cast<size_t>(std::numeric_limits<OFF_T>::max())
103  - sizeof(OFF_T);
104 #elif defined(__x86_64)
105  auto max = std::numeric_limits<OFF_T>::max() - sizeof(OFF_T);
106 #else
107  auto max = std::numeric_limits<size_t>::max() - sizeof(OFF_T);
108 #endif
109  if (nbytes > max)
110  {
111  cout << "Error -- largest size for CMPDIF_BUFFER_SIZE is " << max << endl;
112  ad_exit(1);
113  }
114  buff_end = static_cast<OFF_T>(nbytes);
115 
116 #if defined(_MSC_VER) || defined(__MINGW64__)
117  buff_size = static_cast<unsigned int>(static_cast<size_t>(nbytes) + sizeof(OFF_T));
118 #else
119  buff_size = static_cast<size_t>(nbytes) + sizeof(OFF_T);
120 #endif
121 
122 
123  try
124  {
125  buff = new char[buff_size];
126  }
127  catch (const std::bad_alloc& e)
128  {
129  size_t gb = nbytes / 1073741824;
130  cerr << "Error: Unable to construct DF_FILE memory buffer\n"
131  << "of size " << nbytes << " bytes or " << gb << "GB.\n";
132  ad_exit(1);
133  }
134 
135 #ifndef OPT_LIB
136  //memset(buff, 0, buff_size);
137 #endif
138 
139  offset = 0;
140  toffset = 0;
141 
142  char* path = getenv("ADTMP1"); // NULL if not defined
143 
144 #if defined(USE_ADPVM)
145  adstring string_path;
146  if (path) string_path=path;
147 
148  adstring currdir;
149  ad_getcd(currdir);
151  {
152  int on = 0;
153  int nopt = 0;
154  if ((on = option_match(ad_comm::argc,ad_comm::argv,"-slave",nopt)) > -1)
155  {
156  if (nopt == 1)
157  {
158  int ierr=make_sub_directory(ad_comm::argv[on+1]);
160  string_path+=ad_comm::subdir;
161  path=(char*) string_path;
162  }
163  else
164  {
165  cerr << "Wrong number of options to -slave -- must be 1"
166  " you have " << nopt << endl;
167  ad_exit(1);
168  }
169  }
170  }
171 #endif
172 
173  if (path != NULL && strlen(path) <= 45)
174 #if !defined (_WIN32)
175  {
176  if (id > 0)
177  sprintf(&cmpdif_file_name[0],"%s/cmpdiff%u.tmp", path, id);
178  else
179  sprintf(&cmpdif_file_name[0],"%s/cmpdiff.tmp", path);
180 
181  }
182 #else
183  {
184  if (lastchar(path) != '\\')
185  {
186  if (id > 0)
187  sprintf(&cmpdif_file_name[0],"%s\\cmpdiff%u.tmp", path, id);
188  else
189  sprintf(&cmpdif_file_name[0],"%s\\cmpdiff.tmp", path);
190 
191  }
192  else
193  {
194  if (id > 0)
195  sprintf(&cmpdif_file_name[0],"%scmpdiff%u.tmp", path, id);
196  else
197  sprintf(&cmpdif_file_name[0],"%scmpdiff.tmp", path);
198  }
199  }
200 #endif
201  else
202  {
203  if (id > 0)
204  sprintf(&cmpdif_file_name[0],"cmpdiff%u.tmp",id);
205  else
206  sprintf(&cmpdif_file_name[0],"cmpdiff.tmp");
207  }
208 #if defined (_MSC_VER) || defined (__WAT32__)
209  file_ptr=open(cmpdif_file_name, O_RDWR | O_CREAT | O_TRUNC |
210  O_BINARY, S_IREAD | S_IWRITE);
211 #elif defined (__TURBOC__)
212  file_ptr=open(cmpdif_file_name, O_RDWR | O_CREAT | O_TRUNC |
213  O_BINARY, S_IRUSR | S_IWUSR);
214 #elif defined (__ZTC__)
215  file_ptr=open(cmpdif_file_name, O_RDWR | O_CREAT | O_TRUNC ,
216  S_IRUSR | S_IWUSR);
217 #elif defined (__NDPX__)
218  file_ptr=creat(cmpdif_file_name, O_RDWR);
219 #else
220  file_ptr=open(cmpdif_file_name, O_RDWR | O_CREAT | O_TRUNC |
221  O_BINARY, 0777);
222 #endif
223  if (file_ptr == -1)
224  {
225  ad_printf("Error opening temporary gradient"
226  " file %s\n", cmpdif_file_name );
227  ad_exit(1);
228  }
229 }
234 {
235  delete [] buff;
236  buff = nullptr;
237 
238  int repfs = option_match(ad_comm::argc,ad_comm::argv,"-fsize");
239 
240  if (ad_comm::global_logfile && repfs)
241  {
242  OFF_T pos = LSEEK(file_ptr, 0, SEEK_END);
243  *ad_comm::global_logfile << "size of file " << cmpdif_file_name
244  << " = " << pos << endl;
245  }
246  if (close(file_ptr))
247  {
248  cerr << "Error closing file " << cmpdif_file_name << "\n";
249  }
250 #if !defined (_WIN32)
251  unlink(cmpdif_file_name);
252 #else
253  adstring currentdir;
254  ad_getcd(currentdir);
255  int xxx=remove(cmpdif_file_name);
256  if (xxx)
257  {
258  cerr << "Error trying to delete file " << cmpdif_file_name << endl;
259  xxx=unlink(cmpdif_file_name);
260  cout << xxx << endl;
261  }
262 #endif
263 }
270 void DF_FILE::fread(void* s, const size_t num_bytes)
271 {
272  if (toffset < static_cast<OFF_T>(num_bytes))
273  {
274  OFF_T lpos = LSEEK(file_ptr, -static_cast<OFF_T>(buff_size), SEEK_CUR);
276  offset -= num_bytes;
277  toffset = offset;
278  }
279  else
280  {
281  toffset-=num_bytes; //decrement the temporary offset count
282  }
283 #if defined(__ADSGI__)
284  byte_copy((char*)s, buff + toffset, num_bytes);
285 #else
286  memcpy(s, buff + toffset, num_bytes);
287 #endif
288  offset=toffset;
289 }
296 void DF_FILE::fwrite(const void* s, const size_t num_bytes)
297 {
298 #ifdef NO_DERIVS
300  {
301  return;
302  }
303 #endif
304  toffset += num_bytes; //increment the temporary offset count
305  if (toffset > buff_end)
306  {
307  if (static_cast<OFF_T>(num_bytes) > buff_end)
308  {
309  OFF_T us = toffset + static_cast<OFF_T>(sizeof(size_t) + 2L);
310  cerr << "Need to increase gradient_structure::CMPDIF_BUFFER_SIZE "
311  "to at least" << us << endl;
312  }
314  toffset = static_cast<OFF_T>(num_bytes);
315  offset=0;
316  }
317 #if defined(__ADSGI__)
318  byte_copy(buff+offset,(char *) s,num_bytes);
319 #else
320  memcpy(buff + offset, s, num_bytes);
321 #endif
322  offset = toffset;
323 }
329 {
330  if (lpos == -1L)
331  {
332  cerr << "Error rewinding file in DF_FILE:fread"<<endl;
333  ad_exit(1);
334  }
335  if (READ(file_ptr, buff, buff_size) < 0)
336  {
337  cerr << "End of file trying to read "<< cmpdif_file_name << endl;
338  ad_exit(1);
339  }
340  lpos = LSEEK(file_ptr, -static_cast<OFF_T>(buff_size), SEEK_CUR);
341  for (size_t i = 0;i < sizeof(OFF_T); i++)
342  {
343  fourb[i] = *(buff+buff_end+i);
344  }
345 }
351 {
352  // save the offset at the end of the used part of the buffer
353  for (size_t i = 0; i < sizeof(OFF_T); i++)
354  {
355  *(buff+buff_end+i) = fourb[i];
356  }
357  if (WRITE(file_ptr, buff, buff_size) < 0)
358  {
359  cerr << "End of file trying to write to file "<< cmpdif_file_name << endl;
360  cerr << "There is probably no more room on the TMP1 (if defined) device\n"
361  "If possible set TMP1 environment string to a device with more room\n";
362  ad_exit(1);
363  }
364 }
void read(const test_smartlist &, void *, int nsize)
static adpvm_manager * pvm_manager
Definition: fvar.hpp:8849
void memcpy(test_smartlist &dest, void *source, const size_t nsize)
memcpy for test_smartlist
Definition: df1b2f10.cpp:367
static ofstream * global_logfile
Definition: fvar.hpp:8858
DF_FILE()
Default uses gradient_structure::CMPDIF_BUFFER_SIZE.
static adstring subdir
Definition: fvar.hpp:8851
static char ** argv
Definition: fvar.hpp:8866
char cmpdif_file_name[81]
exitptr ad_exit
Definition: gradstrc.cpp:53
size_t buff_size
char lastchar(char *)
Description not yet available.
Definition: gradstrc.cpp:173
#define LSEEK
Definition: fvar.hpp:75
void write_cmpdif_stack_buffer()
Description not yet available.
Definition: df_file.cpp:350
void read_cmpdif_stack_buffer(OFF_T &lpos)
Description not yet available.
Definition: df_file.cpp:328
#define WRITE
Definition: fvar.hpp:77
prnstream & endl(prnstream &)
#define O_BINARY
Definition: fvar.hpp:163
static int argc
Definition: fvar.hpp:8863
~DF_FILE()
Destructor.
Definition: df_file.cpp:233
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
char fourb[sizeof(OFF_T)]
#define OFF_T
Definition: fvar.hpp:74
void ad_getcd(const adstring &s)
Description not yet available.
Definition: makesub.cpp:56
static void xxx(ivector re_list, ivector fe_list)
Definition: df1b2lp2.cpp:25
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)
size_t pos(const adstring &substr, const adstring &s)
Definition: string3.cpp:56
void fwrite(const void *s, const size_t num_bytes)
Reads num_bytes from s and writes to buffer.
Definition: df_file.cpp:296
void fread(void *s, const size_t num_bytes)
Reads num_bytes from buffer and copies to s.
Definition: df_file.cpp:270
int make_sub_directory(const char *s)
Create a sub directory s.
Definition: makesub.cpp:95
#define max(a, b)
Definition: cbivnorm.cpp:189
#define READ
Definition: fvar.hpp:76
int ad_printf(FILE *stream, const char *format, Args...args)
Definition: fvar.hpp:9487