Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 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/graph/GraphContext.h" |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/graph.h" |
| 27 | #include "arm_compute/graph/Utils.h" |
Georgios Pinitas | 9da19e9 | 2018-10-11 15:33:11 +0100 | [diff] [blame] | 28 | #include "arm_compute/graph/backends/BackendRegistry.h" |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 29 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 30 | namespace arm_compute |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 31 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 32 | namespace graph |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 33 | { |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 34 | GraphContext::GraphContext() |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 35 | : _config(), _memory_managers(), _weights_managers() |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 36 | { |
| 37 | } |
| 38 | |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 39 | GraphContext::~GraphContext() |
| 40 | { |
| 41 | _memory_managers.clear(); |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 42 | _weights_managers.clear(); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 43 | release_default_graph_context(*this); |
| 44 | } |
| 45 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 46 | const GraphConfig &GraphContext::config() const |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 47 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 48 | return _config; |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 49 | } |
| 50 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 51 | void GraphContext::set_config(const GraphConfig &config) |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 52 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 53 | _config = config; |
| 54 | } |
| 55 | |
| 56 | bool GraphContext::insert_memory_management_ctx(MemoryManagerContext &&memory_ctx) |
| 57 | { |
| 58 | Target target = memory_ctx.target; |
| 59 | if(target == Target::UNSPECIFIED || _memory_managers.find(target) != std::end(_memory_managers)) |
| 60 | { |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | _memory_managers[target] = std::move(memory_ctx); |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | MemoryManagerContext *GraphContext::memory_management_ctx(Target target) |
| 69 | { |
| 70 | return (_memory_managers.find(target) != std::end(_memory_managers)) ? &_memory_managers[target] : nullptr; |
| 71 | } |
| 72 | |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame] | 73 | std::map<Target, MemoryManagerContext> &GraphContext::memory_managers() |
| 74 | { |
| 75 | return _memory_managers; |
| 76 | } |
| 77 | |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 78 | bool GraphContext::insert_weights_management_ctx(WeightsManagerContext &&weights_managers) |
| 79 | { |
| 80 | Target target = weights_managers.target; |
| 81 | |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 82 | if(_weights_managers.find(target) != std::end(_weights_managers)) |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 83 | { |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | _weights_managers[target] = std::move(weights_managers); |
| 88 | |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | WeightsManagerContext *GraphContext::weights_management_ctx(Target target) |
| 93 | { |
| 94 | return (_weights_managers.find(target) != std::end(_weights_managers)) ? &_weights_managers[target] : nullptr; |
| 95 | } |
| 96 | |
| 97 | std::map<Target, WeightsManagerContext> &GraphContext::weights_managers() |
| 98 | { |
| 99 | return _weights_managers; |
| 100 | } |
| 101 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 102 | void GraphContext::finalize() |
| 103 | { |
Georgios Pinitas | 9da19e9 | 2018-10-11 15:33:11 +0100 | [diff] [blame] | 104 | const size_t num_pools = 1; |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 105 | for(auto &mm_obj : _memory_managers) |
| 106 | { |
Georgios Pinitas | 9da19e9 | 2018-10-11 15:33:11 +0100 | [diff] [blame] | 107 | ARM_COMPUTE_ERROR_ON(!mm_obj.second.allocator); |
| 108 | |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame] | 109 | // Finalize intra layer memory manager |
| 110 | if(mm_obj.second.intra_mm != nullptr) |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 111 | { |
Georgios Pinitas | 9da19e9 | 2018-10-11 15:33:11 +0100 | [diff] [blame] | 112 | mm_obj.second.intra_mm->populate(*mm_obj.second.allocator, num_pools); |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame] | 113 | } |
| 114 | // Finalize cross layer memory manager |
| 115 | if(mm_obj.second.cross_mm != nullptr) |
| 116 | { |
Georgios Pinitas | 9da19e9 | 2018-10-11 15:33:11 +0100 | [diff] [blame] | 117 | mm_obj.second.cross_mm->populate(*mm_obj.second.allocator, num_pools); |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | } |
| 121 | } // namespace graph |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 122 | } // namespace arm_compute |