blob: f564dc63e291b16888b6d9c2a890313aa26f1b9e [file] [log] [blame]
Aron Virginas-Tar60578952018-10-31 11:04:01 +00001//
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
15namespace armnn
16{
17
18#if defined(ARMCOMPUTENEON_ENABLED) || defined(ARMCOMPUTECL_ENABLED)
19BaseMemoryManager::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
29std::shared_ptr<arm_compute::MemoryManagerOnDemand>
30BaseMemoryManager::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
49void 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
66void 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}
Aron Virginas-Tar56055192018-11-12 18:10:43 +000080#else
81void BaseMemoryManager::Acquire()
82{
83 // No-op if neither NEON nor CL enabled
84}
85
86void BaseMemoryManager::Release()
87{
88 // No-op if neither NEON nor CL enabled
89}
Aron Virginas-Tar60578952018-10-31 11:04:01 +000090#endif
91
92#ifdef ARMCOMPUTENEON_ENABLED
93std::shared_ptr<arm_compute::IMemoryGroup>
94NeonMemoryManager::CreateMemoryGroup(const std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager)
95{
96 return std::make_shared<arm_compute::MemoryGroup>(memoryManager);
97}
98#endif
99
100#ifdef ARMCOMPUTECL_ENABLED
101std::shared_ptr<arm_compute::IMemoryGroup>
102ClMemoryManager::CreateMemoryGroup(const std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager)
103{
104 return std::make_shared<arm_compute::CLMemoryGroup>(memoryManager);
105}
106#endif
107
108}