~ubuntu-branches/ubuntu/raring/blitz++/raring

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
// -*- C++ -*-
/***************************************************************************
 * blitz/Timer.h        Timer class, for use in benchmarking
 *
 * $Id$
 *
 * Copyright (C) 1997-2011 Todd Veldhuizen <tveldhui@acm.org>
 *
 * This file is a part of Blitz.
 *
 * Blitz is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 *
 * Blitz 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public 
 * License along with Blitz.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Suggestions:          blitz-devel@lists.sourceforge.net
 * Bugs:                 blitz-support@lists.sourceforge.net    
 *
 * For more information, please see the Blitz++ Home Page:
 *    https://sourceforge.net/projects/blitz/
 *
 ***************************************************************************/

// This class is not portable to non System V platforms.
// It will need to be rewritten for Windows, NT, Mac.
// NEEDS_WORK

#ifndef BZ_TIMER_H
#define BZ_TIMER_H

#ifndef BZ_BLITZ_H
 #include <blitz/blitz.h>
#endif

#ifdef BZ_HAVE_RUSAGE
 #include <sys/resource.h>
#else
 #include <time.h>
#endif

BZ_NAMESPACE(blitz)

#ifndef BZ_HAVE_LIBPAPI

class Timer {

public:
    Timer() 
    { 
        state_ = uninitialized;
    }

    void start()
    { 
        state_ = running;
        t1_ = systemTime();
    }

    void stop()
    {
        t2_ = systemTime();
        BZPRECONDITION(state_ == running);
        state_ = stopped;
    }
    
/* Compaq cxx compiler in ansi mode cannot print out long double type! */
#if defined(__DECCXX)
    double elapsed() const
#else
    /** Return elapsed time in seconds. */
    double elapsed() const
#endif
    {
        BZPRECONDITION(state_ == stopped);
        return (t2_ - t1_)/1e6;
    }

  long long instr() const { return 0; };
  long long flops() const { return 0; };

  static const string& indep_var() { return ivar_; };

private:
    Timer(Timer&) { }
    void operator=(Timer&) { }

    long int systemTime()
    {
#ifdef BZ_HAVE_RUSAGE
        getrusage(RUSAGE_SELF, &resourceUsage_);
        long int sec = resourceUsage_.ru_utime.tv_sec 
            + resourceUsage_.ru_stime.tv_sec;
        long int usec  = resourceUsage_.ru_utime.tv_usec 
            + resourceUsage_.ru_stime.tv_usec;
        return sec*1000000+usec;
#else
        return 1000000*clock() / (long int) CLOCKS_PER_SEC;
#endif
    }

    enum { uninitialized, running, stopped } state_;

  static const string ivar_;

#ifdef BZ_HAVE_RUSAGE
    struct rusage resourceUsage_;
#endif

    long int t1_, t2_;
};

#else

// implementation using PAPI performance counters

#include <papi.h>

class Timer {

public:
    Timer() 
    { 
      // maybe overhead is less from just reading counters
      if(PAPI_start_counters((int*)Events, nevents)!=PAPI_OK) {
	cerr << "Error starting counters\n";
      }
      state_ = uninitialized;
    }
  ~Timer() 
    { 
      PAPI_stop_counters(counters_.data(), nevents);
    }

    void start()
    { 
        state_ = running;
	// this resets the counters
        PAPI_read_counters(counters_.data(), nevents);
    }

    void stop()
    {
        PAPI_read_counters(counters_.data(), nevents);
	BZPRECONDITION(state_ == running);
	state_ = stopped;
    }

  /** since we don't know the clock frequency of the processor, we
      instead output "flops/clock cycle" which seems like a better
      measure of code performance and not machine performance. */
    long long elapsed() const
    {
        BZPRECONDITION(state_ == stopped);
        return counters_[0];
    }

  long long instr() const { return counters_[1]; };
  long long flops() const { return counters_[2]; };

  static const string& indep_var() { return ivar_; };

private:
    Timer(Timer&) { }
    void operator=(Timer&) { }

    enum { uninitialized, running, stopped } state_;

  static const int nevents=3;
  static const int Events[nevents];
  static const string ivar_;

  TinyVector<long long, nevents> counters_;
};

#endif




BZ_NAMESPACE_END

#endif // BZ_TIMER_H