blob: dbfd8225ca4f2ece20e53edd6c432c4527dec8f7 [file] [log] [blame]
Georgios Pinitas99d40952018-04-23 16:26:46 +01001/*
2 * Copyright (c) 2018 ARM Limited.
3 *
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_RUNTIME_CL_CL_MEMORY_REGION_H__
25#define __ARM_COMPUTE_RUNTIME_CL_CL_MEMORY_REGION_H__
26
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 *
40 * @param[in] ctx OpenCL context
41 * @param[in] size Region size
42 */
43 ICLMemoryRegion(cl::Context ctx, size_t size);
44 /** Default Destructor */
45 virtual ~ICLMemoryRegion() = default;
46 /** Prevent instances of this class from being copied (As this class contains pointers) */
47 ICLMemoryRegion(const ICLMemoryRegion &) = delete;
48 /** Default move constructor */
49 ICLMemoryRegion(ICLMemoryRegion &&) = default;
50 /** Prevent instances of this class from being copied (As this class contains pointers) */
51 ICLMemoryRegion &operator=(const ICLMemoryRegion &) = delete;
52 /** Default move assignment operator */
53 ICLMemoryRegion &operator=(ICLMemoryRegion &&) = default;
54 /** Returns the underlying CL buffer
55 *
56 * @return CL memory buffer object
57 */
58 const cl::Buffer &cl_data() const;
59 /** Host/SVM pointer accessor
60 *
61 * @return Host/SVM pointer base
62 */
63 virtual void *ptr() = 0;
64 /** Enqueue a map operation of the allocated buffer on the given queue.
65 *
66 * @param[in,out] q The CL command queue to use for the mapping operation.
67 * @param[in] blocking If true, then the mapping will be ready to use by the time
68 * this method returns, else it is the caller's responsibility
69 * to flush the queue and wait for the mapping operation to have completed before using the returned mapping pointer.
70 *
71 * @return The mapping address.
72 */
73 virtual void *map(cl::CommandQueue &q, bool blocking) = 0;
74 /** Enqueue an unmap operation of the allocated buffer on the given queue.
75 *
76 * @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
77 * the memory is accessed by the device.
78 *
79 * @param[in,out] q The CL command queue to use for the mapping operation.
80 */
81 virtual void unmap(cl::CommandQueue &q) = 0;
82
83 // Inherited methods overridden :
Georgios Pinitasdf310362018-11-14 13:16:56 +000084 void *buffer() override;
85 void *buffer() const override;
86 std::unique_ptr<IMemoryRegion> extract_subregion(size_t offset, size_t size) override;
Georgios Pinitas99d40952018-04-23 16:26:46 +010087
88protected:
89 cl::Context _ctx;
90 void *_mapping;
91 cl::Buffer _mem;
92};
93
94/** OpenCL buffer memory region implementation */
95class CLBufferMemoryRegion final : public ICLMemoryRegion
96{
97public:
98 /** Constructor
99 *
100 * @param[in] ctx OpenCL context
101 * @param[in] flags Memory flags
102 * @param[in] size Region size
103 */
104 CLBufferMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size);
Georgios Pinitasdf310362018-11-14 13:16:56 +0000105 /** Constructor
106 *
107 * @param[in] buffer Buffer to be used as a memory region
108 */
109 CLBufferMemoryRegion(const cl::Buffer &buffer);
Georgios Pinitas99d40952018-04-23 16:26:46 +0100110
111 // Inherited methods overridden :
Georgios Pinitasdf310362018-11-14 13:16:56 +0000112 void *ptr() final;
113 void *map(cl::CommandQueue &q, bool blocking) final;
114 void unmap(cl::CommandQueue &q) final;
Georgios Pinitas99d40952018-04-23 16:26:46 +0100115};
116
117/** OpenCL SVM memory region interface */
118class ICLSVMMemoryRegion : public ICLMemoryRegion
119{
120protected:
121 /** Constructor
122 *
123 * @param[in] ctx OpenCL context
124 * @param[in] flags Memory flags
125 * @param[in] size Region size
126 * @param[in] alignment Alignment
127 */
128 ICLSVMMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size, size_t alignment);
129 /** Destructor */
130 virtual ~ICLSVMMemoryRegion();
131 /** Prevent instances of this class from being copied (As this class contains pointers) */
132 ICLSVMMemoryRegion(const ICLSVMMemoryRegion &) = delete;
133 /** Default move constructor */
134 ICLSVMMemoryRegion(ICLSVMMemoryRegion &&) = default;
135 /** Prevent instances of this class from being copied (As this class contains pointers) */
136 ICLSVMMemoryRegion &operator=(const ICLSVMMemoryRegion &) = delete;
137 /** Default move assignment operator */
138 ICLSVMMemoryRegion &operator=(ICLSVMMemoryRegion &&) = default;
139
140 // Inherited methods overridden :
141 void *ptr() override;
142
143protected:
144 void *_ptr;
145};
146
147/** OpenCL coarse-grain SVM memory region implementation */
148class CLCoarseSVMMemoryRegion final : public ICLSVMMemoryRegion
149{
150public:
151 /** Constructor
152 *
153 * @param[in] ctx OpenCL context
154 * @param[in] flags Memory flags
155 * @param[in] size Region size
156 * @param[in] alignment Alignment
157 */
158 CLCoarseSVMMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size, size_t alignment);
159
160 // Inherited methods overridden :
Georgios Pinitasdf310362018-11-14 13:16:56 +0000161 void *map(cl::CommandQueue &q, bool blocking) final;
162 void unmap(cl::CommandQueue &q) final;
Georgios Pinitas99d40952018-04-23 16:26:46 +0100163};
164
165/** OpenCL fine-grain SVM memory region implementation */
166class CLFineSVMMemoryRegion final : public ICLSVMMemoryRegion
167{
168public:
169 /** Constructor
170 *
171 * @param[in] ctx OpenCL context
172 * @param[in] flags Memory flags
173 * @param[in] size Region size
174 * @param[in] alignment Alignment
175 */
176 CLFineSVMMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size, size_t alignment);
177
178 // Inherited methods overridden :
Georgios Pinitasdf310362018-11-14 13:16:56 +0000179 void *map(cl::CommandQueue &q, bool blocking) final;
180 void unmap(cl::CommandQueue &q) final;
Georgios Pinitas99d40952018-04-23 16:26:46 +0100181};
182} // namespace arm_compute
183#endif /* __ARM_COMPUTE_RUNTIME_CL_CL_MEMORY_REGION_H__ */