blob: 10850aa2598cd34b50da4b36c4ee59a789f2a410 [file] [log] [blame]
Georgios Pinitasff421f22017-10-04 16:53:58 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2019 Arm Limited.
Georgios Pinitasff421f22017-10-04 16:53:58 +01003 *
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 Barbierb6eb3532018-08-08 13:20:04 +010025
26#include "arm_compute/graph.h"
Georgios Pinitas9da19e92018-10-11 15:33:11 +010027#include "arm_compute/graph/backends/BackendRegistry.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010028#include "arm_compute/graph/Utils.h"
Georgios Pinitasff421f22017-10-04 16:53:58 +010029
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010030namespace arm_compute
Georgios Pinitasff421f22017-10-04 16:53:58 +010031{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010032namespace graph
Georgios Pinitasff421f22017-10-04 16:53:58 +010033{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010034GraphContext::GraphContext() : _config(), _memory_managers(), _weights_managers()
Georgios Pinitasff421f22017-10-04 16:53:58 +010035{
36}
37
Anthony Barbierb6eb3532018-08-08 13:20:04 +010038GraphContext::~GraphContext()
39{
40 _memory_managers.clear();
Michalis Spyrou1a569a32019-09-10 17:20:34 +010041 _weights_managers.clear();
Anthony Barbierb6eb3532018-08-08 13:20:04 +010042 release_default_graph_context(*this);
43}
44
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010045const GraphConfig &GraphContext::config() const
Georgios Pinitasff421f22017-10-04 16:53:58 +010046{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010047 return _config;
Georgios Pinitasff421f22017-10-04 16:53:58 +010048}
49
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010050void GraphContext::set_config(const GraphConfig &config)
Georgios Pinitasff421f22017-10-04 16:53:58 +010051{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010052 _config = config;
53}
54
55bool GraphContext::insert_memory_management_ctx(MemoryManagerContext &&memory_ctx)
56{
57 Target target = memory_ctx.target;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010058 if (target == Target::UNSPECIFIED || _memory_managers.find(target) != std::end(_memory_managers))
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010059 {
60 return false;
61 }
62
63 _memory_managers[target] = std::move(memory_ctx);
64 return true;
65}
66
67MemoryManagerContext *GraphContext::memory_management_ctx(Target target)
68{
69 return (_memory_managers.find(target) != std::end(_memory_managers)) ? &_memory_managers[target] : nullptr;
70}
71
Georgios Pinitas3d1489d2018-05-03 20:47:16 +010072std::map<Target, MemoryManagerContext> &GraphContext::memory_managers()
73{
74 return _memory_managers;
75}
76
Michalis Spyrou1a569a32019-09-10 17:20:34 +010077bool GraphContext::insert_weights_management_ctx(WeightsManagerContext &&weights_managers)
78{
79 Target target = weights_managers.target;
80
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010081 if (_weights_managers.find(target) != std::end(_weights_managers))
Michalis Spyrou1a569a32019-09-10 17:20:34 +010082 {
83 return false;
84 }
85
86 _weights_managers[target] = std::move(weights_managers);
87
88 return true;
89}
90
91WeightsManagerContext *GraphContext::weights_management_ctx(Target target)
92{
93 return (_weights_managers.find(target) != std::end(_weights_managers)) ? &_weights_managers[target] : nullptr;
94}
95
96std::map<Target, WeightsManagerContext> &GraphContext::weights_managers()
97{
98 return _weights_managers;
99}
100
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100101void GraphContext::finalize()
102{
Georgios Pinitas9da19e92018-10-11 15:33:11 +0100103 const size_t num_pools = 1;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100104 for (auto &mm_obj : _memory_managers)
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100105 {
Georgios Pinitas9da19e92018-10-11 15:33:11 +0100106 ARM_COMPUTE_ERROR_ON(!mm_obj.second.allocator);
107
Georgios Pinitas3d1489d2018-05-03 20:47:16 +0100108 // Finalize intra layer memory manager
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100109 if (mm_obj.second.intra_mm != nullptr)
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100110 {
Georgios Pinitas9da19e92018-10-11 15:33:11 +0100111 mm_obj.second.intra_mm->populate(*mm_obj.second.allocator, num_pools);
Georgios Pinitas3d1489d2018-05-03 20:47:16 +0100112 }
113 // Finalize cross layer memory manager
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100114 if (mm_obj.second.cross_mm != nullptr)
Georgios Pinitas3d1489d2018-05-03 20:47:16 +0100115 {
Georgios Pinitas9da19e92018-10-11 15:33:11 +0100116 mm_obj.second.cross_mm->populate(*mm_obj.second.allocator, num_pools);
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100117 }
118 }
119}
120} // namespace graph
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100121} // namespace arm_compute