ArmNN
 24.08
BaseMemoryManager.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017-2024 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #include "BaseMemoryManager.hpp"
6 
7 #if defined(ARMCOMPUTENEON_ENABLED) || defined(ARMCOMPUTECL_ENABLED) || defined(ARMCOMPUTEGPUFSA_ENABLED)
8 #include "arm_compute/runtime/BlobLifetimeManager.h"
9 #include "arm_compute/runtime/PoolManager.h"
10 #include "arm_compute/runtime/OffsetLifetimeManager.h"
11 #endif
12 
13 
14 namespace armnn
15 {
16 
17 #if defined(ARMCOMPUTENEON_ENABLED) || defined(ARMCOMPUTECL_ENABLED) || defined(ARMCOMPUTEGPUFSA_ENABLED)
18 BaseMemoryManager::BaseMemoryManager(std::shared_ptr<arm_compute::IAllocator> alloc,
19  MemoryAffinity memoryAffinity)
20 {
21  ARMNN_THROW_INVALIDARG_MSG_IF_FALSE(alloc, "A null allocator has been passed to BaseMemoryManager.");
22  m_Allocator = std::move(alloc);
23 
26 }
27 
28 std::shared_ptr<arm_compute::MemoryManagerOnDemand>
30 {
31  std::shared_ptr<arm_compute::ILifetimeManager> lifetimeManager = nullptr;
32 
33  if (memoryAffinity == MemoryAffinity::Buffer)
34  {
35  lifetimeManager = std::make_shared<arm_compute::BlobLifetimeManager>();
36  }
37  else
38  {
39  lifetimeManager = std::make_shared<arm_compute::OffsetLifetimeManager>();
40  }
41 
42  auto poolManager = std::make_shared<arm_compute::PoolManager>();
43  auto memoryManager = std::make_shared<arm_compute::MemoryManagerOnDemand>(lifetimeManager, poolManager);
44 
45  return memoryManager;
46 }
47 
49 {
50  static const size_t s_NumPools = 1;
51 
52  // Allocate memory pools for intra-layer memory manager
53  m_IntraLayerMemoryMgr->populate(*m_Allocator, s_NumPools);
54 
55  // Allocate memory pools for inter-layer memory manager
56  m_InterLayerMemoryMgr->populate(*m_Allocator, s_NumPools);
57 
58  // Acquire inter-layer memory group. NOTE: This has to come after allocating the pools
59  m_InterLayerMemoryGroup->acquire();
60 }
61 
63 {
64  // Release inter-layer memory group. NOTE: This has to come before releasing the pools
65  m_InterLayerMemoryGroup->release();
66 
67  // Release memory pools managed by intra-layer memory manager
68  m_IntraLayerMemoryMgr->clear();
69 
70  // Release memory pools managed by inter-layer memory manager
71  m_InterLayerMemoryMgr->clear();
72 }
73 #else
75 {
76  // No-op if neither NEON nor CL enabled
77 }
78 
80 {
81  // No-op if neither NEON nor CL enabled
82 }
83 #endif
84 
85 #if defined(ARMCOMPUTENEON_ENABLED)
86 std::shared_ptr<arm_compute::IMemoryGroup>
87 NeonMemoryManager::CreateMemoryGroup(const std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager)
88 {
89  return std::make_shared<arm_compute::MemoryGroup>(memoryManager);
90 }
91 #endif
92 
93 #if defined(ARMCOMPUTECL_ENABLED)
94 std::shared_ptr<arm_compute::IMemoryGroup>
95 ClMemoryManager::CreateMemoryGroup(const std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager)
96 {
97  return std::make_shared<arm_compute::MemoryGroup>(memoryManager);
98 }
99 #endif
100 
101 #if defined(ARMCOMPUTEGPUFSA_ENABLED)
102 std::shared_ptr<arm_compute::IMemoryGroup>
103 GpuFsaMemoryManager::CreateMemoryGroup(const std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager)
104 {
105  return std::make_shared<arm_compute::MemoryGroup>(memoryManager);
106 }
107 #endif
108 
109 }
armnn::BaseMemoryManager::m_InterLayerMemoryGroup
std::shared_ptr< arm_compute::IMemoryGroup > m_InterLayerMemoryGroup
Definition: BaseMemoryManager.hpp:50
BaseMemoryManager.hpp
armnn::BaseMemoryManager::m_Allocator
std::shared_ptr< arm_compute::IAllocator > m_Allocator
Definition: BaseMemoryManager.hpp:47
armnn::BaseMemoryManager::CreateArmComputeMemoryManager
std::shared_ptr< arm_compute::MemoryManagerOnDemand > CreateArmComputeMemoryManager(MemoryAffinity memoryAffinity)
Definition: BaseMemoryManager.cpp:29
armnn::BaseMemoryManager::MemoryAffinity
MemoryAffinity
Definition: BaseMemoryManager.hpp:27
armnn::BaseMemoryManager::m_InterLayerMemoryMgr
std::shared_ptr< arm_compute::MemoryManagerOnDemand > m_InterLayerMemoryMgr
Definition: BaseMemoryManager.hpp:49
armnn::BaseMemoryManager::MemoryAffinity::Buffer
@ Buffer
armnn::NeonMemoryManager::CreateMemoryGroup
std::shared_ptr< arm_compute::IMemoryGroup > CreateMemoryGroup(const std::shared_ptr< arm_compute::MemoryManagerOnDemand > &memoryManager) override
Definition: BaseMemoryManager.cpp:87
armnn::ClMemoryManager::CreateMemoryGroup
std::shared_ptr< arm_compute::IMemoryGroup > CreateMemoryGroup(const std::shared_ptr< arm_compute::MemoryManagerOnDemand > &memoryManager) override
Definition: BaseMemoryManager.cpp:95
armnn::BaseMemoryManager::Release
void Release() override
Definition: BaseMemoryManager.cpp:62
armnn::BaseMemoryManager::m_IntraLayerMemoryMgr
std::shared_ptr< arm_compute::MemoryManagerOnDemand > m_IntraLayerMemoryMgr
Definition: BaseMemoryManager.hpp:48
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::BaseMemoryManager::Acquire
void Acquire() override
Definition: BaseMemoryManager.cpp:48
armnn::BaseMemoryManager::BaseMemoryManager
BaseMemoryManager()
Definition: BaseMemoryManager.hpp:33
ARMNN_THROW_INVALIDARG_MSG_IF_FALSE
#define ARMNN_THROW_INVALIDARG_MSG_IF_FALSE(_cond, _str)
Definition: Exceptions.hpp:210