Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

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