blob: ef1983364b7d5c37394bc4e244d34d58548f7f70 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Giorgio Arena68e29da2021-02-08 16:31:10 +00002 * Copyright (c) 2017-2021 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"
Giorgio Arena68e29da2021-02-08 16:31:10 +000029#include "tests/framework/Framework.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030
31namespace arm_compute
32{
33namespace test
34{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035/** Accessor implementation for @ref CLTensor objects. */
36class CLAccessor : public IAccessor
37{
38public:
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
Alex Gildayc357c472018-03-21 13:54:09 +000048 /** Prevent instances of this class from being copy constructed */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049 CLAccessor(const CLAccessor &) = delete;
Alex Gildayc357c472018-03-21 13:54:09 +000050 /** Prevent instances of this class from being copied */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010051 CLAccessor &operator=(const CLAccessor &) = delete;
Alex Gildayc357c472018-03-21 13:54:09 +000052 /** Allow instances of this class to be move constructed */
53 CLAccessor(CLAccessor &&) = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054
55 /** Destructor that unmaps the CL memory. */
56 ~CLAccessor();
57
Alex Gildayc357c472018-03-21 13:54:09 +000058 /** 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
Chunosovd621bca2017-11-03 17:33:15 +070070 TensorShape shape() const override;
71 size_t element_size() const override;
72 size_t size() const override;
73 Format format() const override;
Michele Di Giorgio4a65b982018-03-02 11:21:38 +000074 DataLayout data_layout() const override;
Chunosovd621bca2017-11-03 17:33:15 +070075 DataType data_type() const override;
76 int num_channels() const override;
77 int num_elements() const override;
78 PaddingSize padding() const override;
Chunosovd621bca2017-11-03 17:33:15 +070079 QuantizationInfo quantization_info() const override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010080 const void *operator()(const Coordinates &coord) const override;
81 void *operator()(const Coordinates &coord) override;
82
83private:
84 CLTensor &_tensor;
85};
86
87inline CLAccessor::CLAccessor(CLTensor &tensor)
88 : _tensor{ tensor }
89{
Giorgio Arena68e29da2021-02-08 16:31:10 +000090 if(!framework::Framework::get().configure_only() || !framework::Framework::get().new_fixture_call())
91 {
92 _tensor.map();
93 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094}
95
96inline CLAccessor::~CLAccessor()
97{
Giorgio Arena68e29da2021-02-08 16:31:10 +000098 if(!framework::Framework::get().configure_only() || !framework::Framework::get().new_fixture_call())
99 {
100 _tensor.unmap();
101 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102}
103
104inline TensorShape CLAccessor::shape() const
105{
106 return _tensor.info()->tensor_shape();
107}
108
109inline size_t CLAccessor::element_size() const
110{
111 return _tensor.info()->element_size();
112}
113
114inline size_t CLAccessor::size() const
115{
116 return _tensor.info()->total_size();
117}
118
119inline Format CLAccessor::format() const
120{
121 return _tensor.info()->format();
122}
123
Michele Di Giorgio4a65b982018-03-02 11:21:38 +0000124inline DataLayout CLAccessor::data_layout() const
125{
126 return _tensor.info()->data_layout();
127}
128
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129inline DataType CLAccessor::data_type() const
130{
131 return _tensor.info()->data_type();
132}
133
134inline int CLAccessor::num_channels() const
135{
136 return _tensor.info()->num_channels();
137}
138
139inline int CLAccessor::num_elements() const
140{
141 return _tensor.info()->tensor_shape().total_size();
142}
143
Giorgio Arenaa2611812017-07-21 10:08:48 +0100144inline PaddingSize CLAccessor::padding() const
145{
146 return _tensor.info()->padding();
147}
148
Chunosovd621bca2017-11-03 17:33:15 +0700149inline QuantizationInfo CLAccessor::quantization_info() const
150{
151 return _tensor.info()->quantization_info();
152}
153
SiCong Li86b53332017-08-23 11:02:43 +0100154inline const void *CLAccessor::data() const
155{
156 return _tensor.buffer();
157}
158
159inline void *CLAccessor::data()
160{
161 return _tensor.buffer();
162}
163
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100164inline const void *CLAccessor::operator()(const Coordinates &coord) const
165{
166 return _tensor.ptr_to_element(coord);
167}
168
169inline void *CLAccessor::operator()(const Coordinates &coord)
170{
171 return _tensor.ptr_to_element(coord);
172}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100173} // namespace test
174} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000175#endif /* ARM_COMPUTE_TEST_CLACCESSOR_H */