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_TENSORALLOCATOR_H__ |
| 25 | #define __ARM_COMPUTE_TENSORALLOCATOR_H__ |
| 26 | |
| 27 | #include "arm_compute/runtime/ITensorAllocator.h" |
Georgios Pinitas | 84b51ad | 2017-11-07 13:24:57 +0000 | [diff] [blame] | 28 | #include "arm_compute/runtime/Memory.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | |
| 30 | #include <cstdint> |
| 31 | #include <memory> |
| 32 | #include <vector> |
| 33 | |
| 34 | namespace arm_compute |
| 35 | { |
| 36 | class Coordinates; |
| 37 | class TensorInfo; |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 38 | class Tensor; |
| 39 | template <typename> |
| 40 | class MemoryGroupBase; |
| 41 | using MemoryGroup = MemoryGroupBase<Tensor>; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 42 | |
| 43 | /** Basic implementation of a CPU memory tensor allocator. */ |
| 44 | class TensorAllocator : public ITensorAllocator |
| 45 | { |
| 46 | public: |
| 47 | /** Default constructor. */ |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 48 | TensorAllocator(Tensor *owner = nullptr); |
| 49 | /** Default destructor */ |
| 50 | ~TensorAllocator(); |
| 51 | /** Prevent instances of this class from being copied (As this class contains pointers). */ |
| 52 | TensorAllocator(const TensorAllocator &) = delete; |
| 53 | /** Prevent instances of this class from being copy assigned (As this class contains pointers). */ |
| 54 | TensorAllocator &operator=(const TensorAllocator &) = delete; |
| 55 | /** Allow instances of this class to be moved */ |
| 56 | TensorAllocator(TensorAllocator &&) noexcept; |
| 57 | /** Allow instances of this class to be moved */ |
| 58 | TensorAllocator &operator=(TensorAllocator &&) noexcept; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 59 | |
| 60 | /** Make ITensorAllocator's init methods available */ |
| 61 | using ITensorAllocator::init; |
| 62 | |
| 63 | /** Shares the same backing memory with another tensor allocator, while the tensor info might be different. |
| 64 | * In other words this can be used to create a sub-tensor from another tensor while sharing the same memory. |
| 65 | * |
| 66 | * @note TensorAllocator have to be of the same specialized type. |
| 67 | * |
| 68 | * @param[in] allocator The allocator that owns the backing memory to be shared. Ownership becomes shared afterwards. |
| 69 | * @param[in] coords The starting coordinates of the new tensor inside the parent tensor. |
| 70 | * @param[in] sub_info The new tensor information (e.g. shape etc) |
| 71 | */ |
| 72 | void init(const TensorAllocator &allocator, const Coordinates &coords, TensorInfo sub_info); |
| 73 | |
| 74 | /** Returns the pointer to the allocated data. */ |
| 75 | uint8_t *data() const; |
| 76 | |
| 77 | /** Allocate size specified by TensorInfo of CPU memory. |
| 78 | * |
| 79 | * @note The tensor must not already be allocated when calling this function. |
| 80 | * |
| 81 | */ |
| 82 | void allocate() override; |
| 83 | |
| 84 | /** Free allocated CPU memory. |
| 85 | * |
| 86 | * @note The tensor must have been allocated when calling this function. |
| 87 | * |
| 88 | */ |
| 89 | void free() override; |
Georgios Pinitas | 84b51ad | 2017-11-07 13:24:57 +0000 | [diff] [blame] | 90 | /** Import an existing memory as a tensor's backing memory |
| 91 | * |
| 92 | * @warning If the tensor is flagged to be managed by a memory manager, |
| 93 | * this call will lead to an error. |
| 94 | * @warning Ownership of memory depends on the way the @ref Memory object was constructed |
| 95 | * @note Calling free on a tensor with imported memory will just clear |
| 96 | * the internal pointer value. |
| 97 | * |
| 98 | * @param[in] memory Memory to import |
| 99 | * |
| 100 | * @return error status |
| 101 | */ |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 102 | arm_compute::Status import_memory(Memory memory); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 103 | /** Associates the tensor with a memory group |
| 104 | * |
| 105 | * @param[in] associated_memory_group Memory group to associate the tensor with |
| 106 | */ |
| 107 | void set_associated_memory_group(MemoryGroup *associated_memory_group); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 108 | |
| 109 | protected: |
| 110 | /** No-op for CPU memory |
| 111 | * |
| 112 | * @return A pointer to the beginning of the tensor's allocation. |
| 113 | */ |
| 114 | uint8_t *lock() override; |
| 115 | |
| 116 | /** No-op for CPU memory. */ |
| 117 | void unlock() override; |
| 118 | |
| 119 | private: |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 120 | MemoryGroup *_associated_memory_group; /**< Registered memory manager */ |
Georgios Pinitas | 84b51ad | 2017-11-07 13:24:57 +0000 | [diff] [blame] | 121 | Memory _memory; /**< CPU memory */ |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 122 | Tensor *_owner; /**< Owner of the allocator */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 123 | }; |
| 124 | } |
| 125 | #endif /* __ARM_COMPUTE_TENSORALLOCATOR_H__ */ |