• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2018-2020 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 
32 namespace arm_compute
33 {
34 class CLCoreRuntimeContext;
35 /** OpenCL memory region interface */
36 class ICLMemoryRegion : public IMemoryRegion
37 {
38 public:
39     /** Constructor
40      *
41      * @param[in] ctx  Runtime context
42      * @param[in] size Region size
43      */
44     ICLMemoryRegion(CLCoreRuntimeContext *ctx, size_t size);
45     /** 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 :
85     void                          *buffer() override;
86     const void                    *buffer() const override;
87     std::unique_ptr<IMemoryRegion> extract_subregion(size_t offset, size_t size) override;
88 
89 protected:
90     cl::CommandQueue _queue;
91     cl::Context      _ctx;
92     void            *_mapping;
93     cl::Buffer       _mem;
94 };
95 
96 /** OpenCL buffer memory region implementation */
97 class CLBufferMemoryRegion final : public ICLMemoryRegion
98 {
99 public:
100     /** Constructor
101      *
102      * @param[in] ctx   Runtime context
103      * @param[in] flags Memory flags
104      * @param[in] size  Region size
105      */
106     CLBufferMemoryRegion(CLCoreRuntimeContext *ctx, cl_mem_flags flags, size_t size);
107     /** Constructor
108      *
109      * @param[in] buffer Buffer to be used as a memory region
110      * @param[in] ctx    Runtime context
111      */
112     CLBufferMemoryRegion(const cl::Buffer &buffer, CLCoreRuntimeContext *ctx);
113 
114     // Inherited methods overridden :
115     void *ptr() final;
116     void *map(cl::CommandQueue &q, bool blocking) final;
117     void unmap(cl::CommandQueue &q) final;
118 };
119 
120 /** OpenCL SVM memory region interface */
121 class ICLSVMMemoryRegion : public ICLMemoryRegion
122 {
123 protected:
124     /** Constructor
125      *
126      * @param[in] ctx       Runtime context
127      * @param[in] flags     Memory flags
128      * @param[in] size      Region size
129      * @param[in] alignment Alignment
130      */
131     ICLSVMMemoryRegion(CLCoreRuntimeContext *ctx, cl_mem_flags flags, size_t size, size_t alignment);
132     /** 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 
146 protected:
147     void *_ptr;
148 };
149 
150 /** OpenCL coarse-grain SVM memory region implementation */
151 class CLCoarseSVMMemoryRegion final : public ICLSVMMemoryRegion
152 {
153 public:
154     /** Constructor
155      *
156      * @param[in] ctx       Runtime context
157      * @param[in] flags     Memory flags
158      * @param[in] size      Region size
159      * @param[in] alignment Alignment
160      */
161     CLCoarseSVMMemoryRegion(CLCoreRuntimeContext *ctx, cl_mem_flags flags, size_t size, size_t alignment);
162 
163     // Inherited methods overridden :
164     void *map(cl::CommandQueue &q, bool blocking) final;
165     void unmap(cl::CommandQueue &q) final;
166 };
167 
168 /** OpenCL fine-grain SVM memory region implementation */
169 class CLFineSVMMemoryRegion final : public ICLSVMMemoryRegion
170 {
171 public:
172     /** Constructor
173      *
174      * @param[in] ctx       Runtime context
175      * @param[in] flags     Memory flags
176      * @param[in] size      Region size
177      * @param[in] alignment Alignment
178      */
179     CLFineSVMMemoryRegion(CLCoreRuntimeContext *ctx, cl_mem_flags flags, size_t size, size_t alignment);
180 
181     // Inherited methods overridden :
182     void *map(cl::CommandQueue &q, bool blocking) final;
183     void unmap(cl::CommandQueue &q) final;
184 };
185 } // namespace arm_compute
186 #endif /* ARM_COMPUTE_RUNTIME_CL_CL_MEMORY_REGION_H */
187