1 /* 2 * Copyright (c) 2016-2020 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 */ 24 #ifndef ARM_COMPUTE_CLTENSORALLOCATOR_H 25 #define ARM_COMPUTE_CLTENSORALLOCATOR_H 26 27 #include "arm_compute/runtime/CL/CLArray.h" 28 #include "arm_compute/runtime/CL/CLMemory.h" 29 #include "arm_compute/runtime/ITensorAllocator.h" 30 #include "arm_compute/runtime/MemoryGroup.h" 31 32 #include "arm_compute/core/CL/CLTypes.h" 33 #include "arm_compute/core/CL/OpenCL.h" 34 35 #include <cstdint> 36 37 namespace arm_compute 38 { 39 class CLTensor; 40 class CLRuntimeContext; 41 /** Basic implementation of a CL memory tensor allocator. */ 42 class CLTensorAllocator : public ITensorAllocator 43 { 44 public: 45 /** Default constructor. 46 * 47 * @param[in] owner (Optional) Owner of the allocator. 48 * @param[in] ctx (Optional) Runtime context. 49 */ 50 CLTensorAllocator(IMemoryManageable *owner = nullptr, CLRuntimeContext *ctx = nullptr); 51 /** Prevent instances of this class from being copied (As this class contains pointers) */ 52 CLTensorAllocator(const CLTensorAllocator &) = delete; 53 /** Prevent instances of this class from being copy assigned (As this class contains pointers) */ 54 CLTensorAllocator &operator=(const CLTensorAllocator &) = delete; 55 /** Allow instances of this class to be moved */ 56 CLTensorAllocator(CLTensorAllocator &&) = default; 57 /** Allow instances of this class to be moved */ 58 CLTensorAllocator &operator=(CLTensorAllocator &&) = default; 59 60 /** Interface to be implemented by the child class to return the pointer to the mapped data. 61 * 62 * @return pointer to the mapped data. 63 */ 64 uint8_t *data(); 65 /** Interface to be implemented by the child class to return the pointer to the CL data. 66 * 67 * @return pointer to the CL data. 68 */ 69 const cl::Buffer &cl_data() const; 70 /** Wrapped quantization info data accessor 71 * 72 * @return A wrapped quantization info object. 73 */ 74 CLQuantization quantization() const; 75 76 /** Enqueue a map operation of the allocated buffer on the given queue. 77 * 78 * @param[in,out] q The CL command queue to use for the mapping operation. 79 * @param[in] blocking If true, then the mapping will be ready to use by the time 80 * this method returns, else it is the caller's responsibility 81 * to flush the queue and wait for the mapping operation to have completed before using the returned mapping pointer. 82 * 83 * @return The mapping address. 84 */ 85 uint8_t *map(cl::CommandQueue &q, bool blocking); 86 /** Enqueue an unmap operation of the allocated buffer on the given queue. 87 * 88 * @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 89 * the memory is accessed by the device. 90 * 91 * @param[in,out] q The CL command queue to use for the mapping operation. 92 * @param[in] mapping The cpu mapping to unmap. 93 */ 94 void unmap(cl::CommandQueue &q, uint8_t *mapping); 95 96 /** Allocate size specified by TensorInfo of OpenCL memory. 97 * 98 * @note: The tensor must not already be allocated when calling this function. 99 * 100 */ 101 void allocate() override; 102 103 /** Free allocated OpenCL memory. 104 * 105 * @note The tensor must have been allocated when calling this function. 106 * 107 */ 108 void free() override; 109 /** Import an existing memory as a tensor's backing memory 110 * 111 * @warning memory should have been created under the same context that Compute Library uses. 112 * @warning memory is expected to be aligned with the device requirements. 113 * @warning tensor shouldn't be memory managed. 114 * @warning ownership of memory is not transferred. 115 * @warning memory must be writable in case of in-place operations 116 * @warning padding should be accounted by the client code. 117 * @note buffer size will be checked to be compliant with total_size reported by ITensorInfo. 118 * 119 * @param[in] buffer Buffer to be used as backing memory 120 * 121 * @return An error status 122 */ 123 Status import_memory(cl::Buffer buffer); 124 /** Associates the tensor with a memory group 125 * 126 * @param[in] associated_memory_group Memory group to associate the tensor with 127 */ 128 void set_associated_memory_group(IMemoryGroup *associated_memory_group); 129 130 protected: 131 /** Call map() on the OpenCL buffer. 132 * 133 * @return A pointer to the beginning of the tensor's allocation. 134 */ 135 uint8_t *lock() override; 136 /** Call unmap() on the OpenCL buffer. */ 137 void unlock() override; 138 139 private: 140 static const cl::Buffer _empty_buffer; 141 142 private: 143 CLRuntimeContext *_ctx; 144 IMemoryManageable *_owner; /**< Memory manageable object that owns the allocator */ 145 IMemoryGroup *_associated_memory_group; /**< Registered memory manager */ 146 CLMemory _memory; /**< OpenCL memory */ 147 uint8_t *_mapping; /**< Pointer to the CPU mapping of the OpenCL buffer. */ 148 CLFloatArray _scale; /**< Scales array in case of quantized per channel data type */ 149 CLInt32Array _offset; /**< Offsets array in case of quantized per channel data type */ 150 }; 151 } // namespace arm_compute 152 #endif /* ARM_COMPUTE_CLTENSORALLOCATOR_H */ 153