Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame^] | 2 | * Copyright (c) 2018-2020 Arm Limited. |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_RUNTIME_CL_CL_MEMORY_REGION_H |
| 25 | #define ARM_COMPUTE_RUNTIME_CL_CL_MEMORY_REGION_H |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 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 | { |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 34 | class CLCoreRuntimeContext; |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 35 | /** OpenCL memory region interface */ |
| 36 | class ICLMemoryRegion : public IMemoryRegion |
| 37 | { |
| 38 | public: |
| 39 | /** Constructor |
| 40 | * |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 41 | * @param[in] ctx Runtime context |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 42 | * @param[in] size Region size |
| 43 | */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 44 | ICLMemoryRegion(CLCoreRuntimeContext *ctx, size_t size); |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 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 : |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 85 | void *buffer() override; |
Georgios Pinitas | 74ef1db | 2020-01-20 17:01:15 +0000 | [diff] [blame] | 86 | const void *buffer() const override; |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 87 | std::unique_ptr<IMemoryRegion> extract_subregion(size_t offset, size_t size) override; |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 88 | |
| 89 | protected: |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 90 | cl::CommandQueue _queue; |
| 91 | cl::Context _ctx; |
| 92 | void *_mapping; |
| 93 | cl::Buffer _mem; |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | /** OpenCL buffer memory region implementation */ |
| 97 | class CLBufferMemoryRegion final : public ICLMemoryRegion |
| 98 | { |
| 99 | public: |
| 100 | /** Constructor |
| 101 | * |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 102 | * @param[in] ctx Runtime context |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 103 | * @param[in] flags Memory flags |
| 104 | * @param[in] size Region size |
| 105 | */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 106 | CLBufferMemoryRegion(CLCoreRuntimeContext *ctx, cl_mem_flags flags, size_t size); |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 107 | /** Constructor |
| 108 | * |
| 109 | * @param[in] buffer Buffer to be used as a memory region |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 110 | * @param[in] ctx Runtime context |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 111 | */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 112 | CLBufferMemoryRegion(const cl::Buffer &buffer, CLCoreRuntimeContext *ctx); |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 113 | |
| 114 | // Inherited methods overridden : |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 115 | void *ptr() final; |
| 116 | void *map(cl::CommandQueue &q, bool blocking) final; |
| 117 | void unmap(cl::CommandQueue &q) final; |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | /** OpenCL SVM memory region interface */ |
| 121 | class ICLSVMMemoryRegion : public ICLMemoryRegion |
| 122 | { |
| 123 | protected: |
| 124 | /** Constructor |
| 125 | * |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 126 | * @param[in] ctx Runtime context |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 127 | * @param[in] flags Memory flags |
| 128 | * @param[in] size Region size |
| 129 | * @param[in] alignment Alignment |
| 130 | */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 131 | ICLSVMMemoryRegion(CLCoreRuntimeContext *ctx, cl_mem_flags flags, size_t size, size_t alignment); |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 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 | * |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 156 | * @param[in] ctx Runtime context |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 157 | * @param[in] flags Memory flags |
| 158 | * @param[in] size Region size |
| 159 | * @param[in] alignment Alignment |
| 160 | */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 161 | CLCoarseSVMMemoryRegion(CLCoreRuntimeContext *ctx, cl_mem_flags flags, size_t size, size_t alignment); |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 162 | |
| 163 | // Inherited methods overridden : |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 164 | void *map(cl::CommandQueue &q, bool blocking) final; |
| 165 | void unmap(cl::CommandQueue &q) final; |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | /** OpenCL fine-grain SVM memory region implementation */ |
| 169 | class CLFineSVMMemoryRegion final : public ICLSVMMemoryRegion |
| 170 | { |
| 171 | public: |
| 172 | /** Constructor |
| 173 | * |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 174 | * @param[in] ctx Runtime context |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 175 | * @param[in] flags Memory flags |
| 176 | * @param[in] size Region size |
| 177 | * @param[in] alignment Alignment |
| 178 | */ |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 179 | CLFineSVMMemoryRegion(CLCoreRuntimeContext *ctx, cl_mem_flags flags, size_t size, size_t alignment); |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 180 | |
| 181 | // Inherited methods overridden : |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 182 | void *map(cl::CommandQueue &q, bool blocking) final; |
| 183 | void unmap(cl::CommandQueue &q) final; |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 184 | }; |
| 185 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 186 | #endif /* ARM_COMPUTE_RUNTIME_CL_CL_MEMORY_REGION_H */ |