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