blob: 9119e27682c0de50a2f31a17f2f64207d459ae94 [file] [log] [blame]
Georgios Pinitas99d40952018-04-23 16:26:46 +01001/*
Pablo Tellodb8485a2019-09-24 11:03:47 +01002 * Copyright (c) 2018-2019 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{
Pablo Tellodb8485a2019-09-24 11:03:47 +010034class CLCoreRuntimeContext;
Georgios Pinitas99d40952018-04-23 16:26:46 +010035/** OpenCL memory region interface */
36class ICLMemoryRegion : public IMemoryRegion
37{
38public:
39 /** Constructor
40 *
Pablo Tellodb8485a2019-09-24 11:03:47 +010041 * @param[in] ctx Runtime context
Georgios Pinitas99d40952018-04-23 16:26:46 +010042 * @param[in] size Region size
43 */
Pablo Tellodb8485a2019-09-24 11:03:47 +010044 ICLMemoryRegion(CLCoreRuntimeContext *ctx, size_t size);
Georgios Pinitas99d40952018-04-23 16:26:46 +010045 /** Default Destructor */
46 virtual ~ICLMemoryRegion() = default;
47 /** Prevent instances of this class from being copied (As this class contains pointers) */
48 ICLMemoryRegion(const ICLMemoryRegion &) = delete;
49 /** Default move constructor */
50 ICLMemoryRegion(ICLMemoryRegion &&) = default;
51 /** Prevent instances of this class from being copied (As this class contains pointers) */
52 ICLMemoryRegion &operator=(const ICLMemoryRegion &) = delete;
53 /** Default move assignment operator */
54 ICLMemoryRegion &operator=(ICLMemoryRegion &&) = default;
55 /** Returns the underlying CL buffer
56 *
57 * @return CL memory buffer object
58 */
59 const cl::Buffer &cl_data() const;
60 /** Host/SVM pointer accessor
61 *
62 * @return Host/SVM pointer base
63 */
64 virtual void *ptr() = 0;
65 /** Enqueue a map operation of the allocated buffer on the given queue.
66 *
67 * @param[in,out] q The CL command queue to use for the mapping operation.
68 * @param[in] blocking If true, then the mapping will be ready to use by the time
69 * this method returns, else it is the caller's responsibility
70 * to flush the queue and wait for the mapping operation to have completed before using the returned mapping pointer.
71 *
72 * @return The mapping address.
73 */
74 virtual void *map(cl::CommandQueue &q, bool blocking) = 0;
75 /** Enqueue an unmap operation of the allocated buffer on the given queue.
76 *
77 * @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
78 * the memory is accessed by the device.
79 *
80 * @param[in,out] q The CL command queue to use for the mapping operation.
81 */
82 virtual void unmap(cl::CommandQueue &q) = 0;
83
84 // Inherited methods overridden :
Georgios Pinitasdf310362018-11-14 13:16:56 +000085 void *buffer() override;
86 void *buffer() const override;
87 std::unique_ptr<IMemoryRegion> extract_subregion(size_t offset, size_t size) override;
Georgios Pinitas99d40952018-04-23 16:26:46 +010088
89protected:
Pablo Tellodb8485a2019-09-24 11:03:47 +010090 cl::CommandQueue _queue;
91 cl::Context _ctx;
92 void *_mapping;
93 cl::Buffer _mem;
Georgios Pinitas99d40952018-04-23 16:26:46 +010094};
95
96/** OpenCL buffer memory region implementation */
97class CLBufferMemoryRegion final : public ICLMemoryRegion
98{
99public:
100 /** Constructor
101 *
Pablo Tellodb8485a2019-09-24 11:03:47 +0100102 * @param[in] ctx Runtime context
Georgios Pinitas99d40952018-04-23 16:26:46 +0100103 * @param[in] flags Memory flags
104 * @param[in] size Region size
105 */
Pablo Tellodb8485a2019-09-24 11:03:47 +0100106 CLBufferMemoryRegion(CLCoreRuntimeContext *ctx, cl_mem_flags flags, size_t size);
Georgios Pinitasdf310362018-11-14 13:16:56 +0000107 /** Constructor
108 *
109 * @param[in] buffer Buffer to be used as a memory region
Pablo Tellodb8485a2019-09-24 11:03:47 +0100110 * @param[in] ctx Runtime context
Georgios Pinitasdf310362018-11-14 13:16:56 +0000111 */
Pablo Tellodb8485a2019-09-24 11:03:47 +0100112 CLBufferMemoryRegion(const cl::Buffer &buffer, CLCoreRuntimeContext *ctx);
Georgios Pinitas99d40952018-04-23 16:26:46 +0100113
114 // Inherited methods overridden :
Georgios Pinitasdf310362018-11-14 13:16:56 +0000115 void *ptr() final;
116 void *map(cl::CommandQueue &q, bool blocking) final;
117 void unmap(cl::CommandQueue &q) final;
Georgios Pinitas99d40952018-04-23 16:26:46 +0100118};
119
120/** OpenCL SVM memory region interface */
121class ICLSVMMemoryRegion : public ICLMemoryRegion
122{
123protected:
124 /** Constructor
125 *
Pablo Tellodb8485a2019-09-24 11:03:47 +0100126 * @param[in] ctx Runtime context
Georgios Pinitas99d40952018-04-23 16:26:46 +0100127 * @param[in] flags Memory flags
128 * @param[in] size Region size
129 * @param[in] alignment Alignment
130 */
Pablo Tellodb8485a2019-09-24 11:03:47 +0100131 ICLSVMMemoryRegion(CLCoreRuntimeContext *ctx, cl_mem_flags flags, size_t size, size_t alignment);
Georgios Pinitas99d40952018-04-23 16:26:46 +0100132 /** Destructor */
133 virtual ~ICLSVMMemoryRegion();
134 /** Prevent instances of this class from being copied (As this class contains pointers) */
135 ICLSVMMemoryRegion(const ICLSVMMemoryRegion &) = delete;
136 /** Default move constructor */
137 ICLSVMMemoryRegion(ICLSVMMemoryRegion &&) = default;
138 /** Prevent instances of this class from being copied (As this class contains pointers) */
139 ICLSVMMemoryRegion &operator=(const ICLSVMMemoryRegion &) = delete;
140 /** Default move assignment operator */
141 ICLSVMMemoryRegion &operator=(ICLSVMMemoryRegion &&) = default;
142
143 // Inherited methods overridden :
144 void *ptr() override;
145
146protected:
147 void *_ptr;
148};
149
150/** OpenCL coarse-grain SVM memory region implementation */
151class CLCoarseSVMMemoryRegion final : public ICLSVMMemoryRegion
152{
153public:
154 /** Constructor
155 *
Pablo Tellodb8485a2019-09-24 11:03:47 +0100156 * @param[in] ctx Runtime context
Georgios Pinitas99d40952018-04-23 16:26:46 +0100157 * @param[in] flags Memory flags
158 * @param[in] size Region size
159 * @param[in] alignment Alignment
160 */
Pablo Tellodb8485a2019-09-24 11:03:47 +0100161 CLCoarseSVMMemoryRegion(CLCoreRuntimeContext *ctx, cl_mem_flags flags, size_t size, size_t alignment);
Georgios Pinitas99d40952018-04-23 16:26:46 +0100162
163 // Inherited methods overridden :
Georgios Pinitasdf310362018-11-14 13:16:56 +0000164 void *map(cl::CommandQueue &q, bool blocking) final;
165 void unmap(cl::CommandQueue &q) final;
Georgios Pinitas99d40952018-04-23 16:26:46 +0100166};
167
168/** OpenCL fine-grain SVM memory region implementation */
169class CLFineSVMMemoryRegion final : public ICLSVMMemoryRegion
170{
171public:
172 /** Constructor
173 *
Pablo Tellodb8485a2019-09-24 11:03:47 +0100174 * @param[in] ctx Runtime context
Georgios Pinitas99d40952018-04-23 16:26:46 +0100175 * @param[in] flags Memory flags
176 * @param[in] size Region size
177 * @param[in] alignment Alignment
178 */
Pablo Tellodb8485a2019-09-24 11:03:47 +0100179 CLFineSVMMemoryRegion(CLCoreRuntimeContext *ctx, cl_mem_flags flags, size_t size, size_t alignment);
Georgios Pinitas99d40952018-04-23 16:26:46 +0100180
181 // Inherited methods overridden :
Georgios Pinitasdf310362018-11-14 13:16:56 +0000182 void *map(cl::CommandQueue &q, bool blocking) final;
183 void unmap(cl::CommandQueue &q) final;
Georgios Pinitas99d40952018-04-23 16:26:46 +0100184};
185} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000186#endif /* ARM_COMPUTE_RUNTIME_CL_CL_MEMORY_REGION_H */