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