Applied initial patches for wx2.8 compatibility
[ctsim.git] / include / timer.h
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **      Name:         timer.h
5 **      Purpose:      Header file for Timer class
6 **      Author:       Kevin Rosenberg
7 **      Date Started: Sep 2000
8 **
9 **  This is part of the CTSim program
10 **  Copyright (c) 1983-2001 Kevin Rosenberg
11 **
12 **  $Id$
13 **
14 **  This program is free software; you can redistribute it and/or modify
15 **  it under the terms of the GNU General Public License (version 2) as
16 **  published by the Free Software Foundation.
17 **
18 **  This program is distributed in the hope that it will be useful,
19 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 **  GNU General Public License for more details.
22 **
23 **  You should have received a copy of the GNU General Public License
24 **  along with this program; if not, write to the Free Software
25 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 ******************************************************************************/
27
28 #ifndef _TIMER_H
29 #define _TIMER_H
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #ifdef HAVE_SYS_TIME_H
36 #include <sys/time.h>
37 #endif
38
39 #ifdef MSVC
40 #include <sys/timeb.h>
41 #endif
42
43 class Timer
44 {
45  public:
46     Timer (void)
47         { m_timeStart = ttime(); }
48
49     virtual ~Timer (void)
50         {}
51
52     virtual double timerEnd (void)
53       {
54         m_timeEnd = ttime();
55         m_timeElapsed = m_timeEnd - m_timeStart;
56
57         return (m_timeElapsed);
58       }
59
60     virtual void timerReport (const char* const msg) const
61       {
62                 std::cout << msg << ": " << m_timeElapsed << " seconds" << std::endl;
63       }
64
65     virtual double timerEndAndReport (const char* const msg)
66       {
67         double t = timerEnd ();
68         timerReport (msg);
69         return (t);
70       }
71
72     double getTimeElapsed (void) const
73         { return m_timeElapsed; }
74
75  protected:
76     double m_timeStart;
77     double m_timeEnd;
78     double m_timeElapsed;
79
80     double ttime(void) const
81         {
82 #ifdef HAVE_GETTIMEOFDAY
83             struct timeval now;
84             if (gettimeofday (&now, NULL))
85                 return 0;
86
87             return (now.tv_sec + static_cast<double>(now.tv_usec) / 1000000.);
88 #elif defined(MSVC)
89                 struct _timeb now;
90                 _ftime (&now);
91                 return (now.time + static_cast<double>(now.millitm) / 1000.);
92 #else
93             return 0;
94 #endif
95         }
96 };
97
98
99 #ifdef HAVE_MPI
100
101 #include "mpi++.h"
102
103 class TimerMPI : public Timer
104 {
105  public:
106     TimerMPI (MPI::Intracomm& comm)
107         : m_comm(comm)
108       {
109           m_timeStart = MPI::Wtime();
110       }
111
112     virtual ~TimerMPI (void)
113       {}
114
115     virtual double timerEnd (void)
116       {
117         m_timeEnd = MPI::Wtime();
118         m_timeElapsed = m_timeEnd - m_timeStart;
119
120         return (m_timeElapsed);
121       }
122
123     virtual void timerReport (const char* const msg)
124       {
125           if (m_comm.Get_rank() == 0)
126                   std::cout << msg << ": " << m_timeElapsed << " seconds" << std::endl;
127       }
128
129     virtual double timerEndAndReport (const char* const msg)
130       {
131         double t = timerEnd ();
132         timerReport (msg);
133         return (t);
134       }
135
136     virtual void timerReportAllProcesses (const char* const msg)
137       {
138           timerReport (msg);
139       }
140
141  protected:
142     MPI::Intracomm& m_comm;
143 };
144
145 class TimerCollectiveMPI : public TimerMPI
146 {
147  public:
148     TimerCollectiveMPI (MPI::Intracomm& comm)
149         : TimerMPI::TimerMPI (comm)
150       {
151         m_comm.Barrier();
152         m_timeStart = MPI::Wtime();
153       }
154
155     virtual ~TimerCollectiveMPI (void)
156       {}
157
158     virtual double timerEnd (void)
159       {
160         m_timeEnd = MPI::Wtime();
161         m_timeElapsed = m_timeEnd - m_timeStart;
162         m_comm.Reduce (&m_timeElapsed, &m_timeMin, 1, MPI::DOUBLE, MPI::MIN, 0);
163         m_comm.Reduce (&m_timeElapsed, &m_timeMax, 1, MPI::DOUBLE, MPI::MAX, 0);
164
165         return (m_timeElapsed);
166       }
167
168     virtual double timerEndAndReport (const char* const msg)
169       {
170         double t = timerEnd ();
171         timerReport (msg);
172         return (t);
173       }
174
175     virtual void timerReport (const char* const msg)
176       {
177         if (m_comm.Get_rank() == 0)
178                 std::cout << msg << " " << "Minimum=" << m_timeMin << ", Maximum=" << m_timeMax << " seconds" << std::endl;
179       }
180
181     virtual void timerReportAllProcesses (const char* const msg)
182       {
183                 std::cout << msg << ": " << "Minimum=" << m_timeMin << ", Maximum=" << m_timeMax << " seconds (Rank " << m_comm.Get_rank() << ")" << std::endl;
184       }
185
186  private:
187     double m_timeMin;
188     double m_timeMax;
189 };
190 #endif
191
192
193 #endif  // _TIMER_H
194
195