Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
Timer.cpp
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 #include "Timer.h"
11 #include "DeviceUtils.h"
12 #include "../../FaissAssert.h"
13
14 namespace faiss { namespace gpu {
15
16 KernelTimer::KernelTimer(cudaStream_t stream)
17 : startEvent_(0),
18 stopEvent_(0),
19 stream_(stream),
20 valid_(true) {
21 CUDA_VERIFY(cudaEventCreate(&startEvent_));
22 CUDA_VERIFY(cudaEventCreate(&stopEvent_));
23
24 CUDA_VERIFY(cudaEventRecord(startEvent_, stream_));
25 }
26
27 KernelTimer::~KernelTimer() {
28 CUDA_VERIFY(cudaEventDestroy(startEvent_));
29 CUDA_VERIFY(cudaEventDestroy(stopEvent_));
30 }
31
32 float
33 KernelTimer::elapsedMilliseconds() {
34 FAISS_ASSERT(valid_);
35
36 CUDA_VERIFY(cudaEventRecord(stopEvent_, stream_));
37 CUDA_VERIFY(cudaEventSynchronize(stopEvent_));
38
39 auto time = 0.0f;
40 CUDA_VERIFY(cudaEventElapsedTime(&time, startEvent_, stopEvent_));
41 valid_ = false;
42
43 return time;
44 }
45
46 CpuTimer::CpuTimer() {
47 clock_gettime(CLOCK_REALTIME, &start_);
48 }
49
50 float
51 CpuTimer::elapsedMilliseconds() {
52 struct timespec end;
53 clock_gettime(CLOCK_REALTIME, &end);
54
55 auto diffS = end.tv_sec - start_.tv_sec;
56 auto diffNs = end.tv_nsec - start_.tv_nsec;
57
58 return 1000.0f * (float) diffS + ((float) diffNs) / 1000000.0f;
59 }
60
61 } } // namespace
faiss::gpu::CpuTimer::elapsedMilliseconds
float elapsedMilliseconds()
Returns elapsed time in milliseconds.
Definition: Timer.cpp:51
faiss::gpu::KernelTimer::KernelTimer
KernelTimer(cudaStream_t stream=0)
Definition: Timer.cpp:16
faiss::gpu::CpuTimer::CpuTimer
CpuTimer()
Creates and starts a new timer.
Definition: Timer.cpp:46
faiss::gpu::KernelTimer::elapsedMilliseconds
float elapsedMilliseconds()
Definition: Timer.cpp:33
faiss::gpu::KernelTimer::~KernelTimer
~KernelTimer()
Destructor releases event resources.
Definition: Timer.cpp:27