Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, 2017 ARM Limited. |
| 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" |
| 35 | |
| 36 | #include <set> |
| 37 | #include <string> |
| 38 | |
| 39 | using namespace arm_compute; |
| 40 | |
| 41 | CLMagnitudePhaseKernel::CLMagnitudePhaseKernel() |
| 42 | : _gx(nullptr), _gy(nullptr), _magnitude(nullptr), _phase(nullptr), _run_mag(false), _run_phase(false) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | void CLMagnitudePhaseKernel::configure(const ICLTensor *gx, const ICLTensor *gy, ICLTensor *magnitude, ICLTensor *phase, |
| 47 | MagnitudeType mag_type, PhaseType phase_type) |
| 48 | { |
| 49 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(gx, 1, DataType::S16, DataType::S32); |
| 50 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(gy, 1, DataType::S16, DataType::S32); |
| 51 | ARM_COMPUTE_ERROR_ON((magnitude == nullptr) && (phase == nullptr)); |
| 52 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(gx, gy); |
| 53 | |
| 54 | _run_mag = (magnitude != nullptr); |
| 55 | _run_phase = (phase != nullptr); |
| 56 | if(_run_mag) |
| 57 | { |
| 58 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(magnitude, 1, DataType::S16, DataType::S32); |
| 59 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(gx, magnitude); |
| 60 | } |
| 61 | if(_run_phase) |
| 62 | { |
| 63 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(phase, 1, DataType::U8); |
| 64 | } |
| 65 | |
| 66 | if(!_run_mag && !_run_phase) |
| 67 | { |
| 68 | ARM_COMPUTE_ERROR("At least one output must be NOT NULL"); |
| 69 | } |
| 70 | |
| 71 | _gx = gx; |
| 72 | _gy = gy; |
| 73 | _magnitude = magnitude; |
| 74 | _phase = phase; |
| 75 | |
| 76 | // Construct kernel name |
| 77 | std::set<std::string> build_opts = {}; |
| 78 | |
| 79 | // Add magnitude type |
| 80 | if(_run_mag) |
| 81 | { |
| 82 | switch(mag_type) |
| 83 | { |
| 84 | case MagnitudeType::L1NORM: |
| 85 | build_opts.insert("-DMAGNITUDE=1"); |
| 86 | break; |
| 87 | case MagnitudeType::L2NORM: |
| 88 | build_opts.insert("-DMAGNITUDE=2"); |
| 89 | break; |
| 90 | default: |
| 91 | ARM_COMPUTE_ERROR("Unsupported magnitude calculation type."); |
| 92 | build_opts.insert("-DMAGNITUDE=0"); |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // Add phase type |
| 98 | if(_run_phase) |
| 99 | { |
| 100 | switch(phase_type) |
| 101 | { |
| 102 | case PhaseType::UNSIGNED: |
| 103 | build_opts.insert("-DPHASE=1"); |
| 104 | break; |
| 105 | case PhaseType::SIGNED: |
| 106 | build_opts.insert("-DPHASE=2"); |
| 107 | break; |
| 108 | default: |
| 109 | ARM_COMPUTE_ERROR("Unsupported phase calculation type."); |
| 110 | build_opts.insert("-DPHASE=0"); |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // Add data_type |
| 116 | build_opts.insert("-DDATA_TYPE=" + get_cl_type_from_data_type(gx->info()->data_type())); |
| 117 | |
| 118 | // Create kernel |
| 119 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("magnitude_phase", build_opts)); |
| 120 | |
| 121 | // Configure kernel window |
| 122 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 123 | |
| 124 | Window win = calculate_max_window(*gx->info(), Steps(num_elems_processed_per_iteration)); |
| 125 | |
| 126 | AccessWindowHorizontal gx_access(gx->info(), 0, num_elems_processed_per_iteration); |
| 127 | AccessWindowHorizontal gy_access(gy->info(), 0, num_elems_processed_per_iteration); |
| 128 | AccessWindowHorizontal output_magnitude_access(magnitude == nullptr ? nullptr : magnitude->info(), 0, num_elems_processed_per_iteration); |
| 129 | AccessWindowHorizontal output_phase_access(phase == nullptr ? nullptr : phase->info(), 0, num_elems_processed_per_iteration); |
| 130 | |
| 131 | update_window_and_padding(win, |
| 132 | gx_access, gy_access, |
| 133 | output_magnitude_access, output_phase_access); |
| 134 | |
| 135 | ValidRegion valid_region = intersect_valid_regions(gx->info()->valid_region(), |
| 136 | gy->info()->valid_region()); |
| 137 | output_magnitude_access.set_valid_region(win, valid_region); |
| 138 | output_phase_access.set_valid_region(win, valid_region); |
| 139 | |
| 140 | ICLKernel::configure(win); |
| 141 | } |
| 142 | |
| 143 | void CLMagnitudePhaseKernel::run(const Window &window, cl::CommandQueue &queue) |
| 144 | { |
| 145 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 146 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 147 | |
| 148 | Window slice = window.first_slice_window_2D(); |
| 149 | do |
| 150 | { |
| 151 | unsigned int idx = 0; |
| 152 | add_2D_tensor_argument(idx, _gx, slice); |
| 153 | add_2D_tensor_argument(idx, _gy, slice); |
| 154 | |
| 155 | if(_run_mag) |
| 156 | { |
| 157 | add_2D_tensor_argument(idx, _magnitude, slice); |
| 158 | } |
| 159 | |
| 160 | if(_run_phase) |
| 161 | { |
| 162 | add_2D_tensor_argument(idx, _phase, slice); |
| 163 | } |
| 164 | |
| 165 | enqueue(queue, *this, slice); |
| 166 | } |
| 167 | while(window.slide_window_slice_2D(slice)); |
| 168 | } |