blob: 2a776826e58cb4d778455951434b785426d7efb4 [file] [log] [blame]
Anthony Barbier2a07e182017-08-04 18:20:27 +01001/*
Georgios Pinitasd9eb2752018-04-03 13:44:29 +01002 * Copyright (c) 2018 ARM Limited.
Anthony Barbier2a07e182017-08-04 18:20:27 +01003 *
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 */
24#ifndef __ARM_COMPUTE_GRAPH_GRAPH_H__
25#define __ARM_COMPUTE_GRAPH_GRAPH_H__
26
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010027#include "arm_compute/graph/Edge.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010028#include "arm_compute/graph/INode.h"
29#include "arm_compute/graph/Tensor.h"
30#include "arm_compute/graph/Types.h"
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010031
32#include "support/Mutex.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010033#include "support/ToolchainSupport.h"
34
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010035#include <map>
Anthony Barbier2a07e182017-08-04 18:20:27 +010036#include <memory>
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010037#include <string>
38#include <thread>
39#include <utility>
40#include <vector>
Anthony Barbier2a07e182017-08-04 18:20:27 +010041
42namespace arm_compute
43{
Anthony Barbier2a07e182017-08-04 18:20:27 +010044namespace graph
45{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010046/** Graph class
47 *
48 * Represents a multiple source - multiple sink directed graph
49 */
Anthony Barbier2a07e182017-08-04 18:20:27 +010050class Graph final
51{
52public:
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010053 Graph() = default;
54 /** Constructor
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +000055 *
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010056 * @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
Michele Di Giorgioe3fba0a2018-02-14 14:18:01 +000058 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010059 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;
Anthony Barbier2a07e182017-08-04 18:20:27 +010068 /** Adds a node to the graph
69 *
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010070 * @note Models a single output node
Anthony Barbier2a07e182017-08-04 18:20:27 +010071 *
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010072 * @tparam NT Node operation
73 * @tparam Ts Arguments to operation
Anthony Barbier2a07e182017-08-04 18:20:27 +010074 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010075 * @param[in] args Node arguments
Georgios Pinitasff421f22017-10-04 16:53:58 +010076 *
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010077 * @return ID of the node
Georgios Pinitasff421f22017-10-04 16:53:58 +010078 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010079 template <typename NT, typename... Ts>
80 NodeID add_node(Ts &&... args);
81 /** Remove the node with the given ID
82 *
83 * @param[in] nid ID of the node to remove
84 *
85 * @return True if the removal took place else false
86 */
87 bool remove_node(NodeID nid);
88 /** Adds a connection between two nodes
89 *
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
94 *
95 * @return ID of this connection
96 */
97 EdgeID add_connection(NodeID source, size_t source_idx, NodeID sink, size_t sink_idx);
98 /** Removes an edge (connection)
99 *
100 * @param[in] eid Connection to remove
101 *
102 * @return True if the removal took place else false
103 */
104 bool remove_connection(EdgeID eid);
105 /** Returns graph name
106 *
107 * @return Graph name
108 */
109 std::string name() const;
110 /** Returns graph id
111 *
112 * @return Graph id
113 */
114 GraphID id() const;
115 /** Returns graph input nodes
116 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100117 * @param[in] type Type of nodes to return
118 *
119 * @return vector containing the graph node of given type
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100120 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100121 const std::vector<NodeID> &nodes(NodeType type);
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100122 /** Returns nodes of graph
123 *
124 * @warning Nodes can be nullptr if they have been removed during the mutation steps of the graph
125 *
126 * @return Nodes of graph
127 */
128 std::vector<std::unique_ptr<INode>> &nodes();
129 /** Returns nodes of graph
130 *
131 * @warning Nodes can be nullptr if they have been removed during the mutation steps of the graph
132 *
133 * @return Nodes of graph
134 */
135 const std::vector<std::unique_ptr<INode>> &nodes() const;
136 /** Returns edges of graph
137 *
138 * @warning Edges can be nullptr if they have been removed during the mutation steps of the graph
139 *
140 * @return Edges of graph
141 */
142 const std::vector<std::unique_ptr<Edge>> &edges() const;
143 /** Returns tensors of graph
144 *
145 * @warning Tensor can be nullptr if they have been removed during the mutation steps of the graph
146 *
147 * @return Tensors of graph
148 */
149 std::vector<std::unique_ptr<Tensor>> &tensors();
150 /** Returns tensors of graph
151 *
152 * @warning Tensor can be nullptr if they have been removed during the mutation steps of the graph
153 *
154 * @return Tensors of graph
155 */
156 const std::vector<std::unique_ptr<Tensor>> &tensors() const;
157 /** Get node object given its id
158 *
159 * @warning Can be nullptr if node was removed during the mutation steps of the graph
160 *
161 * @param[in] id Node ID
162 *
163 * @return The actual node object
164 */
165 const INode *node(NodeID id) const;
166 /** Get node object given its id
167 *
168 * @warning Can be nullptr if node was removed during the mutation steps of the graph
169 *
170 * @param[in] id Node ID
171 *
172 * @return The actual node object
173 */
174 INode *node(NodeID id);
175 /** Get edge object given its id
176 *
177 * @warning Can be nullptr if node was removed during the mutation steps of the graph
178 *
179 * @param[in] id Edge ID
180 *
181 * @return The actual edge object
182 */
183 const Edge *edge(EdgeID id) const;
184 /** Get edge object given its id
185 *
186 * @warning Can be nullptr if node was removed during the mutation steps of the graph
187 *
188 * @param[in] id Edge ID
189 *
190 * @return The actual edge object
191 */
192 Edge *edge(EdgeID id);
193 /** Get tensor object given its id
194 *
195 * @warning Can be nullptr if tensor was removed during the mutation steps of the graph
196 *
197 * @param[in] id Tensor ID
198 *
199 * @return The actual tensor object
200 */
201 const Tensor *tensor(TensorID id) const;
202 /** Get tensor object given its id
203 *
204 * @warning Can be nullptr if tensor was removed during the mutation steps of the graph
205 *
206 * @param[in] id Tensor ID
207 *
208 * @return The actual tensor object
209 */
210 Tensor *tensor(TensorID id);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100211
212private:
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100213 /** Creates a tensor object
214 *
215 * @param[in] desc Tensor descriptor
216 *
217 * @return Tensor ID
218 */
219 TensorID create_tensor(TensorDescriptor desc = TensorDescriptor());
220
221private:
222 GraphID _id = GraphID(0); /**< Graph id */
223 std::string _name = {}; /**< Graph name */
224 std::vector<std::unique_ptr<INode>> _nodes = {}; /**< Graph nodes */
225 std::vector<std::unique_ptr<Edge>> _edges = {}; /**< Graph edges */
226 std::vector<std::unique_ptr<Tensor>> _tensors = {}; /**< Graph tensors */
227 std::map<NodeType, std::vector<NodeID>> _tagged_nodes = {}; /**< Graph nodes map with the node type as key */
228 arm_compute::Mutex _mtx = {}; /**< Mutex used for graph construction */
Anthony Barbier2a07e182017-08-04 18:20:27 +0100229};
230
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100231template <typename NT, typename... Ts>
232inline NodeID Graph::add_node(Ts &&... args)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100233{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100234 std::lock_guard<arm_compute::Mutex> lock(_mtx);
235
236 // Create node
237 NodeID nid = _nodes.size();
238 auto node = support::cpp14::make_unique<NT>(std::forward<Ts>(args)...);
239 node->set_graph(this);
240 node->set_id(nid);
241
242 // Keep track of input nodes
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100243 _tagged_nodes[node->type()].push_back(nid);
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100244
245 // Associate a new tensor with each output
246 for(auto &output : node->_outputs)
247 {
248 output = create_tensor();
249 }
250
251 // Propagate node shape if possible
252 node->forward_descriptors();
253
254 // Add node to the graph nodes
255 _nodes.push_back(std::move(node));
256
257 return nid;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100258}
259} // namespace graph
260} // namespace arm_compute
261#endif /* __ARM_COMPUTE_GRAPH_GRAPH_H__ */