Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 ARM Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame^] | 24 | #ifndef __ARM_COMPUTE_GRAPH_ALGORITHM_BFS_H__ |
| 25 | #define __ARM_COMPUTE_GRAPH_ALGORITHM_BFS_H__ |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 26 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame^] | 27 | #include "arm_compute/graph/Graph.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 28 | |
| 29 | #include <list> |
| 30 | #include <vector> |
| 31 | |
| 32 | namespace arm_compute |
| 33 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame^] | 34 | namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 35 | { |
| 36 | namespace detail |
| 37 | { |
| 38 | /** Checks if all the input dependencies of a node have been visited |
| 39 | * |
| 40 | * @param[in] node Node to check |
| 41 | * @param[in] visited Vector that contains the visited information |
| 42 | * |
| 43 | * @return True if all inputs dependencies have been visited else false |
| 44 | */ |
| 45 | inline bool all_inputs_are_visited(const INode *node, const std::vector<bool> &visited) |
| 46 | { |
| 47 | ARM_COMPUTE_ERROR_ON(node == nullptr); |
| 48 | const Graph *graph = node->graph(); |
| 49 | ARM_COMPUTE_ERROR_ON(graph == nullptr); |
| 50 | |
| 51 | bool are_all_visited = true; |
| 52 | for(const auto &input_edge_id : node->input_edges()) |
| 53 | { |
| 54 | if(input_edge_id != EmptyNodeID) |
| 55 | { |
| 56 | const Edge *input_edge = graph->edge(input_edge_id); |
| 57 | ARM_COMPUTE_ERROR_ON(input_edge == nullptr); |
| 58 | ARM_COMPUTE_ERROR_ON(input_edge->producer() == nullptr); |
| 59 | if(!visited[input_edge->producer_id()]) |
| 60 | { |
| 61 | are_all_visited = false; |
| 62 | break; |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return are_all_visited; |
| 68 | } |
| 69 | } // namespace detail |
| 70 | |
| 71 | /** Breadth first search traversal |
| 72 | * |
| 73 | * @param g Graph to traverse |
| 74 | * |
| 75 | * @return A vector with the node id traversal order |
| 76 | */ |
| 77 | inline std::vector<NodeID> bfs(Graph &g) |
| 78 | { |
| 79 | std::vector<NodeID> bfs_order_vector; |
| 80 | |
| 81 | // Created visited vector |
| 82 | std::vector<bool> visited(g.nodes().size(), false); |
| 83 | |
| 84 | // Create BFS queue |
| 85 | std::list<NodeID> queue; |
| 86 | |
| 87 | // Push inputs and mark as visited |
| 88 | for(auto &input : g.inputs()) |
| 89 | { |
| 90 | if(input != EmptyNodeID) |
| 91 | { |
| 92 | visited[input] = true; |
| 93 | queue.push_back(input); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // Iterate over vector and edges |
| 98 | while(!queue.empty()) |
| 99 | { |
| 100 | // Dequeue a node from queue and process |
| 101 | NodeID n = queue.front(); |
| 102 | bfs_order_vector.push_back(n); |
| 103 | queue.pop_front(); |
| 104 | |
| 105 | const INode *node = g.node(n); |
| 106 | ARM_COMPUTE_ERROR_ON(node == nullptr); |
| 107 | for(const auto &eid : node->output_edges()) |
| 108 | { |
| 109 | const Edge *e = g.edge(eid); |
| 110 | ARM_COMPUTE_ERROR_ON(e == nullptr); |
| 111 | if(!visited[e->consumer_id()] && detail::all_inputs_are_visited(e->consumer(), visited)) |
| 112 | { |
| 113 | visited[e->consumer_id()] = true; |
| 114 | queue.push_back(e->consumer_id()); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return bfs_order_vector; |
| 120 | } |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame^] | 121 | } // namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 122 | } // namespace arm_compute |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame^] | 123 | #endif /* __ARM_COMPUTE_GRAPH_ALGORITHM_BFS_H__ */ |