imate
C++/CUDA Reference
CudaTimer Class Reference

Records elasped time between two CUDA events. More...

#include <cuda_timer.h>

Public Member Functions

 CudaTimer ()
 constructor for CudaTimer More...
 
 ~CudaTimer ()
 Destructor for CudaTimer. More...
 
void start ()
 Starts the timer. More...
 
void stop ()
 Stops the timer. More...
 
float elapsed () const
 Returns the elapsed time in seconds. More...
 

Protected Attributes

cudaEvent_t start_time
 
cudaEvent_t stop_time
 

Detailed Description

Records elasped time between two CUDA events.

The measured time is the wall time, not the process time of the GPU. In fact, the measured time the same as the wall clock time of CPU.

Example:

It is better to synchronize all GPU threads before reading the time. For instance:

CudaTimer cuda_timer;
cuda_timer.start();

// Some GPU threads here.
// ...

// Note, this CudaTime measures wall time, so the sleep()
// time counts toward the measured time.
sleep(1);

// Sync threads so the CPU do not jump to the next before
// gpu threads are done.
cudaDeviceSynchronize();

cuda_timer.stop();
float elapsed_time = cuda_timer.elapsed();
See also
Timer

Definition at line 61 of file cuda_timer.h.

Constructor & Destructor Documentation

◆ CudaTimer()

CudaTimer::CudaTimer ( )

constructor for CudaTimer

Definition at line 26 of file cuda_timer.cu.

27 {
29  cudaEventCreate(&this->stop_time);
30 }
cudaEvent_t stop_time
Definition: cuda_timer.h:73
cudaEvent_t start_time
Definition: cuda_timer.h:72
cudaError_t cudaEventCreate(cudaEvent_t *event)
Definition of CUDA's cudaEventCreate function using dynamically loaded cudart library.

References cudaEventCreate(), start_time, and stop_time.

Here is the call graph for this function:

◆ ~CudaTimer()

CudaTimer::~CudaTimer ( )

Destructor for CudaTimer.

Definition at line 40 of file cuda_timer.cu.

41 {
44 }
cudaError_t cudaEventDestroy(cudaEvent_t event)
Definition of CUDA's cudaEventDestroy function using dynamically loaded cudart library.

References cudaEventDestroy(), start_time, and stop_time.

Here is the call graph for this function:

Member Function Documentation

◆ elapsed()

float CudaTimer::elapsed ( ) const

Returns the elapsed time in seconds.

Definition at line 80 of file cuda_timer.cu.

81 {
82  float elapsed_time;
84  cudaEventElapsedTime(&elapsed_time, this->start_time, this->stop_time);
85 
86  // Convert from milli-second to second
87  return elapsed_time / 1.0e+3;
88 }
cudaError_t cudaEventSynchronize(cudaEvent_t event)
Definition of CUDA's cudaEventSynchronize function using dynamically loaded cudart library.
cudaError_t cudaEventElapsedTime(float *ms, cudaEvent_t start, cudaEvent_t end)
Definition of CUDA's cudaEventElapsedTime function using dynamically loaded cudart library.

References cudaEventElapsedTime(), cudaEventSynchronize(), start_time, and stop_time.

Referenced by cuTraceEstimator< DataType >::cu_trace_estimator().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ start()

void CudaTimer::start ( )

Starts the timer.

Definition at line 54 of file cuda_timer.cu.

55 {
56  cudaEventRecord(this->start_time, 0);
57 }
cudaError_t cudaEventRecord(cudaEvent_t event, cudaStream_t stream)
Definition of CUDA's cudaEventRecord function using dynamically loaded cudart library.

References cudaEventRecord(), and start_time.

Referenced by cuTraceEstimator< DataType >::cu_trace_estimator().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ stop()

void CudaTimer::stop ( )

Stops the timer.

Definition at line 67 of file cuda_timer.cu.

68 {
69  cudaEventRecord(this->stop_time, 0);
70 }

References cudaEventRecord(), and stop_time.

Referenced by cuTraceEstimator< DataType >::cu_trace_estimator().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ start_time

cudaEvent_t CudaTimer::start_time
protected

Definition at line 72 of file cuda_timer.h.

Referenced by CudaTimer(), elapsed(), start(), and ~CudaTimer().

◆ stop_time

cudaEvent_t CudaTimer::stop_time
protected

Definition at line 73 of file cuda_timer.h.

Referenced by CudaTimer(), elapsed(), stop(), and ~CudaTimer().


The documentation for this class was generated from the following files: