blob: ba9e5163abcac12ba505cca0dfcab8c0cfcb7763 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Alex Gildayc357c472018-03-21 13:54:09 +00002 * Copyright (c) 2016-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 Pinitas84b51ad2017-11-07 13:24:57 +000028#include "arm_compute/runtime/Memory.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029
30#include <cstdint>
31#include <memory>
32#include <vector>
33
34namespace arm_compute
35{
36class Coordinates;
37class TensorInfo;
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010038class Tensor;
39template <typename>
40class MemoryGroupBase;
41using MemoryGroup = MemoryGroupBase<Tensor>;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010042
43/** Basic implementation of a CPU memory tensor allocator. */
44class TensorAllocator : public ITensorAllocator
45{
46public:
Alex Gildayc357c472018-03-21 13:54:09 +000047 /** Default constructor.
48 *
49 * @param[in] owner Owner of the tensor allocator.
50 */
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010051 TensorAllocator(Tensor *owner = nullptr);
52 /** Default destructor */
53 ~TensorAllocator();
Alex Gildayc357c472018-03-21 13:54:09 +000054 /** Prevent instances of this class from being copied (As this class contains pointers) */
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010055 TensorAllocator(const TensorAllocator &) = delete;
Alex Gildayc357c472018-03-21 13:54:09 +000056 /** Prevent instances of this class from being copy assigned (As this class contains pointers) */
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010057 TensorAllocator &operator=(const TensorAllocator &) = delete;
58 /** Allow instances of this class to be moved */
59 TensorAllocator(TensorAllocator &&) noexcept;
60 /** Allow instances of this class to be moved */
61 TensorAllocator &operator=(TensorAllocator &&) noexcept;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010062
63 /** Make ITensorAllocator's init methods available */
64 using ITensorAllocator::init;
65
66 /** Shares the same backing memory with another tensor allocator, while the tensor info might be different.
67 * In other words this can be used to create a sub-tensor from another tensor while sharing the same memory.
68 *
69 * @note TensorAllocator have to be of the same specialized type.
70 *
71 * @param[in] allocator The allocator that owns the backing memory to be shared. Ownership becomes shared afterwards.
72 * @param[in] coords The starting coordinates of the new tensor inside the parent tensor.
73 * @param[in] sub_info The new tensor information (e.g. shape etc)
74 */
75 void init(const TensorAllocator &allocator, const Coordinates &coords, TensorInfo sub_info);
76
Alex Gildayc357c472018-03-21 13:54:09 +000077 /** Returns the pointer to the allocated data.
78 *
79 * @return a pointer to the allocated data.
80 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010081 uint8_t *data() const;
82
83 /** Allocate size specified by TensorInfo of CPU memory.
84 *
85 * @note The tensor must not already be allocated when calling this function.
86 *
87 */
88 void allocate() override;
89
90 /** Free allocated CPU memory.
91 *
92 * @note The tensor must have been allocated when calling this function.
93 *
94 */
95 void free() override;
Georgios Pinitas84b51ad2017-11-07 13:24:57 +000096 /** Import an existing memory as a tensor's backing memory
97 *
Georgios Pinitasdf310362018-11-14 13:16:56 +000098 * @warning ownership of memory is not transferred
Georgios Pinitas84b51ad2017-11-07 13:24:57 +000099 *
100 * @return error status
101 */
Georgios Pinitasdf310362018-11-14 13:16:56 +0000102 arm_compute::Status import_memory(void *memory, size_t size);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100103 /** 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 Barbier6ff3b192017-09-04 18:44:23 +0100108
109protected:
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
119private:
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100120 MemoryGroup *_associated_memory_group; /**< Registered memory manager */
Georgios Pinitas84b51ad2017-11-07 13:24:57 +0000121 Memory _memory; /**< CPU memory */
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100122 Tensor *_owner; /**< Owner of the allocator */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100123};
124}
125#endif /* __ARM_COMPUTE_TENSORALLOCATOR_H__ */