blob: 4059a1d5658f29fd110b7498017a65e10e222554 [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>
6#include <backends/CpuTensorHandle.hpp>
telsoa014fcda012018-03-09 14:13:49 +00007
8#include <cstring>
9
10namespace armnn
11{
12
13ConstCpuTensorHandle::ConstCpuTensorHandle(const TensorInfo& tensorInfo)
14: m_TensorInfo(tensorInfo)
15, m_Memory(nullptr)
16{
17}
18
19template <>
20const void* ConstCpuTensorHandle::GetConstTensor() const
21{
22 return m_Memory;
23}
24
25CpuTensorHandle::CpuTensorHandle(const TensorInfo& tensorInfo)
26: ConstCpuTensorHandle(tensorInfo)
27, m_MutableMemory(nullptr)
28{
29}
30
31template <>
32void* CpuTensorHandle::GetTensor() const
33{
34 return m_MutableMemory;
35}
36
37ScopedCpuTensorHandle::ScopedCpuTensorHandle(const TensorInfo& tensorInfo)
38: CpuTensorHandle(tensorInfo)
39{
40}
41
42ScopedCpuTensorHandle::ScopedCpuTensorHandle(const ConstTensor& tensor)
43: ScopedCpuTensorHandle(tensor.GetInfo())
44{
45 CopyFrom(tensor.GetMemoryArea(), tensor.GetNumBytes());
46}
47
telsoa01c577f2c2018-08-31 09:22:23 +010048ScopedCpuTensorHandle::ScopedCpuTensorHandle(const ConstCpuTensorHandle& tensorHandle)
49: ScopedCpuTensorHandle(tensorHandle.GetTensorInfo())
50{
51 CopyFrom(tensorHandle.GetConstTensor<void>(), tensorHandle.GetTensorInfo().GetNumBytes());
52}
53
telsoa014fcda012018-03-09 14:13:49 +000054ScopedCpuTensorHandle::ScopedCpuTensorHandle(const ScopedCpuTensorHandle& other)
55: CpuTensorHandle(other.GetTensorInfo())
56{
57 CopyFrom(other);
58}
59
60ScopedCpuTensorHandle& ScopedCpuTensorHandle::operator=(const ScopedCpuTensorHandle& other)
61{
62 ::operator delete(GetTensor<void>());
63 SetMemory(nullptr);
64 CopyFrom(other);
65 return *this;
66}
67
68ScopedCpuTensorHandle::~ScopedCpuTensorHandle()
69{
70 ::operator delete(GetTensor<void>());
71}
72
73void ScopedCpuTensorHandle::Allocate()
74{
75 if (GetTensor<void>() == nullptr)
76 {
77 SetMemory(::operator new(GetTensorInfo().GetNumBytes()));
78 }
79 else
80 {
81 throw InvalidArgumentException("CpuTensorHandle::Allocate Trying to allocate a CpuTensorHandle"
82 "that already has allocated memory.");
83 }
84}
85
86void ScopedCpuTensorHandle::CopyFrom(const ScopedCpuTensorHandle& other)
87{
88 CopyFrom(other.GetTensor<void>(), other.GetTensorInfo().GetNumBytes());
89}
90
91void ScopedCpuTensorHandle::CopyFrom(const void* srcMemory, unsigned int numBytes)
92{
93 BOOST_ASSERT(GetTensor<void>() == nullptr);
94 BOOST_ASSERT(GetTensorInfo().GetNumBytes() == numBytes);
95
96 if (srcMemory)
97 {
98 Allocate();
99 memcpy(GetTensor<void>(), srcMemory, numBytes);
100 }
101}
102
103void PassthroughCpuTensorHandle::Allocate()
104{
105 throw InvalidArgumentException("PassthroughCpuTensorHandle::Allocate() should never be called");
106}
107
108void ConstPassthroughCpuTensorHandle::Allocate()
109{
110 throw InvalidArgumentException("ConstPassthroughCpuTensorHandle::Allocate() should never be called");
111}
112
113} // namespace armnn