Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

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