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-2009 Kevin Rosenberg
12 ** This program is free software; you can redistribute it and/or modify
13 ** it under the terms of the GNU General Public License (version 2) as
14 ** published by the Free Software Foundation.
16 ** This program is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ** GNU General Public License for more details.
21 ** You should have received a copy of the GNU General Public License
22 ** along with this program; if not, write to the Free Software
23 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 ******************************************************************************/
33 #ifdef HAVE_SYS_TIME_H
38 #include <sys/timeb.h>
45 { m_timeStart = ttime(); }
50 virtual double timerEnd (void)
53 m_timeElapsed = m_timeEnd - m_timeStart;
55 return (m_timeElapsed);
58 virtual void timerReport (const char* const msg) const
60 std::cout << msg << ": " << m_timeElapsed << " seconds" << std::endl;
63 virtual double timerEndAndReport (const char* const msg)
65 double t = timerEnd ();
70 double getTimeElapsed (void) const
71 { return m_timeElapsed; }
78 double ttime(void) const
80 #ifdef HAVE_GETTIMEOFDAY
82 if (gettimeofday (&now, NULL))
85 return (now.tv_sec + static_cast<double>(now.tv_usec) / 1000000.);
89 return (now.time + static_cast<double>(now.millitm) / 1000.);
101 class TimerMPI : public Timer
104 TimerMPI (MPI::Intracomm& comm)
107 m_timeStart = MPI::Wtime();
110 virtual ~TimerMPI (void)
113 virtual double timerEnd (void)
115 m_timeEnd = MPI::Wtime();
116 m_timeElapsed = m_timeEnd - m_timeStart;
118 return (m_timeElapsed);
121 virtual void timerReport (const char* const msg)
123 if (m_comm.Get_rank() == 0)
124 std::cout << msg << ": " << m_timeElapsed << " seconds" << std::endl;
127 virtual double timerEndAndReport (const char* const msg)
129 double t = timerEnd ();
134 virtual void timerReportAllProcesses (const char* const msg)
140 MPI::Intracomm& m_comm;
143 class TimerCollectiveMPI : public TimerMPI
146 TimerCollectiveMPI (MPI::Intracomm& comm)
147 : TimerMPI::TimerMPI (comm)
150 m_timeStart = MPI::Wtime();
153 virtual ~TimerCollectiveMPI (void)
156 virtual double timerEnd (void)
158 m_timeEnd = MPI::Wtime();
159 m_timeElapsed = m_timeEnd - m_timeStart;
160 m_comm.Reduce (&m_timeElapsed, &m_timeMin, 1, MPI::DOUBLE, MPI::MIN, 0);
161 m_comm.Reduce (&m_timeElapsed, &m_timeMax, 1, MPI::DOUBLE, MPI::MAX, 0);
163 return (m_timeElapsed);
166 virtual double timerEndAndReport (const char* const msg)
168 double t = timerEnd ();
173 virtual void timerReport (const char* const msg)
175 if (m_comm.Get_rank() == 0)
176 std::cout << msg << " " << "Minimum=" << m_timeMin << ", Maximum=" << m_timeMax << " seconds" << std::endl;
179 virtual void timerReportAllProcesses (const char* const msg)
181 std::cout << msg << ": " << "Minimum=" << m_timeMin << ", Maximum=" << m_timeMax << " seconds (Rank " << m_comm.Get_rank() << ")" << std::endl;