Compute Library
 21.08
PaddingCalculator.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2019 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_PADDING_CALCULATOR_H
25 #define ARM_COMPUTE_TEST_PADDING_CALCULATOR_H
26 
27 #include "arm_compute/core/Types.h"
28 
29 #include <cmath>
30 
31 namespace arm_compute
32 {
33 namespace test
34 {
35 /** Calculate required padding. */
36 class PaddingCalculator final
37 {
38 public:
39  /** Options for computing the padding */
40  enum class Option
41  {
44  };
45 
46  /** Construct calculator with size of tensor's dimension and step size.
47  *
48  * @param[in] size Number of elements available.
49  * @param[in] processed_elements Number of elements processed per iteration.
50  */
51  PaddingCalculator(int size, int processed_elements)
52  : _size{ size }, _num_processed_elements{ processed_elements }, _num_accessed_elements{ processed_elements }
53  {
54  }
55 
56  /** Set border mode.
57  *
58  * @param[in] mode Border mode.
59  */
61 
62  /** Set border size.
63  *
64  * @param[in] size Border size in elements.
65  */
66  void set_border_size(int size);
67 
68  /** Set offset of the access relative to the current position.
69  *
70  * @param[in] offset Offset of the access.
71  */
72  void set_access_offset(int offset);
73 
74  /** Set number of processed elements.
75  *
76  * @param[in] elements Number of processed elements per iteration.
77  */
78  void set_processed_elements(int elements);
79 
80  /** Set number of accessed elements.
81  *
82  * @param[in] elements Number of accessed elements per iteration.
83  */
84  void set_accessed_elements(int elements);
85 
86  /** Compute the required padding.
87  *
88  * If access offset is negative and border mode is not undefined, the top,
89  * bottom and left padding is set to boder size. Otherwise it is zero.
90  * The right padding is always computed based on the specified parameters.
91  *
92  * @return Required padding in number of elements.
93  */
95 
96  /** Compute the required padding.
97  *
98  * If @p option is INCLUDE_BORDER and border mode is not undefined, the top,
99  * bottom and left padding is set to boder size. Otherwise it is zero.
100  * The right padding is always computed based on the specified parameters.
101  *
102  * @param[in] option Padding option
103  *
104  * @return Required padding in number of elements.
105  */
106  PaddingSize required_padding(Option option) const;
107 
108 private:
109  int _size;
110  int _num_processed_elements;
111  int _num_accessed_elements;
113  int _border_size{ 0 };
114  int _offset{ 0 };
115 };
116 
118 {
119  _mode = mode;
120 }
121 
123 {
124  _border_size = size;
125 }
126 
128 {
129  _offset = offset;
130 }
131 
133 {
134  _num_processed_elements = elements;
135 }
137 {
138  _num_accessed_elements = elements;
139 }
140 
142 {
144 }
145 
147 {
148  PaddingSize padding{ (_mode == BorderMode::UNDEFINED || option == Option::EXCLUDE_BORDER) ? 0U : static_cast<unsigned int>(std::max(0, _border_size)) };
149 
150  int padding_right = 0;
151 
152  if(_mode == BorderMode::UNDEFINED)
153  {
154  padding_right = (((_size - 2 * _border_size + _num_processed_elements - 1) / _num_processed_elements) - 1) * _num_processed_elements + _num_accessed_elements - _size + _border_size + _offset;
155  }
156  else
157  {
158  padding_right = (((_size + _num_processed_elements - 1) / _num_processed_elements) - 1) * _num_processed_elements + _num_accessed_elements - _size + _offset;
159  }
160 
161  padding.right = std::max(0, padding_right);
162 
163  return padding;
164 }
165 } // namespace test
166 } // namespace arm_compute
167 #endif /* ARM_COMPUTE_TEST_PADDING_CALCULATOR_H */
BorderMode
Methods available to handle borders.
Definition: Types.h:256
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
Definition: helpers.h:861
void set_accessed_elements(int elements)
Set number of accessed elements.
Container for 2D border size.
Definition: Types.h:264
Option
Options for computing the padding.
void set_border_size(int size)
Set border size.
void set_processed_elements(int elements)
Set number of processed elements.
Copyright (c) 2017-2021 Arm Limited.
void set_access_offset(int offset)
Set offset of the access relative to the current position.
PaddingSize required_padding() const
Compute the required padding.
PaddingCalculator(int size, int processed_elements)
Construct calculator with size of tensor&#39;s dimension and step size.
Borders are left undefined.
Calculate required padding.
void set_border_mode(BorderMode mode)
Set border mode.