• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/lite/delegates/gpu/cl/cl_event.h"
17 
18 #include "tensorflow/lite/delegates/gpu/cl/opencl_wrapper.h"
19 
20 namespace tflite {
21 namespace gpu {
22 namespace cl {
23 
CLEvent(cl_event event)24 CLEvent::CLEvent(cl_event event) : event_(event) {}
25 
CLEvent(CLEvent && event)26 CLEvent::CLEvent(CLEvent&& event)
27     : event_(event.event_), name_(std::move(event.name_)) {
28   event.event_ = nullptr;
29 }
30 
operator =(CLEvent && event)31 CLEvent& CLEvent::operator=(CLEvent&& event) {
32   if (this != &event) {
33     Release();
34     std::swap(event_, event.event_);
35     name_ = std::move(event.name_);
36   }
37   return *this;
38 }
39 
GetStartedTimeNs() const40 uint64_t CLEvent::GetStartedTimeNs() const {
41   cl_ulong time_ns;
42   clGetEventProfilingInfo(event_, CL_PROFILING_COMMAND_START, sizeof(cl_ulong),
43                           &time_ns, nullptr);
44   return time_ns;
45 }
46 
GetFinishedTimeNs() const47 uint64_t CLEvent::GetFinishedTimeNs() const {
48   cl_ulong time_ns;
49   clGetEventProfilingInfo(event_, CL_PROFILING_COMMAND_END, sizeof(cl_ulong),
50                           &time_ns, nullptr);
51   return time_ns;
52 }
53 
GetEventTimeMs() const54 double CLEvent::GetEventTimeMs() const {
55   const uint64_t start = GetStartedTimeNs();
56   const uint64_t end = GetFinishedTimeNs();
57   const uint64_t time_ns = (end - start);
58 
59   return static_cast<double>(time_ns) * 1e-6;
60 }
61 
GetEventTimeNs() const62 uint64_t CLEvent::GetEventTimeNs() const {
63   return GetFinishedTimeNs() - GetStartedTimeNs();
64 }
65 
SetName(const std::string & name)66 void CLEvent::SetName(const std::string& name) { name_ = name; }
67 
Wait() const68 void CLEvent::Wait() const { clWaitForEvents(1, &event_); }
69 
~CLEvent()70 CLEvent::~CLEvent() { Release(); }
71 
Release()72 void CLEvent::Release() {
73   if (event_) {
74     clReleaseEvent(event_);
75     event_ = nullptr;
76   }
77 }
78 
79 }  // namespace cl
80 }  // namespace gpu
81 }  // namespace tflite
82