2 * Copyright (c) 2018 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
24 #ifndef __ARM_COMPUTE_GRAPH_GRAPH_H__
25 #define __ARM_COMPUTE_GRAPH_GRAPH_H__
27 #include "arm_compute/graph/Edge.h"
28 #include "arm_compute/graph/INode.h"
29 #include "arm_compute/graph/Tensor.h"
30 #include "arm_compute/graph/Types.h"
32 #include "support/Mutex.h"
33 #include "support/ToolchainSupport.h"
48 * Represents a multiple source - multiple sink directed graph
56 * @param[in] id Graph identification number. Can be used to differentiate between graphs. Default value 0
57 * @param[in] name Graph name. Default value empty string
59 Graph(GraphID id, std::string name);
60 /** Prevent instances of this class from being copied (As this class contains pointers) */
61 Graph(const Graph &) = delete;
62 /** Prevent instances of this class from being copy assigned (As this class contains pointers) */
63 Graph &operator=(const Graph &) = delete;
64 /** Allow instances of this class to be moved */
65 Graph(Graph &&) = default;
66 /** Allow instances of this class to be move assigned */
67 Graph &operator=(Graph &&) = default;
68 /** Adds a node to the graph
70 * @note Models a single output node
72 * @tparam NT Node operation
73 * @tparam Ts Arguments to operation
75 * @param args Node arguments
77 * @return ID of the node
79 template <typename NT, typename... Ts>
80 NodeID add_node(Ts &&... args);
81 /** Remove the node with the given ID
83 * @param[in] nid ID of the node to remove
85 * @return True if the removal took place else false
87 bool remove_node(NodeID nid);
88 /** Adds a connection between two nodes
90 * @param[in] source ID of the source node
91 * @param[in] source_idx Output index of the source node
92 * @param[in] sink ID of the sink node
93 * @param[in] sink_idx Input index of the sink node
95 * @return ID of this connection
97 EdgeID add_connection(NodeID source, size_t source_idx, NodeID sink, size_t sink_idx);
98 /** Removes an edge (connection)
100 * @param[in] eid Connection to remove
102 * @return True if the removal took place else false
104 bool remove_connection(EdgeID eid);
105 /** Returns graph name
109 std::string name() const;
115 /** Returns graph input nodes
117 * @return vector containing the graph inputs
119 const std::vector<NodeID> &inputs();
120 /** Returns nodes of graph
122 * @warning Nodes can be nullptr if they have been removed during the mutation steps of the graph
124 * @return Nodes of graph
126 std::vector<std::unique_ptr<INode>> &nodes();
127 /** Returns nodes of graph
129 * @warning Nodes can be nullptr if they have been removed during the mutation steps of the graph
131 * @return Nodes of graph
133 const std::vector<std::unique_ptr<INode>> &nodes() const;
134 /** Returns edges of graph
136 * @warning Edges can be nullptr if they have been removed during the mutation steps of the graph
138 * @return Edges of graph
140 const std::vector<std::unique_ptr<Edge>> &edges() const;
141 /** Returns tensors of graph
143 * @warning Tensor can be nullptr if they have been removed during the mutation steps of the graph
145 * @return Tensors of graph
147 std::vector<std::unique_ptr<Tensor>> &tensors();
148 /** Returns tensors of graph
150 * @warning Tensor can be nullptr if they have been removed during the mutation steps of the graph
152 * @return Tensors of graph
154 const std::vector<std::unique_ptr<Tensor>> &tensors() const;
155 /** Get node object given its id
157 * @warning Can be nullptr if node was removed during the mutation steps of the graph
159 * @param[in] id Node ID
161 * @return The actual node object
163 const INode *node(NodeID id) const;
164 /** Get node object given its id
166 * @warning Can be nullptr if node was removed during the mutation steps of the graph
168 * @param[in] id Node ID
170 * @return The actual node object
172 INode *node(NodeID id);
173 /** Get edge object given its id
175 * @warning Can be nullptr if node was removed during the mutation steps of the graph
177 * @param[in] id Edge ID
179 * @return The actual edge object
181 const Edge *edge(EdgeID id) const;
182 /** Get edge object given its id
184 * @warning Can be nullptr if node was removed during the mutation steps of the graph
186 * @param[in] id Edge ID
188 * @return The actual edge object
190 Edge *edge(EdgeID id);
191 /** Get tensor object given its id
193 * @warning Can be nullptr if tensor was removed during the mutation steps of the graph
195 * @param[in] id Tensor ID
197 * @return The actual tensor object
199 const Tensor *tensor(TensorID id) const;
200 /** Get tensor object given its id
202 * @warning Can be nullptr if tensor was removed during the mutation steps of the graph
204 * @param[in] id Tensor ID
206 * @return The actual tensor object
208 Tensor *tensor(TensorID id);
211 /** Creates a tensor object
213 * @param[in] desc Tensor descriptor
217 TensorID create_tensor(TensorDescriptor desc = TensorDescriptor());
220 GraphID _id = GraphID(0); /**< Graph id */
221 std::string _name = {}; /**< Graph name */
222 std::vector<std::unique_ptr<INode>> _nodes = {}; /**< Graph nodes */
223 std::vector<std::unique_ptr<Edge>> _edges = {}; /**< Graph edges */
224 std::vector<std::unique_ptr<Tensor>> _tensors = {}; /**< Graph tensors */
225 std::map<NodeType, std::vector<NodeID>> _tagged_nodes = {}; /**< Graph nodes map with the node type as key */
226 arm_compute::Mutex _mtx = {}; /**< Mutex used for graph construction */
229 template <typename NT, typename... Ts>
230 inline NodeID Graph::add_node(Ts &&... args)
232 std::lock_guard<arm_compute::Mutex> lock(_mtx);
235 NodeID nid = _nodes.size();
236 auto node = support::cpp14::make_unique<NT>(std::forward<Ts>(args)...);
237 node->set_graph(this);
240 // Keep track of input nodes
241 if(node->type() == NodeType::Input)
243 _tagged_nodes[NodeType::Input].push_back(nid);
246 // Associate a new tensor with each output
247 for(auto &output : node->_outputs)
249 output = create_tensor();
252 // Propagate node shape if possible
253 node->forward_descriptors();
255 // Add node to the graph nodes
256 _nodes.push_back(std::move(node));
261 } // namespace arm_compute
262 #endif /* __ARM_COMPUTE_GRAPH_GRAPH_H__ */