Compute Library
 20.05
CLAccessor.h
Go to the documentation of this file.
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 
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 
87  : _tensor{ tensor }
88 {
89  _tensor.map();
90 }
91 
93 {
94  _tensor.unmap();
95 }
96 
98 {
99  return _tensor.info()->tensor_shape();
100 }
101 
102 inline size_t CLAccessor::element_size() const
103 {
104  return _tensor.info()->element_size();
105 }
106 
107 inline size_t CLAccessor::size() const
108 {
109  return _tensor.info()->total_size();
110 }
111 
113 {
114  return _tensor.info()->format();
115 }
116 
118 {
119  return _tensor.info()->data_layout();
120 }
121 
123 {
124  return _tensor.info()->data_type();
125 }
126 
127 inline int CLAccessor::num_channels() const
128 {
129  return _tensor.info()->num_channels();
130 }
131 
132 inline int CLAccessor::num_elements() const
133 {
134  return _tensor.info()->tensor_shape().total_size();
135 }
136 
138 {
139  return _tensor.info()->padding();
140 }
141 
143 {
144  return _tensor.info()->quantization_info();
145 }
146 
147 inline const void *CLAccessor::data() const
148 {
149  return _tensor.buffer();
150 }
151 
152 inline void *CLAccessor::data()
153 {
154  return _tensor.buffer();
155 }
156 
157 inline const void *CLAccessor::operator()(const Coordinates &coord) const
158 {
159  return _tensor.ptr_to_element(coord);
160 }
161 
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 */
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:272
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:97
size_t element_size() const override
Size of each element in the tensor in bytes.
Definition: CLAccessor.h:102
PaddingSize padding() const override
Padding of tensor.
Definition: TensorInfo.h:277
Copyright (c) 2017-2020 ARM Limited.
size_t size() const override
Total size of the tensor in bytes.
Definition: CLAccessor.h:107
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:132
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:54
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:117
~CLAccessor()
Destructor that unmaps the CL memory.
Definition: CLAccessor.h:92
const void * data() const
Get the tensor data.
Definition: CLAccessor.h:147
CLAccessor(CLTensor &tensor)
Create an accessor for the given tensor.
Definition: CLAccessor.h:86
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:157
int num_channels() const override
Number of channels of the tensor.
Definition: CLAccessor.h:127
QuantizationInfo quantization_info() const override
Quantization info in case of asymmetric quantized type.
Definition: CLAccessor.h:142
Format format() const override
Image format of the tensor.
Definition: CLAccessor.h:112
PaddingSize padding() const override
Available padding around the tensor.
Definition: CLAccessor.h:137
const TensorShape & tensor_shape() const override
Size for each dimension of the tensor.
Definition: TensorInfo.h:261
DataType
Available data types.
Definition: Types.h:77
void unmap()
Enqueue an unmap operation of the allocated and mapped buffer.
Definition: CLTensor.cpp:71
DataLayout
[DataLayout enum definition]
Definition: Types.h:120
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:122
Basic implementation of the OpenCL tensor interface.
Definition: CLTensor.h:41