Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, 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 | */ |
| 24 | #ifndef __ARM_COMPUTE_CLTENSORALLOCATOR_H__ |
| 25 | #define __ARM_COMPUTE_CLTENSORALLOCATOR_H__ |
| 26 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/runtime/ITensorAllocator.h" |
| 28 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/CL/OpenCL.h" |
| 30 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 31 | #include <cstdint> |
| 32 | |
| 33 | namespace arm_compute |
| 34 | { |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 35 | class CLTensor; |
| 36 | template <typename> |
| 37 | class MemoryGroupBase; |
| 38 | using CLMemoryGroup = MemoryGroupBase<CLTensor>; |
| 39 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 40 | /** Basic implementation of a CL memory tensor allocator. */ |
| 41 | class CLTensorAllocator : public ITensorAllocator |
| 42 | { |
| 43 | public: |
| 44 | /** Default constructor. */ |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 45 | CLTensorAllocator(CLTensor *owner = nullptr); |
| 46 | /** Default destructor */ |
| 47 | ~CLTensorAllocator(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 48 | /** Prevent instances of this class from being copied (As this class contains pointers). */ |
| 49 | CLTensorAllocator(const CLTensorAllocator &) = delete; |
| 50 | /** Prevent instances of this class from being copy assigned (As this class contains pointers). */ |
| 51 | CLTensorAllocator &operator=(const CLTensorAllocator &) = delete; |
| 52 | /** Allow instances of this class to be moved */ |
| 53 | CLTensorAllocator(CLTensorAllocator &&) = default; |
| 54 | /** Allow instances of this class to be moved */ |
| 55 | CLTensorAllocator &operator=(CLTensorAllocator &&) = default; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 56 | |
| 57 | /** Interface to be implemented by the child class to return the pointer to the mapped data. */ |
| 58 | uint8_t *data(); |
| 59 | /** Interface to be implemented by the child class to return the pointer to the CL data. */ |
| 60 | const cl::Buffer &cl_data() const; |
| 61 | /** Enqueue a map operation of the allocated buffer on the given queue. |
| 62 | * |
| 63 | * @param[in,out] q The CL command queue to use for the mapping operation. |
| 64 | * @param[in] blocking If true, then the mapping will be ready to use by the time |
| 65 | * this method returns, else it is the caller's responsibility |
| 66 | * to flush the queue and wait for the mapping operation to have completed before using the returned mapping pointer. |
| 67 | * |
| 68 | * @return The mapping address. |
| 69 | */ |
| 70 | uint8_t *map(cl::CommandQueue &q, bool blocking); |
| 71 | /** Enqueue an unmap operation of the allocated buffer on the given queue. |
| 72 | * |
| 73 | * @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 |
| 74 | * the memory is accessed by the device. |
| 75 | * |
| 76 | * @param[in,out] q The CL command queue to use for the mapping operation. |
| 77 | * @param[in] mapping The cpu mapping to unmap. |
| 78 | */ |
| 79 | void unmap(cl::CommandQueue &q, uint8_t *mapping); |
| 80 | |
| 81 | /** Allocate size specified by TensorInfo of OpenCL memory. |
| 82 | * |
| 83 | * @note: The tensor must not already be allocated when calling this function. |
| 84 | * |
| 85 | */ |
| 86 | void allocate() override; |
| 87 | |
| 88 | /** Free allocated OpenCL memory. |
| 89 | * |
| 90 | * @note The tensor must have been allocated when calling this function. |
| 91 | * |
| 92 | */ |
| 93 | void free() override; |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 94 | /** Associates the tensor with a memory group |
| 95 | * |
| 96 | * @param[in] associated_memory_group Memory group to associate the tensor with |
| 97 | */ |
| 98 | void set_associated_memory_group(CLMemoryGroup *associated_memory_group); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 99 | |
| 100 | protected: |
| 101 | /** Call map() on the OpenCL buffer. |
| 102 | * |
| 103 | * @return A pointer to the beginning of the tensor's allocation. |
| 104 | */ |
| 105 | uint8_t *lock() override; |
| 106 | /** Call unmap() on the OpenCL buffer. */ |
| 107 | void unlock() override; |
| 108 | |
| 109 | private: |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 110 | CLMemoryGroup *_associated_memory_group; /**< Registered memory manager */ |
| 111 | cl::Buffer _buffer; /**< OpenCL buffer containing the tensor data. */ |
| 112 | uint8_t *_mapping; /**< Pointer to the CPU mapping of the OpenCL buffer. */ |
| 113 | CLTensor *_owner; /**< Owner of the allocator */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 114 | }; |
| 115 | } |
| 116 | #endif /* __ARM_COMPUTE_CLTENSORALLOCATOR_H__ */ |