1 /* Copyright 2015 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 #ifndef TENSORFLOW_STREAM_EXECUTOR_TIMER_H_ 17 #define TENSORFLOW_STREAM_EXECUTOR_TIMER_H_ 18 19 #include <memory> 20 21 #include "tensorflow/stream_executor/platform/port.h" 22 23 namespace stream_executor { 24 25 namespace internal { 26 class TimerInterface; 27 } // namespace internal 28 29 class StreamExecutor; 30 31 // An interval timer, suitable for use in timing the operations which occur in 32 // streams. 33 // 34 // Thread-hostile: CUDA associates a CUDA-context with a particular thread in 35 // the system. Any operation that a user attempts to perform by using a Timer 36 // on a thread not-associated with the CUDA-context has unknown behavior at the 37 // current time; see b/13176597 38 class Timer { 39 public: 40 // Instantiate a timer tied to parent as a platform executor. 41 explicit Timer(StreamExecutor *parent); 42 43 // Deallocates any timer resources that the parent StreamExecutor has bestowed 44 // upon this object. 45 ~Timer(); 46 47 // Returns the elapsed number of microseconds for a completed timer. 48 // Completed means has been through a start/stop lifecycle. 49 uint64 Microseconds() const; 50 51 // Returns the elapsed number of nanoseconds for a completed timer. 52 // Completed means has been through a start/stop lifecycle. 53 uint64 Nanoseconds() const; 54 55 // Returns the (opaque) backing platform ITimer instance. Ownership is 56 // not transferred to the caller. implementation()57 internal::TimerInterface *implementation() { return implementation_.get(); } 58 59 private: 60 // The StreamExecutor that manages the platform-specific internals for this 61 // timer. 62 StreamExecutor *parent_; 63 64 // Platform-dependent implementation of the timer internals for the underlying 65 // platform. This class just delegates to this opaque instance. 66 std::unique_ptr<internal::TimerInterface> implementation_; 67 68 SE_DISALLOW_COPY_AND_ASSIGN(Timer); 69 }; 70 71 } // namespace stream_executor 72 73 #endif // TENSORFLOW_STREAM_EXECUTOR_TIMER_H_ 74