blob: 8e800c71c1f050fa5b6b1e686019d8ba81ec1aa6 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgio4a65b982018-03-02 11:21:38 +00002 * Copyright (c) 2017-2018 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 */
Moritz Pflanzerd58cec02017-07-18 15:44:21 +010024#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;
53 /** Allow instances of this class to be moved */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054 CLAccessor &operator=(CLAccessor &&) = default;
55
56 /** Destructor that unmaps the CL memory. */
57 ~CLAccessor();
58
Alex Gildayc357c472018-03-21 13:54:09 +000059 /** 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
Chunosovd621bca2017-11-03 17:33:15 +070071 TensorShape shape() const override;
72 size_t element_size() const override;
73 size_t size() const override;
74 Format format() const override;
Michele Di Giorgio4a65b982018-03-02 11:21:38 +000075 DataLayout data_layout() const override;
Chunosovd621bca2017-11-03 17:33:15 +070076 DataType data_type() const override;
77 int num_channels() const override;
78 int num_elements() const override;
79 PaddingSize padding() const override;
Chunosovd621bca2017-11-03 17:33:15 +070080 QuantizationInfo quantization_info() const override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010081 const void *operator()(const Coordinates &coord) const override;
82 void *operator()(const Coordinates &coord) override;
83
84private:
85 CLTensor &_tensor;
86};
87
88inline CLAccessor::CLAccessor(CLTensor &tensor)
89 : _tensor{ tensor }
90{
91 _tensor.map();
92}
93
94inline CLAccessor::~CLAccessor()
95{
96 _tensor.unmap();
97}
98
99inline TensorShape CLAccessor::shape() const
100{
101 return _tensor.info()->tensor_shape();
102}
103
104inline size_t CLAccessor::element_size() const
105{
106 return _tensor.info()->element_size();
107}
108
109inline size_t CLAccessor::size() const
110{
111 return _tensor.info()->total_size();
112}
113
114inline Format CLAccessor::format() const
115{
116 return _tensor.info()->format();
117}
118
Michele Di Giorgio4a65b982018-03-02 11:21:38 +0000119inline DataLayout CLAccessor::data_layout() const
120{
121 return _tensor.info()->data_layout();
122}
123
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124inline DataType CLAccessor::data_type() const
125{
126 return _tensor.info()->data_type();
127}
128
129inline int CLAccessor::num_channels() const
130{
131 return _tensor.info()->num_channels();
132}
133
134inline int CLAccessor::num_elements() const
135{
136 return _tensor.info()->tensor_shape().total_size();
137}
138
Giorgio Arenaa2611812017-07-21 10:08:48 +0100139inline PaddingSize CLAccessor::padding() const
140{
141 return _tensor.info()->padding();
142}
143
Chunosovd621bca2017-11-03 17:33:15 +0700144inline QuantizationInfo CLAccessor::quantization_info() const
145{
146 return _tensor.info()->quantization_info();
147}
148
SiCong Li86b53332017-08-23 11:02:43 +0100149inline const void *CLAccessor::data() const
150{
151 return _tensor.buffer();
152}
153
154inline void *CLAccessor::data()
155{
156 return _tensor.buffer();
157}
158
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100159inline const void *CLAccessor::operator()(const Coordinates &coord) const
160{
161 return _tensor.ptr_to_element(coord);
162}
163
164inline void *CLAccessor::operator()(const Coordinates &coord)
165{
166 return _tensor.ptr_to_element(coord);
167}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100168} // namespace test
169} // namespace arm_compute
Moritz Pflanzerd58cec02017-07-18 15:44:21 +0100170#endif /* __ARM_COMPUTE_TEST_CLACCESSOR_H__ */