OpenVDB 13.1.0
Loading...
Searching...
No Matches
Timer.h
Go to the documentation of this file.
1// Copyright Contributors to the OpenVDB Project
2// SPDX-License-Identifier: Apache-2.0
3
4/// @file nanovdb/util/cuda/Timer.h
5///
6/// @author Ken Museth
7///
8/// @brief A simple GPU timing class
9
10#ifndef NANOVDB_UTIL_CUDA_TIMER_H_HAS_BEEN_INCLUDED
11#define NANOVDB_UTIL_CUDA_TIMER_H_HAS_BEEN_INCLUDED
12
13#include <iostream>// for std::cerr
14#include <cuda.h>
15#include <cuda_runtime_api.h>
16
17namespace nanovdb {
18
19namespace util::cuda {
20
21class Timer
22{
23 cudaStream_t mStream{0};
24 cudaEvent_t mStart, mStop;
25
26public:
27 /// @brief Default constructor
28 /// @param stream CUDA stream to be timed (defaults to stream 0)
29 /// @note Starts the timer
30 /// @warning @c cudaEventCreate creates the event for the current device
31 /// and @c cudaEventRecord requires that the event and stream are
32 /// associated with the same device. So it's important to call
33 /// @c cudaSetDevice(device) so @c device matches the one used
34 /// when @c stream was created.
35 Timer(cudaStream_t stream = 0) : mStream(stream)
36 {
37 cudaEventCreate(&mStart);
38 cudaEventCreate(&mStop);
39 cudaEventRecord(mStart, mStream);
40 }
41
42 /// @brief Construct and start the timer
43 /// @param msg string message to be printed when timer is started
44 /// @param stream CUDA stream to be timed (defaults to stream 0)
45 /// @param os output stream for the message above
46 /// @warning @c cudaEventCreate creates the event for the current device
47 /// and @c cudaEventRecord requires that the event and stream are
48 /// associated with the same device. So it's important to call
49 /// @c cudaSetDevice(device) so @c device matches the one used
50 /// when @c stream was created.
51 Timer(const std::string &msg, cudaStream_t stream = 0, std::ostream& os = std::cerr)
52 : mStream(stream)
53 {
54 os << msg << " ... " << std::flush;
55 cudaEventCreate(&mStart);
56 cudaEventCreate(&mStop);
57 cudaEventRecord(mStart, mStream);
58 }
59
60 /// @brief Destructor
62 {
63 cudaEventDestroy(mStart);
64 cudaEventDestroy(mStop);
65 }
66
67 /// @brief Start the timer
68 /// @warning @c cudaEventRecord requires that the event and stream are
69 /// associated with the same device. So it's important to call
70 /// @c cudaSetDevice(device) so @c device matches the one used
71 /// when @c mStream was created.
72 void start() {cudaEventRecord(mStart, mStream);}
73
74 /// @brief Start the timer
75 /// @param msg string message to be printed when timer is started
76 /// @param os output stream for the message above
77 void start(const std::string &msg, std::ostream& os = std::cerr)
78 {
79 os << msg << " ... " << std::flush;
80 this->start();
81 }
82
83 /// @brief Start the timer
84 /// @param msg string message to be printed when timer is started
85 /// @param os output stream for the message above
86 void start(const char* msg, std::ostream& os = std::cerr)
87 {
88 os << msg << " ... " << std::flush;
89 this->start();
90 }
91
92 /// @warning @c cudaEventRecord requires that the event and stream are
93 /// associated with the same device. So it's important to call
94 /// @c cudaSetDevice(device) so it matches the device used when
95 /// @c mStream was created.
96 inline void record()
97 {
98 cudaEventRecord(mStop, mStream);
99 cudaEventSynchronize(mStop);
100 }
101
102 /// @brief Return the time in milliseconds since record was called
103 inline float milliseconds() const
104 {
105 float msec = 0.0f;
106 cudaEventElapsedTime(&msec, mStart, mStop);
107 return msec;
108 }
109
110 /// @brief elapsed time (since start) in miliseconds
111 /// @return elapsed time (since start) in miliseconds
112 inline float elapsed()
113 {
114 this->record();
115 return this->milliseconds();
116 }
117
118 /// @brief Prints the elapsed time in milliseconds to a stream
119 /// @param os output stream to print to
120 inline void print(std::ostream& os = std::cerr)
121 {
122 const float msec = this->milliseconds();
123 os << "completed in " << msec << " milliseconds" << std::endl;
124 }
125
126 /// @brief Prints a message followed by the elapsed time in milliseconds to a stream
127 /// @param msg message to print before the time
128 /// @param os stream to print to
129 inline void print(const char* msg, std::ostream& os = std::cerr)
130 {
131 os << msg;
132 this->print(os);
133 }
134
135 /// @brief Like the above method but with a std::string arguments
136 inline void print(const std::string &msg, std::ostream& os = std::cerr){this->print(msg.c_str(), os);}
137
138 /// @brief stop the timer
139 /// @param os output stream for the message above
140 inline void stop(std::ostream& os = std::cerr)
141 {
142 this->record();
143 this->print(os);
144 }
145
146 /// @brief stop and start the timer
147 /// @param msg string message to be printed when timer is started
148 /// @param os output stream for the message above
149 /// @warning Remember to call start before restart
150 inline void restart(const std::string &msg, std::ostream& os = std::cerr)
151 {
152 this->stop(os);
153 this->start(msg, os);
154 }
155};// Timer
156
157}// namespace util::cuda
158
159using GpuTimer [[deprecated("Use nanovdb::util::cuda::Timer instead")]]= util::cuda::Timer;
160
161} // namespace nanovdb
162
163#endif // NANOVDB_UTIL_CUDA_TIMER_H_HAS_BEEN_INCLUDED
Definition Timer.h:22
float elapsed()
elapsed time (since start) in miliseconds
Definition Timer.h:112
void print(const std::string &msg, std::ostream &os=std::cerr)
Like the above method but with a std::string arguments.
Definition Timer.h:136
void restart(const std::string &msg, std::ostream &os=std::cerr)
stop and start the timer
Definition Timer.h:150
void stop(std::ostream &os=std::cerr)
stop the timer
Definition Timer.h:140
Timer(cudaStream_t stream=0)
Default constructor.
Definition Timer.h:35
~Timer()
Destructor.
Definition Timer.h:61
void start()
Start the timer.
Definition Timer.h:72
Timer(const std::string &msg, cudaStream_t stream=0, std::ostream &os=std::cerr)
Construct and start the timer.
Definition Timer.h:51
void print(std::ostream &os=std::cerr)
Prints the elapsed time in milliseconds to a stream.
Definition Timer.h:120
void start(const std::string &msg, std::ostream &os=std::cerr)
Start the timer.
Definition Timer.h:77
void record()
Definition Timer.h:96
void start(const char *msg, std::ostream &os=std::cerr)
Start the timer.
Definition Timer.h:86
float milliseconds() const
Return the time in milliseconds since record was called.
Definition Timer.h:103
void print(const char *msg, std::ostream &os=std::cerr)
Prints a message followed by the elapsed time in milliseconds to a stream.
Definition Timer.h:129
Definition Timer.h:19
Defines a simple memory pool used to call cub functions that use dynamic temporary storage.
Definition GridHandle.h:31