aboutsummaryrefslogtreecommitdiff
path: root/src/armnn/memory/BaseMemoryManager.cpp
blob: 041c042baad6fdf38ddeb04feb37f1dc214eb65d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//
#include "BaseMemoryManager.hpp"

#if defined(ARMCOMPUTENEON_ENABLED) || defined(ARMCOMPUTECL_ENABLED)
#include "memory/BlobLifetimeManager.hpp"
#include "memory/PoolManager.hpp"
#include "memory/OffsetLifetimeManager.hpp"
#endif

#include <boost/polymorphic_cast.hpp>

namespace armnn
{

#if defined(ARMCOMPUTENEON_ENABLED) || defined(ARMCOMPUTECL_ENABLED)
BaseMemoryManager::BaseMemoryManager(std::unique_ptr<arm_compute::IAllocator> alloc,
                                     MemoryAffinity memoryAffinity)
{
    // (Re)create the memory manager components.
    m_Allocator = std::move(alloc);

    m_IntraLayerMemoryMgr = CreateArmComputeMemoryManager(memoryAffinity);
    m_InterLayerMemoryMgr = CreateArmComputeMemoryManager(memoryAffinity);
}

std::shared_ptr<arm_compute::MemoryManagerOnDemand>
BaseMemoryManager::CreateArmComputeMemoryManager(MemoryAffinity memoryAffinity)
{
    std::shared_ptr<arm_compute::ILifetimeManager> lifetimeManager = nullptr;

    if (memoryAffinity == MemoryAffinity::Buffer)
    {
        lifetimeManager = std::make_shared<BlobLifetimeManager>();
    }
    else
    {
        lifetimeManager = std::make_shared<OffsetLifetimeManager>();
    }

    auto poolManager   = std::make_shared<PoolManager>();
    auto memoryManager = std::make_shared<arm_compute::MemoryManagerOnDemand>(lifetimeManager, poolManager);

    // Set allocator that the memory manager will use
    memoryManager->set_allocator(m_Allocator.get());

    return memoryManager;
}

void BaseMemoryManager::FinalizeMemoryManager(arm_compute::MemoryManagerOnDemand& memoryManager)
{
    // Number of pools that the manager will create. This specifies how many layers you want to run in parallel
    memoryManager.set_num_pools(1);

    // Finalize the memory manager. (Validity checks, memory allocations, etc)
    memoryManager.finalize();
}

void BaseMemoryManager::Finalize()
{
    BOOST_ASSERT(m_IntraLayerMemoryMgr);
    FinalizeMemoryManager(*m_IntraLayerMemoryMgr.get());

    BOOST_ASSERT(m_InterLayerMemoryMgr);
    FinalizeMemoryManager(*m_InterLayerMemoryMgr.get());
}

void BaseMemoryManager::Acquire()
{
    // Allocate memory pools for intra-layer memory manager
    BOOST_ASSERT(m_IntraLayerMemoryMgr);
    IPoolManager* poolManager = boost::polymorphic_downcast<IPoolManager*>(m_IntraLayerMemoryMgr->pool_manager());
    BOOST_ASSERT(poolManager);
    poolManager->AllocatePools();

    // Allocate memory pools for inter-layer memory manager
    BOOST_ASSERT(m_InterLayerMemoryMgr);
    poolManager = boost::polymorphic_downcast<IPoolManager*>(m_InterLayerMemoryMgr->pool_manager());
    BOOST_ASSERT(poolManager);
    poolManager->AllocatePools();

    // Acquire inter-layer memory group. NOTE: This has to come after allocating the pools
    BOOST_ASSERT(m_InterLayerMemoryGroup);
    m_InterLayerMemoryGroup->acquire();
}

void BaseMemoryManager::Release()
{
    // Release inter-layer memory group. NOTE: This has to come before releasing the pools
    BOOST_ASSERT(m_InterLayerMemoryGroup);
    m_InterLayerMemoryGroup->release();

    // Release memory pools managed by intra-layer memory manager
    BOOST_ASSERT(m_IntraLayerMemoryMgr);
    IPoolManager* poolManager = boost::polymorphic_downcast<IPoolManager*>(m_IntraLayerMemoryMgr->pool_manager());
    BOOST_ASSERT(poolManager);
    poolManager->ReleasePools();

    // Release memory pools managed by inter-layer memory manager
    BOOST_ASSERT(m_InterLayerMemoryMgr);
    poolManager = boost::polymorphic_downcast<IPoolManager*>(m_InterLayerMemoryMgr->pool_manager());
    BOOST_ASSERT(poolManager);
    poolManager->ReleasePools();
}
#endif

#ifdef ARMCOMPUTENEON_ENABLED
std::shared_ptr<arm_compute::IMemoryGroup>
NeonMemoryManager::CreateMemoryGroup(const std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager)
{
    return std::make_shared<arm_compute::MemoryGroup>(memoryManager);
}
#endif

#ifdef ARMCOMPUTECL_ENABLED
std::shared_ptr<arm_compute::IMemoryGroup>
ClMemoryManager::CreateMemoryGroup(const std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager)
{
    return std::make_shared<arm_compute::CLMemoryGroup>(memoryManager);
}
#endif

}