blob: 690a924f5b2690dae354ea15a946ae3796eeaf31 [file] [log] [blame]
Georgios Pinitas99d40952018-04-23 16:26:46 +01001/*
SiCong Lia359ee92023-04-18 13:49:56 +01002 * Copyright (c) 2018-2021, 2023 Arm Limited.
Georgios Pinitas99d40952018-04-23 16:26:46 +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_RUNTIME_CL_CL_MEMORY_REGION_H
25#define ARM_COMPUTE_RUNTIME_CL_CL_MEMORY_REGION_H
Georgios Pinitas99d40952018-04-23 16:26:46 +010026
27#include "arm_compute/core/CL/OpenCL.h"
28#include "arm_compute/runtime/IMemoryRegion.h"
29
30#include <cstddef>
31
32namespace arm_compute
33{
34/** OpenCL memory region interface */
35class ICLMemoryRegion : public IMemoryRegion
36{
37public:
38 /** Constructor
39 *
Georgios Pinitas99d40952018-04-23 16:26:46 +010040 * @param[in] size Region size
41 */
Georgios Pinitas2cd5b312021-05-04 21:39:57 +010042 ICLMemoryRegion(size_t size);
Georgios Pinitas99d40952018-04-23 16:26:46 +010043 /** Default Destructor */
44 virtual ~ICLMemoryRegion() = default;
45 /** Prevent instances of this class from being copied (As this class contains pointers) */
46 ICLMemoryRegion(const ICLMemoryRegion &) = delete;
47 /** Default move constructor */
48 ICLMemoryRegion(ICLMemoryRegion &&) = default;
49 /** Prevent instances of this class from being copied (As this class contains pointers) */
50 ICLMemoryRegion &operator=(const ICLMemoryRegion &) = delete;
51 /** Default move assignment operator */
52 ICLMemoryRegion &operator=(ICLMemoryRegion &&) = default;
53 /** Returns the underlying CL buffer
54 *
55 * @return CL memory buffer object
56 */
57 const cl::Buffer &cl_data() const;
58 /** Host/SVM pointer accessor
59 *
60 * @return Host/SVM pointer base
61 */
62 virtual void *ptr() = 0;
63 /** Enqueue a map operation of the allocated buffer on the given queue.
64 *
65 * @param[in,out] q The CL command queue to use for the mapping operation.
66 * @param[in] blocking If true, then the mapping will be ready to use by the time
67 * this method returns, else it is the caller's responsibility
68 * to flush the queue and wait for the mapping operation to have completed before using the returned mapping pointer.
69 *
70 * @return The mapping address.
71 */
72 virtual void *map(cl::CommandQueue &q, bool blocking) = 0;
73 /** Enqueue an unmap operation of the allocated buffer on the given queue.
74 *
75 * @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
76 * the memory is accessed by the device.
77 *
78 * @param[in,out] q The CL command queue to use for the mapping operation.
79 */
80 virtual void unmap(cl::CommandQueue &q) = 0;
81
82 // Inherited methods overridden :
Georgios Pinitasdf310362018-11-14 13:16:56 +000083 void *buffer() override;
Georgios Pinitas74ef1db2020-01-20 17:01:15 +000084 const void *buffer() const override;
Georgios Pinitasdf310362018-11-14 13:16:56 +000085 std::unique_ptr<IMemoryRegion> extract_subregion(size_t offset, size_t size) override;
Georgios Pinitas99d40952018-04-23 16:26:46 +010086
87protected:
SiCong Lia359ee92023-04-18 13:49:56 +010088 cl::Context _ctx;
89 void *_mapping;
90 cl::Buffer _mem;
Georgios Pinitas99d40952018-04-23 16:26:46 +010091};
92
93/** OpenCL buffer memory region implementation */
94class CLBufferMemoryRegion final : public ICLMemoryRegion
95{
96public:
97 /** Constructor
98 *
Georgios Pinitas99d40952018-04-23 16:26:46 +010099 * @param[in] flags Memory flags
100 * @param[in] size Region size
101 */
Georgios Pinitas2cd5b312021-05-04 21:39:57 +0100102 CLBufferMemoryRegion(cl_mem_flags flags, size_t size);
Georgios Pinitasdf310362018-11-14 13:16:56 +0000103 /** Constructor
104 *
105 * @param[in] buffer Buffer to be used as a memory region
106 */
Georgios Pinitas2cd5b312021-05-04 21:39:57 +0100107 CLBufferMemoryRegion(const cl::Buffer &buffer);
Georgios Pinitas99d40952018-04-23 16:26:46 +0100108
109 // Inherited methods overridden :
Georgios Pinitasdf310362018-11-14 13:16:56 +0000110 void *ptr() final;
111 void *map(cl::CommandQueue &q, bool blocking) final;
112 void unmap(cl::CommandQueue &q) final;
Georgios Pinitas99d40952018-04-23 16:26:46 +0100113};
114
115/** OpenCL SVM memory region interface */
116class ICLSVMMemoryRegion : public ICLMemoryRegion
117{
118protected:
119 /** Constructor
120 *
Georgios Pinitas99d40952018-04-23 16:26:46 +0100121 * @param[in] flags Memory flags
122 * @param[in] size Region size
123 * @param[in] alignment Alignment
124 */
Georgios Pinitas2cd5b312021-05-04 21:39:57 +0100125 ICLSVMMemoryRegion(cl_mem_flags flags, size_t size, size_t alignment);
Georgios Pinitas99d40952018-04-23 16:26:46 +0100126 /** Destructor */
127 virtual ~ICLSVMMemoryRegion();
128 /** Prevent instances of this class from being copied (As this class contains pointers) */
129 ICLSVMMemoryRegion(const ICLSVMMemoryRegion &) = delete;
130 /** Default move constructor */
131 ICLSVMMemoryRegion(ICLSVMMemoryRegion &&) = default;
132 /** Prevent instances of this class from being copied (As this class contains pointers) */
133 ICLSVMMemoryRegion &operator=(const ICLSVMMemoryRegion &) = delete;
134 /** Default move assignment operator */
135 ICLSVMMemoryRegion &operator=(ICLSVMMemoryRegion &&) = default;
136
137 // Inherited methods overridden :
138 void *ptr() override;
139
140protected:
141 void *_ptr;
142};
143
144/** OpenCL coarse-grain SVM memory region implementation */
145class CLCoarseSVMMemoryRegion final : public ICLSVMMemoryRegion
146{
147public:
148 /** Constructor
149 *
Georgios Pinitas99d40952018-04-23 16:26:46 +0100150 * @param[in] flags Memory flags
151 * @param[in] size Region size
152 * @param[in] alignment Alignment
153 */
Georgios Pinitas2cd5b312021-05-04 21:39:57 +0100154 CLCoarseSVMMemoryRegion(cl_mem_flags flags, size_t size, size_t alignment);
Georgios Pinitas99d40952018-04-23 16:26:46 +0100155
156 // Inherited methods overridden :
Georgios Pinitasdf310362018-11-14 13:16:56 +0000157 void *map(cl::CommandQueue &q, bool blocking) final;
158 void unmap(cl::CommandQueue &q) final;
Georgios Pinitas99d40952018-04-23 16:26:46 +0100159};
160
161/** OpenCL fine-grain SVM memory region implementation */
162class CLFineSVMMemoryRegion final : public ICLSVMMemoryRegion
163{
164public:
165 /** Constructor
166 *
Georgios Pinitas99d40952018-04-23 16:26:46 +0100167 * @param[in] flags Memory flags
168 * @param[in] size Region size
169 * @param[in] alignment Alignment
170 */
Georgios Pinitas2cd5b312021-05-04 21:39:57 +0100171 CLFineSVMMemoryRegion(cl_mem_flags flags, size_t size, size_t alignment);
Georgios Pinitas99d40952018-04-23 16:26:46 +0100172
173 // Inherited methods overridden :
Georgios Pinitasdf310362018-11-14 13:16:56 +0000174 void *map(cl::CommandQueue &q, bool blocking) final;
175 void unmap(cl::CommandQueue &q) final;
Georgios Pinitas99d40952018-04-23 16:26:46 +0100176};
177} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000178#endif /* ARM_COMPUTE_RUNTIME_CL_CL_MEMORY_REGION_H */