blob: 5153e98add81262990a9f804d9830e6fac5f4861 [file] [log] [blame]
Pablo Tello1d1c0262017-12-08 16:02:38 +00001/*
Sheri Zhang7e20e292021-02-02 11:49:34 +00002 * Copyright (c) 2017-2021 Arm Limited.
Pablo Tello1d1c0262017-12-08 16:02:38 +00003 *
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_TEST_CL_HELPER_H
25#define ARM_COMPUTE_TEST_CL_HELPER_H
Pablo Tello1d1c0262017-12-08 16:02:38 +000026
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +000027#include "arm_compute/runtime/CL/CLScheduler.h"
Pablo Tello1d1c0262017-12-08 16:02:38 +000028#include "arm_compute/runtime/CL/ICLSimpleFunction.h"
Sheri Zhang7e20e292021-02-02 11:49:34 +000029#include "arm_compute/runtime/CL/functions/CLFill.h"
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +000030#include "arm_compute/runtime/IFunction.h"
Sheri Zhang7e20e292021-02-02 11:49:34 +000031#include "src/core/CL/kernels/CLFillBorderKernel.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010032
33#include "src/core/CL/ICLKernel.h"
34
Georgios Pinitas40f51a62020-11-21 03:04:18 +000035#include <memory>
Pablo Tello1d1c0262017-12-08 16:02:38 +000036
37namespace arm_compute
38{
39namespace test
40{
Alex Gildayc357c472018-03-21 13:54:09 +000041/** This template synthetizes an ICLSimpleFunction which runs the given kernel K */
Pablo Tello1d1c0262017-12-08 16:02:38 +000042template <typename K>
43class CLSynthetizeFunction : public ICLSimpleFunction
44{
45public:
Alex Gildayc357c472018-03-21 13:54:09 +000046 /** Configure the kernel.
47 *
48 * @param[in] args Configuration arguments.
49 */
Pablo Tello1d1c0262017-12-08 16:02:38 +000050 template <typename... Args>
51 void configure(Args &&... args)
52 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +000053 auto k = std::make_unique<K>();
Pablo Tello1d1c0262017-12-08 16:02:38 +000054 k->configure(std::forward<Args>(args)...);
55 _kernel = std::move(k);
56 }
Gian Marco Iodiced1f54762019-07-19 09:54:47 +010057 /** 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 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +000065 auto k = std::make_unique<K>();
Gian Marco Iodiced1f54762019-07-19 09:54:47 +010066 k->set_target(gpu_target);
67 k->configure(std::forward<Args>(args)...);
68 _kernel = std::move(k);
69 }
Pablo Tello4a626a72018-04-04 10:01:14 +010070 /** 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 }
Pablo Tello1d1c0262017-12-08 16:02:38 +000079};
80
Alex Gildayc357c472018-03-21 13:54:09 +000081/** As above but this also setups a Zero border on the input tensor of the specified bordersize */
Pablo Tello1d1c0262017-12-08 16:02:38 +000082template <typename K, int bordersize>
83class CLSynthetizeFunctionWithZeroConstantBorder : public ICLSimpleFunction
84{
85public:
Alex Gildayc357c472018-03-21 13:54:09 +000086 /** Configure the kernel.
87 *
88 * @param[in] first First configuration argument.
89 * @param[in] args Rest of the configuration arguments.
90 */
Pablo Tello1d1c0262017-12-08 16:02:38 +000091 template <typename T, typename... Args>
92 void configure(T first, Args &&... args)
93 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +000094 auto k = std::make_unique<K>();
Pablo Tello1d1c0262017-12-08 16:02:38 +000095 k->configure(first, std::forward<Args>(args)...);
96 _kernel = std::move(k);
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010097 _border_handler->configure(first, BorderSize(bordersize), BorderMode::CONSTANT, PixelValue());
Pablo Tello1d1c0262017-12-08 16:02:38 +000098 }
99};
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +0000100
101/** As above but this also initializes to zero the input tensor */
102template <typename K, int bordersize>
103class CLSynthetizeFunctionInitOutputWithZeroAndWithZeroConstantBorder : public IFunction
104{
105public:
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 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000115 auto k = std::make_unique<K>();
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +0000116 k->set_target(CLScheduler::get().target());
117 k->configure(first, second, std::forward<Args>(args)...);
118 _kernel = std::move(k);
Manuel Bottini55e16782019-01-15 13:21:57 +0000119 _border_handler.configure(first, BorderSize(bordersize), BorderMode::CONSTANT, PixelValue());
Sheri Zhang7e20e292021-02-02 11:49:34 +0000120 _fill.configure(second, PixelValue());
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +0000121 }
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
Sheri Zhang7e20e292021-02-02 11:49:34 +0000128 _fill.run();
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +0000129 CLScheduler::get().enqueue(_border_handler, false);
130 CLScheduler::get().enqueue(*_kernel);
131 }
132
133private:
Sheri Zhang7e20e292021-02-02 11:49:34 +0000134 CLFill _fill{}; /**< Kernel to initialize the tensor */
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +0000135 CLFillBorderKernel _border_handler{}; /**< Kernel to handle borders */
136 std::unique_ptr<ICLKernel> _kernel{}; /**< Kernel to run */
137};
Pablo Tello1d1c0262017-12-08 16:02:38 +0000138} // namespace test
139} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000140#endif /* ARM_COMPUTE_TEST_CL_HELPER_H */