Release 18.08
[platform/upstream/armnn.git] / src / armnn / backends / ClTensorHandle.hpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // See LICENSE file in the project root for full license information.
4 //
5 #pragma once
6
7 #include "OutputHandler.hpp"
8 #include "ArmComputeTensorUtils.hpp"
9
10 #include <arm_compute/runtime/CL/CLTensor.h>
11 #include <arm_compute/runtime/CL/CLSubTensor.h>
12 #include <arm_compute/runtime/CL/CLMemoryGroup.h>
13 #include <arm_compute/runtime/IMemoryGroup.h>
14 #include <arm_compute/core/TensorShape.h>
15 #include <arm_compute/core/Coordinates.h>
16
17 #include <boost/polymorphic_pointer_cast.hpp>
18
19 namespace armnn
20 {
21
22
23 class IClTensorHandle : public ITensorHandle
24 {
25 public:
26     virtual arm_compute::ICLTensor& GetTensor() = 0;
27     virtual arm_compute::ICLTensor const& GetTensor() const = 0;
28     virtual arm_compute::DataType GetDataType() const = 0;
29     virtual void SetMemoryGroup(const std::shared_ptr<arm_compute::IMemoryGroup>& memoryGroup) = 0;
30 };
31
32 class ClTensorHandle : public IClTensorHandle
33 {
34 public:
35     ClTensorHandle(const TensorInfo& tensorInfo)
36     {
37         armnn::armcomputetensorutils::BuildArmComputeTensor(m_Tensor, tensorInfo);
38     }
39
40     arm_compute::CLTensor& GetTensor() override { return m_Tensor; }
41     arm_compute::CLTensor const& GetTensor() const override { return m_Tensor; }
42     virtual void Allocate() override {armnn::armcomputetensorutils::InitialiseArmComputeTensorEmpty(m_Tensor);}
43
44     virtual void Manage() override
45     {
46         assert(m_MemoryGroup != nullptr);
47         m_MemoryGroup->manage(&m_Tensor);
48     }
49
50     virtual const void* Map(bool blocking = true) const override
51     {
52         const_cast<arm_compute::CLTensor*>(&m_Tensor)->map(blocking);
53         return static_cast<const void*>(m_Tensor.buffer() + m_Tensor.info()->offset_first_element_in_bytes());
54     }
55     virtual void Unmap() const override { const_cast<arm_compute::CLTensor*>(&m_Tensor)->unmap(); }
56
57     virtual ITensorHandle::Type GetType() const override { return ITensorHandle::CL; }
58
59     virtual ITensorHandle* GetParent() const override { return nullptr; }
60
61     virtual arm_compute::DataType GetDataType() const override
62     {
63         return m_Tensor.info()->data_type();
64     }
65
66     virtual void SetMemoryGroup(const std::shared_ptr<arm_compute::IMemoryGroup>& memoryGroup) override
67     {
68         m_MemoryGroup = boost::polymorphic_pointer_downcast<arm_compute::CLMemoryGroup>(memoryGroup);
69     }
70
71     TensorShape GetStrides() const override
72     {
73         return armcomputetensorutils::GetStrides(m_Tensor.info()->strides_in_bytes());
74     }
75
76     TensorShape GetShape() const override
77     {
78         return armcomputetensorutils::GetShape(m_Tensor.info()->tensor_shape());
79     }
80 private:
81     arm_compute::CLTensor m_Tensor;
82     std::shared_ptr<arm_compute::CLMemoryGroup> m_MemoryGroup;
83 };
84
85 class ClSubTensorHandle : public IClTensorHandle
86 {
87 public:
88     ClSubTensorHandle(IClTensorHandle* parent,
89                       const arm_compute::TensorShape& shape,
90                       const arm_compute::Coordinates& coords)
91     : m_Tensor(&parent->GetTensor(), shape, coords)
92     {
93         parentHandle = parent;
94     }
95
96     arm_compute::CLSubTensor& GetTensor() override { return m_Tensor; }
97     arm_compute::CLSubTensor const& GetTensor() const override { return m_Tensor; }
98
99     virtual void Allocate() override {}
100     virtual void Manage() override {}
101
102     virtual const void* Map(bool blocking = true) const override
103     {
104         const_cast<arm_compute::CLSubTensor*>(&m_Tensor)->map(blocking);
105         return static_cast<const void*>(m_Tensor.buffer() + m_Tensor.info()->offset_first_element_in_bytes());
106     }
107     virtual void Unmap() const override { const_cast<arm_compute::CLSubTensor*>(&m_Tensor)->unmap(); }
108
109     virtual ITensorHandle::Type GetType() const override { return ITensorHandle::CL; }
110
111     virtual ITensorHandle* GetParent() const override { return parentHandle; }
112
113     virtual arm_compute::DataType GetDataType() const override
114     {
115         return m_Tensor.info()->data_type();
116     }
117
118     virtual void SetMemoryGroup(const std::shared_ptr<arm_compute::IMemoryGroup>&) override {}
119
120     TensorShape GetStrides() const override
121     {
122         return armcomputetensorutils::GetStrides(m_Tensor.info()->strides_in_bytes());
123     }
124
125     TensorShape GetShape() const override
126     {
127         return armcomputetensorutils::GetShape(m_Tensor.info()->tensor_shape());
128     }
129
130 private:
131     mutable arm_compute::CLSubTensor m_Tensor;
132     ITensorHandle* parentHandle = nullptr;
133
134 };
135
136 }