IVGCVSW-3882 Update ACL pin
[platform/upstream/armnn.git] / src / backends / aclCommon / BaseMemoryManager.cpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #include "BaseMemoryManager.hpp"
6
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"
11 #endif
12
13 #include <boost/polymorphic_cast.hpp>
14
15 namespace armnn
16 {
17
18 #if defined(ARMCOMPUTENEON_ENABLED) || defined(ARMCOMPUTECL_ENABLED)
19 BaseMemoryManager::BaseMemoryManager(std::unique_ptr<arm_compute::IAllocator> alloc,
20                                      MemoryAffinity memoryAffinity)
21 {
22     BOOST_ASSERT(alloc);
23     m_Allocator = std::move(alloc);
24
25     m_IntraLayerMemoryMgr = CreateArmComputeMemoryManager(memoryAffinity);
26     m_InterLayerMemoryMgr = CreateArmComputeMemoryManager(memoryAffinity);
27 }
28
29 std::shared_ptr<arm_compute::MemoryManagerOnDemand>
30 BaseMemoryManager::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<arm_compute::BlobLifetimeManager>();
37     }
38     else
39     {
40         lifetimeManager = std::make_shared<arm_compute::OffsetLifetimeManager>();
41     }
42
43     auto poolManager   = std::make_shared<arm_compute::PoolManager>();
44     auto memoryManager = std::make_shared<arm_compute::MemoryManagerOnDemand>(lifetimeManager, poolManager);
45
46     return memoryManager;
47 }
48
49 void BaseMemoryManager::Acquire()
50 {
51     static const size_t s_NumPools = 1;
52
53     // Allocate memory pools for intra-layer memory manager
54     BOOST_ASSERT(m_IntraLayerMemoryMgr);
55     m_IntraLayerMemoryMgr->populate(*m_Allocator, s_NumPools);
56
57     // Allocate memory pools for inter-layer memory manager
58     BOOST_ASSERT(m_InterLayerMemoryMgr);
59     m_InterLayerMemoryMgr->populate(*m_Allocator, s_NumPools);
60
61     // Acquire inter-layer memory group. NOTE: This has to come after allocating the pools
62     BOOST_ASSERT(m_InterLayerMemoryGroup);
63     m_InterLayerMemoryGroup->acquire();
64 }
65
66 void BaseMemoryManager::Release()
67 {
68     // Release inter-layer memory group. NOTE: This has to come before releasing the pools
69     BOOST_ASSERT(m_InterLayerMemoryGroup);
70     m_InterLayerMemoryGroup->release();
71
72     // Release memory pools managed by intra-layer memory manager
73     BOOST_ASSERT(m_IntraLayerMemoryMgr);
74     m_IntraLayerMemoryMgr->clear();
75
76     // Release memory pools managed by inter-layer memory manager
77     BOOST_ASSERT(m_InterLayerMemoryMgr);
78     m_InterLayerMemoryMgr->clear();
79 }
80 #else
81 void BaseMemoryManager::Acquire()
82 {
83     // No-op if neither NEON nor CL enabled
84 }
85
86 void BaseMemoryManager::Release()
87 {
88     // No-op if neither NEON nor CL enabled
89 }
90 #endif
91
92 #if defined(ARMCOMPUTENEON_ENABLED)
93 std::shared_ptr<arm_compute::IMemoryGroup>
94 NeonMemoryManager::CreateMemoryGroup(const std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager)
95 {
96     return std::make_shared<arm_compute::MemoryGroup>(memoryManager);
97 }
98 #endif
99
100 #if defined(ARMCOMPUTECL_ENABLED)
101 std::shared_ptr<arm_compute::IMemoryGroup>
102 ClMemoryManager::CreateMemoryGroup(const std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager)
103 {
104     return std::make_shared<arm_compute::MemoryGroup>(memoryManager);
105 }
106 #endif
107
108 }