Compute Library
 20.02.1
Accessor.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2019 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_ACCESSOR_H
25 #define ARM_COMPUTE_TEST_ACCESSOR_H
26 
28 #include "tests/IAccessor.h"
29 
30 namespace arm_compute
31 {
32 namespace test
33 {
34 /** Accessor implementation for @ref Tensor objects. */
35 class Accessor : 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  Accessor(ITensor &tensor);
43 
44  /** Prevent instances of this class from being copy constructed */
45  Accessor(const Accessor &) = delete;
46  /** Prevent instances of this class from being copied */
47  Accessor &operator=(const Accessor &) = delete;
48  /** Allow instances of this class to be move constructed */
49  Accessor(Accessor &&) = default;
50  /** Allow instances of this class to be moved */
51  Accessor &operator=(Accessor &&) = default;
52 
53  /** Get the tensor data.
54  *
55  * @return a constant pointer to the tensor data.
56  */
57  const void *data() const;
58  /** Get the tensor data.
59  *
60  * @return a pointer to the tensor data.
61  */
62  void *data();
63 
64  TensorShape shape() const override;
65  size_t element_size() const override;
66  size_t size() const override;
67  Format format() const override;
68  DataLayout data_layout() const override;
69  DataType data_type() const override;
70  int num_channels() const override;
71  int num_elements() const override;
72  PaddingSize padding() const override;
73  QuantizationInfo quantization_info() const override;
74  const void *operator()(const Coordinates &coord) const override;
75  void *operator()(const Coordinates &coord) override;
76 
77 private:
78  ITensor &_tensor;
79 };
80 
81 inline Accessor::Accessor(ITensor &tensor)
82  : _tensor{ tensor }
83 {
84 }
85 
87 {
88  return _tensor.info()->tensor_shape();
89 }
90 
91 inline size_t Accessor::element_size() const
92 {
93  return _tensor.info()->element_size();
94 }
95 
96 inline size_t Accessor::size() const
97 {
98  return _tensor.info()->total_size();
99 }
100 
101 inline Format Accessor::format() const
102 {
103  return _tensor.info()->format();
104 }
105 
107 {
108  return _tensor.info()->data_layout();
109 }
110 
112 {
113  return _tensor.info()->data_type();
114 }
115 
116 inline int Accessor::num_channels() const
117 {
118  return _tensor.info()->num_channels();
119 }
120 
121 inline int Accessor::num_elements() const
122 {
123  return _tensor.info()->tensor_shape().total_size();
124 }
125 
127 {
128  return _tensor.info()->padding();
129 }
130 
132 {
133  return _tensor.info()->quantization_info();
134 }
135 
136 inline const void *Accessor::data() const
137 {
138  return _tensor.buffer();
139 }
140 
141 inline void *Accessor::data()
142 {
143  return _tensor.buffer();
144 }
145 
146 inline const void *Accessor::operator()(const Coordinates &coord) const
147 {
148  return _tensor.ptr_to_element(coord);
149 }
150 
151 inline void *Accessor::operator()(const Coordinates &coord)
152 {
153  return _tensor.ptr_to_element(coord);
154 }
155 } // namespace test
156 } // namespace arm_compute
157 #endif /* ARM_COMPUTE_TEST_ACCESSOR_H */
int num_channels() const override
Number of channels of the tensor.
Definition: Accessor.h:116
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
Container for 2D border size.
Definition: Types.h:269
Format format() const override
Image format of the tensor.
Definition: Accessor.h:101
DataLayout data_layout() const override
Data layout of the tensor.
Definition: Accessor.h:106
virtual DataType data_type() const =0
Data type used for each element of the tensor.
TensorShape shape() const override
Shape of the tensor.
Definition: Accessor.h:86
Interface for NEON tensor.
Definition: ITensor.h:36
Accessor & operator=(const Accessor &)=delete
Prevent instances of this class from being copied.
Copyright (c) 2017-2020 ARM Limited.
size_t element_size() const override
Size of each element in the tensor in bytes.
Definition: Accessor.h:91
const void * data() const
Get the tensor data.
Definition: Accessor.h:136
Quantization information.
virtual Format format() const =0
Colour format of the image.
Accessor implementation for Tensor objects.
Definition: Accessor.h:35
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
Format
Image colour formats.
Definition: Types.h:53
Coordinates of an item.
Definition: Coordinates.h:37
virtual uint8_t * buffer() const =0
Interface to be implemented by the child class to return a pointer to CPU memory.
PaddingSize padding() const override
Available padding around the tensor.
Definition: Accessor.h:126
size_t total_size() const
Collapses all dimensions to a single linear total size.
Definition: TensorShape.h:171
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
virtual size_t element_size() const =0
Element size in bytes calculated as data_size() * num_channels()
virtual PaddingSize padding() const =0
Padding of tensor.
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
const void * operator()(const Coordinates &coord) const override
Read only access to the specified element.
Definition: Accessor.h:146
QuantizationInfo quantization_info() const override
Quantization info in case of asymmetric quantized type.
Definition: Accessor.h:131
int num_elements() const override
Number of elements of the tensor.
Definition: Accessor.h:121
virtual size_t total_size() const =0
Returns the total size of the tensor in bytes.
Accessor(ITensor &tensor)
Create an accessor for the given tensor.
Definition: Accessor.h:81
Common interface to provide information and access to tensor like structures.
Definition: IAccessor.h:37
DataType data_type() const override
Data type of the tensor.
Definition: Accessor.h:111
DataType
Available data types.
Definition: Types.h:75
DataLayout
[DataLayout enum definition]
Definition: Types.h:117
virtual size_t num_channels() const =0
The number of channels for each tensor element.
size_t size() const override
Total size of the tensor in bytes.
Definition: Accessor.h:96
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.