Mila 0.13.48
Deep Neural Network Library
Loading...
Searching...
No Matches
Mila::Dnn::Compute::CudaTimer Class Referenceexport

GPU-accurate interval timer using a CUDA event pair. More...

Public Member Functions

 CudaTimer ()
 Construct a CudaTimer and allocate its CUDA event pair.
 CudaTimer (const CudaTimer &)=delete
 CudaTimer (CudaTimer &&)=delete
 ~CudaTimer ()
 Destructor.
float elapsedMilliseconds () const
 Returns the elapsed GPU time in milliseconds between start and stop.
CudaTimeroperator= (const CudaTimer &)=delete
CudaTimeroperator= (CudaTimer &&)=delete
void start (cudaStream_t stream)
 Record the start event into the given CUDA stream.
void stop (cudaStream_t stream)
 Record the stop event into the given CUDA stream.

Private Attributes

cudaEvent_t start_event_ { nullptr }
cudaEvent_t stop_event_ { nullptr }

Detailed Description

GPU-accurate interval timer using a CUDA event pair.

Records start and stop events on a given CUDA stream. Reading the elapsed time via elapsedMilliseconds() synchronizes on the stop event, ensuring the GPU has reached that point before the measurement is read back.

This approach avoids inserting a full cudaStreamSynchronize() at every measurement boundary, so it is suitable for fine-grained per-operation profiling during the beta.1 pass.

Example usage:

CudaTimer timer;
timer.start( stream );
// ... dispatch CUDA kernels ...
timer.stop( stream );
float elapsed_ms = timer.elapsedMilliseconds();
void start(cudaStream_t stream)
Record the start event into the given CUDA stream.
Definition CudaTimer.ixx:113
CudaTimer()
Construct a CudaTimer and allocate its CUDA event pair.
Definition CudaTimer.ixx:58
void stop(cudaStream_t stream)
Record the stop event into the given CUDA stream.
Definition CudaTimer.ixx:134
float elapsedMilliseconds() const
Returns the elapsed GPU time in milliseconds between start and stop.
Definition CudaTimer.ixx:156

Thread safety: A CudaTimer instance must not be used concurrently from multiple threads. Each CUDA stream should have its own CudaTimer.

Constructor & Destructor Documentation

◆ CudaTimer() [1/3]

Mila::Dnn::Compute::CudaTimer::CudaTimer ( )
inline

Construct a CudaTimer and allocate its CUDA event pair.

Exceptions
std::runtime_errorif either CUDA event cannot be created.
Here is the caller graph for this function:

◆ ~CudaTimer()

Mila::Dnn::Compute::CudaTimer::~CudaTimer ( )
inline

Destructor.

Releases both CUDA events.

◆ CudaTimer() [2/3]

Mila::Dnn::Compute::CudaTimer::CudaTimer ( const CudaTimer & )
delete
Here is the call graph for this function:

◆ CudaTimer() [3/3]

Mila::Dnn::Compute::CudaTimer::CudaTimer ( CudaTimer && )
delete
Here is the call graph for this function:

Member Function Documentation

◆ elapsedMilliseconds()

float Mila::Dnn::Compute::CudaTimer::elapsedMilliseconds ( ) const
inlinenodiscard

Returns the elapsed GPU time in milliseconds between start and stop.

Synchronizes on the stop event (cudaEventSynchronize) before reading back the elapsed time. The caller's thread blocks until the GPU has reached the stop event.

Returns
Elapsed time in milliseconds as reported by the CUDA event subsystem.
Exceptions
std::runtime_errorif synchronization or elapsed time query fails.

◆ operator=() [1/2]

CudaTimer & Mila::Dnn::Compute::CudaTimer::operator= ( const CudaTimer & )
delete
Here is the call graph for this function:

◆ operator=() [2/2]

CudaTimer & Mila::Dnn::Compute::CudaTimer::operator= ( CudaTimer && )
delete
Here is the call graph for this function:

◆ start()

void Mila::Dnn::Compute::CudaTimer::start ( cudaStream_t stream)
inline

Record the start event into the given CUDA stream.

The GPU timestamp is recorded at the point the stream reaches this event, which may be later than the host-side call.

Parameters
streamCUDA stream on which to record the start event.
Exceptions
std::runtime_errorif event recording fails.

◆ stop()

void Mila::Dnn::Compute::CudaTimer::stop ( cudaStream_t stream)
inline

Record the stop event into the given CUDA stream.

The GPU timestamp is recorded at the point the stream reaches this event. Call elapsedMilliseconds() afterwards to read the measured interval.

Parameters
streamCUDA stream on which to record the stop event.
Exceptions
std::runtime_errorif event recording fails.

Member Data Documentation

◆ start_event_

cudaEvent_t Mila::Dnn::Compute::CudaTimer::start_event_ { nullptr }
private

◆ stop_event_

cudaEvent_t Mila::Dnn::Compute::CudaTimer::stop_event_ { nullptr }
private

The documentation for this class was generated from the following file:
  • /__w/Mila/Mila/Mila/Src/Dnn/Compute/Devices/Cuda/Profiling/CudaTimer.ixx