telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [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 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
David Beck | 0dbe0ee | 2018-09-24 15:59:27 +0100 | [diff] [blame] | 5 | #include <armnn/Exceptions.hpp> |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 6 | |
| 7 | #include <backendsCommon/CpuTensorHandle.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 8 | |
| 9 | #include <cstring> |
| 10 | |
| 11 | namespace armnn |
| 12 | { |
| 13 | |
| 14 | ConstCpuTensorHandle::ConstCpuTensorHandle(const TensorInfo& tensorInfo) |
| 15 | : m_TensorInfo(tensorInfo) |
| 16 | , m_Memory(nullptr) |
| 17 | { |
| 18 | } |
| 19 | |
| 20 | template <> |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 21 | const void* ConstCpuTensorHandle::GetConstTensor<void>() const |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 22 | { |
| 23 | return m_Memory; |
| 24 | } |
| 25 | |
| 26 | CpuTensorHandle::CpuTensorHandle(const TensorInfo& tensorInfo) |
| 27 | : ConstCpuTensorHandle(tensorInfo) |
| 28 | , m_MutableMemory(nullptr) |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | template <> |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 33 | void* CpuTensorHandle::GetTensor<void>() const |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 34 | { |
| 35 | return m_MutableMemory; |
| 36 | } |
| 37 | |
| 38 | ScopedCpuTensorHandle::ScopedCpuTensorHandle(const TensorInfo& tensorInfo) |
| 39 | : CpuTensorHandle(tensorInfo) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | ScopedCpuTensorHandle::ScopedCpuTensorHandle(const ConstTensor& tensor) |
| 44 | : ScopedCpuTensorHandle(tensor.GetInfo()) |
| 45 | { |
| 46 | CopyFrom(tensor.GetMemoryArea(), tensor.GetNumBytes()); |
| 47 | } |
| 48 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 49 | ScopedCpuTensorHandle::ScopedCpuTensorHandle(const ConstCpuTensorHandle& tensorHandle) |
| 50 | : ScopedCpuTensorHandle(tensorHandle.GetTensorInfo()) |
| 51 | { |
| 52 | CopyFrom(tensorHandle.GetConstTensor<void>(), tensorHandle.GetTensorInfo().GetNumBytes()); |
| 53 | } |
| 54 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 55 | ScopedCpuTensorHandle::ScopedCpuTensorHandle(const ScopedCpuTensorHandle& other) |
| 56 | : CpuTensorHandle(other.GetTensorInfo()) |
| 57 | { |
| 58 | CopyFrom(other); |
| 59 | } |
| 60 | |
| 61 | ScopedCpuTensorHandle& ScopedCpuTensorHandle::operator=(const ScopedCpuTensorHandle& other) |
| 62 | { |
| 63 | ::operator delete(GetTensor<void>()); |
| 64 | SetMemory(nullptr); |
| 65 | CopyFrom(other); |
| 66 | return *this; |
| 67 | } |
| 68 | |
| 69 | ScopedCpuTensorHandle::~ScopedCpuTensorHandle() |
| 70 | { |
| 71 | ::operator delete(GetTensor<void>()); |
| 72 | } |
| 73 | |
| 74 | void ScopedCpuTensorHandle::Allocate() |
| 75 | { |
| 76 | if (GetTensor<void>() == nullptr) |
| 77 | { |
| 78 | SetMemory(::operator new(GetTensorInfo().GetNumBytes())); |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | throw InvalidArgumentException("CpuTensorHandle::Allocate Trying to allocate a CpuTensorHandle" |
| 83 | "that already has allocated memory."); |
| 84 | } |
| 85 | } |
| 86 | |
David Beck | 09e2f27 | 2018-10-30 11:38:41 +0000 | [diff] [blame] | 87 | void ScopedCpuTensorHandle::CopyOutTo(void* memory) const |
| 88 | { |
| 89 | memcpy(memory, GetTensor<void>(), GetTensorInfo().GetNumBytes()); |
| 90 | } |
| 91 | |
| 92 | void ScopedCpuTensorHandle::CopyInFrom(const void* memory) |
| 93 | { |
| 94 | memcpy(GetTensor<void>(), memory, GetTensorInfo().GetNumBytes()); |
| 95 | } |
| 96 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 97 | void ScopedCpuTensorHandle::CopyFrom(const ScopedCpuTensorHandle& other) |
| 98 | { |
| 99 | CopyFrom(other.GetTensor<void>(), other.GetTensorInfo().GetNumBytes()); |
| 100 | } |
| 101 | |
| 102 | void ScopedCpuTensorHandle::CopyFrom(const void* srcMemory, unsigned int numBytes) |
| 103 | { |
| 104 | BOOST_ASSERT(GetTensor<void>() == nullptr); |
| 105 | BOOST_ASSERT(GetTensorInfo().GetNumBytes() == numBytes); |
| 106 | |
| 107 | if (srcMemory) |
| 108 | { |
| 109 | Allocate(); |
| 110 | memcpy(GetTensor<void>(), srcMemory, numBytes); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void PassthroughCpuTensorHandle::Allocate() |
| 115 | { |
| 116 | throw InvalidArgumentException("PassthroughCpuTensorHandle::Allocate() should never be called"); |
| 117 | } |
| 118 | |
| 119 | void ConstPassthroughCpuTensorHandle::Allocate() |
| 120 | { |
| 121 | throw InvalidArgumentException("ConstPassthroughCpuTensorHandle::Allocate() should never be called"); |
| 122 | } |
| 123 | |
| 124 | } // namespace armnn |