Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [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_CLLUTALLOCATOR_H |
| 25 | #define ARM_COMPUTE_CLLUTALLOCATOR_H |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/runtime/ILutAllocator.h" |
| 28 | |
| 29 | #include "arm_compute/core/CL/OpenCL.h" |
| 30 | |
| 31 | #include <cstdint> |
| 32 | |
| 33 | namespace arm_compute |
| 34 | { |
| 35 | /** Basic implementation of a CL memory LUT allocator. */ |
| 36 | class CLLutAllocator : public ILutAllocator |
| 37 | { |
| 38 | public: |
| 39 | /** Default constructor. */ |
| 40 | CLLutAllocator(); |
| 41 | /** Default destructor. */ |
| 42 | ~CLLutAllocator() = default; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 43 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 44 | CLLutAllocator(const CLLutAllocator &) = delete; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 45 | /** Prevent instances of this class from being copy assigned (As this class contains pointers) */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 46 | const CLLutAllocator &operator=(const CLLutAllocator &) = delete; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 47 | /** Interface to be implemented by the child class to return the pointer to the mapped data. |
| 48 | * |
| 49 | * @return pointer to the mapped data. |
| 50 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 51 | uint8_t *data(); |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 52 | /** Interface to be implemented by the child class to return the pointer to the CL data. |
| 53 | * |
| 54 | * @return pointer to the CL data. |
| 55 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 56 | const cl::Buffer &cl_data() const; |
| 57 | /** Enqueue a map operation of the allocated buffer on the given queue. |
| 58 | * |
| 59 | * @param[in,out] q The CL command queue to use for the mapping operation. |
| 60 | * @param[in] blocking If true, then the mapping will be ready to use by the time |
| 61 | * this method returns, else it is the caller's responsibility |
| 62 | * to flush the queue and wait for the mapping operation to have completed before using the returned mapping pointer. |
| 63 | * |
| 64 | * @return The mapping address. |
| 65 | */ |
| 66 | uint8_t *map(cl::CommandQueue &q, bool blocking); |
| 67 | /** Enqueue an unmap operation of the allocated buffer on the given queue. |
| 68 | * |
| 69 | * @note This method simply enqueue the unmap operation, it is the caller's responsibility to flush the queue and make sure the unmap is finished before |
| 70 | * the memory is accessed by the device. |
| 71 | * |
| 72 | * @param[in,out] q The CL command queue to use for the mapping operation. |
| 73 | * @param[in] mapping The cpu mapping to unmap. |
| 74 | */ |
| 75 | void unmap(cl::CommandQueue &q, uint8_t *mapping); |
| 76 | |
| 77 | protected: |
| 78 | /** Allocate num_elements() * sizeof(type()) of OpenCL memory. */ |
| 79 | void allocate() override; |
| 80 | /** Call map() on the OpenCL buffer. |
| 81 | * |
| 82 | * @return A pointer to the beginning of the LUT's allocation. |
| 83 | */ |
| 84 | uint8_t *lock() override; |
| 85 | /** Call unmap() on the OpenCL buffer. */ |
| 86 | void unlock() override; |
| 87 | |
| 88 | private: |
| 89 | cl::Buffer _buffer; /**< OpenCL buffer containing the LUT data. */ |
| 90 | uint8_t *_mapping; /**< Pointer to the CPU mapping of the OpenCL buffer. */ |
| 91 | }; |
| 92 | } |
| 93 | |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 94 | #endif /* ARM_COMPUTE_CLLUTALLOCATOR_H */ |