Compute Library
 21.11
CLAccessor.h
Go to the documentation of this file.
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 
28 #include "tests/IAccessor.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 
88  : _tensor{ tensor }
89 {
90  if(!framework::Framework::get().configure_only() || !framework::Framework::get().new_fixture_call())
91  {
92  _tensor.map();
93  }
94 }
95 
97 {
98  if(!framework::Framework::get().configure_only() || !framework::Framework::get().new_fixture_call())
99  {
100  _tensor.unmap();
101  }
102 }
103 
105 {
106  return _tensor.info()->tensor_shape();
107 }
108 
109 inline size_t CLAccessor::element_size() const
110 {
111  return _tensor.info()->element_size();
112 }
113 
114 inline size_t CLAccessor::size() const
115 {
116  return _tensor.info()->total_size();
117 }
118 
120 {
121  return _tensor.info()->format();
122 }
123 
125 {
126  return _tensor.info()->data_layout();
127 }
128 
130 {
131  return _tensor.info()->data_type();
132 }
133 
134 inline int CLAccessor::num_channels() const
135 {
136  return _tensor.info()->num_channels();
137 }
138 
139 inline int CLAccessor::num_elements() const
140 {
141  return _tensor.info()->tensor_shape().total_size();
142 }
143 
145 {
146  return _tensor.info()->padding();
147 }
148 
150 {
151  return _tensor.info()->quantization_info();
152 }
153 
154 inline const void *CLAccessor::data() const
155 {
156  return _tensor.buffer();
157 }
158 
159 inline void *CLAccessor::data()
160 {
161  return _tensor.buffer();
162 }
163 
164 inline const void *CLAccessor::operator()(const Coordinates &coord) const
165 {
166  return _tensor.ptr_to_element(coord);
167 }
168 
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 */
uint8_t * ptr_to_element(const Coordinates &id) const
Return a pointer to the element at the passed coordinates.
Definition: ITensor.h:63
Shape of a tensor.
Definition: TensorShape.h:39
TensorInfo * info() const override
Interface to be implemented by the child class to return the tensor's metadata.
Definition: CLTensor.cpp:41
Container for 2D border size.
Definition: Types.h:269
size_t num_channels() const override
The number of channels for each tensor element.
Definition: TensorInfo.h:230
DataLayout data_layout() const override
Get the data layout of the tensor.
Definition: TensorInfo.h:291
QuantizationInfo quantization_info() const override
Get the quantization settings (scale and offset) of the tensor.
Definition: TensorInfo.h:287
CLAccessor & operator=(const CLAccessor &)=delete
Prevent instances of this class from being copied.
TensorShape shape() const override
Shape of the tensor.
Definition: CLAccessor.h:104
size_t element_size() const override
Size of each element in the tensor in bytes.
Definition: CLAccessor.h:109
PaddingSize padding() const override
Padding of tensor.
Definition: TensorInfo.h:254
Copyright (c) 2017-2021 Arm Limited.
size_t size() const override
Total size of the tensor in bytes.
Definition: CLAccessor.h:114
Format format() const override
Colour format of the image.
Definition: TensorInfo.h:246
void map(bool blocking=true)
Enqueue a map operation of the allocated buffer.
Definition: CLTensor.cpp:66
static Framework & get()
Access to the singleton.
Definition: Framework.cpp:124
DataType data_type() const override
Data type used for each element of the tensor.
Definition: TensorInfo.h:242
int num_elements() const override
Number of elements of the tensor.
Definition: CLAccessor.h:139
uint8_t * buffer() const override
Interface to be implemented by the child class to return a pointer to CPU memory. ...
Definition: ICLTensor.cpp:53
Quantization information.
Format
Image colour formats.
Definition: Types.h:56
Accessor implementation for CLTensor objects.
Definition: CLAccessor.h:36
size_t total_size() const override
Returns the total size of the tensor in bytes.
Definition: TensorInfo.h:250
Coordinates of an item.
Definition: Coordinates.h:37
size_t total_size() const
Collapses all dimensions to a single linear total size.
Definition: TensorShape.h:172
DataLayout data_layout() const override
Data layout of the tensor.
Definition: CLAccessor.h:124
~CLAccessor()
Destructor that unmaps the CL memory.
Definition: CLAccessor.h:96
const void * data() const
Get the tensor data.
Definition: CLAccessor.h:154
CLAccessor(CLTensor &tensor)
Create an accessor for the given tensor.
Definition: CLAccessor.h:87
Common interface to provide information and access to tensor like structures.
Definition: IAccessor.h:37
const void * operator()(const Coordinates &coord) const override
Read only access to the specified element.
Definition: CLAccessor.h:164
int num_channels() const override
Number of channels of the tensor.
Definition: CLAccessor.h:134
QuantizationInfo quantization_info() const override
Quantization info in case of asymmetric quantized type.
Definition: CLAccessor.h:149
Format format() const override
Image format of the tensor.
Definition: CLAccessor.h:119
PaddingSize padding() const override
Available padding around the tensor.
Definition: CLAccessor.h:144
const TensorShape & tensor_shape() const override
Size for each dimension of the tensor.
Definition: TensorInfo.h:234
DataType
Available data types.
Definition: Types.h:79
void unmap()
Enqueue an unmap operation of the allocated and mapped buffer.
Definition: CLTensor.cpp:71
DataLayout
[DataLayout enum definition]
Definition: Types.h:113
size_t element_size() const override
Element size in bytes calculated as data_size() * num_channels()
Definition: TensorInfo.h:222
DataType data_type() const override
Data type of the tensor.
Definition: CLAccessor.h:129
Basic implementation of the OpenCL tensor interface.
Definition: CLTensor.h:41