Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
Timer.h
1 /**
2  * Copyright (c) Facebook, Inc. and its affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 
9 #pragma once
10 
11 #include <cuda_runtime.h>
12 #include <time.h>
13 
14 namespace faiss { namespace gpu {
15 
16 /// Utility class for timing execution of a kernel
17 class KernelTimer {
18  public:
19  /// Constructor starts the timer and adds an event into the current
20  /// device stream
21  KernelTimer(cudaStream_t stream = 0);
22 
23  /// Destructor releases event resources
24  ~KernelTimer();
25 
26  /// Adds a stop event then synchronizes on the stop event to get the
27  /// actual GPU-side kernel timings for any kernels launched in the
28  /// current stream. Returns the number of milliseconds elapsed.
29  /// Can only be called once.
30  float elapsedMilliseconds();
31 
32  private:
33  cudaEvent_t startEvent_;
34  cudaEvent_t stopEvent_;
35  cudaStream_t stream_;
36  bool valid_;
37 };
38 
39 /// CPU wallclock elapsed timer
40 class CpuTimer {
41  public:
42  /// Creates and starts a new timer
43  CpuTimer();
44 
45  /// Returns elapsed time in milliseconds
46  float elapsedMilliseconds();
47 
48  private:
49  struct timespec start_;
50 };
51 
52 } } // namespace
float elapsedMilliseconds()
Returns elapsed time in milliseconds.
Definition: Timer.cpp:50
Utility class for timing execution of a kernel.
Definition: Timer.h:17
CPU wallclock elapsed timer.
Definition: Timer.h:40
KernelTimer(cudaStream_t stream=0)
Definition: Timer.cpp:15
CpuTimer()
Creates and starts a new timer.
Definition: Timer.cpp:45
float elapsedMilliseconds()
Definition: Timer.cpp:32
~KernelTimer()
Destructor releases event resources.
Definition: Timer.cpp:26