Compute Library
 21.05
INode.cpp
Go to the documentation of this file.
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  */
25 
26 #include "arm_compute/core/Error.h"
27 #include "arm_compute/graph/Edge.h"
30 
31 namespace arm_compute
32 {
33 namespace graph
34 {
35 // *INDENT-OFF*
36 // clang-format off
38  : _graph(nullptr), _id(EmptyNodeID), _common_params({ "", Target::UNSPECIFIED}),
39  _outputs(), _input_edges(), _output_edges(), _assigned_target(Target::UNSPECIFIED)
40 {
41 }
42 // clang-format on
43 // *INDENT-ON*
44 
46 {
47  return Status{};
48 }
49 
51 {
52  ARM_COMPUTE_ERROR_ON(g == nullptr);
53  _graph = g;
54 }
55 
57 {
58  _id = id;
59 }
60 
62 {
63  _common_params = std::move(common_params);
64 }
65 
67 {
68  _common_params.target = target;
69 }
70 
72 {
73  _assigned_target = target;
74 }
75 
76 void INode::set_output_tensor(TensorID tid, size_t idx)
77 {
78  if(tid != NullTensorID && (idx < _outputs.size()) && (_graph->tensor(tid) != nullptr))
79  {
80  ARM_COMPUTE_ERROR_ON(_graph == nullptr);
81  Tensor *updated_tensor = _graph->tensor(tid);
82  _outputs[idx] = tid;
83 
84  // Set tensor to all output edges of the node
85  for(auto &output_edge_id : _output_edges)
86  {
87  auto output_edge = _graph->edge(output_edge_id);
88  if(output_edge != nullptr)
89  {
90  // Unbind edge from current tensor
91  auto current_output_tensor = output_edge->tensor();
92  current_output_tensor->unbind_edge(output_edge->id());
93 
94  // Update tensor to edge and rebind tensor
95  output_edge->update_bound_tensor(updated_tensor);
96  updated_tensor->bind_edge(output_edge->id());
97  }
98  }
99  }
100 }
101 
103 {
104  return _id;
105 }
106 
107 std::string INode::name() const
108 {
109  return _common_params.name;
110 }
111 
112 const Graph *INode::graph() const
113 {
114  return _graph;
115 }
116 
118 {
119  return _graph;
120 }
121 
122 const std::vector<TensorID> &INode::outputs() const
123 {
124  return _outputs;
125 }
126 
127 const std::vector<EdgeID> &INode::input_edges() const
128 {
129  return _input_edges;
130 }
131 
132 const std::set<EdgeID> &INode::output_edges() const
133 {
134  return _output_edges;
135 }
136 
137 TensorID INode::input_id(size_t idx) const
138 {
139  ARM_COMPUTE_ERROR_ON(idx >= _input_edges.size());
140  Edge *e = _graph->edge(_input_edges[idx]);
141  return (e != nullptr) ? e->tensor_id() : NullTensorID;
142 }
143 
144 TensorID INode::output_id(size_t idx) const
145 {
146  ARM_COMPUTE_ERROR_ON(idx >= _outputs.size());
147  return _outputs[idx];
148 }
149 
150 Tensor *INode::input(size_t idx) const
151 {
152  ARM_COMPUTE_ERROR_ON(_graph == nullptr);
153  ARM_COMPUTE_ERROR_ON(idx >= _input_edges.size());
154  Edge *e = _graph->edge(_input_edges[idx]);
155  return (e != nullptr) ? e->tensor() : nullptr;
156 }
157 
158 Tensor *INode::output(size_t idx) const
159 {
160  ARM_COMPUTE_ERROR_ON(_graph == nullptr);
161  ARM_COMPUTE_ERROR_ON(idx >= _outputs.size());
162  return _graph->tensor(_outputs[idx]);
163 }
164 
165 EdgeID INode::input_edge_id(size_t idx) const
166 {
167  ARM_COMPUTE_ERROR_ON(idx >= _input_edges.size());
168  return _input_edges[idx];
169 }
170 
171 Edge *INode::input_edge(size_t idx) const
172 {
173  ARM_COMPUTE_ERROR_ON(_graph == nullptr);
174  ARM_COMPUTE_ERROR_ON(idx >= _input_edges.size());
175  return _graph->edge(_input_edges[idx]);
176 }
177 
178 size_t INode::num_inputs() const
179 {
180  return _input_edges.size();
181 }
182 
183 size_t INode::num_outputs() const
184 {
185  return _outputs.size();
186 }
187 
189 {
190  return _common_params;
191 }
192 
194 {
195  return _common_params.target;
196 }
197 
199 {
200  return _assigned_target;
201 }
202 } // namespace graph
203 } // namespace arm_compute
Edge * input_edge(size_t idx) const
Returns the edge of a given input of the node.
Definition: INode.cpp:171
Common node parameters.
Definition: Types.h:217
void set_assigned_target(Target target)
Sets the final execution target.
Definition: INode.cpp:71
std::string name() const
Returns node's name.
Definition: INode.cpp:107
const std::set< EdgeID > & output_edges() const
Returns output edge set.
Definition: INode.cpp:132
Target assigned_target() const
Returns assigned target for this node.
Definition: INode.cpp:198
Tensor * tensor() const
Returns the tensor associated with this edge.
Definition: Edge.h:116
TensorID tensor_id() const
Returns the tensor id associated with this edge.
Definition: Edge.h:124
size_t num_outputs() const
Returns number of outputs of the node.
Definition: INode.cpp:183
const Graph * graph() const
Returns node's Graph.
Definition: INode.cpp:112
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
Status class.
Definition: Error.h:52
EdgeID input_edge_id(size_t idx) const
Returns the edge ID of a given input of the node.
Definition: INode.cpp:165
Target target
Node target.
Definition: Types.h:220
Copyright (c) 2017-2021 Arm Limited.
void set_requested_target(Target target)
Sets target preference.
Definition: INode.cpp:66
INode()
Constructor.
Definition: INode.cpp:37
void bind_edge(EdgeID eid)
Binds the tensor with an edge.
Definition: Tensor.cpp:101
void set_graph(Graph *g)
Sets the graph that this node is registered to.
Definition: INode.cpp:50
const std::vector< EdgeID > & input_edges() const
Returns input edge set.
Definition: INode.cpp:127
Tensor * output(size_t idx) const
Returns the tensor of a given output of the node.
Definition: INode.cpp:158
Target requested_target() const
Returns requested target for this node.
Definition: INode.cpp:193
NodeID id() const
Returns node's ID.
Definition: INode.cpp:102
TensorID input_id(size_t idx) const
Returns the tensor ID of a given input of the node.
Definition: INode.cpp:137
const std::vector< TensorID > & outputs() const
Returns outputs of the node.
Definition: INode.cpp:122
void unbind_edge(EdgeID eid)
Unbinds an edge from a tensor.
Definition: Tensor.cpp:106
void set_id(NodeID id)
Sets the node id.
Definition: INode.cpp:56
void set_common_node_parameters(NodeParams common_params)
Sets common node parameters.
Definition: INode.cpp:61
unsigned int EdgeID
Definition: Types.h:69
Graph class.
Definition: Graph.h:53
NodeParams common_node_params() const
Returns common node parameters.
Definition: INode.cpp:188
unsigned int NodeID
Definition: Types.h:68
Graph Edge.
Definition: Edge.h:39
constexpr NodeID EmptyNodeID
Constant EdgeID specifying an equivalent of null edge.
Definition: Types.h:75
std::string name
Node name.
Definition: Types.h:219
virtual Status validate() const
Validate node.
Definition: INode.cpp:45
size_t num_inputs() const
Returns number of inputs of the node.
Definition: INode.cpp:178
const Edge * edge(EdgeID id) const
Get edge object given its id.
Definition: Graph.cpp:214
TensorID output_id(size_t idx) const
Returns the tensor ID of a given output of the node.
Definition: INode.cpp:144
void set_output_tensor(TensorID tid, size_t idx)
Sets the output tensor of at a given index.
Definition: INode.cpp:76
constexpr TensorID NullTensorID
Constant NodeID specifying an equivalent of null node.
Definition: Types.h:73
Tensor * input(size_t idx) const
Returns the tensor of a given input of the node.
Definition: INode.cpp:150
unsigned int TensorID
Definition: Types.h:67
const Tensor * tensor(TensorID id) const
Get tensor object given its id.
Definition: Graph.cpp:224
Tensor object.
Definition: Tensor.h:41