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