blob: d7896c78352abc00ab5cd450fb007a956ca524dc [file] [log] [blame]
Pablo Tello1d1c0262017-12-08 16:02:38 +00001/*
Manuel Bottini55e16782019-01-15 13:21:57 +00002 * Copyright (c) 2017-2019 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/core/CL/ICLKernel.h"
28#include "arm_compute/core/CL/kernels/CLFillBorderKernel.h"
29#include "arm_compute/core/CL/kernels/CLMemsetKernel.h"
30
31#include "arm_compute/runtime/CL/CLScheduler.h"
Pablo Tello1d1c0262017-12-08 16:02:38 +000032#include "arm_compute/runtime/CL/ICLSimpleFunction.h"
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +000033#include "arm_compute/runtime/IFunction.h"
Pablo Tello1d1c0262017-12-08 16:02:38 +000034#include "support/ToolchainSupport.h"
35
36namespace arm_compute
37{
38namespace test
39{
Alex Gildayc357c472018-03-21 13:54:09 +000040/** This template synthetizes an ICLSimpleFunction which runs the given kernel K */
Pablo Tello1d1c0262017-12-08 16:02:38 +000041template <typename K>
42class CLSynthetizeFunction : public ICLSimpleFunction
43{
44public:
Alex Gildayc357c472018-03-21 13:54:09 +000045 /** Configure the kernel.
46 *
47 * @param[in] args Configuration arguments.
48 */
Pablo Tello1d1c0262017-12-08 16:02:38 +000049 template <typename... Args>
50 void configure(Args &&... args)
51 {
52 auto k = arm_compute::support::cpp14::make_unique<K>();
53 k->configure(std::forward<Args>(args)...);
54 _kernel = std::move(k);
55 }
Gian Marco Iodiced1f54762019-07-19 09:54:47 +010056 /** Configure the kernel setting the GPU target as well
57 *
58 * @param[in] gpu_target GPUTarget to set
59 * @param[in] args Configuration arguments.
60 */
61 template <typename... Args>
62 void configure(GPUTarget gpu_target, Args &&... args)
63 {
64 auto k = arm_compute::support::cpp14::make_unique<K>();
65 k->set_target(gpu_target);
66 k->configure(std::forward<Args>(args)...);
67 _kernel = std::move(k);
68 }
Pablo Tello4a626a72018-04-04 10:01:14 +010069 /** Validate input arguments
70 *
71 * @param[in] args Configuration arguments.
72 */
73 template <typename... Args>
74 static Status validate(Args &&... args)
75 {
76 return K::validate(std::forward<Args>(args)...);
77 }
Pablo Tello1d1c0262017-12-08 16:02:38 +000078};
79
Alex Gildayc357c472018-03-21 13:54:09 +000080/** As above but this also setups a Zero border on the input tensor of the specified bordersize */
Pablo Tello1d1c0262017-12-08 16:02:38 +000081template <typename K, int bordersize>
82class CLSynthetizeFunctionWithZeroConstantBorder : public ICLSimpleFunction
83{
84public:
Alex Gildayc357c472018-03-21 13:54:09 +000085 /** Configure the kernel.
86 *
87 * @param[in] first First configuration argument.
88 * @param[in] args Rest of the configuration arguments.
89 */
Pablo Tello1d1c0262017-12-08 16:02:38 +000090 template <typename T, typename... Args>
91 void configure(T first, Args &&... args)
92 {
93 auto k = arm_compute::support::cpp14::make_unique<K>();
94 k->configure(first, std::forward<Args>(args)...);
95 _kernel = std::move(k);
Manuel Bottini55e16782019-01-15 13:21:57 +000096 _border_handler.configure(first, BorderSize(bordersize), BorderMode::CONSTANT, PixelValue());
Pablo Tello1d1c0262017-12-08 16:02:38 +000097 }
98};
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +000099
100/** As above but this also initializes to zero the input tensor */
101template <typename K, int bordersize>
102class CLSynthetizeFunctionInitOutputWithZeroAndWithZeroConstantBorder : public IFunction
103{
104public:
105 /** Configure the kernel.
106 *
107 * @param[in] first First input argument.
108 * @param[in] second Second input argument.
109 * @param[in] args Rest of the configuration arguments.
110 */
111 template <typename T, typename... Args>
112 void configure(T first, T second, Args &&... args)
113 {
114 auto k = arm_compute::support::cpp14::make_unique<K>();
115 k->set_target(CLScheduler::get().target());
116 k->configure(first, second, std::forward<Args>(args)...);
117 _kernel = std::move(k);
Manuel Bottini55e16782019-01-15 13:21:57 +0000118 _border_handler.configure(first, BorderSize(bordersize), BorderMode::CONSTANT, PixelValue());
119 _memset_kernel.configure(second, PixelValue());
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +0000120 }
121
122 // Inherited method overridden:
123 void run() override final
124 {
125 ARM_COMPUTE_ERROR_ON_MSG(!_kernel, "The CL kernel or function isn't configured");
126
127 CLScheduler::get().enqueue(_memset_kernel, false);
128 CLScheduler::get().enqueue(_border_handler, false);
129 CLScheduler::get().enqueue(*_kernel);
130 }
131
132private:
133 CLMemsetKernel _memset_kernel{}; /**< Kernel to initialize the tensor */
134 CLFillBorderKernel _border_handler{}; /**< Kernel to handle borders */
135 std::unique_ptr<ICLKernel> _kernel{}; /**< Kernel to run */
136};
Pablo Tello1d1c0262017-12-08 16:02:38 +0000137} // namespace test
138} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000139#endif /* ARM_COMPUTE_TEST_CL_HELPER_H */