Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | 4a65b98 | 2018-03-02 11:21:38 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | */ |
Moritz Pflanzer | d58cec0 | 2017-07-18 15:44:21 +0100 | [diff] [blame] | 24 | #ifndef __ARM_COMPUTE_TEST_CLACCESSOR_H__ |
| 25 | #define __ARM_COMPUTE_TEST_CLACCESSOR_H__ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/runtime/CL/CLTensor.h" |
Moritz Pflanzer | d58cec0 | 2017-07-18 15:44:21 +0100 | [diff] [blame] | 28 | #include "tests/IAccessor.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | |
| 30 | namespace arm_compute |
| 31 | { |
| 32 | namespace test |
| 33 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 47 | /** Prevent instances of this class from being copy constructed */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 48 | CLAccessor(const CLAccessor &) = delete; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 49 | /** Prevent instances of this class from being copied */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 50 | CLAccessor &operator=(const CLAccessor &) = delete; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 51 | /** Allow instances of this class to be move constructed */ |
| 52 | CLAccessor(CLAccessor &&) = default; |
| 53 | /** Allow instances of this class to be moved */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 54 | CLAccessor &operator=(CLAccessor &&) = default; |
| 55 | |
| 56 | /** Destructor that unmaps the CL memory. */ |
| 57 | ~CLAccessor(); |
| 58 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 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 |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 71 | TensorShape shape() const override; |
| 72 | size_t element_size() const override; |
| 73 | size_t size() const override; |
| 74 | Format format() const override; |
Michele Di Giorgio | 4a65b98 | 2018-03-02 11:21:38 +0000 | [diff] [blame] | 75 | DataLayout data_layout() const override; |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 76 | DataType data_type() const override; |
| 77 | int num_channels() const override; |
| 78 | int num_elements() const override; |
| 79 | PaddingSize padding() const override; |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 80 | QuantizationInfo quantization_info() const override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 81 | const void *operator()(const Coordinates &coord) const override; |
| 82 | void *operator()(const Coordinates &coord) override; |
| 83 | |
| 84 | private: |
| 85 | CLTensor &_tensor; |
| 86 | }; |
| 87 | |
| 88 | inline CLAccessor::CLAccessor(CLTensor &tensor) |
| 89 | : _tensor{ tensor } |
| 90 | { |
| 91 | _tensor.map(); |
| 92 | } |
| 93 | |
| 94 | inline CLAccessor::~CLAccessor() |
| 95 | { |
| 96 | _tensor.unmap(); |
| 97 | } |
| 98 | |
| 99 | inline TensorShape CLAccessor::shape() const |
| 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 | |
| 114 | inline Format CLAccessor::format() const |
| 115 | { |
| 116 | return _tensor.info()->format(); |
| 117 | } |
| 118 | |
Michele Di Giorgio | 4a65b98 | 2018-03-02 11:21:38 +0000 | [diff] [blame] | 119 | inline DataLayout CLAccessor::data_layout() const |
| 120 | { |
| 121 | return _tensor.info()->data_layout(); |
| 122 | } |
| 123 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 124 | inline DataType CLAccessor::data_type() const |
| 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 | |
Giorgio Arena | a261181 | 2017-07-21 10:08:48 +0100 | [diff] [blame] | 139 | inline PaddingSize CLAccessor::padding() const |
| 140 | { |
| 141 | return _tensor.info()->padding(); |
| 142 | } |
| 143 | |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 144 | inline QuantizationInfo CLAccessor::quantization_info() const |
| 145 | { |
| 146 | return _tensor.info()->quantization_info(); |
| 147 | } |
| 148 | |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 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 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 168 | } // namespace test |
| 169 | } // namespace arm_compute |
Moritz Pflanzer | d58cec0 | 2017-07-18 15:44:21 +0100 | [diff] [blame] | 170 | #endif /* __ARM_COMPUTE_TEST_CLACCESSOR_H__ */ |