blob: 525506f316e60e6e26052e2cc744cff0d544794f [file] [log] [blame]
Kaizen8938bd32017-09-28 14:38:23 +01001/*
2 * Copyright (c) 2017 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 */
24#include "arm_compute/graph/Graph.h"
25
26#include "arm_compute/graph/CL/CLMap.h"
27#include "arm_compute/graph/CL/CLUnmap.h"
28#include "arm_compute/graph/INode.h"
29#include "arm_compute/graph/Tensor.h"
30#include "arm_compute/runtime/CL/CLTensor.h"
31#include "arm_compute/runtime/Tensor.h"
32
33using namespace arm_compute::graph;
34
35struct Stage
36{
37 Tensor *_input;
38 Tensor *_output;
39 std::unique_ptr<arm_compute::IFunction> _function;
40};
41
42struct Graph::Private
43{
44public:
45 /** Finalizes the current node's configuration
46 *
47 * @param _next_hint Device execution hint
48 */
49 void configure(Hint _next_hint);
50
51 /** Sets whether to enable information print out
52 *
53 * @param[in] is_enabled Set to true if need info printed out
54 */
55 void set_info_enablement(bool is_enabled);
56
57 std::vector<Stage> _pipeline{};
58 std::vector<std::unique_ptr<Tensor>> _tensors{};
59 std::vector<std::unique_ptr<INode>> _nodes{};
60 Hint _current_hint{ Hint::DONT_CARE };
61 Hint _next_hint{ Hint::DONT_CARE };
62 std::unique_ptr<Tensor> _graph_input{ nullptr };
63 std::unique_ptr<Tensor> _graph_output{ nullptr };
64 std::unique_ptr<INode> _current_node{ nullptr };
65 Tensor *_current_output{ nullptr };
66 bool _info_enabled{ false };
67
68private:
69 Tensor *_current_input{ nullptr };
70 Hint _previous_hint{ Hint::DONT_CARE };
71};
72
73Graph::~Graph() //NOLINT
74{
75 //Can't use =default because the destructor must be defined after Graph::Private's definition
76}
77
78Graph::Graph()
79 : _pimpl{ new Private() }
80{
81}
82
83void Graph::run()
84{
85 while(true)
86 {
87 if(!_pimpl->_graph_input->call_accessor())
88 {
89 return;
90 }
91
92 for(auto &stage : _pimpl->_pipeline)
93 {
94 stage._function->run();
95 }
96
97 if(!_pimpl->_graph_output->call_accessor())
98 {
99 return;
100 }
101 }
102}
103
104//Finalize current node's configuration
105void Graph::Private::configure(Hint _next_hint)
106{
107 ARM_COMPUTE_ERROR_ON(_current_node == nullptr);
108 ARM_COMPUTE_ERROR_ON(_graph_input == nullptr);
109
110 // Is it the first node of the graph ?
111 if(_current_input == nullptr)
112 {
113 _graph_input->set_target(_current_hint);
114 _current_input = _graph_input.get();
115 _previous_hint = _current_hint; // For the first node just assume the previous node was of the same type as this one
116 }
117
118 //Automatic output configuration ?
119 if(_current_output == nullptr)
120 {
121 _tensors.push_back(arm_compute::support::cpp14::make_unique<Tensor>(TensorInfo()));
122 _current_output = _tensors.back().get();
123 }
124
125 // If either the writer or reader node needs OpenCL then use OpenCL memory:
126 if((_next_hint == Hint::OPENCL || _current_hint == Hint::OPENCL))
127 {
128 _current_output->set_target(Hint::OPENCL);
129 }
130 else
131 {
132 _current_output->set_target(Hint::NEON);
133 }
134
135 // Map input if needed
136 std::unique_ptr<arm_compute::IFunction> func = _current_node->instantiate_node(_current_hint, _current_input->tensor(), _current_output->tensor());
137 _current_input->allocate();
138
139 if(_current_input->target() == Hint::OPENCL)
140 {
141 if(_previous_hint == Hint::NEON)
142 {
143 ARM_COMPUTE_ERROR_ON(_current_hint == Hint::NEON);
144 _pipeline.push_back({ _current_input, _current_input, arm_compute::support::cpp14::make_unique<CLUnmap>(_current_input) });
145 }
146 if(_current_hint == Hint::NEON)
147 {
148 ARM_COMPUTE_ERROR_ON(_previous_hint == Hint::NEON);
149 _pipeline.push_back({ _current_input, _current_input, arm_compute::support::cpp14::make_unique<CLMap>(_current_input, true) });
150 }
151 }
152
153 _pipeline.push_back({ _current_input, _current_output, std::move(func) });
154
155 _current_input = _current_output;
156 _current_output = nullptr;
157 _previous_hint = _current_hint;
158 _current_hint = _next_hint;
159}
160
161void Graph::Private::set_info_enablement(bool is_enabled)
162{
163 _info_enabled = is_enabled;
164}
165
166void Graph::add_node(std::unique_ptr<INode> node)
167{
168 ARM_COMPUTE_ERROR_ON_MSG(_pimpl->_graph_input == nullptr, "The graph's input must be set before the first node is added");
169 ARM_COMPUTE_ERROR_ON_MSG(_pimpl->_graph_output != nullptr, "Nothing can be added after the output tensor");
170 //Trigger the creation of the current Node:
171
172 Hint _next_hint = node->override_hint(_pimpl->_next_hint);
173 ARM_COMPUTE_ERROR_ON(_next_hint == Hint::DONT_CARE);
174 if(_pimpl->_current_node)
175 {
176 //Finalize the previous Node:
177 _pimpl->configure(_pimpl->_next_hint);
178
179 if(_pimpl->_info_enabled)
180 {
181 _pimpl->_current_node->print_info();
182 }
183 }
184 else
185 {
186 // If that's the first node then use the same Hint before and after the node.
187 _pimpl->_current_hint = _next_hint;
188 }
189 if(_pimpl->_current_node)
190 {
191 _pimpl->_nodes.push_back(std::move(_pimpl->_current_node));
192 }
193 _pimpl->_current_node = std::move(node);
194}
195void Graph::set_hint(Hint hint)
196{
197 _pimpl->_next_hint = hint;
198}
199
200void Graph::set_info_enablement(bool is_enabled)
201{
202 _pimpl->set_info_enablement(is_enabled);
203}
204
205//Add a tensor with an Accessor (i.e either the input or output of the graph)
206void Graph::add_tensor(std::unique_ptr<Tensor> tensor)
207{
208 // If it's the first Tensor added then it will be the input of the Graph.
209 if(_pimpl->_graph_input == nullptr)
210 {
211 ARM_COMPUTE_ERROR_ON(_pimpl->_graph_output != nullptr);
212 ARM_COMPUTE_ERROR_ON(_pimpl->_current_node != nullptr);
213 _pimpl->_graph_input = std::move(tensor);
214 }
215 else
216 {
217 // Else it will be the output of the Graph
218 ARM_COMPUTE_ERROR_ON(_pimpl->_graph_output != nullptr);
219 ARM_COMPUTE_ERROR_ON(_pimpl->_current_node == nullptr);
220 _pimpl->_graph_output = std::move(tensor);
221 _pimpl->_current_output = _pimpl->_graph_output.get();
222
223 // Finalize the graph by configuring the last Node of the graph:
224 _pimpl->configure(_pimpl->_current_hint); // Ignore _next_hint as this is the last node, and just use the same hint as before this node.
225 _pimpl->_graph_output->allocate();
226 }
227}
228
229void Graph::set_temp(TensorInfo &&tmp)
230{
231 ARM_COMPUTE_ERROR_ON(_pimpl->_graph_input == nullptr);
232 ARM_COMPUTE_ERROR_ON(_pimpl->_graph_output != nullptr);
233 ARM_COMPUTE_ERROR_ON_MSG(_pimpl->_current_output != nullptr, "TensorInfo for temporary tensor already set");
234
235 _pimpl->_tensors.push_back(arm_compute::support::cpp14::make_unique<Tensor>(std::move(tmp)));
236 _pimpl->_current_output = _pimpl->_tensors.back().get();
237}
238
239Graph &arm_compute::graph::operator<<(Graph &graph, TensorInfo &&info)
240{
241 graph.set_temp(std::move(info));
242 return graph;
243}
244
245Graph &arm_compute::graph::operator<<(Graph &graph, Tensor &&tensor)
246{
247 graph.add_tensor(arm_compute::support::cpp14::make_unique<Tensor>(std::move(tensor)));
248 return graph;
249}
250
251Graph &arm_compute::graph::operator<<(Graph &graph, Hint hint)
252{
253 graph.set_hint(hint);
254 return graph;
255}