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 // Defines the GpuTimer type - the CUDA-specific implementation of the generic
17 // StreamExecutor Timer interface.
18 
19 #ifndef TENSORFLOW_STREAM_EXECUTOR_GPU_GPU_TIMER_H_
20 #define TENSORFLOW_STREAM_EXECUTOR_GPU_GPU_TIMER_H_
21 
22 #include "tensorflow/stream_executor/gpu/gpu_driver.h"
23 #include "tensorflow/stream_executor/gpu/gpu_executor.h"
24 #include "tensorflow/stream_executor/stream_executor_internal.h"
25 
26 namespace stream_executor {
27 namespace gpu {
28 
29 class GpuExecutor;
30 class GpuStream;
31 
32 // Wraps a pair of GpuEventHandles in order to satisfy the platform-independent
33 // TimerInterface -- both a start and a stop event are present which may be
34 // recorded in a stream.
35 class GpuTimer : public internal::TimerInterface {
36  public:
GpuTimer(GpuExecutor * parent)37   explicit GpuTimer(GpuExecutor* parent)
38       : parent_(parent), start_event_(nullptr), stop_event_(nullptr) {}
39 
40   // Note: teardown needs to be explicitly handled in this API by a call to
41   // StreamExecutor::DeallocateTimer(), which invokes Destroy().
42   // TODO(csigg): Change to RAII.
~GpuTimer()43   ~GpuTimer() override {}
44 
45   // Allocates the platform-specific pieces of the timer, called as part of
46   // StreamExecutor::AllocateTimer().
47   bool Init();
48 
49   // Deallocates the platform-specific pieces of the timer, called as part of
50   // StreamExecutor::DeallocateTimer().
51   void Destroy();
52 
53   // Records the "timer start" event at the current point in the stream.
54   bool Start(GpuStream* stream);
55 
56   // Records the "timer stop" event at the current point in the stream.
57   bool Stop(GpuStream* stream);
58 
59   // Returns the elapsed time, in milliseconds, between the start and stop
60   // events.
61   float GetElapsedMilliseconds() const;
62 
63   // See Timer::Microseconds().
64   // TODO(leary) make this into an error code interface...
Microseconds()65   uint64 Microseconds() const override {
66     return GetElapsedMilliseconds() * 1e3;
67   }
68 
69   // See Timer::Nanoseconds().
Nanoseconds()70   uint64 Nanoseconds() const override { return GetElapsedMilliseconds() * 1e6; }
71 
72  private:
73   GpuExecutor* parent_;
74   GpuEventHandle start_event_;  // Event recorded to indicate the "start"
75                                 // timestamp executing in a stream.
76   GpuEventHandle stop_event_;   // Event recorded to indicate the "stop"
77                                 // timestamp executing in a stream.
78 };
79 
80 struct GpuTimerDeleter {
operatorGpuTimerDeleter81   void operator()(GpuTimer* t) {
82     t->Destroy();
83     delete t;
84   }
85 };
86 
87 }  // namespace gpu
88 }  // namespace stream_executor
89 
90 #endif  // TENSORFLOW_STREAM_EXECUTOR_GPU_GPU_TIMER_H_
91