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
12 ** $Id: timer.h,v 1.6 2001/01/28 19:10:18 kevin Exp $
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 ******************************************************************************/
32 #ifdef HAVE_SYS_TIME_H
37 #include <sys/timeb.h>
44 { m_timeStart = ttime(); }
49 virtual double timerEnd (void)
52 m_timeElapsed = m_timeEnd - m_timeStart;
54 return (m_timeElapsed);
57 virtual void timerReport (const char* const msg) const
59 std::cout << msg << ": " << m_timeElapsed << " seconds" << std::endl;
62 virtual double timerEndAndReport (const char* const msg)
64 double t = timerEnd ();
69 double getTimeElapsed (void) const
70 { return m_timeElapsed; }
77 double ttime(void) const
79 #ifdef HAVE_GETTIMEOFDAY
81 if (gettimeofday (&now, NULL))
84 return (now.tv_sec + static_cast<double>(now.tv_usec) / 1000000.);
88 return (now.time + static_cast<double>(now.millitm) / 1000.);
100 class TimerMPI : public Timer
103 TimerMPI (MPI::Intracomm& comm)
106 m_timeStart = MPI::Wtime();
109 virtual ~TimerMPI (void)
112 virtual double timerEnd (void)
114 m_timeEnd = MPI::Wtime();
115 m_timeElapsed = m_timeEnd - m_timeStart;
117 return (m_timeElapsed);
120 virtual void timerReport (const char* const msg)
122 if (m_comm.Get_rank() == 0)
123 std::cout << msg << ": " << m_timeElapsed << " seconds" << std::endl;
126 virtual double timerEndAndReport (const char* const msg)
128 double t = timerEnd ();
133 virtual void timerReportAllProcesses (const char* const msg)
139 MPI::Intracomm& m_comm;
142 class TimerCollectiveMPI : public TimerMPI
145 TimerCollectiveMPI (MPI::Intracomm& comm)
146 : TimerMPI::TimerMPI (comm)
149 m_timeStart = MPI::Wtime();
152 virtual ~TimerCollectiveMPI (void)
155 virtual double timerEnd (void)
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);
162 return (m_timeElapsed);
165 virtual double timerEndAndReport (const char* const msg)
167 double t = timerEnd ();
172 virtual void timerReport (const char* const msg)
174 if (m_comm.Get_rank() == 0)
175 std::cout << msg << " " << "Minimum=" << m_timeMin << ", Maximum=" << m_timeMax << " seconds" << std::endl;
178 virtual void timerReportAllProcesses (const char* const msg)
180 std::cout << msg << ": " << "Minimum=" << m_timeMin << ", Maximum=" << m_timeMax << " seconds (Rank " << m_comm.Get_rank() << ")" << std::endl;