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) 2017-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/runtime/CL/functions/CLMinMaxLocation.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 25 | #include "arm_compute/core/CL/CLHelpers.h" |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 26 | #include "src/core/CL/kernels/CLMinMaxLocationKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | |
Moritz Pflanzer | 4726fdf | 2017-09-23 10:47:54 +0100 | [diff] [blame] | 28 | namespace arm_compute |
| 29 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | CLMinMaxLocation::CLMinMaxLocation() |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 31 | : _min_max_kernel(std::make_unique<CLMinMaxKernel>()), |
| 32 | _min_max_loc_kernel(std::make_unique<CLMinMaxLocationKernel>()), |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 33 | _min_max_vals(), |
| 34 | _min_max_count_vals(), |
| 35 | _min(nullptr), |
| 36 | _max(nullptr), |
| 37 | _min_count(nullptr), |
| 38 | _max_count(nullptr), |
| 39 | _min_loc(nullptr), |
| 40 | _max_loc(nullptr) |
| 41 | { |
| 42 | } |
| 43 | |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 44 | CLMinMaxLocation::~CLMinMaxLocation() = default; |
| 45 | |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 46 | void CLMinMaxLocation::configure(const ICLImage *input, void *min, void *max, CLCoordinates2DArray *min_loc, CLCoordinates2DArray *max_loc, uint32_t *min_count, uint32_t *max_count) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 47 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 48 | configure(CLKernelLibrary::get().get_compile_context(), input, min, max, min_loc, max_loc, min_count, max_count); |
| 49 | } |
| 50 | |
| 51 | void CLMinMaxLocation::configure(const CLCompileContext &compile_context, const ICLImage *input, void *min, void *max, CLCoordinates2DArray *min_loc, CLCoordinates2DArray *max_loc, |
| 52 | uint32_t *min_count, |
| 53 | uint32_t *max_count) |
| 54 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 55 | ARM_COMPUTE_ERROR_ON(nullptr == min); |
| 56 | ARM_COMPUTE_ERROR_ON(nullptr == max); |
| 57 | |
| 58 | _min_max_vals = cl::Buffer(CLScheduler::get().context(), CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, 2 * sizeof(int32_t)); |
| 59 | _min_max_count_vals = cl::Buffer(CLScheduler::get().context(), CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, 2 * sizeof(uint32_t)); |
| 60 | _min = min; |
| 61 | _max = max; |
| 62 | _min_count = min_count; |
| 63 | _max_count = max_count; |
| 64 | _min_loc = min_loc; |
| 65 | _max_loc = max_loc; |
| 66 | |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 67 | _min_max_kernel->configure(compile_context, input, &_min_max_vals); |
| 68 | _min_max_loc_kernel->configure(compile_context, input, &_min_max_vals, &_min_max_count_vals, _min_loc, _max_loc); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void CLMinMaxLocation::run() |
| 72 | { |
| 73 | cl::CommandQueue q = CLScheduler::get().queue(); |
| 74 | |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 75 | CLScheduler::get().enqueue(*_min_max_kernel, false); |
| 76 | CLScheduler::get().enqueue(*_min_max_loc_kernel, false); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 77 | |
| 78 | // Update min and max |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 79 | q.enqueueReadBuffer(_min_max_vals, CL_FALSE, 0 * sizeof(int32_t), sizeof(int32_t), static_cast<int32_t *>(_min)); |
| 80 | q.enqueueReadBuffer(_min_max_vals, CL_FALSE, 1 * sizeof(int32_t), sizeof(int32_t), static_cast<int32_t *>(_max)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 81 | |
| 82 | // Update min and max count |
| 83 | if(_min_count != nullptr) |
| 84 | { |
| 85 | q.enqueueReadBuffer(_min_max_count_vals, CL_FALSE, 0 * sizeof(uint32_t), sizeof(uint32_t), _min_count); |
| 86 | } |
| 87 | if(_max_count != nullptr) |
| 88 | { |
| 89 | q.enqueueReadBuffer(_min_max_count_vals, CL_FALSE, 1 * sizeof(uint32_t), sizeof(uint32_t), _max_count); |
| 90 | } |
| 91 | |
| 92 | // Update min/max point arrays (Makes the kernel blocking) |
| 93 | if(_min_loc != nullptr) |
| 94 | { |
| 95 | unsigned int min_count = 0; |
| 96 | q.enqueueReadBuffer(_min_max_count_vals, CL_TRUE, 0 * sizeof(uint32_t), sizeof(uint32_t), &min_count); |
| 97 | size_t min_corner_size = std::min(static_cast<size_t>(min_count), _min_loc->max_num_values()); |
| 98 | _min_loc->resize(min_corner_size); |
| 99 | } |
| 100 | if(_max_loc != nullptr) |
| 101 | { |
| 102 | unsigned int max_count = 0; |
| 103 | q.enqueueReadBuffer(_min_max_count_vals, CL_TRUE, 1 * sizeof(uint32_t), sizeof(uint32_t), &max_count); |
| 104 | size_t max_corner_size = std::min(static_cast<size_t>(max_count), _max_loc->max_num_values()); |
| 105 | _max_loc->resize(max_corner_size); |
| 106 | } |
| 107 | } |
Moritz Pflanzer | 4726fdf | 2017-09-23 10:47:54 +0100 | [diff] [blame] | 108 | } // namespace arm_compute |