blob: db6650cf69ba7799db831a2e55b9e4339100ef7a [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"
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010030#include "arm_compute/graph/TypePrinter.h"
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010031#include "arm_compute/graph/Utils.h"
Georgios Pinitas3d1489d2018-05-03 20:47:16 +010032#include "arm_compute/graph/detail/CrossLayerMemoryManagerHelpers.h"
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010033#include "arm_compute/graph/detail/ExecutionHelpers.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000034
35namespace arm_compute
36{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010037namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +000038{
39GraphManager::GraphManager()
40 : _workloads()
41{
Georgios Pinitasd8734b52017-12-22 15:27:52 +000042}
43
44void GraphManager::finalize_graph(Graph &graph, GraphContext &ctx, PassManager &pm, Target target)
45{
46 // Setup graph context if not done manually
47 setup_default_graph_context(ctx);
48
49 // Check if graph has been registered
Georgios Pinitasae700722018-04-27 18:10:01 +010050 if(_workloads.find(graph.id()) != std::end(_workloads))
51 {
52 ARM_COMPUTE_ERROR("Graph is already registered!");
53 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +000054
55 // Force target to all graph construct
56 // TODO (geopin01) : Support heterogeneous execution
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010057 Target forced_target = target;
58 if(!is_target_supported(target))
59 {
60 forced_target = get_default_target();
61 ARM_COMPUTE_LOG_GRAPH_INFO("Switching target from " << target << " to " << forced_target << std::endl);
62 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +000063 force_target_to_graph(graph, forced_target);
64
65 // Configure all tensors
66 detail::configure_all_tensors(graph);
67
68 // Apply all mutating passes
69 pm.run_all(graph);
70
Georgios Pinitasd8734b52017-12-22 15:27:52 +000071 // Perform topological sort
72 // FIXME : Sort nodes and pass sorted indices in configure all nodes
73
Georgios Pinitas28705162018-03-21 20:10:53 +000074 // Validate all nodes
75 detail::validate_all_nodes(graph);
76
Georgios Pinitasd8734b52017-12-22 15:27:52 +000077 // Configure all nodes
78 auto workload = detail::configure_all_nodes(graph, ctx);
79 ARM_COMPUTE_ERROR_ON_MSG(workload.tasks.empty(), "Could not configure all nodes!");
80
Georgios Pinitas3d1489d2018-05-03 20:47:16 +010081 // Allocate const tensors and call accessors
82 detail::allocate_const_tensors(graph);
83 detail::call_all_const_node_accessors(graph);
84
Georgios Pinitas72219332018-06-05 14:56:06 +010085 // Prepare graph
86 detail::prepare_all_tasks(workload);
Georgios Pinitasd8734b52017-12-22 15:27:52 +000087
Georgios Pinitas3d1489d2018-05-03 20:47:16 +010088 // Setup tensor memory (Allocate all tensors or setup transition manager)
89 if(ctx.config().use_transition_memory_manager)
90 {
91 detail::configure_transition_manager(graph, ctx, workload);
Georgios Pinitase0437672018-05-02 14:07:55 +010092 }
93 else
94 {
Georgios Pinitase0437672018-05-02 14:07:55 +010095 detail::allocate_all_tensors(graph);
Georgios Pinitas3d1489d2018-05-03 20:47:16 +010096 }
Georgios Pinitas1562be32018-03-08 19:09:19 +000097
Georgios Pinitas3d1489d2018-05-03 20:47:16 +010098 // Finalize Graph context
99 ctx.finalize();
Georgios Pinitase0437672018-05-02 14:07:55 +0100100
Georgios Pinitas3d1489d2018-05-03 20:47:16 +0100101 // Register graph
102 _workloads.insert(std::make_pair(graph.id(), std::move(workload)));
103 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Created workload for graph with ID : " << graph.id().get() << std::endl);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000104}
105
106void GraphManager::execute_graph(Graph &graph)
107{
108 // Check if graph is finalized
109 auto it = _workloads.find(graph.id());
110 ARM_COMPUTE_ERROR_ON_MSG(it == std::end(_workloads), "Graph is not registered!");
111
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100112 while(true)
113 {
114 // Call input accessors
115 if(!detail::call_all_input_node_accessors(it->second))
116 {
117 return;
118 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000119
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100120 // Run graph
121 detail::call_all_tasks(it->second);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000122
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100123 // Call output accessors
124 if(!detail::call_all_output_node_accessors(it->second))
125 {
126 return;
127 }
128 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000129}
130
131void GraphManager::invalidate_graph(Graph &graph)
132{
133 auto it = _workloads.find(graph.id());
134 ARM_COMPUTE_ERROR_ON_MSG(it == std::end(_workloads), "Graph is not registered!");
135
136 _workloads.erase(it);
137}
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100138} // namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000139} // namespace arm_compute