1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include "tensorflow/stream_executor/gpu/gpu_timer.h"
17
18 #include "tensorflow/stream_executor/gpu/gpu_driver.h"
19 #include "tensorflow/stream_executor/gpu/gpu_executor.h"
20 #include "tensorflow/stream_executor/gpu/gpu_stream.h"
21 #include "tensorflow/stream_executor/lib/status.h"
22
23 namespace stream_executor {
24 namespace gpu {
25
Init()26 bool GpuTimer::Init() {
27 CHECK(start_event_ == nullptr && stop_event_ == nullptr);
28 GpuContext* context = parent_->gpu_context();
29 port::Status status = GpuDriver::CreateEvent(context, &start_event_,
30 GpuDriver::EventFlags::kDefault);
31 if (!status.ok()) {
32 LOG(ERROR) << status;
33 return false;
34 }
35
36 status = GpuDriver::CreateEvent(context, &stop_event_,
37 GpuDriver::EventFlags::kDefault);
38 if (!status.ok()) {
39 LOG(ERROR) << status;
40 status = GpuDriver::DestroyEvent(context, &start_event_);
41 if (!status.ok()) {
42 LOG(ERROR) << status;
43 }
44 return false;
45 }
46
47 CHECK(start_event_ != nullptr && stop_event_ != nullptr);
48 return true;
49 }
50
Destroy()51 void GpuTimer::Destroy() {
52 GpuContext* context = parent_->gpu_context();
53 port::Status status = GpuDriver::DestroyEvent(context, &start_event_);
54 if (!status.ok()) {
55 LOG(ERROR) << status;
56 }
57
58 status = GpuDriver::DestroyEvent(context, &stop_event_);
59 if (!status.ok()) {
60 LOG(ERROR) << status;
61 }
62 }
63
GetElapsedMilliseconds() const64 float GpuTimer::GetElapsedMilliseconds() const {
65 CHECK(start_event_ != nullptr && stop_event_ != nullptr);
66 // TODO(leary) provide a way to query timer resolution?
67 // CUDA docs say a resolution of about 0.5us
68 float elapsed_milliseconds = NAN;
69 (void)GpuDriver::GetEventElapsedTime(
70 parent_->gpu_context(), &elapsed_milliseconds, start_event_, stop_event_);
71 return elapsed_milliseconds;
72 }
73
Start(GpuStream * stream)74 bool GpuTimer::Start(GpuStream* stream) {
75 port::Status status = GpuDriver::RecordEvent(
76 parent_->gpu_context(), start_event_, stream->gpu_stream());
77 if (!status.ok()) {
78 LOG(ERROR) << status;
79 }
80 return status.ok();
81 }
82
Stop(GpuStream * stream)83 bool GpuTimer::Stop(GpuStream* stream) {
84 port::Status status = GpuDriver::RecordEvent(
85 parent_->gpu_context(), stop_event_, stream->gpu_stream());
86 if (!status.ok()) {
87 LOG(ERROR) << status;
88 }
89 return status.ok();
90 }
91
92 } // namespace gpu
93 } // namespace stream_executor
94