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