blob: 9af100c12985b4781868d01f4430588b7b49a016 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
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 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:
47 /** Default constructor. */
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010048 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 Barbier6ff3b192017-09-04 18:44:23 +010059
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 Pinitas84b51ad2017-11-07 13:24:57 +000090 /** 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 Pinitas631c41a2017-12-06 11:53:03 +0000102 arm_compute::Status import_memory(Memory memory);
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__ */