ArmNN  NotReleased
CpuTensorHandle.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #include <armnn/Exceptions.hpp>
6 
8 
9 #include <cstring>
10 
11 namespace armnn
12 {
13 
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 
31 : m_TensorInfo(tensorInfo)
32 , m_Memory(nullptr)
33 {
34 }
35 
36 template <>
37 const void* ConstCpuTensorHandle::GetConstTensor<void>() const
38 {
39  return m_Memory;
40 }
41 
43 : ConstCpuTensorHandle(tensorInfo)
44 , m_MutableMemory(nullptr)
45 {
46 }
47 
48 template <>
49 void* CpuTensorHandle::GetTensor<void>() const
50 {
51  return m_MutableMemory;
52 }
53 
55 : CpuTensorHandle(tensorInfo)
56 {
57 }
58 
60 : ScopedCpuTensorHandle(tensor.GetInfo())
61 {
62  CopyFrom(tensor.GetMemoryArea(), tensor.GetNumBytes());
63 }
64 
66 : ScopedCpuTensorHandle(tensorHandle.GetTensorInfo())
67 {
68  CopyFrom(tensorHandle.GetConstTensor<void>(), tensorHandle.GetTensorInfo().GetNumBytes());
69 }
70 
73 {
74  CopyFrom(other);
75 }
76 
78 {
79  ::operator delete(GetTensor<void>());
80  SetMemory(nullptr);
81  CopyFrom(other);
82  return *this;
83 }
84 
86 {
87  ::operator delete(GetTensor<void>());
88 }
89 
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 
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 
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 
131 {
132  throw InvalidArgumentException("PassthroughCpuTensorHandle::Allocate() should never be called");
133 }
134 
136 {
137  throw InvalidArgumentException("ConstPassthroughCpuTensorHandle::Allocate() should never be called");
138 }
139 
140 } // namespace armnn
const T * GetConstTensor() const
unsigned int GetNumBytes() const
Definition: Tensor.hpp:174
CpuTensorHandle(const TensorInfo &tensorInfo)
const TensorInfo & GetTensorInfo() const
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:199
ConstCpuTensorHandle(const TensorInfo &tensorInfo)
void SetMemory(void *mem)
ScopedCpuTensorHandle & operator=(const ScopedCpuTensorHandle &other)
constexpr unsigned int GetDataTypeSize(DataType dataType)
Definition: TypesUtils.hpp:113
TensorShape GetUnpaddedTensorStrides(const TensorInfo &tensorInfo)
ScopedCpuTensorHandle(const TensorInfo &tensorInfo)
unsigned int GetNumBytes() const
Definition: Tensor.cpp:213
virtual void Allocate() override
DataType GetDataType() const
Definition: Tensor.hpp:95
MemoryType GetMemoryArea() const
Definition: Tensor.hpp:177
virtual void Allocate() override
const TensorShape & GetShape() const
Definition: Tensor.hpp:88