Compute Library
 21.02
Helper.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2021 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_TEST_CL_HELPER_H
25 #define ARM_COMPUTE_TEST_CL_HELPER_H
26 
32 
33 #include "src/core/CL/ICLKernel.h"
34 
35 #include <memory>
36 
37 namespace arm_compute
38 {
39 namespace test
40 {
41 /** This template synthetizes an ICLSimpleFunction which runs the given kernel K */
42 template <typename K>
44 {
45 public:
46  /** Configure the kernel.
47  *
48  * @param[in] args Configuration arguments.
49  */
50  template <typename... Args>
51  void configure(Args &&... args)
52  {
53  auto k = std::make_unique<K>();
54  k->configure(std::forward<Args>(args)...);
55  _kernel = std::move(k);
56  }
57  /** Configure the kernel setting the GPU target as well
58  *
59  * @param[in] gpu_target GPUTarget to set
60  * @param[in] args Configuration arguments.
61  */
62  template <typename... Args>
63  void configure(GPUTarget gpu_target, Args &&... args)
64  {
65  auto k = std::make_unique<K>();
66  k->set_target(gpu_target);
67  k->configure(std::forward<Args>(args)...);
68  _kernel = std::move(k);
69  }
70  /** Validate input arguments
71  *
72  * @param[in] args Configuration arguments.
73  */
74  template <typename... Args>
75  static Status validate(Args &&... args)
76  {
77  return K::validate(std::forward<Args>(args)...);
78  }
79 };
80 
81 /** As above but this also setups a Zero border on the input tensor of the specified bordersize */
82 template <typename K, int bordersize>
84 {
85 public:
86  /** Configure the kernel.
87  *
88  * @param[in] first First configuration argument.
89  * @param[in] args Rest of the configuration arguments.
90  */
91  template <typename T, typename... Args>
92  void configure(T first, Args &&... args)
93  {
94  auto k = std::make_unique<K>();
95  k->configure(first, std::forward<Args>(args)...);
96  _kernel = std::move(k);
97  _border_handler->configure(first, BorderSize(bordersize), BorderMode::CONSTANT, PixelValue());
98  }
99 };
100 
101 /** As above but this also initializes to zero the input tensor */
102 template <typename K, int bordersize>
104 {
105 public:
106  /** Configure the kernel.
107  *
108  * @param[in] first First input argument.
109  * @param[in] second Second input argument.
110  * @param[in] args Rest of the configuration arguments.
111  */
112  template <typename T, typename... Args>
113  void configure(T first, T second, Args &&... args)
114  {
115  auto k = std::make_unique<K>();
116  k->set_target(CLScheduler::get().target());
117  k->configure(first, second, std::forward<Args>(args)...);
118  _kernel = std::move(k);
119  _border_handler.configure(first, BorderSize(bordersize), BorderMode::CONSTANT, PixelValue());
120  _fill.configure(second, PixelValue());
121  }
122 
123  // Inherited method overridden:
124  void run() override final
125  {
126  ARM_COMPUTE_ERROR_ON_MSG(!_kernel, "The CL kernel or function isn't configured");
127 
128  _fill.run();
129  CLScheduler::get().enqueue(_border_handler, false);
130  CLScheduler::get().enqueue(*_kernel);
131  }
132 
133 private:
134  CLFill _fill{}; /**< Kernel to initialize the tensor */
135  CLFillBorderKernel _border_handler{}; /**< Kernel to handle borders */
136  std::unique_ptr<ICLKernel> _kernel{}; /**< Kernel to run */
137 };
138 } // namespace test
139 } // namespace arm_compute
140 #endif /* ARM_COMPUTE_TEST_CL_HELPER_H */
void configure(T first, T second, Args &&... args)
Configure the kernel.
Definition: Helper.h:113
Class describing the value of a pixel for any image format.
Definition: PixelValue.h:34
static Status validate(Args &&... args)
Validate input arguments.
Definition: Helper.h:75
Base class for all functions.
Definition: IFunction.h:30
void configure(Args &&... args)
Configure the kernel.
Definition: Helper.h:51
Container for 2D border size.
Definition: Types.h:273
static CLScheduler & get()
Access the scheduler singleton.
As above but this also initializes to zero the input tensor.
Definition: Helper.h:103
As above but this also setups a Zero border on the input tensor of the specified bordersize.
Definition: Helper.h:83
Status class.
Definition: Error.h:52
Copyright (c) 2017-2021 Arm Limited.
Interface to enqueue OpenCL kernels and get/set the OpenCL CommandQueue and ICLTuner.
void run() override final
Run the kernels contained in the function.
Definition: Helper.h:124
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
Basic interface for functions which have a single OpenCL kernel.
void configure(T first, Args &&... args)
Configure the kernel.
Definition: Helper.h:92
void enqueue(ICLKernel &kernel, bool flush=true)
Schedule the execution of the passed kernel if possible.
Interface for filling the border of a kernel.
GPUTarget
Available GPU Targets.
Definition: GPUTarget.h:34
void configure(GPUTarget gpu_target, Args &&... args)
Configure the kernel setting the GPU target as well.
Definition: Helper.h:63
This template synthetizes an ICLSimpleFunction which runs the given kernel K.
Definition: Helper.h:43
Basic function to run opencl::kernels::ClFillKernel.
Definition: CLFill.h:38
Status validate(const ITensorInfo *scores_in, const ITensorInfo *boxes_in, const ITensorInfo *batch_splits_in, const ITensorInfo *scores_out, const ITensorInfo *boxes_out, const ITensorInfo *classes, const ITensorInfo *batch_splits_out, const ITensorInfo *keeps, const ITensorInfo *keeps_size, const BoxNMSLimitInfo info)