Compute Library
 21.02
GraphManager.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2019 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  */
25 
34 
36 
37 namespace arm_compute
38 {
39 namespace graph
40 {
42  : _workloads()
43 {
44 }
45 
47 {
48  // Check if graph has been registered
49  if(_workloads.find(graph.id()) != std::end(_workloads))
50  {
51  ARM_COMPUTE_ERROR("Graph is already registered!");
52  }
53 
54  // Apply IR mutating passes
56 
57  // Force target to all graph construct
58  // TODO (COMPMID-2014) : Support heterogeneous execution
59  Target forced_target = target;
60  if(!is_target_supported(target))
61  {
62  forced_target = get_default_target();
63  ARM_COMPUTE_LOG_GRAPH_INFO("Switching target from " << target << " to " << forced_target << std::endl);
64  }
65  force_target_to_graph(graph, forced_target);
66 
67  // Setup backend context
68  // TODO (COMPMID-2014) : Setup all backends needed by the graph
69  setup_requested_backend_context(ctx, forced_target);
70 
71  // Configure all tensors
73 
74  // Apply backend mutating passes
76 
77  // Perform topological sort
78  std::vector<NodeID> topological_sorted_nodes = dfs(graph);
79 
80  // Validate all nodes
82 
83  // Configure all nodes
84  auto workload = detail::configure_all_nodes(graph, ctx, topological_sorted_nodes);
85  ARM_COMPUTE_ERROR_ON_MSG(workload.tasks.empty(), "Could not configure all nodes!");
86 
87  // Allocate const tensors and call accessors
90 
91  // Prepare graph
92  detail::prepare_all_tasks(workload);
93 
94  // Setup tensor memory (Allocate all tensors or setup transition manager)
96  {
97  detail::configure_transition_manager(graph, ctx, workload);
98  }
99  else
100  {
102  }
103 
104  // Finalize Graph context
105  ctx.finalize();
106 
107  // Register graph
108  _workloads.insert(std::make_pair(graph.id(), std::move(workload)));
109  ARM_COMPUTE_LOG_GRAPH_VERBOSE("Created workload for graph with ID : " << graph.id() << std::endl);
110 }
111 
113 {
114  // Check if graph is finalized
115  auto it = _workloads.find(graph.id());
116  ARM_COMPUTE_ERROR_ON_MSG(it == std::end(_workloads), "Graph is not registered!");
117 
118  while(true)
119  {
120  // Call input accessors
122  {
123  return;
124  }
125 
126  // Run graph
127  detail::call_all_tasks(it->second);
128 
129  // Call output accessors
131  {
132  return;
133  }
134  }
135 }
136 
138 {
139  auto it = _workloads.find(graph.id());
140  ARM_COMPUTE_ERROR_ON_MSG(it == std::end(_workloads), "Graph is not registered!");
141 
142  _workloads.erase(it);
143 }
144 } // namespace graph
145 } // namespace arm_compute
void force_target_to_graph(Graph &g, Target target)
Forces a single target to all graph constructs.
Definition: Utils.cpp:56
GraphID id() const
Returns graph id.
Definition: Graph.cpp:169
void configure_transition_manager(Graph &g, GraphContext &ctx, ExecutionWorkload &workload)
Configures transition manager and execution workload.
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
void invalidate_graph(Graph &graph)
Invalidates the graph execution workload.
void allocate_all_tensors(Graph &g)
Allocates all tensors of a graph.
void prepare_all_tasks(ExecutionWorkload &workload)
Prepares all tasks for execution.
void finalize_graph(Graph &graph, GraphContext &ctx, PassManager &pm, Target target)
Finalizes a given graph.
#define ARM_COMPUTE_LOG_GRAPH_INFO(x)
Definition: Logger.h:54
Copyright (c) 2017-2021 Arm Limited.
Target get_default_target()
Returns default target for execution.
Definition: Utils.cpp:39
void configure_all_tensors(Graph &g)
Configures all nodes of a graph.
bool use_transition_memory_manager
Use a memory manager to manager transition buffer memory.
Definition: Types.h:84
void validate_all_nodes(Graph &g)
Validates all nodes.
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
void setup_requested_backend_context(GraphContext &ctx, Target target)
Setups requested backend context if it exists, is supported and hasn&#39;t been initialized already...
Definition: Utils.cpp:111
bool is_target_supported(Target target)
Checks if a specific target is supported.
Definition: Utils.cpp:34
void finalize()
Finalizes memory managers in graph context.
void end(TokenStream &in, bool &valid)
Definition: MLGOParser.cpp:290
void call_all_const_node_accessors(Graph &g)
Call all const node accessors.
bool call_all_input_node_accessors(ExecutionWorkload &workload)
Call all input node accessors.
std::vector< NodeID > dfs(Graph &g)
Depth first search traversal.
void run_type(Graph &g, IGraphMutator::MutationType type)
Runs a mutation passes of a specific type on a given graph.
Definition: PassManager.cpp:73
Graph class.
Definition: Graph.h:53
ExecutionWorkload configure_all_nodes(Graph &g, GraphContext &ctx, const std::vector< NodeID > &node_order)
Configures all nodes of graph.
const GraphConfig & config() const
Graph configuration accessor.
void allocate_const_tensors(Graph &g)
Allocates const tensor of a given graph.
#define ARM_COMPUTE_LOG_GRAPH_VERBOSE(x)
Definition: Logger.h:50
void call_all_tasks(ExecutionWorkload &workload)
Executes all tasks of a workload.
void execute_graph(Graph &graph)
Executes a graph.
bool call_all_output_node_accessors(ExecutionWorkload &workload)
Call all output node accessors.
GraphManager()
Default Constructor.