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.7 2001/02/22 11:05:38 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 ******************************************************************************/
36 #ifdef HAVE_SYS_TIME_H
41 #include <sys/timeb.h>
48 { m_timeStart = ttime(); }
53 virtual double timerEnd (void)
56 m_timeElapsed = m_timeEnd - m_timeStart;
58 return (m_timeElapsed);
61 virtual void timerReport (const char* const msg) const
63 std::cout << msg << ": " << m_timeElapsed << " seconds" << std::endl;
66 virtual double timerEndAndReport (const char* const msg)
68 double t = timerEnd ();
73 double getTimeElapsed (void) const
74 { return m_timeElapsed; }
81 double ttime(void) const
83 #ifdef HAVE_GETTIMEOFDAY
85 if (gettimeofday (&now, NULL))
88 return (now.tv_sec + static_cast<double>(now.tv_usec) / 1000000.);
92 return (now.time + static_cast<double>(now.millitm) / 1000.);
104 class TimerMPI : public Timer
107 TimerMPI (MPI::Intracomm& comm)
110 m_timeStart = MPI::Wtime();
113 virtual ~TimerMPI (void)
116 virtual double timerEnd (void)
118 m_timeEnd = MPI::Wtime();
119 m_timeElapsed = m_timeEnd - m_timeStart;
121 return (m_timeElapsed);
124 virtual void timerReport (const char* const msg)
126 if (m_comm.Get_rank() == 0)
127 std::cout << msg << ": " << m_timeElapsed << " seconds" << std::endl;
130 virtual double timerEndAndReport (const char* const msg)
132 double t = timerEnd ();
137 virtual void timerReportAllProcesses (const char* const msg)
143 MPI::Intracomm& m_comm;
146 class TimerCollectiveMPI : public TimerMPI
149 TimerCollectiveMPI (MPI::Intracomm& comm)
150 : TimerMPI::TimerMPI (comm)
153 m_timeStart = MPI::Wtime();
156 virtual ~TimerCollectiveMPI (void)
159 virtual double timerEnd (void)
161 m_timeEnd = MPI::Wtime();
162 m_timeElapsed = m_timeEnd - m_timeStart;
163 m_comm.Reduce (&m_timeElapsed, &m_timeMin, 1, MPI::DOUBLE, MPI::MIN, 0);
164 m_comm.Reduce (&m_timeElapsed, &m_timeMax, 1, MPI::DOUBLE, MPI::MAX, 0);
166 return (m_timeElapsed);
169 virtual double timerEndAndReport (const char* const msg)
171 double t = timerEnd ();
176 virtual void timerReport (const char* const msg)
178 if (m_comm.Get_rank() == 0)
179 std::cout << msg << " " << "Minimum=" << m_timeMin << ", Maximum=" << m_timeMax << " seconds" << std::endl;
182 virtual void timerReportAllProcesses (const char* const msg)
184 std::cout << msg << ": " << "Minimum=" << m_timeMin << ", Maximum=" << m_timeMax << " seconds (Rank " << m_comm.Get_rank() << ")" << std::endl;