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