Compute Library
 19.11
CLAccessor.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2018 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"
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  /** Allow instances of this class to be moved */
54  CLAccessor &operator=(CLAccessor &&) = default;
55 
56  /** Destructor that unmaps the CL memory. */
57  ~CLAccessor();
58 
59  /** Get the tensor data.
60  *
61  * @return a constant pointer to the tensor data.
62  */
63  const void *data() const;
64  /** Get the tensor data.
65  *
66  * @return a pointer to the tensor data.
67  */
68  void *data();
69 
70  // Inherited method overrides
71  TensorShape shape() const override;
72  size_t element_size() const override;
73  size_t size() const override;
74  Format format() const override;
75  DataLayout data_layout() const override;
76  DataType data_type() const override;
77  int num_channels() const override;
78  int num_elements() const override;
79  PaddingSize padding() const override;
80  QuantizationInfo quantization_info() const override;
81  const void *operator()(const Coordinates &coord) const override;
82  void *operator()(const Coordinates &coord) override;
83 
84 private:
85  CLTensor &_tensor;
86 };
87 
89  : _tensor{ tensor }
90 {
91  _tensor.map();
92 }
93 
95 {
96  _tensor.unmap();
97 }
98 
100 {
101  return _tensor.info()->tensor_shape();
102 }
103 
104 inline size_t CLAccessor::element_size() const
105 {
106  return _tensor.info()->element_size();
107 }
108 
109 inline size_t CLAccessor::size() const
110 {
111  return _tensor.info()->total_size();
112 }
113 
115 {
116  return _tensor.info()->format();
117 }
118 
120 {
121  return _tensor.info()->data_layout();
122 }
123 
125 {
126  return _tensor.info()->data_type();
127 }
128 
129 inline int CLAccessor::num_channels() const
130 {
131  return _tensor.info()->num_channels();
132 }
133 
134 inline int CLAccessor::num_elements() const
135 {
136  return _tensor.info()->tensor_shape().total_size();
137 }
138 
140 {
141  return _tensor.info()->padding();
142 }
143 
145 {
146  return _tensor.info()->quantization_info();
147 }
148 
149 inline const void *CLAccessor::data() const
150 {
151  return _tensor.buffer();
152 }
153 
154 inline void *CLAccessor::data()
155 {
156  return _tensor.buffer();
157 }
158 
159 inline const void *CLAccessor::operator()(const Coordinates &coord) const
160 {
161  return _tensor.ptr_to_element(coord);
162 }
163 
164 inline void *CLAccessor::operator()(const Coordinates &coord)
165 {
166  return _tensor.ptr_to_element(coord);
167 }
168 } // namespace test
169 } // namespace arm_compute
170 #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:268
size_t num_channels() const override
The number of channels for each tensor element.
Definition: TensorInfo.h:257
DataLayout data_layout() const override
Get the data layout of the tensor.
Definition: TensorInfo.h:315
QuantizationInfo quantization_info() const override
Get the quantization settings (scale and offset) of the tensor.
Definition: TensorInfo.h:311
CLAccessor & operator=(const CLAccessor &)=delete
Prevent instances of this class from being copied.
TensorShape shape() const override
Shape of the tensor.
Definition: CLAccessor.h:99
size_t element_size() const override
Size of each element in the tensor in bytes.
Definition: CLAccessor.h:104
PaddingSize padding() const override
Padding of tensor.
Definition: TensorInfo.h:277
Copyright (c) 2017-2019 ARM Limited.
size_t size() const override
Total size of the tensor in bytes.
Definition: CLAccessor.h:109
Format format() const override
Colour format of the image.
Definition: TensorInfo.h:269
void map(bool blocking=true)
Enqueue a map operation of the allocated buffer.
Definition: CLTensor.cpp:66
DataType data_type() const override
Data type used for each element of the tensor.
Definition: TensorInfo.h:265
int num_elements() const override
Number of elements of the tensor.
Definition: CLAccessor.h:134
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:52
Accessor implementation for CLTensor objects.
Definition: CLAccessor.h:35
size_t total_size() const override
Returns the total size of the tensor in bytes.
Definition: TensorInfo.h:273
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:171
DataLayout data_layout() const override
Data layout of the tensor.
Definition: CLAccessor.h:119
~CLAccessor()
Destructor that unmaps the CL memory.
Definition: CLAccessor.h:94
const void * data() const
Get the tensor data.
Definition: CLAccessor.h:149
CLAccessor(CLTensor &tensor)
Create an accessor for the given tensor.
Definition: CLAccessor.h:88
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:159
int num_channels() const override
Number of channels of the tensor.
Definition: CLAccessor.h:129
QuantizationInfo quantization_info() const override
Quantization info in case of asymmetric quantized type.
Definition: CLAccessor.h:144
Format format() const override
Image format of the tensor.
Definition: CLAccessor.h:114
PaddingSize padding() const override
Available padding around the tensor.
Definition: CLAccessor.h:139
const TensorShape & tensor_shape() const override
Size for each dimension of the tensor.
Definition: TensorInfo.h:261
DataType
Available data types.
Definition: Types.h:74
void unmap()
Enqueue an unmap operation of the allocated and mapped buffer.
Definition: CLTensor.cpp:71
DataLayout
[DataLayout enum definition]
Definition: Types.h:116
size_t element_size() const override
Element size in bytes calculated as data_size() * num_channels()
Definition: TensorInfo.h:249
DataType data_type() const override
Data type of the tensor.
Definition: CLAccessor.h:124
Basic implementation of the OpenCL tensor interface.
Definition: CLTensor.h:41