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