HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GpuTimer.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 
4 /// @file GpuTimer.h
5 ///
6 /// @author Ken Museth
7 ///
8 /// @brief A simple GPU timing class
9 
10 #ifndef NANOVDB_GPU_TIMER_H_HAS_BEEN_INCLUDED
11 #define NANOVDB_GPU_TIMER_H_HAS_BEEN_INCLUDED
12 
13 #include <iostream>// for std::cerr
14 #include <cuda.h>
15 #include <cuda_runtime_api.h>
16 
17 namespace nanovdb {
18 
19 class GpuTimer
20 {
21  cudaStream_t mStream{0};
22  cudaEvent_t mStart, mStop;
23 
24 public:
25  /// @brief Default constructor
26  /// @param stream CUDA stream to be timed (defaults to stream 0)
27  /// @note Starts the timer
29  {
30  cudaEventCreate(&mStart);
31  cudaEventCreate(&mStop);
32  cudaEventRecord(mStart, mStream);
33  }
34 
35  /// @brief Construct and start the timer
36  /// @param msg string message to be printed when timer is started
37  /// @param stream CUDA stream to be timed (defaults to stream 0)
38  /// @param os output stream for the message above
39  GpuTimer(const std::string &msg, cudaStream_t stream = 0, std::ostream& os = std::cerr)
40  : mStream(stream)
41  {
42  os << msg << " ... " << std::flush;
43  cudaEventCreate(&mStart);
44  cudaEventCreate(&mStop);
45  cudaEventRecord(mStart, mStream);
46  }
47 
48  /// @brief Destructor
50  {
51  cudaEventDestroy(mStart);
52  cudaEventDestroy(mStop);
53  }
54 
55  /// @brief Start the timer
56  /// @param stream CUDA stream to be timed (defaults to stream 0)
57  /// @param os output stream for the message above
58  void start() {cudaEventRecord(mStart, mStream);}
59 
60  /// @brief Start the timer
61  /// @param msg string message to be printed when timer is started
62 
63  /// @param os output stream for the message above
64  void start(const std::string &msg, std::ostream& os = std::cerr)
65  {
66  os << msg << " ... " << std::flush;
67  this->start();
68  }
69 
70  /// @brief Start the timer
71  /// @param msg string message to be printed when timer is started
72  /// @param os output stream for the message above
73  void start(const char* msg, std::ostream& os = std::cerr)
74  {
75  os << msg << " ... " << std::flush;
76  this->start();
77  }
78 
79  /// @brief elapsed time (since start) in miliseconds
80  /// @return elapsed time (since start) in miliseconds
81  float elapsed()
82  {
83  cudaEventRecord(mStop, mStream);
84  cudaEventSynchronize(mStop);
85  float diff = 0.0f;
86  cudaEventElapsedTime(&diff, mStart, mStop);
87  return diff;
88  }
89 
90  /// @brief stop the timer
91  /// @param os output stream for the message above
92  void stop(std::ostream& os = std::cerr)
93  {
94  float diff = this->elapsed();
95  os << "completed in " << diff << " milliseconds" << std::endl;
96  }
97 
98  /// @brief stop and start the timer
99  /// @param msg string message to be printed when timer is started
100  /// @warning Remember to call start before restart
101  void restart(const std::string &msg, std::ostream& os = std::cerr)
102  {
103  this->stop();
104  this->start(msg, os);
105  }
106 };// GpuTimer
107 
108 } // namespace nanovdb
109 
110 #endif // NANOVDB_GPU_TIMER_H_HAS_BEEN_INCLUDED
GLuint GLuint stream
Definition: glcorearb.h:1832
void start(const char *msg, std::ostream &os=std::cerr)
Start the timer.
Definition: GpuTimer.h:73
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
GpuTimer(cudaStream_t stream=0)
Default constructor.
Definition: GpuTimer.h:28
void start(const std::string &msg, std::ostream &os=std::cerr)
Start the timer.
Definition: GpuTimer.h:64
~GpuTimer()
Destructor.
Definition: GpuTimer.h:49
struct CUstream_st * cudaStream_t
Definition: oidn.h:24
void stop(std::ostream &os=std::cerr)
stop the timer
Definition: GpuTimer.h:92
void restart(const std::string &msg, std::ostream &os=std::cerr)
stop and start the timer
Definition: GpuTimer.h:101
GpuTimer(const std::string &msg, cudaStream_t stream=0, std::ostream &os=std::cerr)
Construct and start the timer.
Definition: GpuTimer.h:39
float elapsed()
elapsed time (since start) in miliseconds
Definition: GpuTimer.h:81
void start()
Start the timer.
Definition: GpuTimer.h:58