blob: 41100e945f551b99cab6dde701dfc7ff6b94549c [file] [log] [blame]
telsoa01c577f2c2018-08-31 09:22:23 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa01c577f2c2018-08-31 09:22:23 +01004//
5#include "BlobLifetimeManager.hpp"
6#include "BlobMemoryPool.hpp"
7
Aron Virginas-Tarf9aeef02018-10-12 15:18:03 +01008#include <arm_compute/runtime/IMemoryGroup.h>
telsoa01c577f2c2018-08-31 09:22:23 +01009
10#include "boost/assert.hpp"
11
12#include <algorithm>
13
14namespace armnn
15{
16
17BlobLifetimeManager::BlobLifetimeManager()
18 : m_BlobSizes()
19{
20}
21
22arm_compute::MappingType BlobLifetimeManager::mapping_type() const
23{
24 return arm_compute::MappingType::BLOBS;
25}
26
27void BlobLifetimeManager::update_blobs_and_mappings()
28{
29 using namespace arm_compute;
30
31 BOOST_ASSERT(are_all_finalized());
32 BOOST_ASSERT(_active_group);
33
34 // Sort free blobs requirements in descending order.
35 _free_blobs.sort([](const Blob & ba, const Blob & bb)
36 {
37 return ba.max_size > bb.max_size;
38 });
39 std::vector<size_t> groupSizes;
40 std::transform(std::begin(_free_blobs), std::end(_free_blobs), std::back_inserter(groupSizes), [](const Blob & b)
41 {
42 return b.max_size;
43 });
44
45 // Update blob sizes
46 size_t max_size = std::max(m_BlobSizes.size(), groupSizes.size());
47 m_BlobSizes.resize(max_size, 0);
48 groupSizes.resize(max_size, 0);
49 std::transform(std::begin(m_BlobSizes), std::end(m_BlobSizes), std::begin(groupSizes),
50 std::begin(m_BlobSizes), [](size_t lhs, size_t rhs)
51 {
52 return std::max(lhs, rhs);
53 });
54
55 // Calculate group mappings
56 auto& groupMappings = _active_group->mappings();
57 unsigned int blobIdx = 0;
58
59 for(auto& freeBlob : _free_blobs)
60 {
61 for(auto& boundElementId : freeBlob.bound_elements)
62 {
63 BOOST_ASSERT(_active_elements.find(boundElementId) != std::end(_active_elements));
64
65 Element& boundElement = _active_elements[boundElementId];
66 groupMappings[boundElement.handle] = blobIdx;
67 }
68
69 ++blobIdx;
70 }
71}
72
73std::unique_ptr<arm_compute::IMemoryPool> BlobLifetimeManager::create_pool(arm_compute::IAllocator* allocator)
74{
75 BOOST_ASSERT(allocator);
76 return std::make_unique<BlobMemoryPool>(allocator, m_BlobSizes);
77}
78
79} // namespace armnn