Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 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/CLMinMaxLocationKernel.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/Helpers.h" |
| 30 | #include "arm_compute/core/Validate.h" |
| 31 | #include "arm_compute/core/Window.h" |
| 32 | |
| 33 | #include <climits> |
| 34 | |
| 35 | using namespace arm_compute; |
| 36 | |
| 37 | CLMinMaxKernel::CLMinMaxKernel() |
| 38 | : _input(nullptr), _min_max(), _data_type_max_min() |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | void CLMinMaxKernel::configure(const ICLImage *input, cl::Buffer *min_max) |
| 43 | { |
| 44 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S16); |
| 45 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input); |
| 46 | ARM_COMPUTE_ERROR_ON(min_max == nullptr); |
| 47 | |
| 48 | _input = input; |
| 49 | _min_max = min_max; |
| 50 | const unsigned int num_elems_processed_per_iteration = input->info()->dimension(0); |
| 51 | |
| 52 | switch(input->info()->data_type()) |
| 53 | { |
| 54 | case DataType::U8: |
| 55 | _data_type_max_min[0] = UCHAR_MAX; |
| 56 | _data_type_max_min[1] = 0; |
| 57 | break; |
| 58 | case DataType::S16: |
| 59 | _data_type_max_min[0] = SHRT_MAX; |
| 60 | _data_type_max_min[1] = SHRT_MIN; |
| 61 | break; |
| 62 | default: |
| 63 | ARM_COMPUTE_ERROR("You called with the wrong image data types"); |
| 64 | } |
| 65 | |
| 66 | // Set kernel build options |
| 67 | std::set<std::string> build_opts; |
| 68 | build_opts.emplace("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
Moritz Pflanzer | 05da6dd | 2017-07-04 12:08:41 +0100 | [diff] [blame] | 69 | build_opts.emplace("-DDATA_TYPE_MAX=" + support::cpp11::to_string(_data_type_max_min[0])); |
| 70 | build_opts.emplace("-DDATA_TYPE_MIN=" + support::cpp11::to_string(_data_type_max_min[1])); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 71 | build_opts.emplace((0 != (num_elems_processed_per_iteration % max_cl_vector_width)) ? "-DNON_MULTIPLE_OF_16" : ""); |
| 72 | |
| 73 | // Create kernel |
| 74 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("minmax", build_opts)); |
| 75 | |
| 76 | // Set fixed arguments |
| 77 | unsigned int idx = num_arguments_per_2D_tensor(); //Skip the input and output parameters |
| 78 | _kernel.setArg(idx++, *_min_max); |
| 79 | _kernel.setArg<cl_uint>(idx++, input->info()->dimension(0)); |
| 80 | |
| 81 | // Configure kernel window |
| 82 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
| 83 | update_window_and_padding(win, AccessWindowHorizontal(input->info(), 0, num_elems_processed_per_iteration)); |
| 84 | ICLKernel::configure(win); |
| 85 | } |
| 86 | |
| 87 | void CLMinMaxKernel::run(const Window &window, cl::CommandQueue &queue) |
| 88 | { |
| 89 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 90 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 91 | |
| 92 | // Reset mininum and maximum values |
| 93 | queue.enqueueWriteBuffer(*_min_max, CL_FALSE /* blocking */, 0, _data_type_max_min.size() * sizeof(int), _data_type_max_min.data()); |
| 94 | |
| 95 | Window slice = window.first_slice_window_2D(); |
| 96 | do |
| 97 | { |
| 98 | unsigned int idx = 0; |
| 99 | add_2D_tensor_argument(idx, _input, slice); |
| 100 | enqueue(queue, *this, slice); |
| 101 | } |
| 102 | while(window.slide_window_slice_2D(slice)); |
| 103 | } |
| 104 | |
| 105 | CLMinMaxLocationKernel::CLMinMaxLocationKernel() |
| 106 | : _input(nullptr), _min_max_count(nullptr) |
| 107 | { |
| 108 | } |
| 109 | |
| 110 | void CLMinMaxLocationKernel::configure(const ICLImage *input, cl::Buffer *min_max, cl::Buffer *min_max_count, ICLCoordinates2DArray *min_loc, ICLCoordinates2DArray *max_loc) |
| 111 | { |
| 112 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S16); |
| 113 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input); |
| 114 | ARM_COMPUTE_ERROR_ON(min_max == nullptr); |
| 115 | ARM_COMPUTE_ERROR_ON(min_max_count == nullptr && min_loc == nullptr && max_loc == nullptr); |
| 116 | |
| 117 | _input = input; |
| 118 | _min_max_count = min_max_count; |
| 119 | |
| 120 | // Set kernel build options |
| 121 | std::set<std::string> build_opts; |
| 122 | build_opts.emplace("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
| 123 | build_opts.emplace((min_max_count != nullptr) ? "-DCOUNT_MIN_MAX" : ""); |
| 124 | build_opts.emplace((min_loc != nullptr) ? "-DLOCATE_MIN" : ""); |
| 125 | build_opts.emplace((max_loc != nullptr) ? "-DLOCATE_MAX" : ""); |
| 126 | |
| 127 | // Create kernel |
| 128 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("minmaxloc", build_opts)); |
| 129 | |
| 130 | // Set static arguments |
| 131 | unsigned int idx = num_arguments_per_2D_tensor(); //Skip the input and output parameters |
| 132 | _kernel.setArg(idx++, *min_max); |
| 133 | _kernel.setArg(idx++, *min_max_count); |
| 134 | if(min_loc != nullptr) |
| 135 | { |
| 136 | _kernel.setArg(idx++, min_loc->cl_buffer()); |
| 137 | _kernel.setArg<cl_uint>(idx++, min_loc->max_num_values()); |
| 138 | } |
| 139 | if(max_loc != nullptr) |
| 140 | { |
| 141 | _kernel.setArg(idx++, max_loc->cl_buffer()); |
| 142 | _kernel.setArg<cl_uint>(idx++, max_loc->max_num_values()); |
| 143 | } |
| 144 | |
| 145 | // Configure kernel window |
| 146 | constexpr unsigned int num_elems_processed_per_iteration = 1; |
| 147 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
| 148 | update_window_and_padding(win, AccessWindowHorizontal(input->info(), 0, num_elems_processed_per_iteration)); |
| 149 | ICLKernel::configure(win); |
| 150 | } |
| 151 | |
| 152 | void CLMinMaxLocationKernel::run(const Window &window, cl::CommandQueue &queue) |
| 153 | { |
| 154 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 155 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 156 | |
| 157 | static const unsigned int zero_count = 0; |
| 158 | queue.enqueueWriteBuffer(*_min_max_count, CL_FALSE, 0 * sizeof(zero_count), sizeof(zero_count), &zero_count); |
| 159 | queue.enqueueWriteBuffer(*_min_max_count, CL_FALSE, 1 * sizeof(zero_count), sizeof(zero_count), &zero_count); |
| 160 | |
| 161 | Window slice = window.first_slice_window_2D(); |
| 162 | do |
| 163 | { |
| 164 | unsigned int idx = 0; |
| 165 | add_2D_tensor_argument(idx, _input, slice); |
| 166 | enqueue(queue, *this, slice); |
| 167 | } |
| 168 | while(window.slide_window_slice_2D(slice)); |
| 169 | } |