blob: c0720ac6858157d9e1cad5936a21b3fa5ca1f161 [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/GraphManager.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/Logger.h"
29#include "arm_compute/graph/PassManager.h"
30#include "arm_compute/graph/Utils.h"
31#include "arm_compute/graph/detail/ExecutionHelpers.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000032
33namespace arm_compute
34{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010035namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +000036{
37GraphManager::GraphManager()
38 : _workloads()
39{
40 detail::default_initialize_backends();
41}
42
43void GraphManager::finalize_graph(Graph &graph, GraphContext &ctx, PassManager &pm, Target target)
44{
45 // Setup graph context if not done manually
46 setup_default_graph_context(ctx);
47
48 // Check if graph has been registered
Georgios Pinitasae700722018-04-27 18:10:01 +010049 if(_workloads.find(graph.id()) != std::end(_workloads))
50 {
51 ARM_COMPUTE_ERROR("Graph is already registered!");
52 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +000053
54 // Force target to all graph construct
55 // TODO (geopin01) : Support heterogeneous execution
56 Target forced_target = is_target_supported(target) ? target : get_default_target();
57 force_target_to_graph(graph, forced_target);
58
59 // Configure all tensors
60 detail::configure_all_tensors(graph);
61
62 // Apply all mutating passes
63 pm.run_all(graph);
64
65 // TODO (geopin01): Perform a graph validation
66
67 // Perform topological sort
68 // FIXME : Sort nodes and pass sorted indices in configure all nodes
69
Georgios Pinitas28705162018-03-21 20:10:53 +000070 // Validate all nodes
71 detail::validate_all_nodes(graph);
72
Georgios Pinitasd8734b52017-12-22 15:27:52 +000073 // Configure all nodes
74 auto workload = detail::configure_all_nodes(graph, ctx);
75 ARM_COMPUTE_ERROR_ON_MSG(workload.tasks.empty(), "Could not configure all nodes!");
76
77 // Allocate all tensors
78 detail::allocate_all_tensors(graph);
79
80 // Call accessors on all Const nodes
81 detail::call_all_const_node_accessors(graph);
82
83 _workloads.insert(std::make_pair(graph.id(), std::move(workload)));
84 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Created workload for graph with ID : " << graph.id().get() << std::endl);
85
86 // Finalize Graph context
87 ctx.finalize();
Georgios Pinitas1562be32018-03-08 19:09:19 +000088
89 // Make first run
90 execute_graph(graph);
91
92 // Release all unused const nodes
93 detail::release_unused_tensors(graph);
Georgios Pinitasd8734b52017-12-22 15:27:52 +000094}
95
96void GraphManager::execute_graph(Graph &graph)
97{
98 // Check if graph is finalized
99 auto it = _workloads.find(graph.id());
100 ARM_COMPUTE_ERROR_ON_MSG(it == std::end(_workloads), "Graph is not registered!");
101
102 // Call input accessors
103 detail::call_all_input_node_accessors(it->second);
104
105 // Run graph
106 detail::call_all_tasks(it->second);
107
108 // Call output accessors
109 detail::call_all_output_node_accessors(it->second);
110}
111
112void GraphManager::invalidate_graph(Graph &graph)
113{
114 auto it = _workloads.find(graph.id());
115 ARM_COMPUTE_ERROR_ON_MSG(it == std::end(_workloads), "Graph is not registered!");
116
117 _workloads.erase(it);
118}
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100119} // namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000120} // namespace arm_compute