Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_TENSORALLOCATOR_H |
| 25 | #define ARM_COMPUTE_TENSORALLOCATOR_H |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | #include "arm_compute/runtime/ITensorAllocator.h" |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 27 | |
Georgios Pinitas | 84b51ad | 2017-11-07 13:24:57 +0000 | [diff] [blame] | 28 | #include "arm_compute/runtime/Memory.h" |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 29 | #include "arm_compute/runtime/MemoryGroup.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | |
| 31 | #include <cstdint> |
| 32 | #include <memory> |
| 33 | #include <vector> |
| 34 | |
| 35 | namespace arm_compute |
| 36 | { |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 37 | // Forward declaration |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 38 | class Coordinates; |
| 39 | class TensorInfo; |
| 40 | |
| 41 | /** Basic implementation of a CPU memory tensor allocator. */ |
| 42 | class TensorAllocator : public ITensorAllocator |
| 43 | { |
| 44 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 45 | /** Default constructor. |
| 46 | * |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 47 | * @param[in] owner Memory manageable owner |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 48 | */ |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 49 | TensorAllocator(IMemoryManageable *owner); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 50 | /** Default destructor */ |
| 51 | ~TensorAllocator(); |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 52 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 53 | TensorAllocator(const TensorAllocator &) = delete; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 54 | /** Prevent instances of this class from being copy assigned (As this class contains pointers) */ |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 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; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | */ |
Michalis Spyrou | 53860dd | 2019-07-01 14:20:56 +0100 | [diff] [blame] | 73 | void init(const TensorAllocator &allocator, const Coordinates &coords, TensorInfo &sub_info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 74 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 75 | /** Returns the pointer to the allocated data. |
| 76 | * |
| 77 | * @return a pointer to the allocated data. |
| 78 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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; |
Georgios Pinitas | 84b51ad | 2017-11-07 13:24:57 +0000 | [diff] [blame] | 94 | /** Import an existing memory as a tensor's backing memory |
| 95 | * |
Georgios Pinitas | 4d0351c | 2019-04-03 15:11:16 +0100 | [diff] [blame] | 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. |
Georgios Pinitas | db09b37 | 2019-06-17 17:46:17 +0100 | [diff] [blame] | 99 | * @warning padding should be accounted by the client code. |
Manuel Bottini | 342d1bc | 2019-07-24 14:51:51 +0100 | [diff] [blame] | 100 | * @warning memory must be writable in case of in-place operations |
Georgios Pinitas | 4d0351c | 2019-04-03 15:11:16 +0100 | [diff] [blame] | 101 | * @note buffer alignment will be checked to be compliant with alignment reported by ITensorInfo. |
Georgios Pinitas | 84b51ad | 2017-11-07 13:24:57 +0000 | [diff] [blame] | 102 | * |
Georgios Pinitas | 4d0351c | 2019-04-03 15:11:16 +0100 | [diff] [blame] | 103 | * @param[in] memory Raw memory pointer to be used as backing memory |
| 104 | * |
| 105 | * @return An error status |
Georgios Pinitas | 84b51ad | 2017-11-07 13:24:57 +0000 | [diff] [blame] | 106 | */ |
Georgios Pinitas | 4d0351c | 2019-04-03 15:11:16 +0100 | [diff] [blame] | 107 | Status import_memory(void *memory); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 108 | /** Associates the tensor with a memory group |
| 109 | * |
| 110 | * @param[in] associated_memory_group Memory group to associate the tensor with |
| 111 | */ |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 112 | void set_associated_memory_group(IMemoryGroup *associated_memory_group); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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: |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 125 | IMemoryManageable *_owner; /**< Memory manageable object that owns the allocator */ |
| 126 | IMemoryGroup *_associated_memory_group; /**< Registered memory manager */ |
| 127 | Memory _memory; /**< CPU memory */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 128 | }; |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 129 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 130 | #endif /* ARM_COMPUTE_TENSORALLOCATOR_H */ |