telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | #pragma once |
| 6 | |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 7 | #include <armnn/Optional.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 8 | #include <boost/assert.hpp> |
| 9 | |
| 10 | #include <functional> |
| 11 | #include <map> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 12 | #include <stack> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 13 | #include <vector> |
| 14 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 15 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 16 | namespace armnnUtils |
| 17 | { |
| 18 | |
| 19 | namespace |
| 20 | { |
| 21 | |
| 22 | enum class NodeState |
| 23 | { |
| 24 | Visiting, |
| 25 | Visited, |
| 26 | }; |
| 27 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 28 | |
| 29 | template <typename TNodeId> |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 30 | armnn::Optional<TNodeId> GetNextChild(TNodeId node, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 31 | std::function<std::vector<TNodeId>(TNodeId)> getIncomingEdges, |
| 32 | std::map<TNodeId, NodeState>& nodeStates) |
| 33 | { |
| 34 | for (TNodeId childNode : getIncomingEdges(node)) |
| 35 | { |
| 36 | if (nodeStates.find(childNode) == nodeStates.end()) |
| 37 | { |
| 38 | return childNode; |
| 39 | } |
| 40 | else |
| 41 | { |
| 42 | if (nodeStates.find(childNode)->second == NodeState::Visiting) |
| 43 | { |
| 44 | return childNode; |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | return {}; |
| 50 | } |
| 51 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 52 | template<typename TNodeId> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 53 | bool TopologicallySort( |
| 54 | TNodeId initialNode, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 55 | std::function<std::vector<TNodeId>(TNodeId)> getIncomingEdges, |
| 56 | std::vector<TNodeId>& outSorted, |
| 57 | std::map<TNodeId, NodeState>& nodeStates) |
| 58 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 59 | std::stack<TNodeId> nodeStack; |
| 60 | |
| 61 | // If the node is never visited we should search it |
| 62 | if (nodeStates.find(initialNode) == nodeStates.end()) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 63 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 64 | nodeStack.push(initialNode); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 65 | } |
| 66 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 67 | while (!nodeStack.empty()) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 68 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 69 | TNodeId current = nodeStack.top(); |
| 70 | |
| 71 | nodeStates[current] = NodeState::Visiting; |
| 72 | |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 73 | auto nextChildOfCurrent = GetNextChild(current, getIncomingEdges, nodeStates); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 74 | |
| 75 | if (nextChildOfCurrent) |
| 76 | { |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 77 | TNodeId nextChild = nextChildOfCurrent.value(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 78 | |
| 79 | // If the child has not been searched, add to the stack and iterate over this node |
| 80 | if (nodeStates.find(nextChild) == nodeStates.end()) |
| 81 | { |
| 82 | nodeStack.push(nextChild); |
| 83 | continue; |
| 84 | } |
| 85 | |
| 86 | // If we re-encounter a node being visited there is a cycle |
| 87 | if (nodeStates[nextChild] == NodeState::Visiting) |
| 88 | { |
| 89 | return false; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | nodeStack.pop(); |
| 94 | |
| 95 | nodeStates[current] = NodeState::Visited; |
| 96 | outSorted.push_back(current); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 97 | } |
| 98 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 99 | return true; |
| 100 | } |
| 101 | |
| 102 | } |
| 103 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 104 | // Sorts a directed acyclic graph (DAG) into a flat list such that all inputs to a node are before the node itself. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 105 | // Returns true if successful or false if there is an error in the graph structure (e.g. it contains a cycle). |
| 106 | // The graph is defined entirely by the "getIncomingEdges" function which the user provides. For a given node, |
| 107 | // it must return the list of nodes which are required to come before it. |
| 108 | // "targetNodes" is the list of nodes where the search begins - i.e. the nodes that you want to evaluate. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 109 | // This is an iterative implementation based on https://en.wikipedia.org/wiki/Topological_sorting#Depth-first_search |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 110 | template<typename TNodeId, typename TTargetNodes> |
| 111 | bool GraphTopologicalSort( |
| 112 | const TTargetNodes& targetNodes, |
| 113 | std::function<std::vector<TNodeId>(TNodeId)> getIncomingEdges, |
| 114 | std::vector<TNodeId>& outSorted) |
| 115 | { |
| 116 | outSorted.clear(); |
| 117 | std::map<TNodeId, NodeState> nodeStates; |
| 118 | |
| 119 | for (TNodeId targetNode : targetNodes) |
| 120 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 121 | if (!TopologicallySort(targetNode, getIncomingEdges, outSorted, nodeStates)) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 122 | { |
| 123 | return false; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | } |