Mesh Oriented datABase  (version 5.5.1)
An array-based unstructured mesh library
CpuTimer.hpp
Go to the documentation of this file.
1 #ifndef CPUTIMER_HPP
2 #define CPUTIMER_HPP
3 
4 #include "moab/MOABConfig.h"
5 #ifdef MOAB_HAVE_MPI
6 #include "moab_mpi.h"
7 #endif
8 
9 #include <ctime>
10 
11 namespace moab
12 {
13 
14 class CpuTimer
15 {
16  private:
17 #ifdef MOAB_HAVE_MPI
18  int mpi_initialized;
19 #endif
20  double tAtBirth, tAtLast;
21  double runtime();
22 
23  public:
25 #ifdef MOAB_HAVE_MPI
26  : mpi_initialized( 0 )
27 #endif
28  {
29 #ifdef MOAB_HAVE_MPI
30  int flag = 0;
31  if( MPI_SUCCESS == MPI_Initialized( &flag ) && flag )
32  {
33  mpi_initialized = 1;
34  }
35 #endif
36  tAtBirth = runtime();
37  tAtLast = tAtBirth;
38  }
40  {
41  return ( tAtLast = runtime() ) - tAtBirth;
42  };
43  double time_elapsed()
44  {
45  double tmp = tAtLast;
46  return ( tAtLast = runtime() ) - tmp;
47  }
48 };
49 
50 inline double CpuTimer::runtime()
51 {
52 #ifdef MOAB_HAVE_MPI
53  if( mpi_initialized )
54  return MPI_Wtime();
55  else
56  return (double)clock() / CLOCKS_PER_SEC;
57 #else
58  return (double)clock() / CLOCKS_PER_SEC;
59 #endif
60 }
61 } // namespace moab
62 
63 #endif