Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 4d0351c | 2019-04-03 15:11:16 +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 | */ |
| 24 | #ifndef __ARM_COMPUTE_CLTENSORALLOCATOR_H__ |
| 25 | #define __ARM_COMPUTE_CLTENSORALLOCATOR_H__ |
| 26 | |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 27 | #include "arm_compute/runtime/CL/CLArray.h" |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 28 | #include "arm_compute/runtime/CL/CLMemory.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | #include "arm_compute/runtime/ITensorAllocator.h" |
| 30 | |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/CL/CLTypes.h" |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/CL/OpenCL.h" |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 33 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | #include <cstdint> |
| 35 | |
| 36 | namespace arm_compute |
| 37 | { |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 38 | class CLTensor; |
| 39 | template <typename> |
| 40 | class MemoryGroupBase; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 41 | /** Memory Group in OpenCL */ |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 42 | using CLMemoryGroup = MemoryGroupBase<CLTensor>; |
| 43 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 44 | /** Basic implementation of a CL memory tensor allocator. */ |
| 45 | class CLTensorAllocator : public ITensorAllocator |
| 46 | { |
| 47 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 48 | /** Default constructor. |
| 49 | * |
| 50 | * @param[in] owner (Optional) Owner of the allocator. |
| 51 | */ |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 52 | CLTensorAllocator(CLTensor *owner = nullptr); |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 53 | /** 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] | 54 | CLTensorAllocator(const CLTensorAllocator &) = delete; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 55 | /** 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] | 56 | CLTensorAllocator &operator=(const CLTensorAllocator &) = delete; |
| 57 | /** Allow instances of this class to be moved */ |
| 58 | CLTensorAllocator(CLTensorAllocator &&) = default; |
| 59 | /** Allow instances of this class to be moved */ |
| 60 | CLTensorAllocator &operator=(CLTensorAllocator &&) = default; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 61 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 62 | /** Interface to be implemented by the child class to return the pointer to the mapped data. |
| 63 | * |
| 64 | * @return pointer to the mapped data. |
| 65 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 66 | uint8_t *data(); |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 67 | /** Interface to be implemented by the child class to return the pointer to the CL data. |
| 68 | * |
| 69 | * @return pointer to the CL data. |
| 70 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 71 | const cl::Buffer &cl_data() const; |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 72 | /** Wrapped quantization info data accessor |
| 73 | * |
| 74 | * @return A wrapped quantization info object. |
| 75 | */ |
| 76 | CLQuantization quantization() const; |
Pablo Tello | e86a09f | 2018-01-11 15:44:48 +0000 | [diff] [blame] | 77 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 78 | /** Enqueue a map operation of the allocated buffer on the given queue. |
| 79 | * |
| 80 | * @param[in,out] q The CL command queue to use for the mapping operation. |
| 81 | * @param[in] blocking If true, then the mapping will be ready to use by the time |
| 82 | * this method returns, else it is the caller's responsibility |
| 83 | * to flush the queue and wait for the mapping operation to have completed before using the returned mapping pointer. |
| 84 | * |
| 85 | * @return The mapping address. |
| 86 | */ |
| 87 | uint8_t *map(cl::CommandQueue &q, bool blocking); |
| 88 | /** Enqueue an unmap operation of the allocated buffer on the given queue. |
| 89 | * |
| 90 | * @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 |
| 91 | * the memory is accessed by the device. |
| 92 | * |
| 93 | * @param[in,out] q The CL command queue to use for the mapping operation. |
| 94 | * @param[in] mapping The cpu mapping to unmap. |
| 95 | */ |
| 96 | void unmap(cl::CommandQueue &q, uint8_t *mapping); |
| 97 | |
| 98 | /** Allocate size specified by TensorInfo of OpenCL memory. |
| 99 | * |
| 100 | * @note: The tensor must not already be allocated when calling this function. |
| 101 | * |
| 102 | */ |
| 103 | void allocate() override; |
| 104 | |
| 105 | /** Free allocated OpenCL memory. |
| 106 | * |
| 107 | * @note The tensor must have been allocated when calling this function. |
| 108 | * |
| 109 | */ |
| 110 | void free() override; |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 111 | /** Import an existing memory as a tensor's backing memory |
| 112 | * |
Georgios Pinitas | 4d0351c | 2019-04-03 15:11:16 +0100 | [diff] [blame] | 113 | * @warning memory should have been created under the same context that ACL uses. |
| 114 | * @warning memory is expected to be aligned with the device requirements. |
| 115 | * @warning tensor shouldn't be memory managed. |
| 116 | * @warning ownership of memory is not transferred. |
Manuel Bottini | 342d1bc | 2019-07-24 14:51:51 +0100 | [diff] [blame] | 117 | * @warning memory must be writable in case of in-place operations |
Georgios Pinitas | db09b37 | 2019-06-17 17:46:17 +0100 | [diff] [blame] | 118 | * @warning padding should be accounted by the client code. |
Georgios Pinitas | 4d0351c | 2019-04-03 15:11:16 +0100 | [diff] [blame] | 119 | * @note buffer size will be checked to be compliant with total_size reported by ITensorInfo. |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 120 | * |
Georgios Pinitas | 4d0351c | 2019-04-03 15:11:16 +0100 | [diff] [blame] | 121 | * @param[in] buffer Buffer to be used as backing memory |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 122 | * |
Georgios Pinitas | 4d0351c | 2019-04-03 15:11:16 +0100 | [diff] [blame] | 123 | * @return An error status |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 124 | */ |
Georgios Pinitas | 4d0351c | 2019-04-03 15:11:16 +0100 | [diff] [blame] | 125 | Status import_memory(cl::Buffer buffer); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 126 | /** Associates the tensor with a memory group |
| 127 | * |
| 128 | * @param[in] associated_memory_group Memory group to associate the tensor with |
| 129 | */ |
| 130 | void set_associated_memory_group(CLMemoryGroup *associated_memory_group); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 131 | |
| 132 | protected: |
| 133 | /** Call map() on the OpenCL buffer. |
| 134 | * |
| 135 | * @return A pointer to the beginning of the tensor's allocation. |
| 136 | */ |
| 137 | uint8_t *lock() override; |
| 138 | /** Call unmap() on the OpenCL buffer. */ |
| 139 | void unlock() override; |
| 140 | |
| 141 | private: |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 142 | static const cl::Buffer _empty_buffer; |
| 143 | |
| 144 | private: |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 145 | CLMemoryGroup *_associated_memory_group; /**< Registered memory manager */ |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 146 | CLMemory _memory; /**< OpenCL memory */ |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 147 | uint8_t *_mapping; /**< Pointer to the CPU mapping of the OpenCL buffer. */ |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 148 | CLTensor *_owner; /**< Owner of the allocator */ |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 149 | CLFloatArray _scale; |
| 150 | CLInt32Array _offset; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 151 | }; |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 152 | } // namespace arm_compute |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 153 | #endif /* __ARM_COMPUTE_CLTENSORALLOCATOR_H__ */ |