Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2019 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_NEMINMAXLOCATIONKERNEL_H |
| 25 | #define ARM_COMPUTE_NEMINMAXLOCATIONKERNEL_H |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/core/IArray.h" |
| 28 | #include "arm_compute/core/NEON/INEKernel.h" |
Michalis Spyrou | 07781ac | 2017-08-31 15:11:41 +0100 | [diff] [blame] | 29 | #include "support/Mutex.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | |
| 31 | #include <cstdint> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | |
| 33 | namespace arm_compute |
| 34 | { |
| 35 | class ITensor; |
| 36 | using IImage = ITensor; |
| 37 | |
| 38 | /** Interface for the kernel to perform min max search on an image. */ |
| 39 | class NEMinMaxKernel : public INEKernel |
| 40 | { |
| 41 | public: |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 42 | const char *name() const override |
| 43 | { |
| 44 | return "NEMinMaxKernel"; |
| 45 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 46 | /** Default constructor */ |
| 47 | NEMinMaxKernel(); |
| 48 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 49 | NEMinMaxKernel(const NEMinMaxKernel &) = delete; |
| 50 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 51 | NEMinMaxKernel &operator=(const NEMinMaxKernel &) = delete; |
| 52 | /** Allow instances of this class to be moved */ |
| 53 | NEMinMaxKernel(NEMinMaxKernel &&) = default; |
| 54 | /** Allow instances of this class to be moved */ |
| 55 | NEMinMaxKernel &operator=(NEMinMaxKernel &&) = default; |
| 56 | /** Default destructor */ |
| 57 | ~NEMinMaxKernel() = default; |
| 58 | |
| 59 | /** Initialise the kernel's input and outputs. |
| 60 | * |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 61 | * @param[in] input Input Image. Data types supported: U8/S16/F32. |
| 62 | * @param[out] min Minimum value of image. Data types supported: S32 if input type is U8/S16, F32 if input type is F32. |
| 63 | * @param[out] max Maximum value of image. Data types supported: S32 if input type is U8/S16, F32 if input type is F32. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 64 | */ |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 65 | void configure(const IImage *input, void *min, void *max); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 66 | /** Resets global minimum and maximum. */ |
| 67 | void reset(); |
| 68 | |
| 69 | // Inherited methods overridden: |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 70 | void run(const Window &window, const ThreadInfo &info) override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 71 | |
| 72 | private: |
| 73 | /** Performs the min/max algorithm on U8 images on a given window. |
| 74 | * |
| 75 | * @param win The window to run the algorithm on. |
| 76 | */ |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 77 | void minmax_U8(Window win); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 78 | /** Performs the min/max algorithm on S16 images on a given window. |
| 79 | * |
| 80 | * @param win The window to run the algorithm on. |
| 81 | */ |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 82 | void minmax_S16(Window win); |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 83 | /** Performs the min/max algorithm on F32 images on a given window. |
| 84 | * |
| 85 | * @param win The window to run the algorithm on. |
| 86 | */ |
| 87 | void minmax_F32(Window win); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 88 | /** Common signature for all the specialised MinMax functions |
| 89 | * |
| 90 | * @param[in] window Region on which to execute the kernel. |
| 91 | */ |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 92 | using MinMaxFunction = void (NEMinMaxKernel::*)(Window window); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 93 | /** MinMax function to use for the particular image types passed to configure() */ |
| 94 | MinMaxFunction _func; |
| 95 | /** Helper to update min/max values **/ |
| 96 | template <typename T> |
| 97 | void update_min_max(T min, T max); |
| 98 | |
Michalis Spyrou | 07781ac | 2017-08-31 15:11:41 +0100 | [diff] [blame] | 99 | const IImage *_input; /**< Input image. */ |
| 100 | void *_min; /**< Minimum value. */ |
| 101 | void *_max; /**< Maximum value. */ |
| 102 | arm_compute::Mutex _mtx; /**< Mutex used for result reduction. */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | /** Interface for the kernel to find min max locations of an image. */ |
| 106 | class NEMinMaxLocationKernel : public INEKernel |
| 107 | { |
| 108 | public: |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 109 | const char *name() const override |
| 110 | { |
| 111 | return "NEMinMaxLocationKernel"; |
| 112 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 113 | /** Default constructor */ |
| 114 | NEMinMaxLocationKernel(); |
| 115 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 116 | NEMinMaxLocationKernel(const NEMinMaxLocationKernel &) = delete; |
| 117 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 118 | NEMinMaxLocationKernel &operator=(const NEMinMaxLocationKernel &) = delete; |
| 119 | /** Allow instances of this class to be moved */ |
| 120 | NEMinMaxLocationKernel(NEMinMaxLocationKernel &&) = default; |
| 121 | /** Allow instances of this class to be moved */ |
| 122 | NEMinMaxLocationKernel &operator=(NEMinMaxLocationKernel &&) = default; |
| 123 | /** Default destructor */ |
| 124 | ~NEMinMaxLocationKernel() = default; |
| 125 | |
| 126 | /** Initialise the kernel's input and outputs. |
| 127 | * |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 128 | * @param[in] input Input Image. Data types supported: U8/S16/F32. |
| 129 | * @param[out] min Minimum value of image. Data types supported: S32 if input type is U8/S16, F32 if input type is F32. |
| 130 | * @param[out] max Maximum value of image. Data types supported: S32 if input type is U8/S16, F32 if input type is F32. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 131 | * @param[out] min_loc Array of minimum value locations. |
| 132 | * @param[out] max_loc Array of maximum value locations. |
| 133 | * @param[out] min_count Number of minimum value encounters. |
| 134 | * @param[out] max_count Number of maximum value encounters. |
| 135 | */ |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 136 | void configure(const IImage *input, void *min, void *max, |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 137 | ICoordinates2DArray *min_loc = nullptr, ICoordinates2DArray *max_loc = nullptr, |
| 138 | uint32_t *min_count = nullptr, uint32_t *max_count = nullptr); |
| 139 | |
| 140 | // Inherited methods overridden: |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 141 | void run(const Window &window, const ThreadInfo &info) override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 142 | bool is_parallelisable() const override; |
| 143 | |
| 144 | private: |
| 145 | /** Performs the min/max location algorithm on T type images on a given window. |
| 146 | * |
| 147 | * @param win The window to run the algorithm on. |
| 148 | */ |
| 149 | template <class T, bool count_min, bool count_max, bool loc_min, bool loc_max> |
| 150 | void minmax_loc(const Window &win); |
| 151 | /** Common signature for all the specialised MinMaxLoc functions |
| 152 | * |
| 153 | * @param[in] window Region on which to execute the kernel. |
| 154 | */ |
| 155 | using MinMaxLocFunction = void (NEMinMaxLocationKernel::*)(const Window &window); |
| 156 | /** MinMaxLoc function to use for the particular image types passed to configure() */ |
| 157 | MinMaxLocFunction _func; |
| 158 | /** Helper to create a function pointer table for the parameterized MinMaxLocation functions. */ |
| 159 | template <class T, typename> |
| 160 | struct create_func_table; |
| 161 | |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 162 | const IImage *_input; /**< Input image. */ |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 163 | void *_min; /**< Minimum value. */ |
| 164 | void *_max; /**< Maximum value. */ |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 165 | uint32_t *_min_count; /**< Count of minimum value encounters. */ |
| 166 | uint32_t *_max_count; /**< Count of maximum value encounters. */ |
| 167 | ICoordinates2DArray *_min_loc; /**< Locations of minimum values. */ |
| 168 | ICoordinates2DArray *_max_loc; /**< Locations of maximum values. */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 169 | }; |
Gian Marco Iodice | 356f643 | 2017-09-22 11:32:21 +0100 | [diff] [blame] | 170 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 171 | #endif /*ARM_COMPUTE_NEMINMAXLOCATIONKERNEL_H */ |