blob: e2d35361693647d5714433580ff0ca06a7043d21 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitas856f66e2021-04-22 21:13:21 +01002 * Copyright (c) 2016-2021 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_ITENSORALLOCATOR_H
25#define ARM_COMPUTE_ITENSORALLOCATOR_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
27#include "arm_compute/core/TensorInfo.h"
28#include "arm_compute/core/Types.h"
29
30#include <cstdint>
31
32namespace arm_compute
33{
34/** Interface to allocate tensors */
35class ITensorAllocator
36{
37public:
38 /** Default constructor. */
Georgios Pinitas856f66e2021-04-22 21:13:21 +010039 ITensorAllocator() = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040 /** Allow instances of this class to be copy constructed */
41 ITensorAllocator(const ITensorAllocator &) = default;
42 /** Allow instances of this class to be copied */
43 ITensorAllocator &operator=(const ITensorAllocator &) = default;
44 /** Allow instances of this class to be move constructed */
45 ITensorAllocator(ITensorAllocator &&) = default;
46 /** Allow instances of this class to be moved */
47 ITensorAllocator &operator=(ITensorAllocator &&) = default;
48 /** Default virtual destructor. */
49 virtual ~ITensorAllocator() = default;
50
51 /** Initialize a tensor based on the passed @ref TensorInfo.
52 *
Georgios Pinitas17b12302018-06-18 18:13:51 +010053 * @param[in] input TensorInfo object containing the description of the tensor to initialize.
54 * @param[in] alignment Alignment in bytes that the underlying base pointer should comply with.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010055 */
Georgios Pinitas17b12302018-06-18 18:13:51 +010056 void init(const TensorInfo &input, size_t alignment = 0);
Georgios Pinitas856f66e2021-04-22 21:13:21 +010057 /** Initialize a tensor based with a reference TensorInfo
58 *
59 * @note ITensorAllocator won't own the TensorInfo thus these need to out-live
60 *
61 * @param[in] input TensorInfo object containing the description of the tensor to initialize.
62 * @param[in] alignment Alignment in bytes that the underlying base pointer should comply with.
63 */
64 void soft_init(TensorInfo &input, size_t alignment = 0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065 /** Return a reference to the tensor's metadata
66 *
67 * @return Reference to the tensor's metadata.
68 */
69 TensorInfo &info();
70 /** Return a constant reference to the tensor's metadata
71 *
72 * @return Constant reference to the tensor's metadata.
73 */
74 const TensorInfo &info() const;
Georgios Pinitas17b12302018-06-18 18:13:51 +010075 /** Return underlying's tensor buffer alignment
76 *
77 * @return Tensor buffer alignment
78 */
79 size_t alignment() const;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010080
81 /** Interface to be implemented by the child class to allocate the tensor.
82 *
83 * @note The child is expected to use the TensorInfo to get the size of the memory allocation.
84 * @warning The tensor must not already be allocated. Otherwise calling the function will fail.
85 */
86 virtual void allocate() = 0;
87
88 /** Interface to be implemented by the child class to free the allocated tensor.
89 *
90 * @warning The tensor must have been allocated previously. Otherwise calling the function will fail.
91 */
92 virtual void free() = 0;
93
94protected:
95 /** Interface to be implemented by the child class to lock the memory allocation for the CPU to access.
96 *
97 * @return Pointer to a CPU mapping of the memory
98 */
99 virtual uint8_t *lock() = 0;
100 /** Interface to be implemented by the child class to unlock the memory allocation after the CPU is done accessing it. */
101 virtual void unlock() = 0;
102
103private:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100104 TensorInfo _info_owned{}; /**< Tensor's metadata. */
105 TensorInfo *_info_external{nullptr}; /**< External Tensor's metadata */
106 size_t _alignment{}; /**< Tensor's alignment in bytes */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107};
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100108} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000109#endif /*ARM_COMPUTE_ITENSORALLOCATOR_H */