1 /* 2 * Copyright (c) 2016-2019 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_TENSORALLOCATOR_H 25 #define ARM_COMPUTE_TENSORALLOCATOR_H 26 #include "arm_compute/runtime/ITensorAllocator.h" 27 28 #include "arm_compute/runtime/Memory.h" 29 #include "arm_compute/runtime/MemoryGroup.h" 30 31 #include <cstdint> 32 #include <memory> 33 #include <vector> 34 35 namespace arm_compute 36 { 37 // Forward declaration 38 class Coordinates; 39 class TensorInfo; 40 41 /** Basic implementation of a CPU memory tensor allocator. */ 42 class TensorAllocator : public ITensorAllocator 43 { 44 public: 45 /** Default constructor. 46 * 47 * @param[in] owner Memory manageable owner 48 */ 49 TensorAllocator(IMemoryManageable *owner); 50 /** Default destructor */ 51 ~TensorAllocator(); 52 /** Prevent instances of this class from being copied (As this class contains pointers) */ 53 TensorAllocator(const TensorAllocator &) = delete; 54 /** Prevent instances of this class from being copy assigned (As this class contains pointers) */ 55 TensorAllocator &operator=(const TensorAllocator &) = delete; 56 /** Allow instances of this class to be moved */ 57 TensorAllocator(TensorAllocator &&) noexcept; 58 /** Allow instances of this class to be moved */ 59 TensorAllocator &operator=(TensorAllocator &&) noexcept; 60 61 /** Make ITensorAllocator's init methods available */ 62 using ITensorAllocator::init; 63 64 /** Shares the same backing memory with another tensor allocator, while the tensor info might be different. 65 * In other words this can be used to create a sub-tensor from another tensor while sharing the same memory. 66 * 67 * @note TensorAllocator have to be of the same specialized type. 68 * 69 * @param[in] allocator The allocator that owns the backing memory to be shared. Ownership becomes shared afterwards. 70 * @param[in] coords The starting coordinates of the new tensor inside the parent tensor. 71 * @param[in] sub_info The new tensor information (e.g. shape etc) 72 */ 73 void init(const TensorAllocator &allocator, const Coordinates &coords, TensorInfo &sub_info); 74 75 /** Returns the pointer to the allocated data. 76 * 77 * @return a pointer to the allocated data. 78 */ 79 uint8_t *data() const; 80 81 /** Allocate size specified by TensorInfo of CPU 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 CPU memory. 89 * 90 * @note The tensor must have been allocated when calling this function. 91 * 92 */ 93 void free() override; 94 /** Import an existing memory as a tensor's backing memory 95 * 96 * @warning size is expected to be compliant with total_size reported by ITensorInfo. 97 * @warning ownership of memory is not transferred. 98 * @warning tensor shouldn't be memory managed. 99 * @warning padding should be accounted by the client code. 100 * @warning memory must be writable in case of in-place operations 101 * @note buffer alignment will be checked to be compliant with alignment reported by ITensorInfo. 102 * 103 * @param[in] memory Raw memory pointer to be used as backing memory 104 * 105 * @return An error status 106 */ 107 Status import_memory(void *memory); 108 /** Associates the tensor with a memory group 109 * 110 * @param[in] associated_memory_group Memory group to associate the tensor with 111 */ 112 void set_associated_memory_group(IMemoryGroup *associated_memory_group); 113 114 protected: 115 /** No-op for CPU memory 116 * 117 * @return A pointer to the beginning of the tensor's allocation. 118 */ 119 uint8_t *lock() override; 120 121 /** No-op for CPU memory. */ 122 void unlock() override; 123 124 private: 125 IMemoryManageable *_owner; /**< Memory manageable object that owns the allocator */ 126 IMemoryGroup *_associated_memory_group; /**< Registered memory manager */ 127 Memory _memory; /**< CPU memory */ 128 }; 129 } // namespace arm_compute 130 #endif /* ARM_COMPUTE_TENSORALLOCATOR_H */ 131