blob: 532692b1ce265e7661e85ff428615e311c645ed1 [file] [log] [blame]
telsoa01c577f2c2018-08-31 09:22:23 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa01c577f2c2018-08-31 09:22:23 +01004//
5#include "BaseMemoryManager.hpp"
6
7#if defined(ARMCOMPUTENEON_ENABLED) || defined(ARMCOMPUTECL_ENABLED)
Aron Virginas-Tarf9aeef02018-10-12 15:18:03 +01008#include "BlobLifetimeManager.hpp"
9#include "PoolManager.hpp"
10#include "OffsetLifetimeManager.hpp"
telsoa01c577f2c2018-08-31 09:22:23 +010011#endif
12
13#include <boost/polymorphic_cast.hpp>
14
15namespace armnn
16{
17
18#if defined(ARMCOMPUTENEON_ENABLED) || defined(ARMCOMPUTECL_ENABLED)
19BaseMemoryManager::BaseMemoryManager(std::unique_ptr<arm_compute::IAllocator> alloc,
20 MemoryAffinity memoryAffinity)
21{
22 // (Re)create the memory manager components.
23 m_Allocator = std::move(alloc);
24
25 m_IntraLayerMemoryMgr = CreateArmComputeMemoryManager(memoryAffinity);
26 m_InterLayerMemoryMgr = CreateArmComputeMemoryManager(memoryAffinity);
27}
28
29std::shared_ptr<arm_compute::MemoryManagerOnDemand>
30BaseMemoryManager::CreateArmComputeMemoryManager(MemoryAffinity memoryAffinity)
31{
32 std::shared_ptr<arm_compute::ILifetimeManager> lifetimeManager = nullptr;
33
34 if (memoryAffinity == MemoryAffinity::Buffer)
35 {
36 lifetimeManager = std::make_shared<BlobLifetimeManager>();
37 }
38 else
39 {
40 lifetimeManager = std::make_shared<OffsetLifetimeManager>();
41 }
42
43 auto poolManager = std::make_shared<PoolManager>();
44 auto memoryManager = std::make_shared<arm_compute::MemoryManagerOnDemand>(lifetimeManager, poolManager);
45
46 // Set allocator that the memory manager will use
47 memoryManager->set_allocator(m_Allocator.get());
48
49 return memoryManager;
50}
51
52void BaseMemoryManager::FinalizeMemoryManager(arm_compute::MemoryManagerOnDemand& memoryManager)
53{
54 // Number of pools that the manager will create. This specifies how many layers you want to run in parallel
55 memoryManager.set_num_pools(1);
56
57 // Finalize the memory manager. (Validity checks, memory allocations, etc)
58 memoryManager.finalize();
59}
60
61void BaseMemoryManager::Finalize()
62{
63 BOOST_ASSERT(m_IntraLayerMemoryMgr);
64 FinalizeMemoryManager(*m_IntraLayerMemoryMgr.get());
65
66 BOOST_ASSERT(m_InterLayerMemoryMgr);
67 FinalizeMemoryManager(*m_InterLayerMemoryMgr.get());
68}
69
70void BaseMemoryManager::Acquire()
71{
72 // Allocate memory pools for intra-layer memory manager
73 BOOST_ASSERT(m_IntraLayerMemoryMgr);
74 IPoolManager* poolManager = boost::polymorphic_downcast<IPoolManager*>(m_IntraLayerMemoryMgr->pool_manager());
75 BOOST_ASSERT(poolManager);
76 poolManager->AllocatePools();
77
78 // Allocate memory pools for inter-layer memory manager
79 BOOST_ASSERT(m_InterLayerMemoryMgr);
80 poolManager = boost::polymorphic_downcast<IPoolManager*>(m_InterLayerMemoryMgr->pool_manager());
81 BOOST_ASSERT(poolManager);
82 poolManager->AllocatePools();
83
84 // Acquire inter-layer memory group. NOTE: This has to come after allocating the pools
85 BOOST_ASSERT(m_InterLayerMemoryGroup);
86 m_InterLayerMemoryGroup->acquire();
87}
88
89void BaseMemoryManager::Release()
90{
91 // Release inter-layer memory group. NOTE: This has to come before releasing the pools
92 BOOST_ASSERT(m_InterLayerMemoryGroup);
93 m_InterLayerMemoryGroup->release();
94
95 // Release memory pools managed by intra-layer memory manager
96 BOOST_ASSERT(m_IntraLayerMemoryMgr);
97 IPoolManager* poolManager = boost::polymorphic_downcast<IPoolManager*>(m_IntraLayerMemoryMgr->pool_manager());
98 BOOST_ASSERT(poolManager);
99 poolManager->ReleasePools();
100
101 // Release memory pools managed by inter-layer memory manager
102 BOOST_ASSERT(m_InterLayerMemoryMgr);
103 poolManager = boost::polymorphic_downcast<IPoolManager*>(m_InterLayerMemoryMgr->pool_manager());
104 BOOST_ASSERT(poolManager);
105 poolManager->ReleasePools();
106}
107#endif
108
109#ifdef ARMCOMPUTENEON_ENABLED
110std::shared_ptr<arm_compute::IMemoryGroup>
111NeonMemoryManager::CreateMemoryGroup(const std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager)
112{
113 return std::make_shared<arm_compute::MemoryGroup>(memoryManager);
114}
115#endif
116
117#ifdef ARMCOMPUTECL_ENABLED
118std::shared_ptr<arm_compute::IMemoryGroup>
119ClMemoryManager::CreateMemoryGroup(const std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager)
120{
121 return std::make_shared<arm_compute::CLMemoryGroup>(memoryManager);
122}
123#endif
124
125}