blob: 037b40b68bedc214b9fc206e051f1a42770043b1 [file] [log] [blame]
Kaizenbf8b01d2017-10-12 14:26:51 +01001/*
Jenkinsb3a371b2018-05-23 11:36:53 +01002 * Copyright (c) 2018 ARM Limited.
Kaizenbf8b01d2017-10-12 14:26:51 +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"
Jenkins52ba29e2018-08-29 15:32:11 +000025
26#include "arm_compute/graph.h"
27#include "arm_compute/graph/Utils.h"
Jenkinsb9abeae2018-11-22 11:58:08 +000028#include "arm_compute/graph/backends/BackendRegistry.h"
Kaizenbf8b01d2017-10-12 14:26:51 +010029
Jenkinsb3a371b2018-05-23 11:36:53 +010030namespace arm_compute
Kaizenbf8b01d2017-10-12 14:26:51 +010031{
Jenkinsb3a371b2018-05-23 11:36:53 +010032namespace graph
Kaizenbf8b01d2017-10-12 14:26:51 +010033{
Kaizenbf8b01d2017-10-12 14:26:51 +010034GraphContext::GraphContext()
Jenkinsb3a371b2018-05-23 11:36:53 +010035 : _config(), _memory_managers()
Kaizenbf8b01d2017-10-12 14:26:51 +010036{
37}
38
Jenkins52ba29e2018-08-29 15:32:11 +000039GraphContext::~GraphContext()
40{
41 _memory_managers.clear();
42 release_default_graph_context(*this);
43}
44
Jenkinsb3a371b2018-05-23 11:36:53 +010045const GraphConfig &GraphContext::config() const
Kaizenbf8b01d2017-10-12 14:26:51 +010046{
Jenkinsb3a371b2018-05-23 11:36:53 +010047 return _config;
Kaizenbf8b01d2017-10-12 14:26:51 +010048}
49
Jenkinsb3a371b2018-05-23 11:36:53 +010050void GraphContext::set_config(const GraphConfig &config)
Kaizenbf8b01d2017-10-12 14:26:51 +010051{
Jenkinsb3a371b2018-05-23 11:36:53 +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
72std::map<Target, MemoryManagerContext> &GraphContext::memory_managers()
73{
74 return _memory_managers;
75}
76
77void GraphContext::finalize()
78{
Jenkinsb9abeae2018-11-22 11:58:08 +000079 const size_t num_pools = 1;
Jenkinsb3a371b2018-05-23 11:36:53 +010080 for(auto &mm_obj : _memory_managers)
81 {
Jenkinsb9abeae2018-11-22 11:58:08 +000082 ARM_COMPUTE_ERROR_ON(!mm_obj.second.allocator);
83
Jenkinsb3a371b2018-05-23 11:36:53 +010084 // Finalize intra layer memory manager
85 if(mm_obj.second.intra_mm != nullptr)
86 {
Jenkinsb9abeae2018-11-22 11:58:08 +000087 mm_obj.second.intra_mm->populate(*mm_obj.second.allocator, num_pools);
Jenkinsb3a371b2018-05-23 11:36:53 +010088 }
89 // Finalize cross layer memory manager
90 if(mm_obj.second.cross_mm != nullptr)
91 {
Jenkinsb9abeae2018-11-22 11:58:08 +000092 mm_obj.second.cross_mm->populate(*mm_obj.second.allocator, num_pools);
Jenkinsb3a371b2018-05-23 11:36:53 +010093 }
94 }
95}
96} // namespace graph
Jenkins52ba29e2018-08-29 15:32:11 +000097} // namespace arm_compute