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 | |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 14 | TensorShape GetUnpaddedTensorStrides(const TensorInfo& tensorInfo) |
| 15 | { |
| 16 | TensorShape shape(tensorInfo.GetShape()); |
| 17 | auto size = GetDataTypeSize(tensorInfo.GetDataType()); |
| 18 | auto runningSize = size; |
| 19 | std::vector<unsigned int> strides(shape.GetNumDimensions()); |
| 20 | auto lastIdx = shape.GetNumDimensions()-1; |
| 21 | for (unsigned int i=0; i < lastIdx ; i++) |
| 22 | { |
| 23 | strides[lastIdx-i] = runningSize; |
| 24 | runningSize *= shape[lastIdx-i]; |
| 25 | } |
| 26 | strides[0] = runningSize; |
| 27 | return TensorShape(shape.GetNumDimensions(), strides.data()); |
| 28 | } |
| 29 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 30 | ConstCpuTensorHandle::ConstCpuTensorHandle(const TensorInfo& tensorInfo) |
| 31 | : m_TensorInfo(tensorInfo) |
| 32 | , m_Memory(nullptr) |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | template <> |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 37 | const void* ConstCpuTensorHandle::GetConstTensor<void>() const |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 38 | { |
| 39 | return m_Memory; |
| 40 | } |
| 41 | |
| 42 | CpuTensorHandle::CpuTensorHandle(const TensorInfo& tensorInfo) |
| 43 | : ConstCpuTensorHandle(tensorInfo) |
| 44 | , m_MutableMemory(nullptr) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | template <> |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 49 | void* CpuTensorHandle::GetTensor<void>() const |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 50 | { |
| 51 | return m_MutableMemory; |
| 52 | } |
| 53 | |
| 54 | ScopedCpuTensorHandle::ScopedCpuTensorHandle(const TensorInfo& tensorInfo) |
| 55 | : CpuTensorHandle(tensorInfo) |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | ScopedCpuTensorHandle::ScopedCpuTensorHandle(const ConstTensor& tensor) |
| 60 | : ScopedCpuTensorHandle(tensor.GetInfo()) |
| 61 | { |
| 62 | CopyFrom(tensor.GetMemoryArea(), tensor.GetNumBytes()); |
| 63 | } |
| 64 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 65 | ScopedCpuTensorHandle::ScopedCpuTensorHandle(const ConstCpuTensorHandle& tensorHandle) |
| 66 | : ScopedCpuTensorHandle(tensorHandle.GetTensorInfo()) |
| 67 | { |
| 68 | CopyFrom(tensorHandle.GetConstTensor<void>(), tensorHandle.GetTensorInfo().GetNumBytes()); |
| 69 | } |
| 70 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 71 | ScopedCpuTensorHandle::ScopedCpuTensorHandle(const ScopedCpuTensorHandle& other) |
| 72 | : CpuTensorHandle(other.GetTensorInfo()) |
| 73 | { |
| 74 | CopyFrom(other); |
| 75 | } |
| 76 | |
| 77 | ScopedCpuTensorHandle& ScopedCpuTensorHandle::operator=(const ScopedCpuTensorHandle& other) |
| 78 | { |
| 79 | ::operator delete(GetTensor<void>()); |
| 80 | SetMemory(nullptr); |
| 81 | CopyFrom(other); |
| 82 | return *this; |
| 83 | } |
| 84 | |
| 85 | ScopedCpuTensorHandle::~ScopedCpuTensorHandle() |
| 86 | { |
| 87 | ::operator delete(GetTensor<void>()); |
| 88 | } |
| 89 | |
| 90 | void ScopedCpuTensorHandle::Allocate() |
| 91 | { |
| 92 | if (GetTensor<void>() == nullptr) |
| 93 | { |
| 94 | SetMemory(::operator new(GetTensorInfo().GetNumBytes())); |
| 95 | } |
| 96 | else |
| 97 | { |
| 98 | throw InvalidArgumentException("CpuTensorHandle::Allocate Trying to allocate a CpuTensorHandle" |
| 99 | "that already has allocated memory."); |
| 100 | } |
| 101 | } |
| 102 | |
David Beck | 09e2f27 | 2018-10-30 11:38:41 +0000 | [diff] [blame] | 103 | void ScopedCpuTensorHandle::CopyOutTo(void* memory) const |
| 104 | { |
| 105 | memcpy(memory, GetTensor<void>(), GetTensorInfo().GetNumBytes()); |
| 106 | } |
| 107 | |
| 108 | void ScopedCpuTensorHandle::CopyInFrom(const void* memory) |
| 109 | { |
| 110 | memcpy(GetTensor<void>(), memory, GetTensorInfo().GetNumBytes()); |
| 111 | } |
| 112 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 113 | void ScopedCpuTensorHandle::CopyFrom(const ScopedCpuTensorHandle& other) |
| 114 | { |
| 115 | CopyFrom(other.GetTensor<void>(), other.GetTensorInfo().GetNumBytes()); |
| 116 | } |
| 117 | |
| 118 | void ScopedCpuTensorHandle::CopyFrom(const void* srcMemory, unsigned int numBytes) |
| 119 | { |
| 120 | BOOST_ASSERT(GetTensor<void>() == nullptr); |
| 121 | BOOST_ASSERT(GetTensorInfo().GetNumBytes() == numBytes); |
| 122 | |
| 123 | if (srcMemory) |
| 124 | { |
| 125 | Allocate(); |
| 126 | memcpy(GetTensor<void>(), srcMemory, numBytes); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | void PassthroughCpuTensorHandle::Allocate() |
| 131 | { |
| 132 | throw InvalidArgumentException("PassthroughCpuTensorHandle::Allocate() should never be called"); |
| 133 | } |
| 134 | |
| 135 | void ConstPassthroughCpuTensorHandle::Allocate() |
| 136 | { |
| 137 | throw InvalidArgumentException("ConstPassthroughCpuTensorHandle::Allocate() should never be called"); |
| 138 | } |
| 139 | |
| 140 | } // namespace armnn |