Compute Library
 21.02
NESobel3x3Kernel Class Reference

Interface for the kernel to run a 3x3 Sobel X filter on a tensor. More...

#include <NESobel3x3Kernel.h>

Collaboration diagram for NESobel3x3Kernel:
[legend]

Public Member Functions

const char * name () const override
 Name of the kernel. More...
 
 NESobel3x3Kernel ()
 Default constructor. More...
 
 NESobel3x3Kernel (const NESobel3x3Kernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
NESobel3x3Kerneloperator= (const NESobel3x3Kernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 NESobel3x3Kernel (NESobel3x3Kernel &&)=default
 Allow instances of this class to be moved. More...
 
NESobel3x3Kerneloperator= (NESobel3x3Kernel &&)=default
 Allow instances of this class to be moved. More...
 
 ~NESobel3x3Kernel ()=default
 Default destructor. More...
 
void configure (const ITensor *input, ITensor *output_x, ITensor *output_y, bool border_undefined)
 Initialise the kernel's source, destination and border mode. More...
 
void run (const Window &window, const ThreadInfo &info) override
 Execute the kernel on the passed window. More...
 
BorderSize border_size () const override
 The size of the border for that kernel. More...
 
- Public Member Functions inherited from ICPPKernel
virtual ~ICPPKernel ()=default
 Default destructor. More...
 
virtual void run_nd (const Window &window, const ThreadInfo &info, const Window &thread_locator)
 legacy compatibility layer for implemantions which do not support thread_locator In these cases we simply narrow the interface down the legacy version More...
 
virtual void run_op (ITensorPack &tensors, const Window &window, const ThreadInfo &info)
 Execute the kernel on the passed window. More...
 
- Public Member Functions inherited from IKernel
 IKernel ()
 Constructor. More...
 
virtual ~IKernel ()=default
 Destructor. More...
 
virtual bool is_parallelisable () const
 Indicates whether or not the kernel is parallelisable. More...
 
const Windowwindow () const
 The maximum window the kernel can be executed on. More...
 

Detailed Description

Interface for the kernel to run a 3x3 Sobel X filter on a tensor.

\[ \mathbf{G}_x=\begin{vmatrix} -1 & 0 & +1\\ -2 & 0 & +2\\ -1 & 0 & +1 \end{vmatrix} \]

Definition at line 43 of file NESobel3x3Kernel.h.

Constructor & Destructor Documentation

◆ NESobel3x3Kernel() [1/3]

Default constructor.

Definition at line 40 of file NESobel3x3Kernel.cpp.

Referenced by NESobel3x3Kernel::name().

41  : _run_sobel_x(false), _run_sobel_y(false), _input(nullptr), _output_x(nullptr), _output_y(nullptr)
42 {
43 }

◆ NESobel3x3Kernel() [2/3]

NESobel3x3Kernel ( const NESobel3x3Kernel )
delete

Prevent instances of this class from being copied (As this class contains pointers)

◆ NESobel3x3Kernel() [3/3]

Allow instances of this class to be moved.

◆ ~NESobel3x3Kernel()

~NESobel3x3Kernel ( )
default

Default destructor.

Referenced by NESobel3x3Kernel::name().

Member Function Documentation

◆ border_size()

BorderSize border_size ( ) const
overridevirtual

The size of the border for that kernel.

Returns
The width in number of elements of the border.

Reimplemented from IKernel.

Definition at line 45 of file NESobel3x3Kernel.cpp.

Referenced by NESobel3x3Kernel::configure(), and NESobel3x3Kernel::name().

46 {
47  return BorderSize{ 1 };
48 }
Container for 2D border size.
Definition: Types.h:273

◆ configure()

void configure ( const ITensor input,
ITensor output_x,
ITensor output_y,
bool  border_undefined 
)

Initialise the kernel's source, destination and border mode.

Note
At least one of output_x or output_y must be set.
Parameters
[in]inputSource tensor. Data type supported: U8.
[out]output_x(Optional) Destination tensor for the X gradient. Data type supported: S16.
[out]output_y(Optional) Destination tensor for the Y gradient. Data type supported: S16.
[in]border_undefinedTrue if the border mode is undefined. False if it's replicate or constant.

Definition at line 50 of file NESobel3x3Kernel.cpp.

References ARM_COMPUTE_ERROR_ON, ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN, NESobel3x3Kernel::border_size(), arm_compute::calculate_max_window(), ITensor::info(), arm_compute::test::validation::input, BorderSize::left, num_elems_processed_per_iteration, arm_compute::S16, BorderSize::top, arm_compute::U8, arm_compute::update_window_and_padding(), and ITensorInfo::valid_region().

Referenced by NESobel3x3Kernel::name().

51 {
53  ARM_COMPUTE_ERROR_ON((output_x == nullptr) && (output_y == nullptr));
54 
55  _run_sobel_x = output_x != nullptr;
56  _run_sobel_y = output_y != nullptr;
57 
58  if(_run_sobel_x)
59  {
61  }
62 
63  if(_run_sobel_y)
64  {
66  }
67 
68  _input = input;
69  _output_x = output_x;
70  _output_y = output_y;
71 
72  // Configure kernel window
73  constexpr unsigned int num_elems_processed_per_iteration = 8;
74  constexpr unsigned int num_elems_read_per_iteration = 16;
75  constexpr unsigned int num_elems_written_per_iteration = 8;
76  constexpr unsigned int num_rows_read_per_iteration = 3;
77 
78  Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
79  AccessWindowHorizontal output_x_access(output_x == nullptr ? nullptr : output_x->info(), 0, num_elems_written_per_iteration);
80  AccessWindowHorizontal output_y_access(output_y == nullptr ? nullptr : output_y->info(), 0, num_elems_written_per_iteration);
81 
83  AccessWindowRectangle(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration),
84  output_x_access,
85  output_y_access);
86 
87  output_x_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
88  output_y_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
89 
90  INEKernel::configure(win);
91 }
unsigned int top
top of the border
Definition: Types.h:375
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
1 channel, 1 U8 per channel
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
virtual ValidRegion valid_region() const =0
Valid region of the tensor.
Implementation of a rectangular access pattern.
bool update_window_and_padding(Window &win, Ts &&... patterns)
Update window and padding size for each of the access patterns.
Definition: WindowHelpers.h:46
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
Implementation of a row access pattern.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
unsigned int left
left of the border
Definition: Types.h:378
1 channel, 1 S16 per channel
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:790
unsigned int num_elems_processed_per_iteration
Describe a multidimensional execution window.
Definition: Window.h:39
BorderSize border_size() const override
The size of the border for that kernel.

◆ name()

◆ operator=() [1/2]

NESobel3x3Kernel& operator= ( const NESobel3x3Kernel )
delete

Prevent instances of this class from being copied (As this class contains pointers)

Referenced by NESobel3x3Kernel::name().

◆ operator=() [2/2]

NESobel3x3Kernel& operator= ( NESobel3x3Kernel &&  )
default

Allow instances of this class to be moved.

◆ run()

void run ( const Window window,
const ThreadInfo info 
)
overridevirtual

Execute the kernel on the passed window.

Warning
If is_parallelisable() returns false then the passed window must be equal to window()
Note
The window has to be a region within the window returned by the window() method
The width of the window has to be a multiple of num_elems_processed_per_iteration().
Parameters
[in]windowRegion on which to execute the kernel. (Must be a region of the window returned by window())
[in]infoInfo about executing thread and CPU.

Reimplemented from ICPPKernel.

Definition at line 93 of file NESobel3x3Kernel.cpp.

References ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW, ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL, ARM_COMPUTE_UNUSED, arm_compute::execute_window_loop(), arm_compute::test::validation::input, Iterator::offset(), Iterator::ptr(), ITensor::ptr_to_element(), and IKernel::window().

Referenced by NESobel3x3Kernel::name().

94 {
95  ARM_COMPUTE_UNUSED(info);
98 
99  const unsigned char *const input_top_ptr = _input->ptr_to_element(Coordinates(-1, -1));
100  const unsigned char *const input_mid_ptr = _input->ptr_to_element(Coordinates(-1, 0));
101  const unsigned char *const input_bot_ptr = _input->ptr_to_element(Coordinates(-1, 1));
102 
103  Iterator input(_input, window);
104  Iterator output_y;
105  Iterator output_x;
106 
107  if(_run_sobel_y)
108  {
109  output_y = Iterator(_output_y, window);
110  }
111 
112  if(_run_sobel_x)
113  {
114  output_x = Iterator(_output_x, window);
115  }
116 
117  static const int16x8_t two = vdupq_n_s16(2);
118  static const int16x8_t minustwo = vdupq_n_s16(-2);
119 
120  if(_run_sobel_y && _run_sobel_x)
121  {
122  execute_window_loop(window, [&](const Coordinates &)
123  {
124  const uint8x16_t top_data = vld1q_u8(input_top_ptr + input.offset());
125  const uint8x16_t mid_data = vld1q_u8(input_mid_ptr + input.offset());
126  const uint8x16_t bot_data = vld1q_u8(input_bot_ptr + input.offset());
127 
128  const int16x8x2_t top_s16 =
129  {
130  {
131  vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(top_data))),
132  vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(top_data)))
133  }
134  };
135  const int16x8x2_t mid_s16 =
136  {
137  {
138  vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mid_data))),
139  vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mid_data)))
140  }
141  };
142  const int16x8x2_t bot_s16 =
143  {
144  {
145  vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bot_data))),
146  vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bot_data)))
147  }
148  };
149 
150  //SOBEL Y
151  //top left
152  int16x8_t out_y = vnegq_s16(top_s16.val[0]);
153  //top mid
154  out_y = vmlaq_s16(out_y, vextq_s16(top_s16.val[0], top_s16.val[1], 1), minustwo);
155  //top right
156  out_y = vsubq_s16(out_y, vextq_s16(top_s16.val[0], top_s16.val[1], 2));
157  //bot left
158  out_y = vaddq_s16(out_y, bot_s16.val[0]);
159  //bot mid
160  out_y = vmlaq_s16(out_y, vextq_s16(bot_s16.val[0], bot_s16.val[1], 1), two);
161  //bot right
162  out_y = vaddq_s16(out_y, vextq_s16(bot_s16.val[0], bot_s16.val[1], 2));
163 
164  vst1q_s16(reinterpret_cast<int16_t *>(output_y.ptr()), out_y);
165 
166  //SOBEL X
167  //top left
168  int16x8_t out_x = vnegq_s16(top_s16.val[0]);
169  //top right
170  out_x = vaddq_s16(out_x, vextq_s16(top_s16.val[0], top_s16.val[1], 2));
171  //mid left
172  out_x = vmlaq_s16(out_x, mid_s16.val[0], minustwo);
173  //mid right
174  out_x = vmlaq_s16(out_x, vextq_s16(mid_s16.val[0], mid_s16.val[1], 2), two);
175  //bot left
176  out_x = vsubq_s16(out_x, bot_s16.val[0]);
177  //bot right
178  out_x = vaddq_s16(out_x, vextq_s16(bot_s16.val[0], bot_s16.val[1], 2));
179 
180  vst1q_s16(reinterpret_cast<int16_t *>(output_x.ptr()), out_x);
181  },
182  input, output_x, output_y);
183  }
184  else if(_run_sobel_x)
185  {
186  execute_window_loop(window, [&](const Coordinates &)
187  {
188  const uint8x16_t top_data = vld1q_u8(input_top_ptr + input.offset());
189  const uint8x16_t mid_data = vld1q_u8(input_mid_ptr + input.offset());
190  const uint8x16_t bot_data = vld1q_u8(input_bot_ptr + input.offset());
191 
192  const int16x8x2_t top_s16 =
193  {
194  {
195  vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(top_data))),
196  vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(top_data)))
197  }
198  };
199  const int16x8x2_t mid_s16 =
200  {
201  {
202  vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mid_data))),
203  vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mid_data)))
204  }
205  };
206  const int16x8x2_t bot_s16 =
207  {
208  {
209  vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bot_data))),
210  vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bot_data)))
211  }
212  };
213 
214  //SOBEL X
215  //top left
216  int16x8_t out = vnegq_s16(top_s16.val[0]);
217  //top right
218  out = vaddq_s16(out, vextq_s16(top_s16.val[0], top_s16.val[1], 2));
219  //mid left
220  out = vmlaq_s16(out, mid_s16.val[0], minustwo);
221  //mid right
222  out = vmlaq_s16(out, vextq_s16(mid_s16.val[0], mid_s16.val[1], 2), two);
223  //bot left
224  out = vsubq_s16(out, bot_s16.val[0]);
225  //bot right
226  out = vaddq_s16(out, vextq_s16(bot_s16.val[0], bot_s16.val[1], 2));
227 
228  vst1q_s16(reinterpret_cast<int16_t *>(output_x.ptr()), out);
229  },
230  input, output_x);
231  }
232  else if(_run_sobel_y)
233  {
234  execute_window_loop(window, [&](const Coordinates &)
235  {
236  const uint8x16_t top_data = vld1q_u8(input_top_ptr + input.offset());
237  const uint8x16_t bot_data = vld1q_u8(input_bot_ptr + input.offset());
238 
239  const int16x8x2_t top_s16 =
240  {
241  {
242  vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(top_data))),
243  vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(top_data)))
244  }
245  };
246  const int16x8x2_t bot_s16 =
247  {
248  {
249  vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bot_data))),
250  vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bot_data)))
251  }
252  };
253 
254  //SOBEL Y
255  //top left
256  int16x8_t out = vnegq_s16(top_s16.val[0]);
257  //top mid
258  out = vmlaq_s16(out, vextq_s16(top_s16.val[0], top_s16.val[1], 1), minustwo);
259  //top right
260  out = vsubq_s16(out, vextq_s16(top_s16.val[0], top_s16.val[1], 2));
261  //bot left
262  out = vaddq_s16(out, bot_s16.val[0]);
263  //bot mid
264  out = vmlaq_s16(out, vextq_s16(bot_s16.val[0], bot_s16.val[1], 1), two);
265  //bot right
266  out = vaddq_s16(out, vextq_s16(bot_s16.val[0], bot_s16.val[1], 2));
267 
268  vst1q_s16(reinterpret_cast<int16_t *>(output_y.ptr()), out);
269  },
270  input, output_y);
271  }
272 }
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
uint8_t * ptr_to_element(const Coordinates &id) const
Return a pointer to the element at the passed coordinates.
Definition: ITensor.h:63
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
Coordinates of an item.
Definition: Coordinates.h:37
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:139
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Definition: Helpers.inl:77
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205

The documentation for this class was generated from the following files: