blob: a5e16c4d902f758811d671e237ea747805a4cf8a [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_TENSORALLOCATOR_H
25#define ARM_COMPUTE_TENSORALLOCATOR_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026#include "arm_compute/runtime/ITensorAllocator.h"
Georgios Pinitas26014cf2019-09-09 19:00:57 +010027
Georgios Pinitas84b51ad2017-11-07 13:24:57 +000028#include "arm_compute/runtime/Memory.h"
Georgios Pinitas26014cf2019-09-09 19:00:57 +010029#include "arm_compute/runtime/MemoryGroup.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030
31#include <cstdint>
32#include <memory>
33#include <vector>
34
35namespace arm_compute
36{
Georgios Pinitas26014cf2019-09-09 19:00:57 +010037// Forward declaration
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038class Coordinates;
39class TensorInfo;
40
41/** Basic implementation of a CPU memory tensor allocator. */
42class TensorAllocator : public ITensorAllocator
43{
44public:
Alex Gildayc357c472018-03-21 13:54:09 +000045 /** Default constructor.
46 *
Georgios Pinitas26014cf2019-09-09 19:00:57 +010047 * @param[in] owner Memory manageable owner
Alex Gildayc357c472018-03-21 13:54:09 +000048 */
Georgios Pinitas26014cf2019-09-09 19:00:57 +010049 TensorAllocator(IMemoryManageable *owner);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010050 /** Default destructor */
51 ~TensorAllocator();
Alex Gildayc357c472018-03-21 13:54:09 +000052 /** Prevent instances of this class from being copied (As this class contains pointers) */
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010053 TensorAllocator(const TensorAllocator &) = delete;
Alex Gildayc357c472018-03-21 13:54:09 +000054 /** Prevent instances of this class from being copy assigned (As this class contains pointers) */
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010055 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 Barbier6ff3b192017-09-04 18:44:23 +010060
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 Spyrou53860dd2019-07-01 14:20:56 +010073 void init(const TensorAllocator &allocator, const Coordinates &coords, TensorInfo &sub_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074
Alex Gildayc357c472018-03-21 13:54:09 +000075 /** Returns the pointer to the allocated data.
76 *
77 * @return a pointer to the allocated data.
78 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079 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 Pinitas84b51ad2017-11-07 13:24:57 +000094 /** Import an existing memory as a tensor's backing memory
95 *
Georgios Pinitas4d0351c2019-04-03 15:11:16 +010096 * @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 Pinitasdb09b372019-06-17 17:46:17 +010099 * @warning padding should be accounted by the client code.
Manuel Bottini342d1bc2019-07-24 14:51:51 +0100100 * @warning memory must be writable in case of in-place operations
Georgios Pinitas4d0351c2019-04-03 15:11:16 +0100101 * @note buffer alignment will be checked to be compliant with alignment reported by ITensorInfo.
Georgios Pinitas84b51ad2017-11-07 13:24:57 +0000102 *
Georgios Pinitas4d0351c2019-04-03 15:11:16 +0100103 * @param[in] memory Raw memory pointer to be used as backing memory
104 *
105 * @return An error status
Georgios Pinitas84b51ad2017-11-07 13:24:57 +0000106 */
Georgios Pinitas4d0351c2019-04-03 15:11:16 +0100107 Status import_memory(void *memory);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100108 /** Associates the tensor with a memory group
109 *
110 * @param[in] associated_memory_group Memory group to associate the tensor with
111 */
Georgios Pinitas26014cf2019-09-09 19:00:57 +0100112 void set_associated_memory_group(IMemoryGroup *associated_memory_group);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113
114protected:
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
124private:
Georgios Pinitas26014cf2019-09-09 19:00:57 +0100125 IMemoryManageable *_owner; /**< Memory manageable object that owns the allocator */
126 IMemoryGroup *_associated_memory_group; /**< Registered memory manager */
127 Memory _memory; /**< CPU memory */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128};
Georgios Pinitas26014cf2019-09-09 19:00:57 +0100129} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000130#endif /* ARM_COMPUTE_TENSORALLOCATOR_H */