r111: initial cvs import
[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         timerEnd ();
32         timerReport (msg);
33       }
34
35     double getTimeElapsed (void) const
36         { return m_timeElapsed; }
37
38  protected:
39     double m_timeStart;
40     double m_timeEnd;
41     double m_timeElapsed;
42
43     double ttime(void) const
44         {
45             struct timeval now;
46             if (gettimeofday (&now, NULL))
47                 return 0;
48             
49             return (now.tv_sec + static_cast<double>(now.tv_usec) / 1000000.);
50         }
51 };
52
53
54 #ifdef HAVE_MPI
55
56 #include "mpi++.h"
57
58 class TimerMPI : public Timer
59 {
60  public:
61     TimerMPI (MPI::Intracomm& comm)
62         : m_comm(comm)
63       {
64           m_timeStart = MPI::Wtime();
65       }
66
67     virtual ~TimerMPI (void)
68       {}
69
70     virtual double timerEnd (void)
71       {
72         m_timeEnd = MPI::Wtime();
73         m_timeElapsed = m_timeEnd - m_timeStart;
74   
75         return (m_timeElapsed);
76       }
77
78     virtual void timerReport (const char* const msg)
79       {
80           if (m_comm.Get_rank() == 0)
81               cout << msg << ": " << m_timeElapsed << " seconds" << endl;
82       }
83
84     virtual double timerEndAndReport (const char* const msg)
85       {
86         timerEnd ();
87         timerReport (msg);
88       }
89
90     virtual void timerReportAllProcesses (const char* const msg)
91       {
92           timerReport (msg);
93       }
94
95  protected:
96     MPI::Intracomm& m_comm;
97 };
98
99 class TimerCollectiveMPI : public TimerMPI
100 {
101  public:
102     TimerCollectiveMPI (MPI::Intracomm& comm)
103         : TimerMPI::TimerMPI (comm)
104       {
105         m_comm.Barrier();
106         m_timeStart = MPI::Wtime();
107       }
108
109     virtual ~TimerCollectiveMPI (void)
110       {}
111
112     virtual double timerEnd (void)
113       {
114         m_timeEnd = MPI::Wtime();
115         m_timeElapsed = m_timeEnd - m_timeStart;
116         m_comm.Reduce (&m_timeElapsed, &m_timeMin, 1, MPI::DOUBLE, MPI::MIN, 0);
117         m_comm.Reduce (&m_timeElapsed, &m_timeMax, 1, MPI::DOUBLE, MPI::MAX, 0);
118   
119         return (m_timeElapsed);
120       }
121
122     virtual double timerEndAndReport (const char* const msg)
123       {
124         timerEnd ();
125         timerReport (msg);
126       }
127
128     virtual void timerReport (const char* const msg)
129       {
130         if (m_comm.Get_rank() == 0)
131           cout << msg << " " << "Minimum=" << m_timeMin << ", Maximum=" << m_timeMax << " seconds" << endl;
132       }
133
134     virtual void timerReportAllProcesses (const char* const msg)
135       {
136           cout << msg << ": " << "Minimum=" << m_timeMin << ", Maximum=" << m_timeMax << " seconds (Rank " << m_comm.Get_rank() << ")" << endl;
137       }
138
139  private:
140     double m_timeMin;
141     double m_timeMax;
142 };
143 #endif
144
145
146
147