blob: de83048340d447468d6d06c13e3067bc95485e26 [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
David Beck0dbe0ee2018-09-24 15:59:27 +01005#include <armnn/Exceptions.hpp>
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +00006
7#include <backendsCommon/CpuTensorHandle.hpp>
telsoa014fcda012018-03-09 14:13:49 +00008
9#include <cstring>
10
11namespace armnn
12{
13
Matthew Bentham4cefc412019-06-18 16:14:34 +010014TensorShape 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
telsoa014fcda012018-03-09 14:13:49 +000030ConstCpuTensorHandle::ConstCpuTensorHandle(const TensorInfo& tensorInfo)
31: m_TensorInfo(tensorInfo)
32, m_Memory(nullptr)
33{
34}
35
36template <>
Matteo Martincigh747ef822018-12-18 09:26:39 +000037const void* ConstCpuTensorHandle::GetConstTensor<void>() const
telsoa014fcda012018-03-09 14:13:49 +000038{
39 return m_Memory;
40}
41
42CpuTensorHandle::CpuTensorHandle(const TensorInfo& tensorInfo)
43: ConstCpuTensorHandle(tensorInfo)
44, m_MutableMemory(nullptr)
45{
46}
47
48template <>
Matteo Martincigh747ef822018-12-18 09:26:39 +000049void* CpuTensorHandle::GetTensor<void>() const
telsoa014fcda012018-03-09 14:13:49 +000050{
51 return m_MutableMemory;
52}
53
54ScopedCpuTensorHandle::ScopedCpuTensorHandle(const TensorInfo& tensorInfo)
55: CpuTensorHandle(tensorInfo)
56{
57}
58
59ScopedCpuTensorHandle::ScopedCpuTensorHandle(const ConstTensor& tensor)
60: ScopedCpuTensorHandle(tensor.GetInfo())
61{
62 CopyFrom(tensor.GetMemoryArea(), tensor.GetNumBytes());
63}
64
telsoa01c577f2c2018-08-31 09:22:23 +010065ScopedCpuTensorHandle::ScopedCpuTensorHandle(const ConstCpuTensorHandle& tensorHandle)
66: ScopedCpuTensorHandle(tensorHandle.GetTensorInfo())
67{
68 CopyFrom(tensorHandle.GetConstTensor<void>(), tensorHandle.GetTensorInfo().GetNumBytes());
69}
70
telsoa014fcda012018-03-09 14:13:49 +000071ScopedCpuTensorHandle::ScopedCpuTensorHandle(const ScopedCpuTensorHandle& other)
72: CpuTensorHandle(other.GetTensorInfo())
73{
74 CopyFrom(other);
75}
76
77ScopedCpuTensorHandle& ScopedCpuTensorHandle::operator=(const ScopedCpuTensorHandle& other)
78{
79 ::operator delete(GetTensor<void>());
80 SetMemory(nullptr);
81 CopyFrom(other);
82 return *this;
83}
84
85ScopedCpuTensorHandle::~ScopedCpuTensorHandle()
86{
87 ::operator delete(GetTensor<void>());
88}
89
90void 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 Beck09e2f272018-10-30 11:38:41 +0000103void ScopedCpuTensorHandle::CopyOutTo(void* memory) const
104{
105 memcpy(memory, GetTensor<void>(), GetTensorInfo().GetNumBytes());
106}
107
108void ScopedCpuTensorHandle::CopyInFrom(const void* memory)
109{
110 memcpy(GetTensor<void>(), memory, GetTensorInfo().GetNumBytes());
111}
112
telsoa014fcda012018-03-09 14:13:49 +0000113void ScopedCpuTensorHandle::CopyFrom(const ScopedCpuTensorHandle& other)
114{
115 CopyFrom(other.GetTensor<void>(), other.GetTensorInfo().GetNumBytes());
116}
117
118void 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
130void PassthroughCpuTensorHandle::Allocate()
131{
132 throw InvalidArgumentException("PassthroughCpuTensorHandle::Allocate() should never be called");
133}
134
135void ConstPassthroughCpuTensorHandle::Allocate()
136{
137 throw InvalidArgumentException("ConstPassthroughCpuTensorHandle::Allocate() should never be called");
138}
139
140} // namespace armnn