2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
5 #include "BaseMemoryManager.hpp"
7 #if defined(ARMCOMPUTENEON_ENABLED) || defined(ARMCOMPUTECL_ENABLED)
8 #include "arm_compute/runtime/BlobLifetimeManager.h"
9 #include "arm_compute/runtime/PoolManager.h"
10 #include "arm_compute/runtime/OffsetLifetimeManager.h"
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)
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<arm_compute::BlobLifetimeManager>();
40 lifetimeManager = std::make_shared<arm_compute::OffsetLifetimeManager>();
43 auto poolManager = std::make_shared<arm_compute::PoolManager>();
44 auto memoryManager = std::make_shared<arm_compute::MemoryManagerOnDemand>(lifetimeManager, poolManager);
49 void BaseMemoryManager::Acquire()
51 static const size_t s_NumPools = 1;
53 // Allocate memory pools for intra-layer memory manager
54 BOOST_ASSERT(m_IntraLayerMemoryMgr);
55 m_IntraLayerMemoryMgr->populate(*m_Allocator, s_NumPools);
57 // Allocate memory pools for inter-layer memory manager
58 BOOST_ASSERT(m_InterLayerMemoryMgr);
59 m_InterLayerMemoryMgr->populate(*m_Allocator, s_NumPools);
61 // Acquire inter-layer memory group. NOTE: This has to come after allocating the pools
62 BOOST_ASSERT(m_InterLayerMemoryGroup);
63 m_InterLayerMemoryGroup->acquire();
66 void BaseMemoryManager::Release()
68 // Release inter-layer memory group. NOTE: This has to come before releasing the pools
69 BOOST_ASSERT(m_InterLayerMemoryGroup);
70 m_InterLayerMemoryGroup->release();
72 // Release memory pools managed by intra-layer memory manager
73 BOOST_ASSERT(m_IntraLayerMemoryMgr);
74 m_IntraLayerMemoryMgr->clear();
76 // Release memory pools managed by inter-layer memory manager
77 BOOST_ASSERT(m_InterLayerMemoryMgr);
78 m_InterLayerMemoryMgr->clear();
81 void BaseMemoryManager::Acquire()
83 // No-op if neither NEON nor CL enabled
86 void BaseMemoryManager::Release()
88 // No-op if neither NEON nor CL enabled
92 #if defined(ARMCOMPUTENEON_ENABLED)
93 std::shared_ptr<arm_compute::IMemoryGroup>
94 NeonMemoryManager::CreateMemoryGroup(const std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager)
96 return std::make_shared<arm_compute::MemoryGroup>(memoryManager);
100 #if defined(ARMCOMPUTECL_ENABLED)
101 std::shared_ptr<arm_compute::IMemoryGroup>
102 ClMemoryManager::CreateMemoryGroup(const std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager)
104 return std::make_shared<arm_compute::MemoryGroup>(memoryManager);