1 /*****************************************************************************
5 ** Purpose: Header file for Timer class
6 ** Author: Kevin Rosenberg
7 ** Date Started: Sep 2000
9 ** This is part of the CTSim program
10 ** Copyright (c) 1983-2001 Kevin Rosenberg
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.
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.
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 ******************************************************************************/
31 // pragma line required for Fedora 4 and wxWin 2.4.2
32 #if defined(__GNUG__) && !defined(__APPLE__)
33 #pragma interface "timer.h"
40 #ifdef HAVE_SYS_TIME_H
45 #include <sys/timeb.h>
52 { m_timeStart = ttime(); }
57 virtual double timerEnd (void)
60 m_timeElapsed = m_timeEnd - m_timeStart;
62 return (m_timeElapsed);
65 virtual void timerReport (const char* const msg) const
67 std::cout << msg << ": " << m_timeElapsed << " seconds" << std::endl;
70 virtual double timerEndAndReport (const char* const msg)
72 double t = timerEnd ();
77 double getTimeElapsed (void) const
78 { return m_timeElapsed; }
85 double ttime(void) const
87 #ifdef HAVE_GETTIMEOFDAY
89 if (gettimeofday (&now, NULL))
92 return (now.tv_sec + static_cast<double>(now.tv_usec) / 1000000.);
96 return (now.time + static_cast<double>(now.millitm) / 1000.);
108 class TimerMPI : public Timer
111 TimerMPI (MPI::Intracomm& comm)
114 m_timeStart = MPI::Wtime();
117 virtual ~TimerMPI (void)
120 virtual double timerEnd (void)
122 m_timeEnd = MPI::Wtime();
123 m_timeElapsed = m_timeEnd - m_timeStart;
125 return (m_timeElapsed);
128 virtual void timerReport (const char* const msg)
130 if (m_comm.Get_rank() == 0)
131 std::cout << msg << ": " << m_timeElapsed << " seconds" << std::endl;
134 virtual double timerEndAndReport (const char* const msg)
136 double t = timerEnd ();
141 virtual void timerReportAllProcesses (const char* const msg)
147 MPI::Intracomm& m_comm;
150 class TimerCollectiveMPI : public TimerMPI
153 TimerCollectiveMPI (MPI::Intracomm& comm)
154 : TimerMPI::TimerMPI (comm)
157 m_timeStart = MPI::Wtime();
160 virtual ~TimerCollectiveMPI (void)
163 virtual double timerEnd (void)
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);
170 return (m_timeElapsed);
173 virtual double timerEndAndReport (const char* const msg)
175 double t = timerEnd ();
180 virtual void timerReport (const char* const msg)
182 if (m_comm.Get_rank() == 0)
183 std::cout << msg << " " << "Minimum=" << m_timeMin << ", Maximum=" << m_timeMax << " seconds" << std::endl;
186 virtual void timerReportAllProcesses (const char* const msg)
188 std::cout << msg << ": " << "Minimum=" << m_timeMin << ", Maximum=" << m_timeMax << " seconds (Rank " << m_comm.Get_rank() << ")" << std::endl;