Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 1 | /* |
SiCong Li | a359ee9 | 2023-04-18 13:49:56 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2021, 2023 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 | */ |
| 24 | #include "arm_compute/runtime/CL/CLMemoryRegion.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 28 | |
| 29 | namespace arm_compute |
| 30 | { |
Georgios Pinitas | 2cd5b31 | 2021-05-04 21:39:57 +0100 | [diff] [blame] | 31 | ICLMemoryRegion::ICLMemoryRegion(size_t size) |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 32 | : IMemoryRegion(size), |
Georgios Pinitas | 2cd5b31 | 2021-05-04 21:39:57 +0100 | [diff] [blame] | 33 | _ctx(CLScheduler::get().context()), |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 34 | _mapping(nullptr), |
| 35 | _mem() |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 36 | { |
| 37 | } |
| 38 | |
| 39 | const cl::Buffer &ICLMemoryRegion::cl_data() const |
| 40 | { |
| 41 | return _mem; |
| 42 | } |
| 43 | |
| 44 | void *ICLMemoryRegion::buffer() |
| 45 | { |
| 46 | return _mapping; |
| 47 | } |
| 48 | |
Georgios Pinitas | 74ef1db | 2020-01-20 17:01:15 +0000 | [diff] [blame] | 49 | const void *ICLMemoryRegion::buffer() const |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 50 | { |
| 51 | return _mapping; |
| 52 | } |
| 53 | |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 54 | std::unique_ptr<IMemoryRegion> ICLMemoryRegion::extract_subregion(size_t offset, size_t size) |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 55 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 56 | ARM_COMPUTE_UNUSED(offset, size); |
| 57 | return nullptr; |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 58 | } |
| 59 | |
Georgios Pinitas | 2cd5b31 | 2021-05-04 21:39:57 +0100 | [diff] [blame] | 60 | CLBufferMemoryRegion::CLBufferMemoryRegion(cl_mem_flags flags, size_t size) |
| 61 | : ICLMemoryRegion(size) |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 62 | { |
| 63 | if(_size != 0) |
| 64 | { |
Georgios Pinitas | 2cd5b31 | 2021-05-04 21:39:57 +0100 | [diff] [blame] | 65 | _mem = cl::Buffer(CLScheduler::get().context(), flags, _size); |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Georgios Pinitas | 2cd5b31 | 2021-05-04 21:39:57 +0100 | [diff] [blame] | 69 | CLBufferMemoryRegion::CLBufferMemoryRegion(const cl::Buffer &buffer) |
| 70 | : ICLMemoryRegion(buffer.getInfo<CL_MEM_SIZE>()) |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 71 | { |
| 72 | _mem = buffer; |
| 73 | } |
| 74 | |
SiCong Li | 7d91c61 | 2023-08-11 17:58:56 +0100 | [diff] [blame^] | 75 | CLBufferMemoryRegion::~CLBufferMemoryRegion() |
| 76 | { |
| 77 | // Flush the command queue to ensure all commands that may use this memory buffer are scheduled to be finished before |
| 78 | // this buffer is freed |
| 79 | // Do not call finish as it is a blocking call which affects the performance |
| 80 | CLScheduler::get().queue().flush(); |
| 81 | } |
| 82 | |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 83 | void *CLBufferMemoryRegion::ptr() |
| 84 | { |
| 85 | return nullptr; |
| 86 | } |
| 87 | |
| 88 | void *CLBufferMemoryRegion::map(cl::CommandQueue &q, bool blocking) |
| 89 | { |
| 90 | ARM_COMPUTE_ERROR_ON(_mem.get() == nullptr); |
| 91 | _mapping = q.enqueueMapBuffer(_mem, blocking ? CL_TRUE : CL_FALSE, CL_MAP_READ | CL_MAP_WRITE, 0, _size); |
| 92 | return _mapping; |
| 93 | } |
| 94 | |
| 95 | void CLBufferMemoryRegion::unmap(cl::CommandQueue &q) |
| 96 | { |
| 97 | ARM_COMPUTE_ERROR_ON(_mem.get() == nullptr); |
| 98 | q.enqueueUnmapMemObject(_mem, _mapping); |
| 99 | _mapping = nullptr; |
| 100 | } |
| 101 | |
Georgios Pinitas | 2cd5b31 | 2021-05-04 21:39:57 +0100 | [diff] [blame] | 102 | ICLSVMMemoryRegion::ICLSVMMemoryRegion(cl_mem_flags flags, size_t size, size_t alignment) |
| 103 | : ICLMemoryRegion(size), _ptr(nullptr) |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 104 | { |
| 105 | if(size != 0) |
| 106 | { |
Georgios Pinitas | 2cd5b31 | 2021-05-04 21:39:57 +0100 | [diff] [blame] | 107 | _ptr = clSVMAlloc(CLScheduler::get().context().get(), flags, size, alignment); |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 108 | if(_ptr != nullptr) |
| 109 | { |
Georgios Pinitas | 2cd5b31 | 2021-05-04 21:39:57 +0100 | [diff] [blame] | 110 | _mem = cl::Buffer(CLScheduler::get().context(), CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, _size, _ptr); |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | ICLSVMMemoryRegion::~ICLSVMMemoryRegion() |
| 116 | { |
| 117 | if(_ptr != nullptr) |
| 118 | { |
Georgios Pinitas | 5a1320b | 2019-06-27 17:50:43 +0100 | [diff] [blame] | 119 | try |
| 120 | { |
SiCong Li | 7d91c61 | 2023-08-11 17:58:56 +0100 | [diff] [blame^] | 121 | // Can only use the blocking finish instead of the non-blocking flush here, because clSVMFree requires all |
| 122 | // commands that may use the svm pointer to finish beforehand |
| 123 | // https://registry.khronos.org/OpenCL/sdk/3.0/docs/man/html/clSVMFree.html |
SiCong Li | a359ee9 | 2023-04-18 13:49:56 +0100 | [diff] [blame] | 124 | clFinish(CLScheduler::get().queue().get()); |
Georgios Pinitas | 5a1320b | 2019-06-27 17:50:43 +0100 | [diff] [blame] | 125 | _mem = cl::Buffer(); |
| 126 | clSVMFree(_ctx.get(), _ptr); |
| 127 | } |
| 128 | catch(...) |
| 129 | { |
| 130 | } |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | void *ICLSVMMemoryRegion::ptr() |
| 135 | { |
| 136 | return _ptr; |
| 137 | } |
| 138 | |
Georgios Pinitas | 2cd5b31 | 2021-05-04 21:39:57 +0100 | [diff] [blame] | 139 | CLCoarseSVMMemoryRegion::CLCoarseSVMMemoryRegion(cl_mem_flags flags, size_t size, size_t alignment) |
| 140 | : ICLSVMMemoryRegion(flags, size, alignment) |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 141 | { |
| 142 | } |
| 143 | |
| 144 | void *CLCoarseSVMMemoryRegion::map(cl::CommandQueue &q, bool blocking) |
| 145 | { |
| 146 | ARM_COMPUTE_ERROR_ON(_ptr == nullptr); |
| 147 | clEnqueueSVMMap(q.get(), blocking ? CL_TRUE : CL_FALSE, CL_MAP_READ | CL_MAP_WRITE, _ptr, _size, 0, nullptr, nullptr); |
| 148 | _mapping = _ptr; |
| 149 | return _mapping; |
| 150 | } |
| 151 | |
| 152 | void CLCoarseSVMMemoryRegion::unmap(cl::CommandQueue &q) |
| 153 | { |
| 154 | ARM_COMPUTE_ERROR_ON(_ptr == nullptr); |
| 155 | clEnqueueSVMUnmap(q.get(), _ptr, 0, nullptr, nullptr); |
| 156 | _mapping = nullptr; |
| 157 | } |
| 158 | |
Georgios Pinitas | 2cd5b31 | 2021-05-04 21:39:57 +0100 | [diff] [blame] | 159 | CLFineSVMMemoryRegion::CLFineSVMMemoryRegion(cl_mem_flags flags, size_t size, size_t alignment) |
| 160 | : ICLSVMMemoryRegion(flags, size, alignment) |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 161 | { |
| 162 | } |
| 163 | |
| 164 | void *CLFineSVMMemoryRegion::map(cl::CommandQueue &q, bool blocking) |
| 165 | { |
| 166 | if(blocking) |
| 167 | { |
| 168 | clFinish(q.get()); |
| 169 | } |
| 170 | _mapping = _ptr; |
| 171 | return _mapping; |
| 172 | } |
| 173 | |
| 174 | void CLFineSVMMemoryRegion::unmap(cl::CommandQueue &q) |
| 175 | { |
| 176 | ARM_COMPUTE_UNUSED(q); |
| 177 | _mapping = nullptr; |
| 178 | } |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 179 | } // namespace arm_compute |