1 /*
2 * Copyright (c) 2018-2019 Arm Limited.
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"
25
26 #include "arm_compute/graph.h"
27 #include "arm_compute/graph/Utils.h"
28 #include "arm_compute/graph/backends/BackendRegistry.h"
29
30 namespace arm_compute
31 {
32 namespace graph
33 {
GraphContext()34 GraphContext::GraphContext()
35 : _config(), _memory_managers(), _weights_managers()
36 {
37 }
38
~GraphContext()39 GraphContext::~GraphContext()
40 {
41 _memory_managers.clear();
42 _weights_managers.clear();
43 release_default_graph_context(*this);
44 }
45
config() const46 const GraphConfig &GraphContext::config() const
47 {
48 return _config;
49 }
50
set_config(const GraphConfig & config)51 void GraphContext::set_config(const GraphConfig &config)
52 {
53 _config = config;
54 }
55
insert_memory_management_ctx(MemoryManagerContext && memory_ctx)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
memory_management_ctx(Target target)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
memory_managers()73 std::map<Target, MemoryManagerContext> &GraphContext::memory_managers()
74 {
75 return _memory_managers;
76 }
77
insert_weights_management_ctx(WeightsManagerContext && weights_managers)78 bool GraphContext::insert_weights_management_ctx(WeightsManagerContext &&weights_managers)
79 {
80 Target target = weights_managers.target;
81
82 if(_weights_managers.find(target) != std::end(_weights_managers))
83 {
84 return false;
85 }
86
87 _weights_managers[target] = std::move(weights_managers);
88
89 return true;
90 }
91
weights_management_ctx(Target target)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
weights_managers()97 std::map<Target, WeightsManagerContext> &GraphContext::weights_managers()
98 {
99 return _weights_managers;
100 }
101
finalize()102 void GraphContext::finalize()
103 {
104 const size_t num_pools = 1;
105 for(auto &mm_obj : _memory_managers)
106 {
107 ARM_COMPUTE_ERROR_ON(!mm_obj.second.allocator);
108
109 // Finalize intra layer memory manager
110 if(mm_obj.second.intra_mm != nullptr)
111 {
112 mm_obj.second.intra_mm->populate(*mm_obj.second.allocator, num_pools);
113 }
114 // Finalize cross layer memory manager
115 if(mm_obj.second.cross_mm != nullptr)
116 {
117 mm_obj.second.cross_mm->populate(*mm_obj.second.allocator, num_pools);
118 }
119 }
120 }
121 } // namespace graph
122 } // namespace arm_compute
123