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