blob: 88e716726a786dd903001a70b42e84417c0b539c [file] [log] [blame]
Pablo Tello1d1c0262017-12-08 16:02:38 +00001/*
Alex Gildayc357c472018-03-21 13:54:09 +00002 * Copyright (c) 2017-2018 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 */
24#ifndef __ARM_COMPUTE_TEST_CL_HELPER_H__
25#define __ARM_COMPUTE_TEST_CL_HELPER_H__
26
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 }
Pablo Tello4a626a72018-04-04 10:01:14 +010056 /** Validate input arguments
57 *
58 * @param[in] args Configuration arguments.
59 */
60 template <typename... Args>
61 static Status validate(Args &&... args)
62 {
63 return K::validate(std::forward<Args>(args)...);
64 }
Pablo Tello1d1c0262017-12-08 16:02:38 +000065};
66
Alex Gildayc357c472018-03-21 13:54:09 +000067/** As above but this also setups a Zero border on the input tensor of the specified bordersize */
Pablo Tello1d1c0262017-12-08 16:02:38 +000068template <typename K, int bordersize>
69class CLSynthetizeFunctionWithZeroConstantBorder : public ICLSimpleFunction
70{
71public:
Alex Gildayc357c472018-03-21 13:54:09 +000072 /** Configure the kernel.
73 *
74 * @param[in] first First configuration argument.
75 * @param[in] args Rest of the configuration arguments.
76 */
Pablo Tello1d1c0262017-12-08 16:02:38 +000077 template <typename T, typename... Args>
78 void configure(T first, Args &&... args)
79 {
80 auto k = arm_compute::support::cpp14::make_unique<K>();
81 k->configure(first, std::forward<Args>(args)...);
82 _kernel = std::move(k);
83 _border_handler.configure(first, BorderSize(bordersize), BorderMode::CONSTANT, PixelValue(0));
84 }
85};
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +000086
87/** As above but this also initializes to zero the input tensor */
88template <typename K, int bordersize>
89class CLSynthetizeFunctionInitOutputWithZeroAndWithZeroConstantBorder : public IFunction
90{
91public:
92 /** Configure the kernel.
93 *
94 * @param[in] first First input argument.
95 * @param[in] second Second input argument.
96 * @param[in] args Rest of the configuration arguments.
97 */
98 template <typename T, typename... Args>
99 void configure(T first, T second, Args &&... args)
100 {
101 auto k = arm_compute::support::cpp14::make_unique<K>();
102 k->set_target(CLScheduler::get().target());
103 k->configure(first, second, std::forward<Args>(args)...);
104 _kernel = std::move(k);
105 _border_handler.configure(first, BorderSize(bordersize), BorderMode::CONSTANT, PixelValue(0));
106 _memset_kernel.configure(second, PixelValue(0));
107 }
108
109 // Inherited method overridden:
110 void run() override final
111 {
112 ARM_COMPUTE_ERROR_ON_MSG(!_kernel, "The CL kernel or function isn't configured");
113
114 CLScheduler::get().enqueue(_memset_kernel, false);
115 CLScheduler::get().enqueue(_border_handler, false);
116 CLScheduler::get().enqueue(*_kernel);
117 }
118
119private:
120 CLMemsetKernel _memset_kernel{}; /**< Kernel to initialize the tensor */
121 CLFillBorderKernel _border_handler{}; /**< Kernel to handle borders */
122 std::unique_ptr<ICLKernel> _kernel{}; /**< Kernel to run */
123};
Pablo Tello1d1c0262017-12-08 16:02:38 +0000124} // namespace test
125} // namespace arm_compute
126#endif /* __ARM_COMPUTE_TEST_CL_HELPER_H__ */