blob: d9ced1393e5860d324111a8f7453bdc7e7fbce6e [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2016-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_CLMEANSTDDEV_H
25#define ARM_COMPUTE_CLMEANSTDDEV_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
27#include "arm_compute/core/CL/OpenCL.h"
Michalis Spyroud1794eb2018-06-15 16:15:26 +010028#include "arm_compute/runtime/CL/functions/CLReductionOperation.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/runtime/IFunction.h"
Michalis Spyroud1794eb2018-06-15 16:15:26 +010030#include "arm_compute/runtime/IMemoryManager.h"
Georgios Pinitas26014cf2019-09-09 19:00:57 +010031#include "arm_compute/runtime/MemoryGroup.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010033#include <memory>
34
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035namespace arm_compute
36{
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010037class CLCompileContext;
38class ICLTensor;
39class ITensorInfo;
40class CLFillBorderKernel;
41class CLMeanStdDevKernel;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010042/** Basic function to execute mean and standard deviation by calling @ref CLMeanStdDevKernel */
43class CLMeanStdDev : public IFunction
44{
45public:
46 /** Default Constructor. */
Michalis Spyroud1794eb2018-06-15 16:15:26 +010047 CLMeanStdDev(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
48 /** Prevent instances of this class from being copied (As this class contains pointers) */
49 CLMeanStdDev(const CLMeanStdDev &) = delete;
50 /** Prevent instances of this class from being copied (As this class contains pointers) */
51 CLMeanStdDev &operator=(const CLMeanStdDev &) = delete;
52 /** Allow instances of this class to be moved */
53 CLMeanStdDev(CLMeanStdDev &&) = default;
54 /** Allow instances of this class to be moved */
55 CLMeanStdDev &operator=(CLMeanStdDev &&) = default;
56 /** Default destructor */
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010057 ~CLMeanStdDev();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010058 /** Initialise the kernel's inputs and outputs.
59 *
Michalis Spyroud1794eb2018-06-15 16:15:26 +010060 * @param[in, out] input Input image. Data types supported: U8/F16/F32. (Written to only for border filling)
Giorgio Arenaa2611812017-07-21 10:08:48 +010061 * @param[out] mean Output average pixel value.
Michalis Spyroud1794eb2018-06-15 16:15:26 +010062 * @param[out] stddev (Optional) Output standard deviation of pixel values.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010063 */
Giorgio Arenaa2611812017-07-21 10:08:48 +010064 void configure(ICLImage *input, float *mean, float *stddev = nullptr);
Manuel Bottini2b84be52020-04-08 10:15:51 +010065 /** Initialise the kernel's inputs and outputs.
66 *
67 * @param[in] compile_context The compile context to be used.
68 * @param[in, out] input Input image. Data types supported: U8/F16/F32. (Written to only for border filling)
69 * @param[out] mean Output average pixel value.
70 * @param[out] stddev (Optional) Output standard deviation of pixel values.
71 */
72 void configure(const CLCompileContext &compile_context, ICLImage *input, float *mean, float *stddev = nullptr);
Michalis Spyroud1794eb2018-06-15 16:15:26 +010073 /** Static function to check if given info will lead to a valid configuration of @ref CLMeanStdDev
74 *
75 * @param[in] input Input image. Data types supported: U8/F16/F32.
76 * @param[in] mean Output average pixel value.
77 * @param[in] stddev (Optional) Output standard deviation of pixel values.
78 *
79 * @return a status
80 */
81 static Status validate(ITensorInfo *input, float *mean, float *stddev = nullptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010082
83 // Inherited methods overridden:
84 void run() override;
85
86private:
Michalis Spyroud1794eb2018-06-15 16:15:26 +010087 template <typename T>
88 void run_float();
89 void run_int();
90
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010091 MemoryGroup _memory_group; /**< Function's memory group */
92 DataType _data_type; /**< Input data type. */
93 unsigned int _num_pixels; /**< Number of image's pixels. */
94 bool _run_stddev; /**< Flag for knowing if we should run stddev reduction function. */
95 CLReductionOperation _reduction_operation_mean; /**< Reduction operation function for computing mean value. */
96 CLReductionOperation _reduction_operation_stddev; /**< Reduction operation function for computing standard deviation. */
97 CLTensor _reduction_output_mean; /**< Reduction operation output tensor for mean value. */
98 CLTensor _reduction_output_stddev; /**< Reduction operation output tensor for standard deviation value. */
99 float *_mean; /**< Pointer that holds the mean value. */
100 float *_stddev; /**< Pointer that holds the standard deviation value. */
101 std::unique_ptr<CLMeanStdDevKernel> _mean_stddev_kernel; /**< Kernel that standard deviation calculation. */
102 std::unique_ptr<CLFillBorderKernel> _fill_border_kernel; /**< Kernel that fills the border with zeroes. */
103 cl::Buffer _global_sum; /**< Variable that holds the global sum among calls in order to ease reduction */
104 cl::Buffer _global_sum_squared; /**< Variable that holds the global sum of squared values among calls in order to ease reduction */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105};
106}
Michalis Spyrouf4643372019-11-29 16:17:13 +0000107#endif /*ARM_COMPUTE_CLMEANSTDDEV_H */