blob: 6df67fcfec62f87bf919ac7038f7593e406c8240 [file] [log] [blame]
Georgios Pinitasd8734b52017-12-22 15:27:52 +00001/*
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 Pinitasd9eb2752018-04-03 13:44:29 +010024#include "arm_compute/graph/detail/ExecutionHelpers.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000025
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010026#include "arm_compute/graph/Graph.h"
27#include "arm_compute/graph/GraphContext.h"
28#include "arm_compute/graph/GraphManager.h"
29#include "arm_compute/graph/Tensor.h"
30#include "arm_compute/graph/backends/BackendRegistry.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000031
32namespace arm_compute
33{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010034namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +000035{
36namespace detail
37{
Georgios Pinitascac13b12018-04-27 19:07:19 +010038void validate_all_nodes(Graph &g)
39{
40 auto &nodes = g.nodes();
41
42 // Create tasks
43 for(auto &node : nodes)
44 {
45 if(node != nullptr)
46 {
47 Target assigned_target = node->assigned_target();
48 auto backend = backends::BackendRegistry::get().find_backend(assigned_target);
49 ARM_COMPUTE_ERROR_ON_MSG(!backend, "Requested backend doesn't exist!");
50 Status status = backend->validate_node(*node);
51 ARM_COMPUTE_ERROR_ON_MSG(!bool(status), status.error_description().c_str());
52 }
53 }
54}
55
Georgios Pinitasd8734b52017-12-22 15:27:52 +000056void configure_all_tensors(Graph &g)
57{
58 auto &tensors = g.tensors();
59
60 for(auto &tensor : tensors)
61 {
62 if(tensor)
63 {
64 Target target = tensor->desc().target;
65 auto backend = backends::BackendRegistry::get().find_backend(target);
66 ARM_COMPUTE_ERROR_ON_MSG(!backend, "Requested backend doesn't exist!");
67 auto handle = backend->create_tensor(*tensor);
68 ARM_COMPUTE_ERROR_ON_MSG(!backend, "Couldn't create backend handle!");
69 tensor->set_handle(std::move(handle));
70 }
71 }
72}
73
Georgios Pinitase0437672018-05-02 14:07:55 +010074void allocate_all_input_tensors(INode &node)
75{
76 for(unsigned int i = 0; i < node.num_inputs(); ++i)
77 {
78 Tensor *tensor = node.input(i);
79 if(tensor != nullptr && !tensor->bound_edges().empty())
80 {
81 ARM_COMPUTE_ERROR_ON_MSG(!tensor->handle(), "Tensor handle is not configured!");
82 tensor->handle()->allocate();
83 }
84 }
85}
86
87void allocate_all_output_tensors(INode &node)
88{
89 for(unsigned int i = 0; i < node.num_outputs(); ++i)
90 {
91 Tensor *tensor = node.output(i);
92 if(tensor != nullptr && !tensor->bound_edges().empty())
93 {
94 ARM_COMPUTE_ERROR_ON_MSG(!tensor->handle(), "Tensor handle is not configured!");
95 tensor->handle()->allocate();
96 }
97 }
98}
99
100void allocate_const_tensors(Graph &g)
101{
102 for(auto &node : g.nodes())
103 {
104 if(node != nullptr)
105 {
106 switch(node->type())
107 {
108 case NodeType::Const:
109 case NodeType::Input:
110 allocate_all_output_tensors(*node);
111 break;
112 case NodeType::Output:
113 allocate_all_input_tensors(*node);
114 default:
115 break;
116 }
117 }
118 }
119}
120
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000121void allocate_all_tensors(Graph &g)
122{
123 auto &tensors = g.tensors();
124
125 for(auto &tensor : tensors)
126 {
Georgios Pinitase0437672018-05-02 14:07:55 +0100127 if(tensor && !tensor->bound_edges().empty() && tensor->handle() != nullptr && tensor->handle()->tensor().info()->is_resizable() && tensor->handle()->tensor().is_used())
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000128 {
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000129 tensor->handle()->allocate();
130 }
131 }
132}
133
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000134ExecutionWorkload configure_all_nodes(Graph &g, GraphContext &ctx)
135{
136 ExecutionWorkload workload;
Georgios Pinitase0437672018-05-02 14:07:55 +0100137 workload.graph = &g;
Georgios Pinitas3d1489d2018-05-03 20:47:16 +0100138 workload.ctx = &ctx;
139
140 auto &nodes = g.nodes();
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000141
142 // Create tasks
143 for(auto &node : nodes)
144 {
145 if(node != nullptr)
146 {
147 Target assigned_target = node->assigned_target();
148 auto backend = backends::BackendRegistry::get().find_backend(assigned_target);
149 ARM_COMPUTE_ERROR_ON_MSG(!backend, "Requested backend doesn't exist!");
150 auto func = backend->configure_node(*node, ctx);
151 if(func != nullptr)
152 {
153 ExecutionTask task;
154 task.task = std::move(func);
155 task.node = node.get();
156 workload.tasks.push_back(std::move(task));
157 }
158 }
159 }
160
161 // Add inputs and outputs
162 for(auto &node : nodes)
163 {
164 if(node != nullptr && node->type() == NodeType::Input)
165 {
166 workload.inputs.push_back(node->output(0));
167 }
168
169 if(node != nullptr && node->type() == NodeType::Output)
170 {
171 workload.outputs.push_back(node->input(0));
172 continue;
173 }
174 }
175
176 return workload;
177}
178
Georgios Pinitas1562be32018-03-08 19:09:19 +0000179void release_unused_tensors(Graph &g)
180{
181 for(auto &tensor : g.tensors())
182 {
183 if(tensor != nullptr && tensor->handle() != nullptr)
184 {
185 tensor->handle()->release_if_unused();
186 }
187 }
188}
189
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000190void call_tensor_accessor(Tensor *tensor)
191{
192 ARM_COMPUTE_ERROR_ON(!tensor);
193 tensor->call_accessor();
194}
195
196void call_all_const_node_accessors(Graph &g)
197{
198 auto &nodes = g.nodes();
199
200 for(auto &node : nodes)
201 {
202 if(node != nullptr && node->type() == NodeType::Const)
203 {
204 call_tensor_accessor(node->output(0));
205 }
206 }
207}
208
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100209bool call_all_input_node_accessors(ExecutionWorkload &workload)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000210{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100211 return !std::any_of(std::begin(workload.inputs), std::end(workload.inputs), [](Tensor * input_tensor)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000212 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100213 return (input_tensor == nullptr) || !input_tensor->call_accessor();
214 });
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000215}
216
Georgios Pinitase0437672018-05-02 14:07:55 +0100217void prepare_all_tasks(ExecutionWorkload &workload)
218{
219 ARM_COMPUTE_ERROR_ON(workload.graph == nullptr);
220 for(auto &task : workload.tasks)
221 {
222 task.prepare();
223 release_unused_tensors(*workload.graph);
224 }
225}
226
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000227void call_all_tasks(ExecutionWorkload &workload)
228{
Georgios Pinitas3d1489d2018-05-03 20:47:16 +0100229 ARM_COMPUTE_ERROR_ON(workload.ctx == nullptr);
230
231 // Acquire memory for the transition buffers
232 for(auto &mm_ctx : workload.ctx->memory_managers())
233 {
234 if(mm_ctx.second.cross_group != nullptr)
235 {
236 mm_ctx.second.cross_group->acquire();
237 }
238 }
239
240 // Execute tasks
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000241 for(auto &task : workload.tasks)
242 {
243 task();
244 }
Georgios Pinitas3d1489d2018-05-03 20:47:16 +0100245
246 // Release memory for the transition buffers
247 for(auto &mm_ctx : workload.ctx->memory_managers())
248 {
249 if(mm_ctx.second.cross_group != nullptr)
250 {
251 mm_ctx.second.cross_group->release();
252 }
253 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000254}
255
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100256bool call_all_output_node_accessors(ExecutionWorkload &workload)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000257{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100258 bool is_valid = true;
259 std::for_each(std::begin(workload.outputs), std::end(workload.outputs), [&](Tensor * output_tensor)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000260 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100261 is_valid = is_valid && (output_tensor != nullptr) && output_tensor->call_accessor();
262 });
263
264 return is_valid;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000265}
266} // namespace detail
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100267} // namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000268} // namespace arm_compute