blob: 01dd54e39185f98e99c1da28f0666eaa544400c5 [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 :
84 void *buffer() override;
85 void *buffer() const override;
86 void **handle() override;
87
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);
105
106 // Inherited methods overridden :
107 void *ptr() override;
108 void *map(cl::CommandQueue &q, bool blocking) override;
109 void unmap(cl::CommandQueue &q) override;
110};
111
112/** OpenCL SVM memory region interface */
113class ICLSVMMemoryRegion : public ICLMemoryRegion
114{
115protected:
116 /** Constructor
117 *
118 * @param[in] ctx OpenCL context
119 * @param[in] flags Memory flags
120 * @param[in] size Region size
121 * @param[in] alignment Alignment
122 */
123 ICLSVMMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size, size_t alignment);
124 /** Destructor */
125 virtual ~ICLSVMMemoryRegion();
126 /** Prevent instances of this class from being copied (As this class contains pointers) */
127 ICLSVMMemoryRegion(const ICLSVMMemoryRegion &) = delete;
128 /** Default move constructor */
129 ICLSVMMemoryRegion(ICLSVMMemoryRegion &&) = default;
130 /** Prevent instances of this class from being copied (As this class contains pointers) */
131 ICLSVMMemoryRegion &operator=(const ICLSVMMemoryRegion &) = delete;
132 /** Default move assignment operator */
133 ICLSVMMemoryRegion &operator=(ICLSVMMemoryRegion &&) = default;
134
135 // Inherited methods overridden :
136 void *ptr() override;
137
138protected:
139 void *_ptr;
140};
141
142/** OpenCL coarse-grain SVM memory region implementation */
143class CLCoarseSVMMemoryRegion final : public ICLSVMMemoryRegion
144{
145public:
146 /** Constructor
147 *
148 * @param[in] ctx OpenCL context
149 * @param[in] flags Memory flags
150 * @param[in] size Region size
151 * @param[in] alignment Alignment
152 */
153 CLCoarseSVMMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size, size_t alignment);
154
155 // Inherited methods overridden :
156 void *map(cl::CommandQueue &q, bool blocking) override;
157 void unmap(cl::CommandQueue &q) override;
158};
159
160/** OpenCL fine-grain SVM memory region implementation */
161class CLFineSVMMemoryRegion final : public ICLSVMMemoryRegion
162{
163public:
164 /** Constructor
165 *
166 * @param[in] ctx OpenCL context
167 * @param[in] flags Memory flags
168 * @param[in] size Region size
169 * @param[in] alignment Alignment
170 */
171 CLFineSVMMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size, size_t alignment);
172
173 // Inherited methods overridden :
174 void *map(cl::CommandQueue &q, bool blocking) override;
175 void unmap(cl::CommandQueue &q) override;
176};
177} // namespace arm_compute
178#endif /* __ARM_COMPUTE_RUNTIME_CL_CL_MEMORY_REGION_H__ */