blob: 7b74c2fe0e563a7bc91536146eedd1b726a9995b [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"
27#include "arm_compute/graph/Utils.h"
Georgios Pinitas9da19e92018-10-11 15:33:11 +010028#include "arm_compute/graph/backends/BackendRegistry.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{
Georgios Pinitasff421f22017-10-04 16:53:58 +010034GraphContext::GraphContext()
Michalis Spyrou1a569a32019-09-10 17:20:34 +010035 : _config(), _memory_managers(), _weights_managers()
Georgios Pinitasff421f22017-10-04 16:53:58 +010036{
37}
38
Anthony Barbierb6eb3532018-08-08 13:20:04 +010039GraphContext::~GraphContext()
40{
41 _memory_managers.clear();
Michalis Spyrou1a569a32019-09-10 17:20:34 +010042 _weights_managers.clear();
Anthony Barbierb6eb3532018-08-08 13:20:04 +010043 release_default_graph_context(*this);
44}
45
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010046const GraphConfig &GraphContext::config() const
Georgios Pinitasff421f22017-10-04 16:53:58 +010047{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010048 return _config;
Georgios Pinitasff421f22017-10-04 16:53:58 +010049}
50
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010051void GraphContext::set_config(const GraphConfig &config)
Georgios Pinitasff421f22017-10-04 16:53:58 +010052{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010053 _config = config;
54}
55
56bool 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
68MemoryManagerContext *GraphContext::memory_management_ctx(Target target)
69{
70 return (_memory_managers.find(target) != std::end(_memory_managers)) ? &_memory_managers[target] : nullptr;
71}
72
Georgios Pinitas3d1489d2018-05-03 20:47:16 +010073std::map<Target, MemoryManagerContext> &GraphContext::memory_managers()
74{
75 return _memory_managers;
76}
77
Michalis Spyrou1a569a32019-09-10 17:20:34 +010078bool GraphContext::insert_weights_management_ctx(WeightsManagerContext &&weights_managers)
79{
80 Target target = weights_managers.target;
81
Michalis Spyroub27e13a2019-09-27 11:04:27 +010082 if(_weights_managers.find(target) != std::end(_weights_managers))
Michalis Spyrou1a569a32019-09-10 17:20:34 +010083 {
84 return false;
85 }
86
87 _weights_managers[target] = std::move(weights_managers);
88
89 return true;
90}
91
92WeightsManagerContext *GraphContext::weights_management_ctx(Target target)
93{
94 return (_weights_managers.find(target) != std::end(_weights_managers)) ? &_weights_managers[target] : nullptr;
95}
96
97std::map<Target, WeightsManagerContext> &GraphContext::weights_managers()
98{
99 return _weights_managers;
100}
101
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100102void GraphContext::finalize()
103{
Georgios Pinitas9da19e92018-10-11 15:33:11 +0100104 const size_t num_pools = 1;
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100105 for(auto &mm_obj : _memory_managers)
106 {
Georgios Pinitas9da19e92018-10-11 15:33:11 +0100107 ARM_COMPUTE_ERROR_ON(!mm_obj.second.allocator);
108
Georgios Pinitas3d1489d2018-05-03 20:47:16 +0100109 // Finalize intra layer memory manager
110 if(mm_obj.second.intra_mm != nullptr)
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100111 {
Georgios Pinitas9da19e92018-10-11 15:33:11 +0100112 mm_obj.second.intra_mm->populate(*mm_obj.second.allocator, num_pools);
Georgios Pinitas3d1489d2018-05-03 20:47:16 +0100113 }
114 // Finalize cross layer memory manager
115 if(mm_obj.second.cross_mm != nullptr)
116 {
Georgios Pinitas9da19e92018-10-11 15:33:11 +0100117 mm_obj.second.cross_mm->populate(*mm_obj.second.allocator, num_pools);
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100118 }
119 }
120}
121} // namespace graph
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100122} // namespace arm_compute