ArmNN
 24.02
BaseMemoryManager.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017-2023 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_ASSERT(alloc);
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
54  m_IntraLayerMemoryMgr->populate(*m_Allocator, s_NumPools);
55 
56  // Allocate memory pools for inter-layer memory manager
58  m_InterLayerMemoryMgr->populate(*m_Allocator, s_NumPools);
59 
60  // Acquire inter-layer memory group. NOTE: This has to come after allocating the pools
62  m_InterLayerMemoryGroup->acquire();
63 }
64 
66 {
67  // Release inter-layer memory group. NOTE: This has to come before releasing the pools
69  m_InterLayerMemoryGroup->release();
70 
71  // Release memory pools managed by intra-layer memory manager
73  m_IntraLayerMemoryMgr->clear();
74 
75  // Release memory pools managed by inter-layer memory manager
77  m_InterLayerMemoryMgr->clear();
78 }
79 #else
81 {
82  // No-op if neither NEON nor CL enabled
83 }
84 
86 {
87  // No-op if neither NEON nor CL enabled
88 }
89 #endif
90 
91 #if defined(ARMCOMPUTENEON_ENABLED)
92 std::shared_ptr<arm_compute::IMemoryGroup>
93 NeonMemoryManager::CreateMemoryGroup(const std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager)
94 {
95  return std::make_shared<arm_compute::MemoryGroup>(memoryManager);
96 }
97 #endif
98 
99 #if defined(ARMCOMPUTECL_ENABLED)
100 std::shared_ptr<arm_compute::IMemoryGroup>
101 ClMemoryManager::CreateMemoryGroup(const std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager)
102 {
103  return std::make_shared<arm_compute::MemoryGroup>(memoryManager);
104 }
105 #endif
106 
107 #if defined(ARMCOMPUTEGPUFSA_ENABLED)
108 std::shared_ptr<arm_compute::IMemoryGroup>
109 GpuFsaMemoryManager::CreateMemoryGroup(const std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager)
110 {
111  return std::make_shared<arm_compute::MemoryGroup>(memoryManager);
112 }
113 #endif
114 
115 }
ARMNN_ASSERT
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
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:93
armnn::ClMemoryManager::CreateMemoryGroup
std::shared_ptr< arm_compute::IMemoryGroup > CreateMemoryGroup(const std::shared_ptr< arm_compute::MemoryManagerOnDemand > &memoryManager) override
Definition: BaseMemoryManager.cpp:101
armnn::BaseMemoryManager::Release
void Release() override
Definition: BaseMemoryManager.cpp:65
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