blob: 037b40b68bedc214b9fc206e051f1a42770043b1 [file] [log] [blame]
Georgios Pinitasff421f22017-10-04 16:53:58 +01001/*
Georgios Pinitasd9eb2752018-04-03 13:44:29 +01002 * Copyright (c) 2018 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()
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010035 : _config(), _memory_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();
42 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;
58 if(target == Target::UNSPECIFIED || _memory_managers.find(target) != std::end(_memory_managers))
59 {
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
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010077void GraphContext::finalize()
78{
Georgios Pinitas9da19e92018-10-11 15:33:11 +010079 const size_t num_pools = 1;
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010080 for(auto &mm_obj : _memory_managers)
81 {
Georgios Pinitas9da19e92018-10-11 15:33:11 +010082 ARM_COMPUTE_ERROR_ON(!mm_obj.second.allocator);
83
Georgios Pinitas3d1489d2018-05-03 20:47:16 +010084 // Finalize intra layer memory manager
85 if(mm_obj.second.intra_mm != nullptr)
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010086 {
Georgios Pinitas9da19e92018-10-11 15:33:11 +010087 mm_obj.second.intra_mm->populate(*mm_obj.second.allocator, num_pools);
Georgios Pinitas3d1489d2018-05-03 20:47:16 +010088 }
89 // Finalize cross layer memory manager
90 if(mm_obj.second.cross_mm != nullptr)
91 {
Georgios Pinitas9da19e92018-10-11 15:33:11 +010092 mm_obj.second.cross_mm->populate(*mm_obj.second.allocator, num_pools);
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010093 }
94 }
95}
96} // namespace graph
Anthony Barbierb6eb3532018-08-08 13:20:04 +010097} // namespace arm_compute