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