File: timer.h

package info (click to toggle)
ctsim 4.5.2-1.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 6,536 kB
  • ctags: 3,720
  • sloc: cpp: 26,768; sh: 3,691; ansic: 1,254; perl: 296; makefile: 263
file content (200 lines) | stat: -rw-r--r-- 4,530 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*****************************************************************************
** FILE IDENTIFICATION
**
**      Name:         timer.h
**      Purpose:      Header file for Timer class
**      Author:       Kevin Rosenberg
**      Date Started: Sep 2000 
**
**  This is part of the CTSim program
**  Copyright (c) 1983-2001 Kevin Rosenberg
**
**  $Id: timer.h 10680 2005-08-31 04:27:46Z kevin $
**
**  This program is free software; you can redistribute it and/or modify
**  it under the terms of the GNU General Public License (version 2) as
**  published by the Free Software Foundation.
**
**  This program is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**  GNU General Public License for more details.
**
**  You should have received a copy of the GNU General Public License
**  along with this program; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
******************************************************************************/

#ifndef _TIMER_H
#define _TIMER_H

// pragma line required for Fedora 4 and wxWin 2.4.2 
#if defined(__GNUG__) && !defined(__APPLE__)
    #pragma interface "timer.h"
#endif

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif

#ifdef MSVC
#include <sys/timeb.h>
#endif

class Timer
{
 public:
    Timer (void)
	{ m_timeStart = ttime(); }

    virtual ~Timer (void)
	{}

    virtual double timerEnd (void)
      {
	m_timeEnd = ttime();
	m_timeElapsed = m_timeEnd - m_timeStart;
  
	return (m_timeElapsed);
      }

    virtual void timerReport (const char* const msg) const
      {
		std::cout << msg << ": " << m_timeElapsed << " seconds" << std::endl;
      }

    virtual double timerEndAndReport (const char* const msg)
      {
	double t = timerEnd ();
	timerReport (msg);
	return (t);
      }

    double getTimeElapsed (void) const
	{ return m_timeElapsed; }

 protected:
    double m_timeStart;
    double m_timeEnd;
    double m_timeElapsed;

    double ttime(void) const
	{
#ifdef HAVE_GETTIMEOFDAY
	    struct timeval now;
	    if (gettimeofday (&now, NULL))
		return 0;
	    
	    return (now.tv_sec + static_cast<double>(now.tv_usec) / 1000000.);
#elif defined(MSVC)
		struct _timeb now;
		_ftime (&now);
		return (now.time + static_cast<double>(now.millitm) / 1000.);
#else
	    return 0;
#endif
	}
};


#ifdef HAVE_MPI

#include "mpi++.h"

class TimerMPI : public Timer
{
 public:
    TimerMPI (MPI::Intracomm& comm)
	: m_comm(comm)
      {
	  m_timeStart = MPI::Wtime();
      }

    virtual ~TimerMPI (void)
      {}

    virtual double timerEnd (void)
      {
	m_timeEnd = MPI::Wtime();
	m_timeElapsed = m_timeEnd - m_timeStart;
  
	return (m_timeElapsed);
      }

    virtual void timerReport (const char* const msg)
      {
	  if (m_comm.Get_rank() == 0)
		  std::cout << msg << ": " << m_timeElapsed << " seconds" << std::endl;
      }

    virtual double timerEndAndReport (const char* const msg)
      {
	double t = timerEnd ();
	timerReport (msg);
	return (t);
      }

    virtual void timerReportAllProcesses (const char* const msg)
      {
	  timerReport (msg);
      }

 protected:
    MPI::Intracomm& m_comm;
};

class TimerCollectiveMPI : public TimerMPI
{
 public:
    TimerCollectiveMPI (MPI::Intracomm& comm)
	: TimerMPI::TimerMPI (comm)
      {
	m_comm.Barrier();
	m_timeStart = MPI::Wtime();
      }

    virtual ~TimerCollectiveMPI (void)
      {}

    virtual double timerEnd (void)
      {
	m_timeEnd = MPI::Wtime();
	m_timeElapsed = m_timeEnd - m_timeStart;
	m_comm.Reduce (&m_timeElapsed, &m_timeMin, 1, MPI::DOUBLE, MPI::MIN, 0);
	m_comm.Reduce (&m_timeElapsed, &m_timeMax, 1, MPI::DOUBLE, MPI::MAX, 0);
  
	return (m_timeElapsed);
      }

    virtual double timerEndAndReport (const char* const msg)
      {
	double t = timerEnd ();
	timerReport (msg);
	return (t);
      }

    virtual void timerReport (const char* const msg)
      {
	if (m_comm.Get_rank() == 0)
		std::cout << msg << " " << "Minimum=" << m_timeMin << ", Maximum=" << m_timeMax << " seconds" << std::endl;
      }

    virtual void timerReportAllProcesses (const char* const msg)
      {
		std::cout << msg << ": " << "Minimum=" << m_timeMin << ", Maximum=" << m_timeMax << " seconds (Rank " << m_comm.Get_rank() << ")" << std::endl;
      }

 private:
    double m_timeMin;
    double m_timeMax;
};
#endif


#endif  // _TIMER_H