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