Compute Library
 21.11
GraphManager.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2021 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 
35 
36 #include "src/common/utils/Log.h"
37 
38 namespace arm_compute
39 {
40 namespace graph
41 {
43  : _workloads()
44 {
45 }
46 
48 {
49  ARM_COMPUTE_LOG_INFO_WITH_FUNCNAME_ACL("Initiate graph configuration!");
50 
51  // Check if graph has been registered
52  if(_workloads.find(graph.id()) != std::end(_workloads))
53  {
54  ARM_COMPUTE_ERROR("Graph is already registered!");
55  }
56 
57  // Apply IR mutating passes
59 
60  // Force target to all graph construct
61  Target forced_target = target;
62 
63  // In case CLVK is selected, use the CL backend and
64  // update config
65  if(target == Target::CLVK)
66  {
67  forced_target = Target::CL;
68  GraphConfig config = ctx.config();
70 
71  ctx.set_config(config);
72  }
73 
74  if(!is_target_supported(target))
75  {
76  forced_target = get_default_target();
77  ARM_COMPUTE_LOG_GRAPH_INFO("Switching target from " << target << " to " << forced_target << std::endl);
78  }
79  force_target_to_graph(graph, forced_target);
80 
81  // Setup backend context
82  setup_requested_backend_context(ctx, forced_target);
83 
84  // Configure all tensors
86 
87  // Apply backend mutating passes
89 
90  // Perform topological sort
91  std::vector<NodeID> topological_sorted_nodes = dfs(graph);
92 
93  // Validate all nodes
95 
96  // Configure all nodes
97  auto workload = detail::configure_all_nodes(graph, ctx, topological_sorted_nodes);
98  ARM_COMPUTE_ERROR_ON_MSG(workload.tasks.empty(), "Could not configure all nodes!");
99 
100  // Allocate const tensors and call accessors
103 
104  // Prepare graph
105  detail::prepare_all_tasks(workload);
106 
107  // Setup tensor memory (Allocate all tensors or setup transition manager)
109  {
110  detail::configure_transition_manager(graph, ctx, workload);
111  }
112  else
113  {
115  }
116 
117  // Finalize Graph context
118  ctx.finalize();
119 
120  // Register graph
121  _workloads.insert(std::make_pair(graph.id(), std::move(workload)));
122  ARM_COMPUTE_LOG_GRAPH_VERBOSE("Created workload for graph with ID : " << graph.id() << std::endl);
123 }
124 
126 {
127  ARM_COMPUTE_LOG_INFO_WITH_FUNCNAME_ACL("Initiate graph execution!");
128 
129  // Check if graph is finalized
130  auto it = _workloads.find(graph.id());
131  ARM_COMPUTE_ERROR_ON_MSG(it == std::end(_workloads), "Graph is not registered!");
132 
133  while(true)
134  {
135  // Call input accessors
137  {
138  return;
139  }
140 
141  // Run graph
142  detail::call_all_tasks(it->second);
143 
144  // Call output accessors
146  {
147  return;
148  }
149  }
150 }
151 
153 {
154  auto it = _workloads.find(graph.id());
155  ARM_COMPUTE_ERROR_ON_MSG(it == std::end(_workloads), "Graph is not registered!");
156 
157  _workloads.erase(it);
158 }
159 } // namespace graph
160 } // namespace arm_compute
void force_target_to_graph(Graph &g, Target target)
Forces a single target to all graph constructs.
Definition: Utils.cpp:52
Graph configuration structure Device target types.
Definition: Types.h:84
GraphID id() const
Returns graph id.
Definition: Graph.cpp:169
#define ARM_COMPUTE_LOG_INFO_WITH_FUNCNAME_ACL(msg)
Log an information message to the logger with function name before the message.
Definition: Log.h:99
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.
CLVK capable target device.
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.
void set_config(const GraphConfig &config)
Sets graph configuration.
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:88
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:130
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.
OpenCL capable target device.
CLBackendType backend_type
CL backend type to use.
Definition: Types.h:96
bool call_all_output_node_accessors(ExecutionWorkload &workload)
Call all output node accessors.
GraphManager()
Default Constructor.