1 /* 2 * Copyright (c) 2016-2020 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 "src/core/NEON/INEKernel.h" 29 #include "support/Mutex.h" 30 31 #include <cstdint> 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: name()42 const char *name() const override 43 { 44 return "NEMinMaxKernel"; 45 } 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 /** Prevent instances of this class from being moved (As this class contains non movable objects) */ 53 NEMinMaxKernel(NEMinMaxKernel &&) = delete; 54 /** Prevent instances of this class from being moved (As this class contains non movable objects) */ 55 NEMinMaxKernel &operator=(NEMinMaxKernel &&) = delete; 56 /** Default destructor */ 57 ~NEMinMaxKernel() = default; 58 59 /** Initialise the kernel's input and outputs. 60 * 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. 64 */ 65 void configure(const IImage *input, void *min, void *max); 66 /** Resets global minimum and maximum. */ 67 void reset(); 68 69 // Inherited methods overridden: 70 void run(const Window &window, const ThreadInfo &info) override; 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 */ 77 void minmax_U8(Window win); 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 */ 82 void minmax_S16(Window win); 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); 88 /** Common signature for all the specialised MinMax functions 89 * 90 * @param[in] window Region on which to execute the kernel. 91 */ 92 using MinMaxFunction = void (NEMinMaxKernel::*)(Window window); 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 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. */ 103 }; 104 105 /** Interface for the kernel to find min max locations of an image. */ 106 class NEMinMaxLocationKernel : public INEKernel 107 { 108 public: name()109 const char *name() const override 110 { 111 return "NEMinMaxLocationKernel"; 112 } 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 * 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. 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 */ 136 void configure(const IImage *input, void *min, void *max, 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: 141 void run(const Window &window, const ThreadInfo &info) override; 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 162 const IImage *_input; /**< Input image. */ 163 void *_min; /**< Minimum value. */ 164 void *_max; /**< Maximum value. */ 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. */ 169 }; 170 } // namespace arm_compute 171 #endif /*ARM_COMPUTE_NEMINMAXLOCATIONKERNEL_H */ 172