2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // See LICENSE file in the project root for full license information.
5 #include "BaseMemoryManager.hpp"
7 #if defined(ARMCOMPUTENEON_ENABLED) || defined(ARMCOMPUTECL_ENABLED)
8 #include "memory/BlobLifetimeManager.hpp"
9 #include "memory/PoolManager.hpp"
10 #include "memory/OffsetLifetimeManager.hpp"
13 #include <boost/polymorphic_cast.hpp>
18 #if defined(ARMCOMPUTENEON_ENABLED) || defined(ARMCOMPUTECL_ENABLED)
19 BaseMemoryManager::BaseMemoryManager(std::unique_ptr<arm_compute::IAllocator> alloc,
20 MemoryAffinity memoryAffinity)
22 // (Re)create the memory manager components.
23 m_Allocator = std::move(alloc);
25 m_IntraLayerMemoryMgr = CreateArmComputeMemoryManager(memoryAffinity);
26 m_InterLayerMemoryMgr = CreateArmComputeMemoryManager(memoryAffinity);
29 std::shared_ptr<arm_compute::MemoryManagerOnDemand>
30 BaseMemoryManager::CreateArmComputeMemoryManager(MemoryAffinity memoryAffinity)
32 std::shared_ptr<arm_compute::ILifetimeManager> lifetimeManager = nullptr;
34 if (memoryAffinity == MemoryAffinity::Buffer)
36 lifetimeManager = std::make_shared<BlobLifetimeManager>();
40 lifetimeManager = std::make_shared<OffsetLifetimeManager>();
43 auto poolManager = std::make_shared<PoolManager>();
44 auto memoryManager = std::make_shared<arm_compute::MemoryManagerOnDemand>(lifetimeManager, poolManager);
46 // Set allocator that the memory manager will use
47 memoryManager->set_allocator(m_Allocator.get());
52 void BaseMemoryManager::FinalizeMemoryManager(arm_compute::MemoryManagerOnDemand& memoryManager)
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);
57 // Finalize the memory manager. (Validity checks, memory allocations, etc)
58 memoryManager.finalize();
61 void BaseMemoryManager::Finalize()
63 BOOST_ASSERT(m_IntraLayerMemoryMgr);
64 FinalizeMemoryManager(*m_IntraLayerMemoryMgr.get());
66 BOOST_ASSERT(m_InterLayerMemoryMgr);
67 FinalizeMemoryManager(*m_InterLayerMemoryMgr.get());
70 void BaseMemoryManager::Acquire()
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();
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();
84 // Acquire inter-layer memory group. NOTE: This has to come after allocating the pools
85 BOOST_ASSERT(m_InterLayerMemoryGroup);
86 m_InterLayerMemoryGroup->acquire();
89 void BaseMemoryManager::Release()
91 // Release inter-layer memory group. NOTE: This has to come before releasing the pools
92 BOOST_ASSERT(m_InterLayerMemoryGroup);
93 m_InterLayerMemoryGroup->release();
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();
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();
109 #ifdef ARMCOMPUTENEON_ENABLED
110 std::shared_ptr<arm_compute::IMemoryGroup>
111 NeonMemoryManager::CreateMemoryGroup(const std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager)
113 return std::make_shared<arm_compute::MemoryGroup>(memoryManager);
117 #ifdef ARMCOMPUTECL_ENABLED
118 std::shared_ptr<arm_compute::IMemoryGroup>
119 ClMemoryManager::CreateMemoryGroup(const std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager)
121 return std::make_shared<arm_compute::CLMemoryGroup>(memoryManager);