DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
utils.h
Go to the documentation of this file.
1 
12 #ifndef UTILS_H
13 #define UTILS_H
14 
15 #include <iostream>
16 #include <cstdlib>
17 #include <cmath>
18 #include <assert.h>
19 
20 #ifdef HAVE_MKL // obsolete
21 //#include <mkl_cblas.h>
22 //#else
23 //#include "cblas.h"
24 #endif
25 #ifdef USE_BLAS_LIB
26 //#include "blas.h"
27 #else
28 #include "cblas.h" // dependency upon cblas libraries has been removed in a recent version
29 #endif
30 
31 #include <limits>
32 
33 #ifdef _OPENMP
34 #include <omp.h>
35 #endif
36 
37 #ifndef MATLAB_MEX_FILE
38 typedef int mwSize;
39 #endif
40 
41 #ifndef MAX_THREADS
42 #define MAX_THREADS 64
43 #endif
44 
45 
46 // MIN, MAX macros
47 #define MIN(a,b) (((a) > (b)) ? (b) : (a))
48 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
49 #define SIGN(a) (((a) < 0) ? -1.0 : 1.0)
50 #ifndef ABS
51 #define ABS(a) (((a) < 0) ? -(a) : (a))
52 #endif
53 // DEBUG macros
54 #define PRINT_I(name) printf(#name " : %d\n",name);
55 #define PRINT_F(name) printf(#name " : %g\n",name);
56 #define PRINT_S(name) printf("%s\n",name);
57 #define FLAG(a) printf("flag : %d \n",a);
58 
59 // ALGORITHM constants
60 #define EPSILON 10e-10
61 #ifndef INFINITY
62 #define INFINITY 10e20
63 #endif
64 #define EPSILON_OMEGA 0.001
65 #define TOL_CGRAD 10e-6
66 #define MAX_ITER_CGRAD 40
67 
68 
69 #ifdef _MSC_VER
70 
71 #include <time.h>
72 #include <windows.h>
73 #define random rand
74 #define srandom srand
75 
76 #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
77 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
78 #else
79 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
80 #endif
81 
82 
83 struct timezone
84 {
85  int tz_minuteswest; /* minutes W of Greenwich */
86  int tz_dsttime; /* type of dst correction */
87 };
88 
89 int gettimeofday(struct timeval *tv, struct timezone *tz)
90 {
91  FILETIME ft;
92  unsigned __int64 tmpres = 0;
93  static int tzflag = 0;
94 
95  if (NULL != tv)
96  {
97  GetSystemTimeAsFileTime(&ft);
98 
99  tmpres |= ft.dwHighDateTime;
100  tmpres <<= 32;
101  tmpres |= ft.dwLowDateTime;
102 
103  tmpres /= 10; /*convert into microseconds*/
104  /*converting file time to unix epoch*/
105  tmpres -= DELTA_EPOCH_IN_MICROSECS;
106  tv->tv_sec = (long)(tmpres / 1000000UL);
107  tv->tv_usec = (long)(tmpres % 1000000UL);
108  }
109 
110  if (NULL != tz)
111  {
112  if (!tzflag)
113  {
114  _tzset();
115  tzflag++;
116  }
117  tz->tz_minuteswest = _timezone / 60;
118  tz->tz_dsttime = _daylight;
119  }
120 
121  return 0;
122 }
123 
124 #else
125 #include <sys/time.h>
126 #endif
127 
128 
129 #include "linalg.h"
130 
131 
132 namespace spams
133 {
134 
135 using namespace std;
136 
138 class Timer {
139  public:
141  inline Timer();
143  inline ~Timer();
144 
146  void inline start() { _running=true;
147  gettimeofday(_time1,NULL); };
149  void inline stop() {
150  gettimeofday(_time2,NULL);
151  _running=false;
152  _cumul+= static_cast<double>((_time2->tv_sec - (_time1->tv_sec))*1000000 + _time2->tv_usec-_time1->tv_usec)/1000000.0;
153  };
155  void inline reset() { _cumul=0;
156  gettimeofday(_time1,NULL); };
158  void inline printElapsed();
160  double inline getElapsed() const;
161 
162  private:
163  struct timeval* _time1;
164  struct timeval* _time2;
165  bool _running;
166  double _cumul;
167 };
168 
170 Timer::Timer() :_running(false) ,_cumul(0) {
171  _time1 = (struct timeval*)malloc(sizeof(struct timeval));
172  _time2 = (struct timeval*)malloc(sizeof(struct timeval));
173 };
174 
177  free(_time1);
178  free(_time2);
179 }
180 
182 inline void Timer::printElapsed() {
183  if (_running) {
184  gettimeofday(_time2,NULL);
185  cerr << "Time elapsed : " << _cumul + static_cast<double>((_time2->tv_sec -
186  _time1->tv_sec)*1000000 + _time2->tv_usec-_time1->tv_usec)/1000000.0 << endl;
187  } else {
188  cerr << "Time elapsed : " << _cumul << endl;
189  }
190 };
191 
193 double inline Timer::getElapsed() const {
194  if (_running) {
195  gettimeofday(_time2,NULL);
196  return _cumul +
197  static_cast<double>((_time2->tv_sec -
198  _time1->tv_sec)*1000000 + _time2->tv_usec-
199  _time1->tv_usec)/1000000.0;
200  } else {
201  return _cumul;
202  }
203 }
204 
205 }
206 
207 #endif
Definition: dag.h:26
struct timeval * _time2
Definition: utils.h:164
void stop()
stop the time
Definition: utils.h:149
STL namespace.
int mwSize
Definition: utils.h:38
Class Timer.
Definition: utils.h:138
~Timer()
Destructor.
Definition: utils.h:176
double _cumul
Definition: utils.h:166
struct timeval * _time1
Definition: utils.h:163
void start()
start the time
Definition: utils.h:146
void printElapsed()
print the elapsed time
Definition: utils.h:182
double getElapsed() const
print the elapsed time
Definition: utils.h:193
void reset()
reset the timer
Definition: utils.h:155
bool _running
Definition: utils.h:165