xref: /aosp_15_r20/external/ComputeLibrary/tests/CL/CLAccessor.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2017-2021 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 #include "tests/framework/Framework.h"
30 
31 namespace arm_compute
32 {
33 namespace test
34 {
35 /** Accessor implementation for @ref CLTensor objects. */
36 class CLAccessor : public IAccessor
37 {
38 public:
39     /** Create an accessor for the given @p tensor.
40      *
41      * @param[in, out] tensor To be accessed tensor.
42      *
43      * @note The CL memory is mapped by the constructor.
44      *
45      */
46     CLAccessor(CLTensor &tensor);
47 
48     /** Prevent instances of this class from being copy constructed */
49     CLAccessor(const CLAccessor &) = delete;
50     /** Prevent instances of this class from being copied */
51     CLAccessor &operator=(const CLAccessor &) = delete;
52     /** Allow instances of this class to be move constructed */
53     CLAccessor(CLAccessor &&) = default;
54 
55     /** Destructor that unmaps the CL memory. */
56     ~CLAccessor();
57 
58     /** Get the tensor data.
59      *
60      * @return a constant pointer to the tensor data.
61      */
62     const void *data() const;
63     /** Get the tensor data.
64      *
65      * @return a pointer to the tensor data.
66      */
67     void *data();
68 
69     // Inherited method overrides
70     TensorShape      shape() const override;
71     size_t           element_size() const override;
72     size_t           size() const override;
73     Format           format() const override;
74     DataLayout       data_layout() const override;
75     DataType         data_type() const override;
76     int              num_channels() const override;
77     int              num_elements() const override;
78     PaddingSize      padding() const override;
79     QuantizationInfo quantization_info() const override;
80     const void *operator()(const Coordinates &coord) const override;
81     void *operator()(const Coordinates &coord) override;
82 
83 private:
84     CLTensor &_tensor;
85 };
86 
CLAccessor(CLTensor & tensor)87 inline CLAccessor::CLAccessor(CLTensor &tensor)
88     : _tensor{ tensor }
89 {
90     if(!framework::Framework::get().configure_only() || !framework::Framework::get().new_fixture_call())
91     {
92         _tensor.map();
93     }
94 }
95 
~CLAccessor()96 inline CLAccessor::~CLAccessor()
97 {
98     if(!framework::Framework::get().configure_only() || !framework::Framework::get().new_fixture_call())
99     {
100         _tensor.unmap();
101     }
102 }
103 
shape()104 inline TensorShape CLAccessor::shape() const
105 {
106     return _tensor.info()->tensor_shape();
107 }
108 
element_size()109 inline size_t CLAccessor::element_size() const
110 {
111     return _tensor.info()->element_size();
112 }
113 
size()114 inline size_t CLAccessor::size() const
115 {
116     return _tensor.info()->total_size();
117 }
118 
format()119 inline Format CLAccessor::format() const
120 {
121     return _tensor.info()->format();
122 }
123 
data_layout()124 inline DataLayout CLAccessor::data_layout() const
125 {
126     return _tensor.info()->data_layout();
127 }
128 
data_type()129 inline DataType CLAccessor::data_type() const
130 {
131     return _tensor.info()->data_type();
132 }
133 
num_channels()134 inline int CLAccessor::num_channels() const
135 {
136     return _tensor.info()->num_channels();
137 }
138 
num_elements()139 inline int CLAccessor::num_elements() const
140 {
141     return _tensor.info()->tensor_shape().total_size();
142 }
143 
padding()144 inline PaddingSize CLAccessor::padding() const
145 {
146     return _tensor.info()->padding();
147 }
148 
quantization_info()149 inline QuantizationInfo CLAccessor::quantization_info() const
150 {
151     return _tensor.info()->quantization_info();
152 }
153 
data()154 inline const void *CLAccessor::data() const
155 {
156     return _tensor.buffer();
157 }
158 
data()159 inline void *CLAccessor::data()
160 {
161     return _tensor.buffer();
162 }
163 
operator()164 inline const void *CLAccessor::operator()(const Coordinates &coord) const
165 {
166     return _tensor.ptr_to_element(coord);
167 }
168 
operator()169 inline void *CLAccessor::operator()(const Coordinates &coord)
170 {
171     return _tensor.ptr_to_element(coord);
172 }
173 } // namespace test
174 } // namespace arm_compute
175 #endif /* ARM_COMPUTE_TEST_CLACCESSOR_H */
176