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