• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CLMEANSTDDEV_H
25 #define ARM_COMPUTE_CLMEANSTDDEV_H
26 
27 #include "arm_compute/core/CL/OpenCL.h"
28 #include "arm_compute/runtime/CL/functions/CLReductionOperation.h"
29 #include "arm_compute/runtime/IFunction.h"
30 #include "arm_compute/runtime/IMemoryManager.h"
31 #include "arm_compute/runtime/MemoryGroup.h"
32 
33 #include <memory>
34 
35 namespace arm_compute
36 {
37 class CLCompileContext;
38 class ICLTensor;
39 class ITensorInfo;
40 class CLFillBorderKernel;
41 class CLMeanStdDevKernel;
42 /** Basic function to execute mean and standard deviation by calling @ref CLMeanStdDevKernel */
43 class CLMeanStdDev : public IFunction
44 {
45 public:
46     /** Default Constructor. */
47     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 */
57     ~CLMeanStdDev();
58     /** Initialise the kernel's inputs and outputs.
59      *
60      * @param[in, out] input  Input image. Data types supported: U8/F16/F32. (Written to only for border filling)
61      * @param[out]     mean   Output average pixel value.
62      * @param[out]     stddev (Optional) Output standard deviation of pixel values.
63      */
64     void configure(ICLImage *input, float *mean, float *stddev = nullptr);
65     /** 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);
73     /** 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);
82 
83     // Inherited methods overridden:
84     void run() override;
85 
86 private:
87     template <typename T>
88     void run_float();
89     void run_int();
90 
91     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 */
105 };
106 }
107 #endif /*ARM_COMPUTE_CLMEANSTDDEV_H */
108