blob: e405ea5ae4f836b35a0048c01c7c2482bd1bf01d [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2016, 2017 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_NEMINMAXLOCATIONKERNEL_H__
25#define __ARM_COMPUTE_NEMINMAXLOCATIONKERNEL_H__
26
27#include "arm_compute/core/IArray.h"
28#include "arm_compute/core/NEON/INEKernel.h"
29
30#include <cstdint>
31#include <mutex>
32
33namespace arm_compute
34{
35class ITensor;
36using IImage = ITensor;
37
38/** Interface for the kernel to perform min max search on an image. */
39class NEMinMaxKernel : public INEKernel
40{
41public:
42 /** Default constructor */
43 NEMinMaxKernel();
44 /** Prevent instances of this class from being copied (As this class contains pointers) */
45 NEMinMaxKernel(const NEMinMaxKernel &) = delete;
46 /** Prevent instances of this class from being copied (As this class contains pointers) */
47 NEMinMaxKernel &operator=(const NEMinMaxKernel &) = delete;
48 /** Allow instances of this class to be moved */
49 NEMinMaxKernel(NEMinMaxKernel &&) = default;
50 /** Allow instances of this class to be moved */
51 NEMinMaxKernel &operator=(NEMinMaxKernel &&) = default;
52 /** Default destructor */
53 ~NEMinMaxKernel() = default;
54
55 /** Initialise the kernel's input and outputs.
56 *
57 * @param[in] input Input Image. Data types supported: U8/S16.
58 * @param[out] min Minimum value of image.
59 * @param[out] max Maximum value of image.
60 */
61 void configure(const IImage *input, int32_t *min, int32_t *max);
62 /** Resets global minimum and maximum. */
63 void reset();
64
65 // Inherited methods overridden:
66 void run(const Window &window) override;
67
68private:
69 /** Performs the min/max algorithm on U8 images on a given window.
70 *
71 * @param win The window to run the algorithm on.
72 */
73 void minmax_U8(const Window &win);
74 /** Performs the min/max algorithm on S16 images on a given window.
75 *
76 * @param win The window to run the algorithm on.
77 */
78 void minmax_S16(const Window &win);
79 /** Common signature for all the specialised MinMax functions
80 *
81 * @param[in] window Region on which to execute the kernel.
82 */
83 using MinMaxFunction = void (NEMinMaxKernel::*)(const Window &window);
84 /** MinMax function to use for the particular image types passed to configure() */
85 MinMaxFunction _func;
86 /** Helper to update min/max values **/
87 template <typename T>
88 void update_min_max(T min, T max);
89
90 const IImage *_input; /**< Input image. */
91 int32_t *_min; /**< Minimum value. */
92 int32_t *_max; /**< Maximum value. */
93 int32_t _min_init; /**< Value to initialise global minimum value. */
94 int32_t _max_init; /**< Value to initialise global maximum value. */
95 std::mutex _mtx; /**< Mutex used for result reduction. */
96};
97
98/** Interface for the kernel to find min max locations of an image. */
99class NEMinMaxLocationKernel : public INEKernel
100{
101public:
102 /** Default constructor */
103 NEMinMaxLocationKernel();
104 /** Prevent instances of this class from being copied (As this class contains pointers) */
105 NEMinMaxLocationKernel(const NEMinMaxLocationKernel &) = delete;
106 /** Prevent instances of this class from being copied (As this class contains pointers) */
107 NEMinMaxLocationKernel &operator=(const NEMinMaxLocationKernel &) = delete;
108 /** Allow instances of this class to be moved */
109 NEMinMaxLocationKernel(NEMinMaxLocationKernel &&) = default;
110 /** Allow instances of this class to be moved */
111 NEMinMaxLocationKernel &operator=(NEMinMaxLocationKernel &&) = default;
112 /** Default destructor */
113 ~NEMinMaxLocationKernel() = default;
114
115 /** Initialise the kernel's input and outputs.
116 *
117 * @param[in] input Input Image. Data types supported: U8 or S16.
118 * @param[out] min Minimum value of image.
119 * @param[out] max Maximum value of image.
120 * @param[out] min_loc Array of minimum value locations.
121 * @param[out] max_loc Array of maximum value locations.
122 * @param[out] min_count Number of minimum value encounters.
123 * @param[out] max_count Number of maximum value encounters.
124 */
125 void configure(const IImage *input, int32_t *min, int32_t *max,
126 ICoordinates2DArray *min_loc = nullptr, ICoordinates2DArray *max_loc = nullptr,
127 uint32_t *min_count = nullptr, uint32_t *max_count = nullptr);
128
129 // Inherited methods overridden:
130 void run(const Window &window) override;
131 bool is_parallelisable() const override;
132
133private:
134 /** Performs the min/max location algorithm on T type images on a given window.
135 *
136 * @param win The window to run the algorithm on.
137 */
138 template <class T, bool count_min, bool count_max, bool loc_min, bool loc_max>
139 void minmax_loc(const Window &win);
140 /** Common signature for all the specialised MinMaxLoc functions
141 *
142 * @param[in] window Region on which to execute the kernel.
143 */
144 using MinMaxLocFunction = void (NEMinMaxLocationKernel::*)(const Window &window);
145 /** MinMaxLoc function to use for the particular image types passed to configure() */
146 MinMaxLocFunction _func;
147 /** Helper to create a function pointer table for the parameterized MinMaxLocation functions. */
148 template <class T, typename>
149 struct create_func_table;
150
151 const IImage *_input; /**< Input image. */
152 int32_t *_min; /**< Minimum value. */
153 int32_t *_max; /**< Maximum value. */
154 uint32_t *_min_count; /**< Count of minimum value encounters. */
155 uint32_t *_max_count; /**< Count of maximum value encounters. */
156 ICoordinates2DArray *_min_loc; /**< Locations of minimum values. */
157 ICoordinates2DArray *_max_loc; /**< Locations of maximum values. */
158 unsigned int _num_elems_processed_per_iteration; /**< Elements processed per iteration. */
159};
160}
161#endif /*__ARM_COMPUTE_NEMINMAXLOCATIONKERNEL_H__ */