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 #ifndef TENSORFLOW_CORE_PROFILER_CONVERT_OP_STACK_H_ 17 #define TENSORFLOW_CORE_PROFILER_CONVERT_OP_STACK_H_ 18 19 #include <memory> 20 #include <utility> 21 #include <vector> 22 23 #include "tensorflow/core/platform/types.h" 24 25 namespace tensorflow { 26 namespace profiler { 27 28 template <typename OpInfo> 29 class OpStack { 30 public: 31 // Pushes an Op onto the stack. Push(uint32 op_id,std::unique_ptr<OpInfo> op_info)32 void Push(uint32 op_id, std::unique_ptr<OpInfo> op_info) { 33 stack_.emplace_back(op_id, std::move(op_info)); 34 } 35 36 // Pops the Op with the given op_id from the stack. Pop(uint32 op_id)37 std::unique_ptr<OpInfo> Pop(uint32 op_id) { 38 // Pop until match or stack_ is empty. 39 std::unique_ptr<OpInfo> result; 40 while (!stack_.empty()) { 41 auto back = std::move(stack_.back()); 42 stack_.pop_back(); 43 if (op_id == back.first) { 44 result = std::move(back.second); 45 break; 46 } 47 } 48 return result; 49 } 50 51 // Returns the Op at the top of the stack. Top()52 OpInfo* Top() const { 53 return stack_.empty() ? nullptr : stack_.back().second.get(); 54 } 55 56 // Returns true if the stack is empty. Empty()57 bool Empty() const { return stack_.empty(); } 58 59 // Clears the stack. Clear()60 void Clear() { stack_.clear(); } 61 62 private: 63 std::vector<std::pair<uint32 /*op_id*/, std::unique_ptr<OpInfo>>> stack_; 64 }; 65 66 } // namespace profiler 67 } // namespace tensorflow 68 69 #endif // TENSORFLOW_CORE_PROFILER_CONVERT_OP_STACK_H_ 70