blob: 28141134cb5f10d7f737d9921bca807e38e69de3 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitas4d0351c2019-04-03 15:11:16 +01002 * Copyright (c) 2016-2019 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 */
Michalis Spyrou53860dd2019-07-01 14:20:56 +010075 void init(const TensorAllocator &allocator, const Coordinates &coords, TensorInfo &sub_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076
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 Pinitas4d0351c2019-04-03 15:11:16 +010098 * @warning size is expected to be compliant with total_size reported by ITensorInfo.
99 * @warning ownership of memory is not transferred.
100 * @warning tensor shouldn't be memory managed.
Georgios Pinitasdb09b372019-06-17 17:46:17 +0100101 * @warning padding should be accounted by the client code.
Manuel Bottini342d1bc2019-07-24 14:51:51 +0100102 * @warning memory must be writable in case of in-place operations
Georgios Pinitas4d0351c2019-04-03 15:11:16 +0100103 * @note buffer alignment will be checked to be compliant with alignment reported by ITensorInfo.
Georgios Pinitas84b51ad2017-11-07 13:24:57 +0000104 *
Georgios Pinitas4d0351c2019-04-03 15:11:16 +0100105 * @param[in] memory Raw memory pointer to be used as backing memory
106 *
107 * @return An error status
Georgios Pinitas84b51ad2017-11-07 13:24:57 +0000108 */
Georgios Pinitas4d0351c2019-04-03 15:11:16 +0100109 Status import_memory(void *memory);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100110 /** Associates the tensor with a memory group
111 *
112 * @param[in] associated_memory_group Memory group to associate the tensor with
113 */
114 void set_associated_memory_group(MemoryGroup *associated_memory_group);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115
116protected:
117 /** No-op for CPU memory
118 *
119 * @return A pointer to the beginning of the tensor's allocation.
120 */
121 uint8_t *lock() override;
122
123 /** No-op for CPU memory. */
124 void unlock() override;
125
126private:
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100127 MemoryGroup *_associated_memory_group; /**< Registered memory manager */
Georgios Pinitas84b51ad2017-11-07 13:24:57 +0000128 Memory _memory; /**< CPU memory */
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100129 Tensor *_owner; /**< Owner of the allocator */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130};
131}
132#endif /* __ARM_COMPUTE_TENSORALLOCATOR_H__ */