Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | #include "RefTensorHandle.hpp" |
| 6 | |
| 7 | namespace armnn |
| 8 | { |
| 9 | |
Matthew Bentham | 7c1603a | 2019-06-21 17:22:23 +0100 | [diff] [blame] | 10 | RefTensorHandle::RefTensorHandle(const TensorInfo &tensorInfo, std::shared_ptr<RefMemoryManager> &memoryManager): |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 11 | m_TensorInfo(tensorInfo), |
Matthew Bentham | 7c1603a | 2019-06-21 17:22:23 +0100 | [diff] [blame] | 12 | m_MemoryManager(memoryManager), |
| 13 | m_Pool(nullptr), |
| 14 | m_UnmanagedMemory(nullptr) |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 15 | { |
| 16 | |
| 17 | } |
| 18 | |
| 19 | RefTensorHandle::~RefTensorHandle() |
| 20 | { |
Matthew Bentham | 7c1603a | 2019-06-21 17:22:23 +0100 | [diff] [blame] | 21 | if (!m_Pool) |
| 22 | { |
| 23 | // unmanaged |
| 24 | ::operator delete(m_UnmanagedMemory); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | void RefTensorHandle::Manage() |
| 29 | { |
| 30 | BOOST_ASSERT_MSG(!m_Pool, "RefTensorHandle::Manage() called twice"); |
| 31 | BOOST_ASSERT_MSG(!m_UnmanagedMemory, "RefTensorHandle::Manage() called after Allocate()"); |
| 32 | |
| 33 | m_Pool = m_MemoryManager->Manage(m_TensorInfo.GetNumBytes()); |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | void RefTensorHandle::Allocate() |
| 37 | { |
Matthew Bentham | 7c1603a | 2019-06-21 17:22:23 +0100 | [diff] [blame] | 38 | if (!m_UnmanagedMemory) |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 39 | { |
Matthew Bentham | 7c1603a | 2019-06-21 17:22:23 +0100 | [diff] [blame] | 40 | if (!m_Pool) |
| 41 | { |
| 42 | // unmanaged |
| 43 | m_UnmanagedMemory = ::operator new(m_TensorInfo.GetNumBytes()); |
| 44 | } |
| 45 | else |
| 46 | { |
| 47 | m_MemoryManager->Allocate(m_Pool); |
| 48 | } |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 49 | } |
| 50 | else |
| 51 | { |
| 52 | throw InvalidArgumentException("RefTensorHandle::Allocate Trying to allocate a RefTensorHandle" |
Matthew Bentham | 7c1603a | 2019-06-21 17:22:23 +0100 | [diff] [blame] | 53 | "that already has allocated memory."); |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
Matthew Bentham | 7c1603a | 2019-06-21 17:22:23 +0100 | [diff] [blame] | 57 | const void* RefTensorHandle::Map(bool /*unused*/) const |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 58 | { |
Matthew Bentham | 7c1603a | 2019-06-21 17:22:23 +0100 | [diff] [blame] | 59 | return GetPointer(); |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 60 | } |
| 61 | |
Matthew Bentham | 7c1603a | 2019-06-21 17:22:23 +0100 | [diff] [blame] | 62 | void* RefTensorHandle::GetPointer() const |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 63 | { |
Matthew Bentham | 7c1603a | 2019-06-21 17:22:23 +0100 | [diff] [blame] | 64 | if (m_UnmanagedMemory) |
| 65 | { |
| 66 | return m_UnmanagedMemory; |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | BOOST_ASSERT_MSG(m_Pool, "RefTensorHandle::GetPointer called on unmanaged, unallocated tensor handle"); |
| 71 | return m_MemoryManager->GetPointer(m_Pool); |
| 72 | } |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Matthew Bentham | 7c1603a | 2019-06-21 17:22:23 +0100 | [diff] [blame] | 75 | void RefTensorHandle::CopyOutTo(void* dest) const |
| 76 | { |
| 77 | const void *src = GetPointer(); |
| 78 | BOOST_ASSERT(src); |
| 79 | memcpy(dest, src, m_TensorInfo.GetNumBytes()); |
| 80 | } |
| 81 | |
| 82 | void RefTensorHandle::CopyInFrom(const void* src) |
| 83 | { |
| 84 | void *dest = GetPointer(); |
| 85 | BOOST_ASSERT(dest); |
| 86 | memcpy(dest, src, m_TensorInfo.GetNumBytes()); |
| 87 | } |
| 88 | |
| 89 | } |