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