00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef BL_UTILITY_H
00024 #define BL_UTILITY_H
00025
00026
00027
00028 #include <winstd.H>
00029
00030 #include <iostream>
00031 #include <string>
00032
00033 #ifndef WIN32
00034 #include <sys/types.h>
00035 #include <sys/wait.h>
00036 #endif
00037
00038 #include <BLassert.H>
00039 #include <REAL.H>
00040
00041
00042
00044
00052 namespace BoxLib
00053 {
00058 double second (double* t = 0);
00059
00064 double wsecond (double* t = 0);
00065
00067
00068 void ResetWallClockTime ();
00069
00071
00072 bool is_integer (const char* str);
00073
00075
00076 std::string Concatenate (const std::string& root,
00077 int num);
00078
00097 bool UtilCreateDirectory (const std::string& path,
00098 mode_t mode);
00099
00101
00102 void CreateDirectoryFailed (const std::string& dir);
00103
00105
00106 void FileOpenFailed (const std::string& dir);
00107
00109
00110 void UnlinkFile (const std::string& file);
00111
00116 void OutOfMemory ();
00117
00135 double Random ();
00136
00139 void InitRandom (unsigned long seed);
00140
00145 pid_t Execute (const char* cmd);
00146
00147
00148
00149 class Time
00150 {
00151 public:
00152 Time();
00153 Time(long s, long n = 0);
00154 Time(double d);
00155 double as_double() const;
00156 long as_long() const;
00157 Time& operator+=(const Time&);
00158 Time operator+(const Time&) const;
00159 static Time get_time();
00160 private:
00161 long tv_sec;
00162 long tv_nsec;
00163 void normalize();
00164 };
00165
00166
00167
00168 template <double (*FCN)(double*)> class base_Timer;
00169 template <double (*FCN)(double*)> std::ostream& operator<<(std::ostream&, const base_Timer<FCN>&);
00170
00171 typedef base_Timer<BoxLib::wsecond> WallTimer;
00172 typedef base_Timer<BoxLib::second> CPUTimer;
00173
00174 template <double (*FCN)(double*)>
00175 class base_Timer
00176 {
00177 public:
00178 class bad_timer;
00179 base_Timer();
00180 ~base_Timer();
00181 void start();
00182 void stop();
00183 void reset();
00184 double time() const;
00185 double accum_time() const;
00186 int count() const;
00187 bool is_running() const;
00188 static double tick();
00189 private:
00190 bool running;
00191 double val;
00192 double held;
00193 double accum_val;
00194 int cnt;
00195 };
00196
00197
00198
00199 class mt19937
00200 {
00201 public:
00202 typedef unsigned long seed_type;
00203 explicit mt19937 (seed_type seed = 4357UL);
00204 mt19937 (seed_type array[], int array_len);
00205 void rewind();
00206
00207 double d1_value ();
00208 double d_value ();
00209 long l_value ();
00210 unsigned long u_value ();
00211 private:
00212 void sgenrand (unsigned long seed);
00213 void sgenrand (seed_type seed_array[], int len);
00214 unsigned long igenrand ();
00215 void reload ();
00216 private:
00217 enum { N = 624 };
00218 unsigned long init_seed;
00219 unsigned long mt[N];
00220 int mti;
00221 };
00222
00223 class expect;
00224 std::istream& operator>>(std::istream&, const expect& exp);
00225
00226 class expect
00227 {
00228 friend std::istream& operator>>(std::istream&, const expect& exp);
00229 public:
00230 explicit expect(const std::string& str_);
00231 explicit expect(const char* istr_);
00232 explicit expect(char c);
00233 const std::string& the_string() const;
00234 private:
00235 std::string istr;
00236 };
00237 }
00238
00239
00240
00241
00242
00243
00244
00245 #ifdef BL_ARCH_CRAY
00246 #undef BL_ARCH_CRAY
00247
00252 #define BL_ARCH_CRAY 1
00253 #endif
00254
00255 #ifdef BL_LANG_FORT
00256 #undef BL_LANG_FORT
00257
00260 #define BL_LANG_FORT 1
00261 #endif
00262
00263 #ifdef BL_LANG_CC
00264 #undef BL_LANG_CC
00265
00268 #define BL_LANG_CC 1
00269 #endif
00270
00271 #ifdef BL_FORT_USE_UNDERSCORE
00272 #undef BL_FORT_USE_UNDERSCORE
00273
00282 #define BL_FORT_USE_UNDERSCORE 1
00283 #endif
00284
00285 #ifdef BL_FORT_USE_UPPERCASE
00286 #undef BL_FORT_USE_UPPERCASE
00287
00296 #define BL_FORT_USE_UPPERCASE 1
00297 #endif
00298
00299 #ifdef BL_FORT_USE_LOWERCASE
00300 #undef BL_FORT_USE_LOWERCASE
00301
00309 #define BL_FORT_USE_LOWERCASE 1
00310 #endif
00311
00321 #define BL_IGNORE_MAX 100000
00322
00323
00324
00325 template <double (*FCN)(double*)>
00326 BoxLib::base_Timer<FCN>::base_Timer()
00327 : running(false), val(0.0), accum_val(0.0), cnt(0)
00328 {
00329 }
00330
00331 template <double (*FCN)(double*)>
00332 BoxLib::base_Timer<FCN>::~base_Timer()
00333 {
00334 BL_ASSERT( !running );
00335 }
00336
00337 template <double (*FCN)(double*)>
00338 bool
00339 BoxLib::base_Timer<FCN>::is_running() const
00340 {
00341 return running;
00342 }
00343
00344 template <double (*FCN)(double*)>
00345 inline
00346 void
00347 BoxLib::base_Timer<FCN>::start()
00348 {
00349 BL_ASSERT( !running );
00350 held = FCN(0);
00351 running = true;
00352 }
00353
00354 template <double (*FCN)(double*)>
00355 inline
00356 void
00357 BoxLib::base_Timer<FCN>::stop()
00358 {
00359 BL_ASSERT( running );
00360 val = (FCN(0) - held);
00361 #ifndef NDEBUG
00362 if ( val < 0 )
00363 {
00364 std::cout << "Got a negative time " << val << "!" << std::endl;
00365 }
00366 #endif
00367 accum_val += val;
00368 cnt += 1;
00369 running = false;
00370 }
00371
00372 template <double (*FCN)(double*)>
00373 void
00374 BoxLib::base_Timer<FCN>::reset()
00375 {
00376 BL_ASSERT( !running );
00377 accum_val = 0;
00378 cnt = 0;
00379 }
00380
00381 template <double (*FCN)(double*)>
00382 double
00383 BoxLib::base_Timer<FCN>::accum_time() const
00384 {
00385 BL_ASSERT( !running );
00386 return accum_val;
00387 }
00388
00389 template <double (*FCN)(double*)>
00390 double
00391 BoxLib::base_Timer<FCN>::time() const
00392 {
00393 BL_ASSERT( !running );
00394 return val;
00395 }
00396
00397 template <double (*FCN)(double*)>
00398 int
00399 BoxLib::base_Timer<FCN>::count() const
00400 {
00401 BL_ASSERT( !running );
00402 return cnt;
00403 }
00404
00405
00406 template <double (*FCN)(double*)>
00407 double
00408 BoxLib::base_Timer<FCN>::tick()
00409 {
00410 const int M = 100;
00411 double timesfound[M];
00412
00413
00414
00415 for ( int i = 0; i < M; ++i )
00416 {
00417 double t2;
00418 double t1 = FCN(0);
00419 while( ((t2 = FCN(0)) - t1) == 0 )
00420 {
00421 }
00422 timesfound[i] = t2;
00423 }
00424
00425 double minDelta = timesfound[1] - timesfound[0];
00426 for ( int i = 2; i < M; i++ )
00427 {
00428 minDelta = std::min(minDelta, std::max(timesfound[i]-timesfound[i-1], 0.0));
00429 }
00430 return minDelta;
00431 }
00432
00433 template <double (*FCN)(double*)>
00434 std::ostream& operator<<(std::ostream& os, const BoxLib::base_Timer<FCN>& bt)
00435 {
00436 return os << "["
00437 << bt.accum_time() << "/"
00438 << bt.count()
00439 << "]";
00440 }
00441
00442 #endif