r259: MSVC modifications
[ctsim.git] / include / timer.h
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #ifdef HAVE_SYS_TIME_H
6 #include <sys/time.h>
7 #endif
8
9 class Timer
10 {
11  public:
12     Timer (void)
13         { m_timeStart = ttime(); }
14
15     virtual ~Timer (void)
16         {}
17
18     virtual double timerEnd (void)
19       {
20         m_timeEnd = ttime();
21         m_timeElapsed = m_timeEnd - m_timeStart;
22   
23         return (m_timeElapsed);
24       }
25
26     virtual void timerReport (const char* const msg) const
27       {
28                 std::cout << msg << ": " << m_timeElapsed << " seconds" << std::endl;
29       }
30
31     virtual double timerEndAndReport (const char* const msg)
32       {
33         double t = timerEnd ();
34         timerReport (msg);
35         return (t);
36       }
37
38     double getTimeElapsed (void) const
39         { return m_timeElapsed; }
40
41  protected:
42     double m_timeStart;
43     double m_timeEnd;
44     double m_timeElapsed;
45
46     double ttime(void) const
47         {
48 #ifdef HAVE_GETTIMEOFDAY
49             struct timeval now;
50             if (gettimeofday (&now, NULL))
51                 return 0;
52             
53             return (now.tv_sec + static_cast<double>(now.tv_usec) / 1000000.);
54 #else
55             return 0;
56 #endif
57         }
58 };
59
60
61 #ifdef HAVE_MPI
62
63 #include "mpi++.h"
64
65 class TimerMPI : public Timer
66 {
67  public:
68     TimerMPI (MPI::Intracomm& comm)
69         : m_comm(comm)
70       {
71           m_timeStart = MPI::Wtime();
72       }
73
74     virtual ~TimerMPI (void)
75       {}
76
77     virtual double timerEnd (void)
78       {
79         m_timeEnd = MPI::Wtime();
80         m_timeElapsed = m_timeEnd - m_timeStart;
81   
82         return (m_timeElapsed);
83       }
84
85     virtual void timerReport (const char* const msg)
86       {
87           if (m_comm.Get_rank() == 0)
88                   std::cout << msg << ": " << m_timeElapsed << " seconds" << std::endl;
89       }
90
91     virtual double timerEndAndReport (const char* const msg)
92       {
93         double t = timerEnd ();
94         timerReport (msg);
95         return (t);
96       }
97
98     virtual void timerReportAllProcesses (const char* const msg)
99       {
100           timerReport (msg);
101       }
102
103  protected:
104     MPI::Intracomm& m_comm;
105 };
106
107 class TimerCollectiveMPI : public TimerMPI
108 {
109  public:
110     TimerCollectiveMPI (MPI::Intracomm& comm)
111         : TimerMPI::TimerMPI (comm)
112       {
113         m_comm.Barrier();
114         m_timeStart = MPI::Wtime();
115       }
116
117     virtual ~TimerCollectiveMPI (void)
118       {}
119
120     virtual double timerEnd (void)
121       {
122         m_timeEnd = MPI::Wtime();
123         m_timeElapsed = m_timeEnd - m_timeStart;
124         m_comm.Reduce (&m_timeElapsed, &m_timeMin, 1, MPI::DOUBLE, MPI::MIN, 0);
125         m_comm.Reduce (&m_timeElapsed, &m_timeMax, 1, MPI::DOUBLE, MPI::MAX, 0);
126   
127         return (m_timeElapsed);
128       }
129
130     virtual double timerEndAndReport (const char* const msg)
131       {
132         double t = timerEnd ();
133         timerReport (msg);
134         return (t);
135       }
136
137     virtual void timerReport (const char* const msg)
138       {
139         if (m_comm.Get_rank() == 0)
140                 std::cout << msg << " " << "Minimum=" << m_timeMin << ", Maximum=" << m_timeMax << " seconds" << std::endl;
141       }
142
143     virtual void timerReportAllProcesses (const char* const msg)
144       {
145                 std::cout << msg << ": " << "Minimum=" << m_timeMin << ", Maximum=" << m_timeMax << " seconds (Rank " << m_comm.Get_rank() << ")" << std::endl;
146       }
147
148  private:
149     double m_timeMin;
150     double m_timeMax;
151 };
152 #endif
153
154
155
156