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