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 // Copyright 2004-present Facebook. All Rights Reserved.
10 
11 #pragma once
12 
13 #include <cuda_runtime.h>
14 #include <time.h>
15 
16 namespace faiss { namespace gpu {
17 
18 /// Utility class for timing execution of a kernel
19 class KernelTimer {
20  public:
21  /// Constructor starts the timer and adds an event into the current
22  /// device stream
23  KernelTimer(cudaStream_t stream = 0);
24 
25  /// Destructor releases event resources
26  ~KernelTimer();
27 
28  /// Adds a stop event then synchronizes on the stop event to get the
29  /// actual GPU-side kernel timings for any kernels launched in the
30  /// current stream. Returns the number of milliseconds elapsed.
31  /// Can only be called once.
32  float elapsedMilliseconds();
33 
34  private:
35  cudaEvent_t startEvent_;
36  cudaEvent_t stopEvent_;
37  cudaStream_t stream_;
38  bool valid_;
39 };
40 
41 /// CPU wallclock elapsed timer
42 class CpuTimer {
43  public:
44  /// Creates and starts a new timer
45  CpuTimer();
46 
47  /// Returns elapsed time in milliseconds
48  float elapsedMilliseconds();
49 
50  private:
51  struct timespec start_;
52 };
53 
54 } } // namespace
float elapsedMilliseconds()
Returns elapsed time in milliseconds.
Definition: Timer.cpp:52
Utility class for timing execution of a kernel.
Definition: Timer.h:19
CPU wallclock elapsed timer.
Definition: Timer.h:42
KernelTimer(cudaStream_t stream=0)
Definition: Timer.cpp:17
CpuTimer()
Creates and starts a new timer.
Definition: Timer.cpp:47
float elapsedMilliseconds()
Definition: Timer.cpp:34
~KernelTimer()
Destructor releases event resources.
Definition: Timer.cpp:28