blob: 46a7cb8251d316d46ca414e50b9979424f5facf1 [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 <>
21const void* ConstCpuTensorHandle::GetConstTensor() const
22{
23 return m_Memory;
24}
25
26CpuTensorHandle::CpuTensorHandle(const TensorInfo& tensorInfo)
27: ConstCpuTensorHandle(tensorInfo)
28, m_MutableMemory(nullptr)
29{
30}
31
32template <>
33void* CpuTensorHandle::GetTensor() const
34{
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
87void ScopedCpuTensorHandle::CopyFrom(const ScopedCpuTensorHandle& other)
88{
89 CopyFrom(other.GetTensor<void>(), other.GetTensorInfo().GetNumBytes());
90}
91
92void ScopedCpuTensorHandle::CopyFrom(const void* srcMemory, unsigned int numBytes)
93{
94 BOOST_ASSERT(GetTensor<void>() == nullptr);
95 BOOST_ASSERT(GetTensorInfo().GetNumBytes() == numBytes);
96
97 if (srcMemory)
98 {
99 Allocate();
100 memcpy(GetTensor<void>(), srcMemory, numBytes);
101 }
102}
103
104void PassthroughCpuTensorHandle::Allocate()
105{
106 throw InvalidArgumentException("PassthroughCpuTensorHandle::Allocate() should never be called");
107}
108
109void ConstPassthroughCpuTensorHandle::Allocate()
110{
111 throw InvalidArgumentException("ConstPassthroughCpuTensorHandle::Allocate() should never be called");
112}
113
114} // namespace armnn