telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4 | // |
| 5 | #include "BlobLifetimeManager.hpp" |
| 6 | #include "BlobMemoryPool.hpp" |
| 7 | |
Aron Virginas-Tar | f9aeef0 | 2018-10-12 15:18:03 +0100 | [diff] [blame] | 8 | #include <arm_compute/runtime/IMemoryGroup.h> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 9 | |
| 10 | #include "boost/assert.hpp" |
| 11 | |
| 12 | #include <algorithm> |
| 13 | |
| 14 | namespace armnn |
| 15 | { |
| 16 | |
| 17 | BlobLifetimeManager::BlobLifetimeManager() |
| 18 | : m_BlobSizes() |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | arm_compute::MappingType BlobLifetimeManager::mapping_type() const |
| 23 | { |
| 24 | return arm_compute::MappingType::BLOBS; |
| 25 | } |
| 26 | |
| 27 | void 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 | |
| 73 | std::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 |