blob: 1b061ee1d68481c0837cd159743fd56a7391f313 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitas035004e2021-04-13 19:44:17 +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_CLTENSORALLOCATOR_H
25#define ARM_COMPUTE_CLTENSORALLOCATOR_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010027#include "arm_compute/runtime/CL/CLArray.h"
Georgios Pinitas99d40952018-04-23 16:26:46 +010028#include "arm_compute/runtime/CL/CLMemory.h"
Georgios Pinitas035004e2021-04-13 19:44:17 +010029#include "arm_compute/runtime/IAllocator.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/runtime/ITensorAllocator.h"
Georgios Pinitas26014cf2019-09-09 19:00:57 +010031#include "arm_compute/runtime/MemoryGroup.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010033#include "arm_compute/core/CL/CLTypes.h"
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010034#include "arm_compute/core/CL/OpenCL.h"
Georgios Pinitas99d40952018-04-23 16:26:46 +010035
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036#include <cstdint>
37
38namespace arm_compute
39{
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010040class CLTensor;
Pablo Tellodb8485a2019-09-24 11:03:47 +010041class CLRuntimeContext;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010042/** Basic implementation of a CL memory tensor allocator. */
43class CLTensorAllocator : public ITensorAllocator
44{
45public:
Alex Gildayc357c472018-03-21 13:54:09 +000046 /** Default constructor.
47 *
48 * @param[in] owner (Optional) Owner of the allocator.
Pablo Tellodb8485a2019-09-24 11:03:47 +010049 * @param[in] ctx (Optional) Runtime context.
Alex Gildayc357c472018-03-21 13:54:09 +000050 */
Pablo Tellodb8485a2019-09-24 11:03:47 +010051 CLTensorAllocator(IMemoryManageable *owner = nullptr, CLRuntimeContext *ctx = nullptr);
Alex Gildayc357c472018-03-21 13:54:09 +000052 /** Prevent instances of this class from being copied (As this class contains pointers) */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053 CLTensorAllocator(const CLTensorAllocator &) = delete;
Alex Gildayc357c472018-03-21 13:54:09 +000054 /** Prevent instances of this class from being copy assigned (As this class contains pointers) */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010055 CLTensorAllocator &operator=(const CLTensorAllocator &) = delete;
56 /** Allow instances of this class to be moved */
57 CLTensorAllocator(CLTensorAllocator &&) = default;
58 /** Allow instances of this class to be moved */
59 CLTensorAllocator &operator=(CLTensorAllocator &&) = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010060
Alex Gildayc357c472018-03-21 13:54:09 +000061 /** Interface to be implemented by the child class to return the pointer to the mapped data.
62 *
63 * @return pointer to the mapped data.
64 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065 uint8_t *data();
Alex Gildayc357c472018-03-21 13:54:09 +000066 /** Interface to be implemented by the child class to return the pointer to the CL data.
67 *
68 * @return pointer to the CL data.
69 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010070 const cl::Buffer &cl_data() const;
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010071 /** Wrapped quantization info data accessor
72 *
73 * @return A wrapped quantization info object.
74 */
75 CLQuantization quantization() const;
Pablo Telloe86a09f2018-01-11 15:44:48 +000076
Anthony Barbier6ff3b192017-09-04 18:44:23 +010077 /** Enqueue a map operation of the allocated buffer on the given queue.
78 *
79 * @param[in,out] q The CL command queue to use for the mapping operation.
80 * @param[in] blocking If true, then the mapping will be ready to use by the time
81 * this method returns, else it is the caller's responsibility
82 * to flush the queue and wait for the mapping operation to have completed before using the returned mapping pointer.
83 *
84 * @return The mapping address.
85 */
86 uint8_t *map(cl::CommandQueue &q, bool blocking);
87 /** Enqueue an unmap operation of the allocated buffer on the given queue.
88 *
89 * @note This method simply enqueue the unmap operation, it is the caller's responsibility to flush the queue and make sure the unmap is finished before
90 * the memory is accessed by the device.
91 *
92 * @param[in,out] q The CL command queue to use for the mapping operation.
93 * @param[in] mapping The cpu mapping to unmap.
94 */
95 void unmap(cl::CommandQueue &q, uint8_t *mapping);
96
97 /** Allocate size specified by TensorInfo of OpenCL memory.
98 *
99 * @note: The tensor must not already be allocated when calling this function.
100 *
101 */
102 void allocate() override;
103
104 /** Free allocated OpenCL memory.
105 *
106 * @note The tensor must have been allocated when calling this function.
107 *
108 */
109 void free() override;
Georgios Pinitas99d40952018-04-23 16:26:46 +0100110 /** Import an existing memory as a tensor's backing memory
111 *
Michele Di Giorgio57f30a92020-09-08 14:03:51 +0100112 * @warning memory should have been created under the same context that Compute Library uses.
Georgios Pinitas4d0351c2019-04-03 15:11:16 +0100113 * @warning memory is expected to be aligned with the device requirements.
114 * @warning tensor shouldn't be memory managed.
115 * @warning ownership of memory is not transferred.
Manuel Bottini342d1bc2019-07-24 14:51:51 +0100116 * @warning memory must be writable in case of in-place operations
Georgios Pinitasdb09b372019-06-17 17:46:17 +0100117 * @warning padding should be accounted by the client code.
Georgios Pinitas4d0351c2019-04-03 15:11:16 +0100118 * @note buffer size will be checked to be compliant with total_size reported by ITensorInfo.
Georgios Pinitas99d40952018-04-23 16:26:46 +0100119 *
Georgios Pinitas4d0351c2019-04-03 15:11:16 +0100120 * @param[in] buffer Buffer to be used as backing memory
Georgios Pinitas99d40952018-04-23 16:26:46 +0100121 *
Georgios Pinitas4d0351c2019-04-03 15:11:16 +0100122 * @return An error status
Georgios Pinitas99d40952018-04-23 16:26:46 +0100123 */
Georgios Pinitas4d0351c2019-04-03 15:11:16 +0100124 Status import_memory(cl::Buffer buffer);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100125 /** Associates the tensor with a memory group
126 *
127 * @param[in] associated_memory_group Memory group to associate the tensor with
128 */
Georgios Pinitas26014cf2019-09-09 19:00:57 +0100129 void set_associated_memory_group(IMemoryGroup *associated_memory_group);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130
Georgios Pinitas035004e2021-04-13 19:44:17 +0100131 /** Sets global allocator that will be used by all CLTensor objects
132 *
133 *
134 * @param[in] allocator Allocator to be used as a global allocator
135 */
136 static void set_global_allocator(IAllocator *allocator);
137
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100138protected:
139 /** Call map() on the OpenCL buffer.
140 *
141 * @return A pointer to the beginning of the tensor's allocation.
142 */
143 uint8_t *lock() override;
144 /** Call unmap() on the OpenCL buffer. */
145 void unlock() override;
146
147private:
Georgios Pinitasdf310362018-11-14 13:16:56 +0000148 static const cl::Buffer _empty_buffer;
149
150private:
Pablo Tellodb8485a2019-09-24 11:03:47 +0100151 CLRuntimeContext *_ctx;
Georgios Pinitas26014cf2019-09-09 19:00:57 +0100152 IMemoryManageable *_owner; /**< Memory manageable object that owns the allocator */
153 IMemoryGroup *_associated_memory_group; /**< Registered memory manager */
154 CLMemory _memory; /**< OpenCL memory */
155 uint8_t *_mapping; /**< Pointer to the CPU mapping of the OpenCL buffer. */
156 CLFloatArray _scale; /**< Scales array in case of quantized per channel data type */
157 CLInt32Array _offset; /**< Offsets array in case of quantized per channel data type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100158};
Georgios Pinitas99d40952018-04-23 16:26:46 +0100159} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000160#endif /* ARM_COMPUTE_CLTENSORALLOCATOR_H */