WTFIT
Os.h
Go to the documentation of this file.
1 
8 #ifndef _OS_H
9 #define _OS_H
10 
11 #include <stdlib.h>
12 
13 #include <algorithm>
14 #include <sstream>
15 
16 #include <Debug.h>
17 
18 
19 #ifdef _WIN32
20  #include <cwchar>
21  #include <direct.h>
22  #include <float.h>
23  #include <iomanip>
24  #include <math.h>
25  #include <stdint.h>
26  #include <time.h>
27  #include <windows.h>
28 
29  #define drand48() (double(rand()) / RAND_MAX)
30  #define isnan(x) _isnan(x)
31  #define round(x) OsCall::roundToNearestInt(x)
32  #define srand48(seed) srand(seed)
33 #endif
34 
35 #ifdef __unix__
36 #include <cfloat>
37 #include <climits>
38 #include <dirent.h>
39 #include <iomanip>
40 #include <math.h>
41 #include <sys/stat.h>
42 #include <sys/time.h>
43 #include <sys/types.h>
44 #include <unistd.h>
45 #endif
46 
47 #ifdef __APPLE__
48 #include <climits>
49 #include <cfloat>
50 #include <dirent.h>
51 #include <math.h>
52 #include <sys/stat.h>
53 #include <sys/time.h>
54 #include <sys/types.h>
55 #include <unistd.h>
56 #endif
57 
58 //#define SINGLE_PRECISION
59 
60 #ifdef SINGLE_PRECISION
61 #define REAL_TYPE float
62 #define REAL_TYPE_STRING "float"
63 #define REAL_MAX FLT_MAX
64 #else
65 #define REAL_TYPE double
66 #define REAL_TYPE_STRING "double"
67 #define REAL_MAX DBL_MAX
68 #endif
69 
70 #define DBL_SIGNIFICANT_DIGITS 14
71 #define FLT_SIGNIFICANT_DIGITS 7
72 
73 #ifdef SINGLE_PRECISION
74 #define REAL_SIGNIFICANT_DIGITS FLT_SIGNIFICANT_DIGITS
75 #else
76 #define REAL_SIGNIFICANT_DIGITS DBL_SIGNIFICANT_DIGITS
77 #endif
78 
79 #define M_PI 3.14159265358979323846
80 
81 using namespace std;
82 
83 namespace wtfit{
84 
85 #ifndef withEigen
86  #ifdef SINGLE_PRECISION
87  typedef float real;
88  #else
89  typedef double real;
90  #endif
91 #endif
92 
93  class OsCall{
94 
95 
96  public:
97 
98  inline static int getCurrentDirectory(string &directoryPath){
99  #ifdef _WIN32
100  directoryPath = _getcwd(NULL, 0);
101  #else
102  directoryPath = getcwd(NULL, PATH_MAX);
103  #endif
104  directoryPath += "/";
105 
106  return 0;
107  }
108 
109  inline static float getMemoryInstantUsage(){
110  #ifdef __linux__
111  // horrible hack since getrusage() doesn't seem to work well under
112  // linux
113  stringstream procFileName;
114  procFileName << "/proc/" << getpid() << "/statm";
115 
116  ifstream procFile(procFileName.str().data(), ios::in);
117  if(procFile){
118  float memoryUsage;
119  procFile >> memoryUsage;
120  procFile.close();
121  return memoryUsage/1024.0;
122  }
123  #endif
124  return 0;
125  };
126 
127  inline static int getNumberOfCores(){
128  #ifdef withOpenMP
129  return omp_get_num_procs();
130  #endif
131  return 1;
132  }
133 
134  inline static double getTimeStamp(){
135  #ifdef _WIN32
136  LARGE_INTEGER frequency;
137  QueryPerformanceFrequency(&frequency);
138 
139  LARGE_INTEGER temp;
140  QueryPerformanceCounter(&temp);
141 
142  return (double) temp.QuadPart/frequency.QuadPart;
143  #endif
144 
145  #ifdef __APPLE__
146  struct timeval stamp;
147  gettimeofday(&stamp, NULL);
148  return (stamp.tv_sec*1000000 + stamp.tv_usec)/1000000.0;
149  #endif
150 
151  #ifdef __unix__
152  struct timeval stamp;
153  gettimeofday(&stamp, NULL);
154  return (stamp.tv_sec*1000000 + stamp.tv_usec)/1000000.0;
155  #endif
156  };
157 
158  inline static vector<string> listFilesInDirectory(
159  const string &directoryName, const string &extension){
160 
161  vector< string > filesInDir;
162  #ifdef _WIN32
163  WIN32_FIND_DATA FindFileData;
164  char* buffer;
165  buffer = _getcwd( NULL, 0 );
166  if((buffer = _getcwd( NULL, 0 )) == NULL)
167  perror( "_getcwd error" );
168  else{
169  free(buffer);
170  }
171 
172  HANDLE hFind = FindFirstFile(directoryName.data(), &FindFileData);
173  if(hFind == INVALID_HANDLE_VALUE){
174  stringstream msg;
175  msg << "[Os] Could not open directory `"
176  << directoryName << "'. Error: "<< GetLastError() << endl;
177  Debug d;
178  d.dMsg(cerr, msg.str(), 0);
179  }
180  else{
181  string entryExtension(FindFileData.cFileName);
182  entryExtension =
183  entryExtension.substr(entryExtension.find_last_of('.') + 1);
184 
185  if(entryExtension == extension)
186  filesInDir.push_back(string(FindFileData.cFileName));
187  string dir = directoryName;
188  dir.resize(dir.size()-1);
189  while(FindNextFile(hFind, &FindFileData)){
190  if(extension.size()){
191  string entryExtension(FindFileData.cFileName);
192  entryExtension = entryExtension.substr(
193  entryExtension.find_last_of('.') + 1);
194  if(entryExtension == extension)
195  filesInDir.push_back(dir
196  + string(FindFileData.cFileName));
197  }
198  else{
199  if((string(FindFileData.cFileName) != ".")
200  &&(string(FindFileData.cFileName) != "..")){
201  filesInDir.push_back(directoryName
202  + "/"
203  + string(FindFileData.cFileName));
204  }
205  }
206  }
207  }
208  FindClose(hFind);
209  #else
210  DIR *d = opendir((directoryName + "/").data());
211  if(!d){
212  stringstream msg;
213  msg
214  << "[Os] Could not open directory `"
215  << directoryName << "'..." << endl;
216  Debug d;
217  d.dMsg(cerr, msg.str(), 0);
218  }
219  else{
220  struct dirent *dirEntry;
221  while((dirEntry = readdir(d)) != NULL){
222  if(extension.size()){
223  string entryExtension(dirEntry->d_name);
224  entryExtension =
225  entryExtension.substr(entryExtension.find_last_of('.') + 1);
226  if(entryExtension == extension)
227  filesInDir.push_back(directoryName
228  + "/"
229  + string(dirEntry->d_name));
230  }
231  else{
232  if((string(dirEntry->d_name) != ".")
233  &&(string(dirEntry->d_name) != ".."))
234  filesInDir.push_back(directoryName
235  + "/"
236  + string(dirEntry->d_name));
237  }
238  }
239  }
240  closedir(d);
241  #endif
242 
243  sort(filesInDir.begin(), filesInDir.end());
244 
245  return filesInDir;
246  }
247 
248  inline static int mkDir(const string &directoryName){
249  #ifdef _WIN32
250  return _mkdir(directoryName.data());
251  #else
252  return mkdir(directoryName.data(), 0777);
253  #endif
254  }
255 
256  inline static int nearbyint(const double &x){
257  #ifdef _WIN32
258  const double upperBound = ceil( x );
259  const double lowerBound = floor( x );
260 
261  if( upperBound-x <= x-lowerBound )
262  return (int)upperBound;
263  else
264  return (int)lowerBound;
265  #else
266  return nearbyint(x);
267  #endif
268 
269  };
270 
271  inline static int rmDir(const string &directoryName){
272  #ifdef _WIN32
273  // NOTE:
274  // the directory will be deleted with this call
275  // only if it's empty...
276  return _rmdir(directoryName.data());
277  #else
278  stringstream cmd;
279  cmd << "rm -R " << directoryName << " 2> /dev/null";
280  return system(cmd.str().data());
281  #endif
282  }
283 
284  inline static int rmFile(const string &fileName){
285 
286  stringstream cmd;
287 
288  #ifdef _WIN32
289  cmd << "del";
290  #else
291  cmd << "rm";
292  #endif
293 
294  cmd << " " << fileName;
295 
296  #ifndef _WIN32
297  cmd << " 2> /dev/null";
298  #endif
299 
300  return system(cmd.str().data());
301  }
302 
303  int static roundToNearestInt(const double &val){
304 
305  const double upperBound = ceil(val);
306  const double lowerBound = floor(val);
307 
308  if(upperBound - val <= val - lowerBound){
309  return (int)upperBound;
310  }
311  else{
312  return (int)lowerBound;
313  }
314  }
315 
316 
317  protected:
318 
319 
320  private:
321 
322  };
323 
324  class Memory{
325 
326  public:
327 
328  Memory(){ initialMemory_ = OsCall::getMemoryInstantUsage();};
329 
330  inline float getInitialMemoryUsage(){
331  return initialMemory_;
332  };
333 
334  inline float getInstantUsage(){
335  return OsCall::getMemoryInstantUsage();
336  };
337 
338  inline float getElapsedUsage(){
339  return OsCall::getMemoryInstantUsage() - initialMemory_;
340  };
341 
342  protected:
343 
344  float initialMemory_;
345  };
346 
347  class Timer {
348 
349  public:
350 
351  Timer(){
352  start_ = getTimeStamp();
353  };
354 
355  Timer(const Timer &other){
356  start_ = other.start_;
357  }
358 
359  inline double getElapsedTime(){
360 
361  double end = getTimeStamp();
362  return end - start_;
363  };
364 
365  inline double getStartTime(){
366  return start_;
367  };
368 
369  inline void reStart(){
370  start_ = getTimeStamp();
371  };
372 
373 
374  protected:
375 
376  inline const double getTimeStamp(){
377  return OsCall::getTimeStamp();
378  };
379 
380  double start_;
381  };
382 }
383 
384 #endif
Definition: Os.h:324
static int roundToNearestInt(const double &val)
Definition: Os.h:303
static int getCurrentDirectory(string &directoryPath)
Definition: Os.h:98
double getStartTime()
Definition: Os.h:365
float getElapsedUsage()
Definition: Os.h:338
Memory()
Definition: Os.h:328
static int nearbyint(const double &x)
Definition: Os.h:256
static vector< string > listFilesInDirectory(const string &directoryName, const string &extension)
Definition: Os.h:158
static float getMemoryInstantUsage()
Definition: Os.h:109
static int rmFile(const string &fileName)
Definition: Os.h:284
virtual const int dMsg(ostream &stream, string msg, const int &debugLevel=infoMsg) const
Definition: Debug.cpp:52
static int mkDir(const string &directoryName)
Definition: Os.h:248
Timer(const Timer &other)
Definition: Os.h:355
double getElapsedTime()
Definition: Os.h:359
Os-specifics.
Definition: Os.h:93
Definition: Os.h:347
double start_
Definition: Os.h:378
static double getTimeStamp()
Definition: Os.h:134
static int getNumberOfCores()
Definition: Os.h:127
Timer()
Definition: Os.h:351
Minimalist debugging class.
Definition: Debug.h:39
void reStart()
Definition: Os.h:369
float getInstantUsage()
Definition: Os.h:334
Definition: CommandLineParser.h:13
const double getTimeStamp()
Definition: Os.h:376
static int rmDir(const string &directoryName)
Definition: Os.h:271
double real
Definition: Os.h:89
float getInitialMemoryUsage()
Definition: Os.h:330