blob: e708dc6a61fefff306dea9d1d3409bbf1cdff34d [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 */
24#include "arm_compute/graph2/GraphManager.h"
25
26#include "arm_compute/graph2/Graph.h"
27#include "arm_compute/graph2/GraphContext.h"
28#include "arm_compute/graph2/Logger.h"
29#include "arm_compute/graph2/PassManager.h"
30#include "arm_compute/graph2/Utils.h"
31#include "arm_compute/graph2/detail/ExecutionHelpers.h"
32
33namespace arm_compute
34{
35namespace graph2
36{
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
49 ARM_COMPUTE_ERROR_ON_MSG(_workloads.find(graph.id()) != std::end(_workloads), "Graph is already registered!");
50
51 // Force target to all graph construct
52 // TODO (geopin01) : Support heterogeneous execution
53 Target forced_target = is_target_supported(target) ? target : get_default_target();
54 force_target_to_graph(graph, forced_target);
55
56 // Configure all tensors
57 detail::configure_all_tensors(graph);
58
59 // Apply all mutating passes
60 pm.run_all(graph);
61
62 // TODO (geopin01): Perform a graph validation
63
64 // Perform topological sort
65 // FIXME : Sort nodes and pass sorted indices in configure all nodes
66
Georgios Pinitas28705162018-03-21 20:10:53 +000067 // Validate all nodes
68 detail::validate_all_nodes(graph);
69
Georgios Pinitasd8734b52017-12-22 15:27:52 +000070 // Configure all nodes
71 auto workload = detail::configure_all_nodes(graph, ctx);
72 ARM_COMPUTE_ERROR_ON_MSG(workload.tasks.empty(), "Could not configure all nodes!");
73
74 // Allocate all tensors
75 detail::allocate_all_tensors(graph);
76
77 // Call accessors on all Const nodes
78 detail::call_all_const_node_accessors(graph);
79
80 _workloads.insert(std::make_pair(graph.id(), std::move(workload)));
81 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Created workload for graph with ID : " << graph.id().get() << std::endl);
82
83 // Finalize Graph context
84 ctx.finalize();
85}
86
87void GraphManager::execute_graph(Graph &graph)
88{
89 // Check if graph is finalized
90 auto it = _workloads.find(graph.id());
91 ARM_COMPUTE_ERROR_ON_MSG(it == std::end(_workloads), "Graph is not registered!");
92
93 // Call input accessors
94 detail::call_all_input_node_accessors(it->second);
95
96 // Run graph
97 detail::call_all_tasks(it->second);
98
99 // Call output accessors
100 detail::call_all_output_node_accessors(it->second);
101}
102
103void GraphManager::invalidate_graph(Graph &graph)
104{
105 auto it = _workloads.find(graph.id());
106 ARM_COMPUTE_ERROR_ON_MSG(it == std::end(_workloads), "Graph is not registered!");
107
108 _workloads.erase(it);
109}
110} // namespace graph2
111} // namespace arm_compute