Fermat
timer.h
1 /*
2  * cugar
3  * Copyright (c) 2011-2018, 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 <cugar/basic/types.h>
31 #include <cugar/basic/numbers.h>
32 
33 #include <algorithm>
34 #include <string>
35 #include <deque>
36 
37 namespace cugar {
38 
47 
50 
54 
55 #ifdef WIN32
56 
60 struct CUGAR_API Timer
61 {
63  Timer();
64 
66  void start();
67 
69  void stop();
70 
71  float seconds() const;
72 
73  uint64 m_freq;
74  uint64 m_start;
75  uint64 m_stop;
76 };
77 
78 #else
79 
83 struct CUGAR_API Timer
84 {
86  Timer() {}
87 
89  void start();
90 
92  void stop();
93 
94  float seconds() const;
95 
96 private:
97  int64 m_start;
98  int64 m_stop;
99  int64 m_start_ns;
100  int64 m_stop_ns;
101 };
102 
103 #endif
104 
109 template <typename T>
111 {
112  ScopedTimer(T* time) : m_time( time ), m_timer() { m_timer.start(); }
113  ~ScopedTimer() { m_timer.stop(); *m_time += m_timer.seconds(); }
114 
115  T* m_time;
116  Timer m_timer;
117 };
118 
122 struct FakeTimer
123 {
124  void start() {}
125  void stop() {}
126 
127  float seconds() const { return 0.0f; }
128 };
129 
134 {
137  TimeSeries() : num(0), calls(0), time(0.0f), device_time(0.0f), max_speed(0.0f)
138  {
139  for (uint32 i = 0; i < 32; ++i)
140  {
141  bin_calls[i] = 0;
142  bin_items[i] = 0;
143  bin_time[i] = 0.0f;
144  bin_speed[i] = 0.0f;
145  }
146  }
147 
153  void add(const uint32 c, const float t, const float dt = 0.0f)
154  {
155  num++;
156  calls += c;
157  time += t;
158  device_time += dt;
159  max_speed = std::max( max_speed, float(c) / t );
160  if (info.size() == 10000)
161  info.pop_front();
162  info.push_back( std::make_pair( c, t ) );
163 
164  const uint32 bin = c ? cugar::log2( c ) : 0u;
165  bin_calls[bin]++;
166  bin_time[bin] += t;
167  bin_speed[bin] += float(c) / t;
168  bin_items[bin] += c;
169  }
170 
171  // return the average speed
172  float avg_speed() const { return float(calls) / time; }
173 
174  std::string name;
175  std::string units;
176 
177  uint32 num;
178  uint64 calls;
179  float time;
180  float device_time;
181  float max_speed;
182  uint32 bin_calls[32];
183  uint64 bin_items[32];
184  float bin_time[32];
185  float bin_speed[32];
186  std::deque< std::pair<uint32,float> > info;
187 };
188 
191 
192 } // namespace cugar
Timer()
constructor
Definition: timer.h:86
TimeSeries()
Definition: timer.h:137
CUGAR_HOST_DEVICE uint32 log2(uint32 n)
Definition: numbers.h:618
Definition: timer.h:83
Definition: timer.h:122
Definition: timer.h:133
Define a vector_view POD type and plain_view() for std::vector.
Definition: diff.h:38
void add(const uint32 c, const float t, const float dt=0.0f)
Definition: timer.h:153
Definition: timer.h:110