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-2019 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_ICLARRAY_H |
| 25 | #define ARM_COMPUTE_ICLARRAY_H |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/core/CL/OpenCL.h" |
| 28 | #include "arm_compute/core/IArray.h" |
| 29 | #include "arm_compute/core/ITensor.h" |
| 30 | |
| 31 | namespace arm_compute |
| 32 | { |
| 33 | /** Interface for OpenCL Array */ |
| 34 | template <class T> |
| 35 | class ICLArray : public IArray<T> |
| 36 | { |
| 37 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 38 | /** Constructor |
| 39 | * |
| 40 | * @param[in] max_num_values Maximum size of the array. |
| 41 | * |
| 42 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 43 | explicit ICLArray(size_t max_num_values) |
| 44 | : IArray<T>(max_num_values), _mapping(nullptr) |
| 45 | { |
| 46 | } |
| 47 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 48 | /** Prevent instances of this class from being copy constructed */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 49 | ICLArray(const ICLArray &) = delete; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 50 | /** Prevent instances of this class from being copied */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 51 | ICLArray &operator=(const ICLArray &) = delete; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 52 | /** Allow instances of this class to be move constructed */ |
| 53 | ICLArray(ICLArray &&) = default; |
| 54 | /** Allow instances of this class to be moved */ |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 55 | ICLArray &operator=(ICLArray &&) = default; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 56 | /** Default virtual destructor. */ |
| 57 | virtual ~ICLArray() = default; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 58 | /** Interface to be implemented by the child class to return a reference to the OpenCL buffer containing the array's data. |
| 59 | * |
| 60 | * @return A reference to an OpenCL buffer containing the array's data. |
| 61 | */ |
| 62 | virtual const cl::Buffer &cl_buffer() const = 0; |
| 63 | /** Enqueue a map operation of the allocated buffer on the given queue. |
| 64 | * |
| 65 | * @param[in,out] q The CL command queue to use for the mapping operation. |
| 66 | * @param[in] blocking If true, then the mapping will be ready to use by the time |
| 67 | * this method returns, else it is the caller's responsibility |
| 68 | * to flush the queue and wait for the mapping operation to have completed before using the returned mapping pointer. |
| 69 | * |
| 70 | * @return The mapping address. |
| 71 | */ |
| 72 | void map(cl::CommandQueue &q, bool blocking = true) |
| 73 | { |
| 74 | _mapping = do_map(q, blocking); |
| 75 | } |
| 76 | /** Enqueue an unmap operation of the allocated and mapped buffer on the given queue. |
| 77 | * |
| 78 | * @note This method simply enqueues the unmap operation, it is the caller's responsibility to flush the queue and make sure the unmap is finished before |
| 79 | * the memory is accessed by the device. |
| 80 | * |
| 81 | * @param[in,out] q The CL command queue to use for the mapping operation. |
| 82 | */ |
| 83 | void unmap(cl::CommandQueue &q) |
| 84 | { |
| 85 | do_unmap(q, _mapping); |
| 86 | _mapping = nullptr; |
| 87 | } |
| 88 | |
| 89 | // Inherited methods overridden: |
| 90 | T *buffer() const override |
| 91 | { |
| 92 | return reinterpret_cast<T *>(_mapping); |
| 93 | } |
| 94 | |
| 95 | protected: |
| 96 | /** Method to be implemented by the child class to map the OpenCL buffer |
| 97 | * |
| 98 | * @param[in,out] q The CL command queue to use for the mapping operation. |
| 99 | * @param[in] blocking If true, then the mapping will be ready to use by the time |
| 100 | * this method returns, else it is the caller's responsibility |
| 101 | * to flush the queue and wait for the mapping operation to have completed before using the returned mapping pointer. |
| 102 | */ |
| 103 | virtual uint8_t *do_map(cl::CommandQueue &q, bool blocking) = 0; |
| 104 | /** Method to be implemented by the child class to unmap the OpenCL buffer |
| 105 | * |
| 106 | * @note This method simply enqueues the unmap operation, it is the caller's responsibility to flush the queue and make sure the unmap is finished before |
| 107 | * the memory is accessed by the device. |
| 108 | * |
| 109 | * @param[in,out] q The CL command queue to use for the mapping operation. |
| 110 | * @param[in] mapping Pointer to the buffer to be unmapped. |
| 111 | */ |
| 112 | virtual void do_unmap(cl::CommandQueue &q, uint8_t *mapping) = 0; |
| 113 | |
| 114 | private: |
| 115 | uint8_t *_mapping; |
| 116 | }; |
| 117 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 118 | /** Interface for OpenCL Array of Key Points. */ |
| 119 | using ICLKeyPointArray = ICLArray<KeyPoint>; |
| 120 | /** Interface for OpenCL Array of 2D Coordinates. */ |
| 121 | using ICLCoordinates2DArray = ICLArray<Coordinates2D>; |
| 122 | /** Interface for OpenCL Array of Detection Windows. */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 123 | using ICLDetectionWindowArray = ICLArray<DetectionWindow>; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 124 | /** Interface for OpenCL Array of 2D Sizes. */ |
| 125 | using ICLSize2DArray = ICLArray<Size2D>; |
| 126 | /** Interface for OpenCL Array of uint8s. */ |
| 127 | using ICLUInt8Array = ICLArray<cl_uchar>; |
| 128 | /** Interface for OpenCL Array of uint16s. */ |
| 129 | using ICLUInt16Array = ICLArray<cl_ushort>; |
| 130 | /** Interface for OpenCL Array of uint32s. */ |
| 131 | using ICLUInt32Array = ICLArray<cl_uint>; |
| 132 | /** Interface for OpenCL Array of int16s. */ |
| 133 | using ICLInt16Array = ICLArray<cl_short>; |
| 134 | /** Interface for OpenCL Array of int32s. */ |
| 135 | using ICLInt32Array = ICLArray<cl_int>; |
| 136 | /** Interface for OpenCL Array of floats. */ |
| 137 | using ICLFloatArray = ICLArray<cl_float>; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 138 | } |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 139 | #endif /*ARM_COMPUTE_ICLARRAY_H*/ |