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 ******************************************************************************/
35 #ifdef HAVE_SYS_TIME_H
40 #include <sys/timeb.h>
47 { m_timeStart = ttime(); }
52 virtual double timerEnd (void)
55 m_timeElapsed = m_timeEnd - m_timeStart;
57 return (m_timeElapsed);
60 virtual void timerReport (const char* const msg) const
62 std::cout << msg << ": " << m_timeElapsed << " seconds" << std::endl;
65 virtual double timerEndAndReport (const char* const msg)
67 double t = timerEnd ();
72 double getTimeElapsed (void) const
73 { return m_timeElapsed; }
80 double ttime(void) const
82 #ifdef HAVE_GETTIMEOFDAY
84 if (gettimeofday (&now, NULL))
87 return (now.tv_sec + static_cast<double>(now.tv_usec) / 1000000.);
91 return (now.time + static_cast<double>(now.millitm) / 1000.);
103 class TimerMPI : public Timer
106 TimerMPI (MPI::Intracomm& comm)
109 m_timeStart = MPI::Wtime();
112 virtual ~TimerMPI (void)
115 virtual double timerEnd (void)
117 m_timeEnd = MPI::Wtime();
118 m_timeElapsed = m_timeEnd - m_timeStart;
120 return (m_timeElapsed);
123 virtual void timerReport (const char* const msg)
125 if (m_comm.Get_rank() == 0)
126 std::cout << msg << ": " << m_timeElapsed << " seconds" << std::endl;
129 virtual double timerEndAndReport (const char* const msg)
131 double t = timerEnd ();
136 virtual void timerReportAllProcesses (const char* const msg)
142 MPI::Intracomm& m_comm;
145 class TimerCollectiveMPI : public TimerMPI
148 TimerCollectiveMPI (MPI::Intracomm& comm)
149 : TimerMPI::TimerMPI (comm)
152 m_timeStart = MPI::Wtime();
155 virtual ~TimerCollectiveMPI (void)
158 virtual double timerEnd (void)
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);
165 return (m_timeElapsed);
168 virtual double timerEndAndReport (const char* const msg)
170 double t = timerEnd ();
175 virtual void timerReport (const char* const msg)
177 if (m_comm.Get_rank() == 0)
178 std::cout << msg << " " << "Minimum=" << m_timeMin << ", Maximum=" << m_timeMax << " seconds" << std::endl;
181 virtual void timerReportAllProcesses (const char* const msg)
183 std::cout << msg << ": " << "Minimum=" << m_timeMin << ", Maximum=" << m_timeMax << " seconds (Rank " << m_comm.Get_rank() << ")" << std::endl;