Compute Library
 20.02.1
CLMinMaxLocation.cpp
Go to the documentation of this file.
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  */
25 
27 
28 namespace arm_compute
29 {
31  : _min_max_kernel(),
32  _min_max_loc_kernel(),
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 
44 void CLMinMaxLocation::configure(const ICLImage *input, void *min, void *max, CLCoordinates2DArray *min_loc, CLCoordinates2DArray *max_loc, uint32_t *min_count, uint32_t *max_count)
45 {
46  ARM_COMPUTE_ERROR_ON(nullptr == min);
47  ARM_COMPUTE_ERROR_ON(nullptr == max);
48 
49  _min_max_vals = cl::Buffer(CLScheduler::get().context(), CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, 2 * sizeof(int32_t));
50  _min_max_count_vals = cl::Buffer(CLScheduler::get().context(), CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, 2 * sizeof(uint32_t));
51  _min = min;
52  _max = max;
53  _min_count = min_count;
54  _max_count = max_count;
55  _min_loc = min_loc;
56  _max_loc = max_loc;
57 
58  _min_max_kernel.configure(input, &_min_max_vals);
59  _min_max_loc_kernel.configure(input, &_min_max_vals, &_min_max_count_vals, _min_loc, _max_loc);
60 }
61 
63 {
64  cl::CommandQueue q = CLScheduler::get().queue();
65 
66  CLScheduler::get().enqueue(_min_max_kernel, false);
67  CLScheduler::get().enqueue(_min_max_loc_kernel, false);
68 
69  // Update min and max
70  q.enqueueReadBuffer(_min_max_vals, CL_FALSE, 0 * sizeof(int32_t), sizeof(int32_t), static_cast<int32_t *>(_min));
71  q.enqueueReadBuffer(_min_max_vals, CL_FALSE, 1 * sizeof(int32_t), sizeof(int32_t), static_cast<int32_t *>(_max));
72 
73  // Update min and max count
74  if(_min_count != nullptr)
75  {
76  q.enqueueReadBuffer(_min_max_count_vals, CL_FALSE, 0 * sizeof(uint32_t), sizeof(uint32_t), _min_count);
77  }
78  if(_max_count != nullptr)
79  {
80  q.enqueueReadBuffer(_min_max_count_vals, CL_FALSE, 1 * sizeof(uint32_t), sizeof(uint32_t), _max_count);
81  }
82 
83  // Update min/max point arrays (Makes the kernel blocking)
84  if(_min_loc != nullptr)
85  {
86  unsigned int min_count = 0;
87  q.enqueueReadBuffer(_min_max_count_vals, CL_TRUE, 0 * sizeof(uint32_t), sizeof(uint32_t), &min_count);
88  size_t min_corner_size = std::min(static_cast<size_t>(min_count), _min_loc->max_num_values());
89  _min_loc->resize(min_corner_size);
90  }
91  if(_max_loc != nullptr)
92  {
93  unsigned int max_count = 0;
94  q.enqueueReadBuffer(_min_max_count_vals, CL_TRUE, 1 * sizeof(uint32_t), sizeof(uint32_t), &max_count);
95  size_t max_corner_size = std::min(static_cast<size_t>(max_count), _max_loc->max_num_values());
96  _max_loc->resize(max_corner_size);
97  }
98 }
99 } // namespace arm_compute
static CLScheduler & get()
Access the scheduler singleton.
Definition: CLScheduler.cpp:99
void resize(size_t num)
Resizes the array to contain "num" elements.
Definition: IArray.h:128
void configure(const ICLImage *input, void *min, void *max, CLCoordinates2DArray *min_loc=nullptr, CLCoordinates2DArray *max_loc=nullptr, uint32_t *min_count=nullptr, uint32_t *max_count=nullptr)
Initialise the kernel's inputs and outputs.
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
Copyright (c) 2017-2020 ARM Limited.
void configure(const ICLImage *input, cl::Buffer *min_max)
Initialise the kernel's input and output.
void configure(const ICLImage *input, cl::Buffer *min_max, cl::Buffer *min_max_count, ICLCoordinates2DArray *min_loc=nullptr, ICLCoordinates2DArray *max_loc=nullptr)
Initialise the kernel's input and outputs.
cl::CommandQueue & queue()
Accessor for the associated CL command queue.
Definition: CLScheduler.cpp:41
void enqueue(ICLKernel &kernel, bool flush=true)
Schedule the execution of the passed kernel if possible.
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
size_t max_num_values() const
Maximum number of values which can be stored in this array.
Definition: IArray.h:58
CLArray implementation.
Definition: CLArray.h:37
void run() override
Run the kernels contained in the function.