NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
timer.h
Go to the documentation of this file.
1 /*
2  * nvbio
3  * Copyright (c) 2011-2014, NVIDIA CORPORATION. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the NVIDIA CORPORATION nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #pragma once
29 
30 #include <nvbio/basic/types.h>
31 #include <nvbio/basic/numbers.h>
32 
33 #include <string>
34 #include <deque>
35 
36 namespace nvbio {
37 
46 
47 #ifdef WIN32
48 
52 struct Timer
53 {
55  Timer();
56 
58  void start();
59 
61  void stop();
62 
63  float seconds() const;
64 
65  uint64 m_freq;
66  uint64 m_start;
67  uint64 m_stop;
68 };
69 
70 #else
71 
75 struct Timer
76 {
78  Timer() {}
79 
81  void start();
82 
84  void stop();
85 
86  float seconds() const;
87 
88 private:
89  int64 m_start;
90  int64 m_stop;
91  int64 m_start_ns;
92  int64 m_stop_ns;
93 };
94 
95 #endif
96 
101 template <typename T>
103 {
104  ScopedTimer(T* time) : m_time( time ), m_timer() { m_timer.start(); }
106 
107  T* m_time;
109 };
110 
114 struct FakeTimer
115 {
116  void start();
117  void stop();
118 
119  float seconds() const { return 0.0f; }
120 };
121 
126 {
129  TimeSeries() : num(0), calls(0), time(0.0f), device_time(0.0f), max_speed(0.0f)
130  {
131  for (uint32 i = 0; i < 32; ++i)
132  {
133  bin_calls[i] = 0;
134  bin_items[i] = 0;
135  bin_time[i] = 0.0f;
136  bin_speed[i] = 0.0f;
137 
138  user[i] = 0.0f;
139  user_names[i] = NULL;
140  user_units[i] = "";
141  }
142  }
143 
149  void add(const uint32 c, const float t, const float dt = 0.0f)
150  {
151  num++;
152  calls += c;
153  time += t;
154  device_time += dt;
155  max_speed = std::max( max_speed, float(c) / t );
156  if (info.size() == 10000)
157  info.pop_front();
158  info.push_back( std::make_pair( c, t ) );
159 
160  const uint32 bin = c ? nvbio::log2( c ) : 0u;
161  bin_calls[bin]++;
162  bin_time[bin] += t;
163  bin_speed[bin] += float(c) / t;
164  bin_items[bin] += c;
165  }
166 
167  // return the average speed
168  float avg_speed() const { return float(calls) / time; }
169 
170  std::string name;
171  std::string units;
172 
175  float time;
176  float device_time;
177  float max_speed;
180  float bin_time[32];
181  float bin_speed[32];
182  std::deque< std::pair<uint32,float> > info;
183 
184  float user[32];
185  const char* user_names[32];
186  const char* user_units[32];
187  bool user_avg[32];
188 };
189 
190 } // namespace nvbio