blob: d127def325e29c1dd9d591925b9b2cb0a6e99081 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_TEST_CLACCESSOR_H
25#define ARM_COMPUTE_TEST_CLACCESSOR_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
27#include "arm_compute/runtime/CL/CLTensor.h"
Moritz Pflanzerd58cec02017-07-18 15:44:21 +010028#include "tests/IAccessor.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029
30namespace arm_compute
31{
32namespace test
33{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034/** Accessor implementation for @ref CLTensor objects. */
35class CLAccessor : public IAccessor
36{
37public:
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
Alex Gildayc357c472018-03-21 13:54:09 +000047 /** Prevent instances of this class from being copy constructed */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010048 CLAccessor(const CLAccessor &) = delete;
Alex Gildayc357c472018-03-21 13:54:09 +000049 /** Prevent instances of this class from being copied */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010050 CLAccessor &operator=(const CLAccessor &) = delete;
Alex Gildayc357c472018-03-21 13:54:09 +000051 /** Allow instances of this class to be move constructed */
52 CLAccessor(CLAccessor &&) = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053
54 /** Destructor that unmaps the CL memory. */
55 ~CLAccessor();
56
Alex Gildayc357c472018-03-21 13:54:09 +000057 /** 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
Chunosovd621bca2017-11-03 17:33:15 +070069 TensorShape shape() const override;
70 size_t element_size() const override;
71 size_t size() const override;
72 Format format() const override;
Michele Di Giorgio4a65b982018-03-02 11:21:38 +000073 DataLayout data_layout() const override;
Chunosovd621bca2017-11-03 17:33:15 +070074 DataType data_type() const override;
75 int num_channels() const override;
76 int num_elements() const override;
77 PaddingSize padding() const override;
Chunosovd621bca2017-11-03 17:33:15 +070078 QuantizationInfo quantization_info() const override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079 const void *operator()(const Coordinates &coord) const override;
80 void *operator()(const Coordinates &coord) override;
81
82private:
83 CLTensor &_tensor;
84};
85
86inline CLAccessor::CLAccessor(CLTensor &tensor)
87 : _tensor{ tensor }
88{
89 _tensor.map();
90}
91
92inline CLAccessor::~CLAccessor()
93{
94 _tensor.unmap();
95}
96
97inline TensorShape CLAccessor::shape() const
98{
99 return _tensor.info()->tensor_shape();
100}
101
102inline size_t CLAccessor::element_size() const
103{
104 return _tensor.info()->element_size();
105}
106
107inline size_t CLAccessor::size() const
108{
109 return _tensor.info()->total_size();
110}
111
112inline Format CLAccessor::format() const
113{
114 return _tensor.info()->format();
115}
116
Michele Di Giorgio4a65b982018-03-02 11:21:38 +0000117inline DataLayout CLAccessor::data_layout() const
118{
119 return _tensor.info()->data_layout();
120}
121
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122inline DataType CLAccessor::data_type() const
123{
124 return _tensor.info()->data_type();
125}
126
127inline int CLAccessor::num_channels() const
128{
129 return _tensor.info()->num_channels();
130}
131
132inline int CLAccessor::num_elements() const
133{
134 return _tensor.info()->tensor_shape().total_size();
135}
136
Giorgio Arenaa2611812017-07-21 10:08:48 +0100137inline PaddingSize CLAccessor::padding() const
138{
139 return _tensor.info()->padding();
140}
141
Chunosovd621bca2017-11-03 17:33:15 +0700142inline QuantizationInfo CLAccessor::quantization_info() const
143{
144 return _tensor.info()->quantization_info();
145}
146
SiCong Li86b53332017-08-23 11:02:43 +0100147inline const void *CLAccessor::data() const
148{
149 return _tensor.buffer();
150}
151
152inline void *CLAccessor::data()
153{
154 return _tensor.buffer();
155}
156
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100157inline const void *CLAccessor::operator()(const Coordinates &coord) const
158{
159 return _tensor.ptr_to_element(coord);
160}
161
162inline void *CLAccessor::operator()(const Coordinates &coord)
163{
164 return _tensor.ptr_to_element(coord);
165}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100166} // namespace test
167} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000168#endif /* ARM_COMPUTE_TEST_CLACCESSOR_H */