1 /*
2 * Copyright (c) 2017-2020 Arm Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24 #ifndef ARM_COMPUTE_TEST_CLACCESSOR_H
25 #define ARM_COMPUTE_TEST_CLACCESSOR_H
26
27 #include "arm_compute/runtime/CL/CLTensor.h"
28 #include "tests/IAccessor.h"
29
30 namespace arm_compute
31 {
32 namespace test
33 {
34 /** Accessor implementation for @ref CLTensor objects. */
35 class CLAccessor : public IAccessor
36 {
37 public:
38 /** Create an accessor for the given @p tensor.
39 *
40 * @param[in, out] tensor To be accessed tensor.
41 *
42 * @note The CL memory is mapped by the constructor.
43 *
44 */
45 CLAccessor(CLTensor &tensor);
46
47 /** Prevent instances of this class from being copy constructed */
48 CLAccessor(const CLAccessor &) = delete;
49 /** Prevent instances of this class from being copied */
50 CLAccessor &operator=(const CLAccessor &) = delete;
51 /** Allow instances of this class to be move constructed */
52 CLAccessor(CLAccessor &&) = default;
53
54 /** Destructor that unmaps the CL memory. */
55 ~CLAccessor();
56
57 /** Get the tensor data.
58 *
59 * @return a constant pointer to the tensor data.
60 */
61 const void *data() const;
62 /** Get the tensor data.
63 *
64 * @return a pointer to the tensor data.
65 */
66 void *data();
67
68 // Inherited method overrides
69 TensorShape shape() const override;
70 size_t element_size() const override;
71 size_t size() const override;
72 Format format() const override;
73 DataLayout data_layout() const override;
74 DataType data_type() const override;
75 int num_channels() const override;
76 int num_elements() const override;
77 PaddingSize padding() const override;
78 QuantizationInfo quantization_info() const override;
79 const void *operator()(const Coordinates &coord) const override;
80 void *operator()(const Coordinates &coord) override;
81
82 private:
83 CLTensor &_tensor;
84 };
85
CLAccessor(CLTensor & tensor)86 inline CLAccessor::CLAccessor(CLTensor &tensor)
87 : _tensor{ tensor }
88 {
89 _tensor.map();
90 }
91
~CLAccessor()92 inline CLAccessor::~CLAccessor()
93 {
94 _tensor.unmap();
95 }
96
shape()97 inline TensorShape CLAccessor::shape() const
98 {
99 return _tensor.info()->tensor_shape();
100 }
101
element_size()102 inline size_t CLAccessor::element_size() const
103 {
104 return _tensor.info()->element_size();
105 }
106
size()107 inline size_t CLAccessor::size() const
108 {
109 return _tensor.info()->total_size();
110 }
111
format()112 inline Format CLAccessor::format() const
113 {
114 return _tensor.info()->format();
115 }
116
data_layout()117 inline DataLayout CLAccessor::data_layout() const
118 {
119 return _tensor.info()->data_layout();
120 }
121
data_type()122 inline DataType CLAccessor::data_type() const
123 {
124 return _tensor.info()->data_type();
125 }
126
num_channels()127 inline int CLAccessor::num_channels() const
128 {
129 return _tensor.info()->num_channels();
130 }
131
num_elements()132 inline int CLAccessor::num_elements() const
133 {
134 return _tensor.info()->tensor_shape().total_size();
135 }
136
padding()137 inline PaddingSize CLAccessor::padding() const
138 {
139 return _tensor.info()->padding();
140 }
141
quantization_info()142 inline QuantizationInfo CLAccessor::quantization_info() const
143 {
144 return _tensor.info()->quantization_info();
145 }
146
data()147 inline const void *CLAccessor::data() const
148 {
149 return _tensor.buffer();
150 }
151
data()152 inline void *CLAccessor::data()
153 {
154 return _tensor.buffer();
155 }
156
operator()157 inline const void *CLAccessor::operator()(const Coordinates &coord) const
158 {
159 return _tensor.ptr_to_element(coord);
160 }
161
operator()162 inline void *CLAccessor::operator()(const Coordinates &coord)
163 {
164 return _tensor.ptr_to_element(coord);
165 }
166 } // namespace test
167 } // namespace arm_compute
168 #endif /* ARM_COMPUTE_TEST_CLACCESSOR_H */
169