blob: de5f482d054559230fa94a06a9bc5830fea76bbd [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Alex Gildayc357c472018-03-21 13:54:09 +00002 * Copyright (c) 2016-2018 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_CLTENSORALLOCATOR_H__
25#define __ARM_COMPUTE_CLTENSORALLOCATOR_H__
26
Georgios Pinitas99d40952018-04-23 16:26:46 +010027#include "arm_compute/runtime/CL/CLMemory.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/runtime/ITensorAllocator.h"
29
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010030#include "arm_compute/core/CL/OpenCL.h"
Georgios Pinitas99d40952018-04-23 16:26:46 +010031
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include <cstdint>
33
34namespace arm_compute
35{
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010036class CLTensor;
37template <typename>
38class MemoryGroupBase;
Alex Gildayc357c472018-03-21 13:54:09 +000039/** Memory Group in OpenCL */
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010040using CLMemoryGroup = MemoryGroupBase<CLTensor>;
41
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.
49 */
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010050 CLTensorAllocator(CLTensor *owner = nullptr);
Alex Gildayc357c472018-03-21 13:54:09 +000051 /** Prevent instances of this class from being copied (As this class contains pointers) */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010052 CLTensorAllocator(const CLTensorAllocator &) = delete;
Alex Gildayc357c472018-03-21 13:54:09 +000053 /** Prevent instances of this class from being copy assigned (As this class contains pointers) */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054 CLTensorAllocator &operator=(const CLTensorAllocator &) = delete;
55 /** Allow instances of this class to be moved */
56 CLTensorAllocator(CLTensorAllocator &&) = default;
57 /** Allow instances of this class to be moved */
58 CLTensorAllocator &operator=(CLTensorAllocator &&) = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059
Alex Gildayc357c472018-03-21 13:54:09 +000060 /** Interface to be implemented by the child class to return the pointer to the mapped data.
61 *
62 * @return pointer to the mapped data.
63 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064 uint8_t *data();
Alex Gildayc357c472018-03-21 13:54:09 +000065 /** Interface to be implemented by the child class to return the pointer to the CL data.
66 *
67 * @return pointer to the CL data.
68 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010069 const cl::Buffer &cl_data() const;
Pablo Telloe86a09f2018-01-11 15:44:48 +000070
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071 /** Enqueue a map operation of the allocated buffer on the given queue.
72 *
73 * @param[in,out] q The CL command queue to use for the mapping operation.
74 * @param[in] blocking If true, then the mapping will be ready to use by the time
75 * this method returns, else it is the caller's responsibility
76 * to flush the queue and wait for the mapping operation to have completed before using the returned mapping pointer.
77 *
78 * @return The mapping address.
79 */
80 uint8_t *map(cl::CommandQueue &q, bool blocking);
81 /** Enqueue an unmap operation of the allocated buffer on the given queue.
82 *
83 * @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
84 * the memory is accessed by the device.
85 *
86 * @param[in,out] q The CL command queue to use for the mapping operation.
87 * @param[in] mapping The cpu mapping to unmap.
88 */
89 void unmap(cl::CommandQueue &q, uint8_t *mapping);
90
91 /** Allocate size specified by TensorInfo of OpenCL memory.
92 *
93 * @note: The tensor must not already be allocated when calling this function.
94 *
95 */
96 void allocate() override;
97
98 /** Free allocated OpenCL memory.
99 *
100 * @note The tensor must have been allocated when calling this function.
101 *
102 */
103 void free() override;
Georgios Pinitas99d40952018-04-23 16:26:46 +0100104 /** Import an existing memory as a tensor's backing memory
105 *
Georgios Pinitasdf310362018-11-14 13:16:56 +0000106 * @warning ownership of memory is not transferred
Georgios Pinitas99d40952018-04-23 16:26:46 +0100107 *
Georgios Pinitasdf310362018-11-14 13:16:56 +0000108 * @param[in] buffer Buffer to import
Georgios Pinitas99d40952018-04-23 16:26:46 +0100109 *
110 * @return error status
111 */
Georgios Pinitasdf310362018-11-14 13:16:56 +0000112 arm_compute::Status import_memory(cl::Buffer buffer);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100113 /** Associates the tensor with a memory group
114 *
115 * @param[in] associated_memory_group Memory group to associate the tensor with
116 */
117 void set_associated_memory_group(CLMemoryGroup *associated_memory_group);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100118
119protected:
120 /** Call map() on the OpenCL buffer.
121 *
122 * @return A pointer to the beginning of the tensor's allocation.
123 */
124 uint8_t *lock() override;
125 /** Call unmap() on the OpenCL buffer. */
126 void unlock() override;
127
128private:
Georgios Pinitasdf310362018-11-14 13:16:56 +0000129 static const cl::Buffer _empty_buffer;
130
131private:
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100132 CLMemoryGroup *_associated_memory_group; /**< Registered memory manager */
Georgios Pinitas99d40952018-04-23 16:26:46 +0100133 CLMemory _memory; /**< OpenCL memory */
Georgios Pinitasdf310362018-11-14 13:16:56 +0000134 uint8_t *_mapping; /**< Pointer to the CPU mapping of the OpenCL buffer. */
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100135 CLTensor *_owner; /**< Owner of the allocator */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136};
Georgios Pinitas99d40952018-04-23 16:26:46 +0100137} // namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100138#endif /* __ARM_COMPUTE_CLTENSORALLOCATOR_H__ */