1 /* Copyright 2017 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_GRAPPLER_UTILS_TOPOLOGICAL_SORT_H_ 17 #define TENSORFLOW_CORE_GRAPPLER_UTILS_TOPOLOGICAL_SORT_H_ 18 19 #include "absl/types/span.h" 20 #include "tensorflow/core/framework/graph.pb.h" 21 #include "tensorflow/core/lib/core/status.h" 22 23 namespace tensorflow { 24 namespace grappler { 25 26 // TODO(ezhulenev, b/121379902): We should be consistent with GraphTopologyView 27 // and use `GraphView::Edge` to pass extra dependencies. 28 struct TopologicalDependency { TopologicalDependencyTopologicalDependency29 TopologicalDependency(const NodeDef* from, const NodeDef* to) 30 : from(from), to(to) {} 31 const NodeDef* from; 32 const NodeDef* to; 33 }; 34 35 // Computes a topological ordering for the graph nodes and outputs nodes in the 36 // topological order to the `topo_order` output argument. 37 // 38 // It's possible to pass additional edges that do not exists in a graph, but 39 // must be respected when computing graph topological order. Example: Tensorflow 40 // runtime allows concurrent execution of dequeue/enqueue ops from the same 41 // queue resource, but we might want to enforce ordering between them. 42 Status ComputeTopologicalOrder( 43 const GraphDef& graph, 44 absl::Span<const TopologicalDependency> extra_dependencies, 45 std::vector<const NodeDef*>* topo_order); 46 Status ComputeTopologicalOrder(const GraphDef& graph, 47 std::vector<const NodeDef*>* topo_order); 48 49 // Sorts a graph in topological order. 50 Status TopologicalSort(GraphDef* graph); 51 52 // Sorts a graph in topological order and reverse it. 53 Status ReversedTopologicalSort(GraphDef* graph); 54 55 } // namespace grappler 56 } // namespace tensorflow 57 58 #endif // TENSORFLOW_CORE_GRAPPLER_UTILS_TOPOLOGICAL_SORT_H_ 59