Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2020 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [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 | #include "arm_compute/core/CL/kernels/CLMagnitudePhaseKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 28 | #include "arm_compute/core/CL/ICLTensor.h" |
| 29 | #include "arm_compute/core/CL/OpenCL.h" |
| 30 | #include "arm_compute/core/Error.h" |
| 31 | #include "arm_compute/core/Helpers.h" |
| 32 | #include "arm_compute/core/TensorInfo.h" |
| 33 | #include "arm_compute/core/Validate.h" |
| 34 | #include "arm_compute/core/Window.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 35 | #include "support/StringSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | |
| 37 | #include <set> |
| 38 | #include <string> |
| 39 | |
| 40 | using namespace arm_compute; |
| 41 | |
| 42 | CLMagnitudePhaseKernel::CLMagnitudePhaseKernel() |
| 43 | : _gx(nullptr), _gy(nullptr), _magnitude(nullptr), _phase(nullptr), _run_mag(false), _run_phase(false) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | void CLMagnitudePhaseKernel::configure(const ICLTensor *gx, const ICLTensor *gy, ICLTensor *magnitude, ICLTensor *phase, |
| 48 | MagnitudeType mag_type, PhaseType phase_type) |
| 49 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 50 | configure(CLKernelLibrary::get().get_compile_context(), gx, gy, magnitude, phase, mag_type, phase_type); |
| 51 | } |
| 52 | |
Manuel Bottini | 679fc96 | 2020-04-21 16:08:53 +0100 | [diff] [blame] | 53 | void CLMagnitudePhaseKernel::configure(const CLCompileContext &compile_context, const ICLTensor *gx, const ICLTensor *gy, ICLTensor *magnitude, ICLTensor *phase, |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 54 | MagnitudeType mag_type, PhaseType phase_type) |
| 55 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 56 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(gx, 1, DataType::S16, DataType::S32); |
| 57 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(gy, 1, DataType::S16, DataType::S32); |
| 58 | ARM_COMPUTE_ERROR_ON((magnitude == nullptr) && (phase == nullptr)); |
| 59 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(gx, gy); |
| 60 | |
| 61 | _run_mag = (magnitude != nullptr); |
| 62 | _run_phase = (phase != nullptr); |
| 63 | if(_run_mag) |
| 64 | { |
| 65 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(magnitude, 1, DataType::S16, DataType::S32); |
| 66 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(gx, magnitude); |
| 67 | } |
| 68 | if(_run_phase) |
| 69 | { |
| 70 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(phase, 1, DataType::U8); |
| 71 | } |
| 72 | |
| 73 | if(!_run_mag && !_run_phase) |
| 74 | { |
| 75 | ARM_COMPUTE_ERROR("At least one output must be NOT NULL"); |
| 76 | } |
| 77 | |
| 78 | _gx = gx; |
| 79 | _gy = gy; |
| 80 | _magnitude = magnitude; |
| 81 | _phase = phase; |
| 82 | |
| 83 | // Construct kernel name |
| 84 | std::set<std::string> build_opts = {}; |
| 85 | |
| 86 | // Add magnitude type |
| 87 | if(_run_mag) |
| 88 | { |
| 89 | switch(mag_type) |
| 90 | { |
| 91 | case MagnitudeType::L1NORM: |
| 92 | build_opts.insert("-DMAGNITUDE=1"); |
| 93 | break; |
| 94 | case MagnitudeType::L2NORM: |
| 95 | build_opts.insert("-DMAGNITUDE=2"); |
| 96 | break; |
| 97 | default: |
| 98 | ARM_COMPUTE_ERROR("Unsupported magnitude calculation type."); |
| 99 | build_opts.insert("-DMAGNITUDE=0"); |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // Add phase type |
| 105 | if(_run_phase) |
| 106 | { |
| 107 | switch(phase_type) |
| 108 | { |
| 109 | case PhaseType::UNSIGNED: |
| 110 | build_opts.insert("-DPHASE=1"); |
| 111 | break; |
| 112 | case PhaseType::SIGNED: |
| 113 | build_opts.insert("-DPHASE=2"); |
| 114 | break; |
| 115 | default: |
| 116 | ARM_COMPUTE_ERROR("Unsupported phase calculation type."); |
| 117 | build_opts.insert("-DPHASE=0"); |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // Add data_type |
| 123 | build_opts.insert("-DDATA_TYPE=" + get_cl_type_from_data_type(gx->info()->data_type())); |
| 124 | |
| 125 | // Create kernel |
Gary Antcliffe | fffbdbc | 2019-05-28 11:40:21 +0100 | [diff] [blame] | 126 | const std::string kernel_name = std::string("magnitude_phase"); |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 127 | _kernel = create_kernel(compile_context, kernel_name, build_opts); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 128 | |
| 129 | // Configure kernel window |
| 130 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 131 | |
| 132 | Window win = calculate_max_window(*gx->info(), Steps(num_elems_processed_per_iteration)); |
| 133 | |
| 134 | AccessWindowHorizontal gx_access(gx->info(), 0, num_elems_processed_per_iteration); |
| 135 | AccessWindowHorizontal gy_access(gy->info(), 0, num_elems_processed_per_iteration); |
| 136 | AccessWindowHorizontal output_magnitude_access(magnitude == nullptr ? nullptr : magnitude->info(), 0, num_elems_processed_per_iteration); |
| 137 | AccessWindowHorizontal output_phase_access(phase == nullptr ? nullptr : phase->info(), 0, num_elems_processed_per_iteration); |
| 138 | |
| 139 | update_window_and_padding(win, |
| 140 | gx_access, gy_access, |
| 141 | output_magnitude_access, output_phase_access); |
| 142 | |
| 143 | ValidRegion valid_region = intersect_valid_regions(gx->info()->valid_region(), |
| 144 | gy->info()->valid_region()); |
| 145 | output_magnitude_access.set_valid_region(win, valid_region); |
| 146 | output_phase_access.set_valid_region(win, valid_region); |
| 147 | |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 148 | ICLKernel::configure_internal(win); |
Gary Antcliffe | fffbdbc | 2019-05-28 11:40:21 +0100 | [diff] [blame] | 149 | |
| 150 | // Set config_id for enabling LWS tuning |
| 151 | _config_id = kernel_name; |
| 152 | _config_id += "_"; |
| 153 | _config_id += lower_string(string_from_data_type(gx->info()->data_type())); |
| 154 | _config_id += "_"; |
| 155 | _config_id += support::cpp11::to_string(gx->info()->dimension(0)); |
| 156 | _config_id += "_"; |
| 157 | _config_id += support::cpp11::to_string(gx->info()->dimension(1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void CLMagnitudePhaseKernel::run(const Window &window, cl::CommandQueue &queue) |
| 161 | { |
| 162 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 163 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 164 | |
| 165 | Window slice = window.first_slice_window_2D(); |
| 166 | do |
| 167 | { |
| 168 | unsigned int idx = 0; |
| 169 | add_2D_tensor_argument(idx, _gx, slice); |
| 170 | add_2D_tensor_argument(idx, _gy, slice); |
Michalis Spyrou | e1651a5 | 2019-07-11 15:00:49 +0100 | [diff] [blame] | 171 | add_2D_tensor_argument_if((_run_mag), idx, _magnitude, slice); |
| 172 | add_2D_tensor_argument_if((_run_phase), idx, _phase, slice); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 173 | |
Michele Di Giorgio | 6b9f388 | 2019-07-01 16:37:04 +0100 | [diff] [blame] | 174 | enqueue(queue, *this, slice, lws_hint()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 175 | } |
| 176 | while(window.slide_window_slice_2D(slice)); |
| 177 | } |