Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame^] | 1 | /* |
| 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 | |
| 33 | namespace arm_compute |
| 34 | { |
| 35 | namespace graph2 |
| 36 | { |
| 37 | GraphManager::GraphManager() |
| 38 | : _workloads() |
| 39 | { |
| 40 | detail::default_initialize_backends(); |
| 41 | } |
| 42 | |
| 43 | void 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 | |
| 67 | // Configure all nodes |
| 68 | auto workload = detail::configure_all_nodes(graph, ctx); |
| 69 | ARM_COMPUTE_ERROR_ON_MSG(workload.tasks.empty(), "Could not configure all nodes!"); |
| 70 | |
| 71 | // Allocate all tensors |
| 72 | detail::allocate_all_tensors(graph); |
| 73 | |
| 74 | // Call accessors on all Const nodes |
| 75 | detail::call_all_const_node_accessors(graph); |
| 76 | |
| 77 | _workloads.insert(std::make_pair(graph.id(), std::move(workload))); |
| 78 | ARM_COMPUTE_LOG_GRAPH_VERBOSE("Created workload for graph with ID : " << graph.id().get() << std::endl); |
| 79 | |
| 80 | // Finalize Graph context |
| 81 | ctx.finalize(); |
| 82 | } |
| 83 | |
| 84 | void GraphManager::execute_graph(Graph &graph) |
| 85 | { |
| 86 | // Check if graph is finalized |
| 87 | auto it = _workloads.find(graph.id()); |
| 88 | ARM_COMPUTE_ERROR_ON_MSG(it == std::end(_workloads), "Graph is not registered!"); |
| 89 | |
| 90 | // Call input accessors |
| 91 | detail::call_all_input_node_accessors(it->second); |
| 92 | |
| 93 | // Run graph |
| 94 | detail::call_all_tasks(it->second); |
| 95 | |
| 96 | // Call output accessors |
| 97 | detail::call_all_output_node_accessors(it->second); |
| 98 | } |
| 99 | |
| 100 | void GraphManager::invalidate_graph(Graph &graph) |
| 101 | { |
| 102 | auto it = _workloads.find(graph.id()); |
| 103 | ARM_COMPUTE_ERROR_ON_MSG(it == std::end(_workloads), "Graph is not registered!"); |
| 104 | |
| 105 | _workloads.erase(it); |
| 106 | } |
| 107 | } // namespace graph2 |
| 108 | } // namespace arm_compute |