aboutsummaryrefslogtreecommitdiff
path: root/src/backends/aclCommon/memory/OffsetLifetimeManager.cpp
blob: d0174f893c1fb987a40fd6b792837e016455bed5 (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
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//
#include "OffsetLifetimeManager.hpp"
#include "OffsetMemoryPool.hpp"

#include <arm_compute/runtime/IMemoryGroup.h>

#include <numeric>

#include <boost/assert.hpp>

namespace armnn
{

OffsetLifetimeManager::OffsetLifetimeManager()
    : m_BlobSize(0)
{
}

std::unique_ptr<arm_compute::IMemoryPool> OffsetLifetimeManager::create_pool(arm_compute::IAllocator* allocator)
{
    BOOST_ASSERT(allocator);
    return std::make_unique<OffsetMemoryPool>(allocator, m_BlobSize);
}

arm_compute::MappingType OffsetLifetimeManager::mapping_type() const
{
    return arm_compute::MappingType::OFFSETS;
}

void OffsetLifetimeManager::update_blobs_and_mappings()
{
    BOOST_ASSERT(are_all_finalized());
    BOOST_ASSERT(_active_group);

    // Update blob size
    size_t maxGroupSize = std::accumulate(std::begin(_free_blobs), std::end(_free_blobs),
        static_cast<size_t>(0), [](size_t s, const Blob& b)
    {
        return s + b.max_size;
    });
    m_BlobSize = std::max(m_BlobSize, maxGroupSize);

    // Calculate group mappings
    auto& groupMappings = _active_group->mappings();
    size_t offset = 0;
    for(auto& freeBlob : _free_blobs)
    {
        for(auto& boundElementId : freeBlob.bound_elements)
        {
            BOOST_ASSERT(_active_elements.find(boundElementId) != std::end(_active_elements));
            Element& boundElement = _active_elements[boundElementId];
            groupMappings[boundElement.handle] = offset;
        }
        offset += freeBlob.max_size;
        BOOST_ASSERT(offset <= m_BlobSize);
    }
}

} // namespace armnn