Pablo Tello | 1d1c026 | 2017-12-08 16:02:38 +0000 | [diff] [blame] | 1 | /* |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Pablo Tello | 1d1c026 | 2017-12-08 16:02:38 +0000 | [diff] [blame] | 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 | |
Gian Marco Iodice | 5ba5e09 | 2018-12-06 17:13:09 +0000 | [diff] [blame] | 27 | #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 Tello | 1d1c026 | 2017-12-08 16:02:38 +0000 | [diff] [blame] | 32 | #include "arm_compute/runtime/CL/ICLSimpleFunction.h" |
Gian Marco Iodice | 5ba5e09 | 2018-12-06 17:13:09 +0000 | [diff] [blame] | 33 | #include "arm_compute/runtime/IFunction.h" |
Pablo Tello | 1d1c026 | 2017-12-08 16:02:38 +0000 | [diff] [blame] | 34 | #include "support/ToolchainSupport.h" |
| 35 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | namespace test |
| 39 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 40 | /** This template synthetizes an ICLSimpleFunction which runs the given kernel K */ |
Pablo Tello | 1d1c026 | 2017-12-08 16:02:38 +0000 | [diff] [blame] | 41 | template <typename K> |
| 42 | class CLSynthetizeFunction : public ICLSimpleFunction |
| 43 | { |
| 44 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 45 | /** Configure the kernel. |
| 46 | * |
| 47 | * @param[in] args Configuration arguments. |
| 48 | */ |
Pablo Tello | 1d1c026 | 2017-12-08 16:02:38 +0000 | [diff] [blame] | 49 | 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 Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame] | 56 | /** 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 Tello | 1d1c026 | 2017-12-08 16:02:38 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 67 | /** As above but this also setups a Zero border on the input tensor of the specified bordersize */ |
Pablo Tello | 1d1c026 | 2017-12-08 16:02:38 +0000 | [diff] [blame] | 68 | template <typename K, int bordersize> |
| 69 | class CLSynthetizeFunctionWithZeroConstantBorder : public ICLSimpleFunction |
| 70 | { |
| 71 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 72 | /** Configure the kernel. |
| 73 | * |
| 74 | * @param[in] first First configuration argument. |
| 75 | * @param[in] args Rest of the configuration arguments. |
| 76 | */ |
Pablo Tello | 1d1c026 | 2017-12-08 16:02:38 +0000 | [diff] [blame] | 77 | 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 Iodice | 5ba5e09 | 2018-12-06 17:13:09 +0000 | [diff] [blame] | 86 | |
| 87 | /** As above but this also initializes to zero the input tensor */ |
| 88 | template <typename K, int bordersize> |
| 89 | class CLSynthetizeFunctionInitOutputWithZeroAndWithZeroConstantBorder : public IFunction |
| 90 | { |
| 91 | public: |
| 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 | |
| 119 | private: |
| 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 Tello | 1d1c026 | 2017-12-08 16:02:38 +0000 | [diff] [blame] | 124 | } // namespace test |
| 125 | } // namespace arm_compute |
| 126 | #endif /* __ARM_COMPUTE_TEST_CL_HELPER_H__ */ |