Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 5a1320b | 2019-06-27 17:50:43 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 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 | |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame^] | 26 | #include "arm_compute/core/CL/CLCoreRuntimeContext.h" |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 29 | |
| 30 | namespace arm_compute |
| 31 | { |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame^] | 32 | ICLMemoryRegion::ICLMemoryRegion(CLCoreRuntimeContext *ctx, size_t size) |
| 33 | : IMemoryRegion(size), |
| 34 | _queue((ctx != nullptr) ? ctx->queue() : CLScheduler::get().queue()), |
| 35 | _ctx((ctx != nullptr) ? ctx->context() : CLScheduler::get().context()), |
| 36 | _mapping(nullptr), |
| 37 | _mem() |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 38 | { |
| 39 | } |
| 40 | |
| 41 | const cl::Buffer &ICLMemoryRegion::cl_data() const |
| 42 | { |
| 43 | return _mem; |
| 44 | } |
| 45 | |
| 46 | void *ICLMemoryRegion::buffer() |
| 47 | { |
| 48 | return _mapping; |
| 49 | } |
| 50 | |
| 51 | void *ICLMemoryRegion::buffer() const |
| 52 | { |
| 53 | return _mapping; |
| 54 | } |
| 55 | |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 56 | 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] | 57 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 58 | ARM_COMPUTE_UNUSED(offset, size); |
| 59 | return nullptr; |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 60 | } |
| 61 | |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame^] | 62 | CLBufferMemoryRegion::CLBufferMemoryRegion(CLCoreRuntimeContext *ctx, cl_mem_flags flags, size_t size) |
| 63 | : ICLMemoryRegion(ctx, size) |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 64 | { |
| 65 | if(_size != 0) |
| 66 | { |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame^] | 67 | _mem = cl::Buffer((ctx != nullptr) ? ctx->context() : CLScheduler::get().context(), flags, _size); |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame^] | 71 | CLBufferMemoryRegion::CLBufferMemoryRegion(const cl::Buffer &buffer, CLCoreRuntimeContext *ctx) |
| 72 | : ICLMemoryRegion(ctx, buffer.getInfo<CL_MEM_SIZE>()) |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 73 | { |
| 74 | _mem = buffer; |
| 75 | } |
| 76 | |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 77 | void *CLBufferMemoryRegion::ptr() |
| 78 | { |
| 79 | return nullptr; |
| 80 | } |
| 81 | |
| 82 | void *CLBufferMemoryRegion::map(cl::CommandQueue &q, bool blocking) |
| 83 | { |
| 84 | ARM_COMPUTE_ERROR_ON(_mem.get() == nullptr); |
| 85 | _mapping = q.enqueueMapBuffer(_mem, blocking ? CL_TRUE : CL_FALSE, CL_MAP_READ | CL_MAP_WRITE, 0, _size); |
| 86 | return _mapping; |
| 87 | } |
| 88 | |
| 89 | void CLBufferMemoryRegion::unmap(cl::CommandQueue &q) |
| 90 | { |
| 91 | ARM_COMPUTE_ERROR_ON(_mem.get() == nullptr); |
| 92 | q.enqueueUnmapMemObject(_mem, _mapping); |
| 93 | _mapping = nullptr; |
| 94 | } |
| 95 | |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame^] | 96 | ICLSVMMemoryRegion::ICLSVMMemoryRegion(CLCoreRuntimeContext *ctx, cl_mem_flags flags, size_t size, size_t alignment) |
| 97 | : ICLMemoryRegion(ctx, size), _ptr(nullptr) |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 98 | { |
| 99 | if(size != 0) |
| 100 | { |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame^] | 101 | _ptr = clSVMAlloc((ctx != nullptr) ? ctx->context().get() : CLScheduler::get().context().get(), flags, size, alignment); |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 102 | if(_ptr != nullptr) |
| 103 | { |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame^] | 104 | _mem = cl::Buffer((ctx != nullptr) ? ctx->context() : 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] | 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | ICLSVMMemoryRegion::~ICLSVMMemoryRegion() |
| 110 | { |
| 111 | if(_ptr != nullptr) |
| 112 | { |
Georgios Pinitas | 5a1320b | 2019-06-27 17:50:43 +0100 | [diff] [blame] | 113 | try |
| 114 | { |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame^] | 115 | clFinish(_queue.get()); |
Georgios Pinitas | 5a1320b | 2019-06-27 17:50:43 +0100 | [diff] [blame] | 116 | _mem = cl::Buffer(); |
| 117 | clSVMFree(_ctx.get(), _ptr); |
| 118 | } |
| 119 | catch(...) |
| 120 | { |
| 121 | } |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | void *ICLSVMMemoryRegion::ptr() |
| 126 | { |
| 127 | return _ptr; |
| 128 | } |
| 129 | |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame^] | 130 | CLCoarseSVMMemoryRegion::CLCoarseSVMMemoryRegion(CLCoreRuntimeContext *ctx, cl_mem_flags flags, size_t size, size_t alignment) |
| 131 | : ICLSVMMemoryRegion(ctx, flags, size, alignment) |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 132 | { |
| 133 | } |
| 134 | |
| 135 | void *CLCoarseSVMMemoryRegion::map(cl::CommandQueue &q, bool blocking) |
| 136 | { |
| 137 | ARM_COMPUTE_ERROR_ON(_ptr == nullptr); |
| 138 | clEnqueueSVMMap(q.get(), blocking ? CL_TRUE : CL_FALSE, CL_MAP_READ | CL_MAP_WRITE, _ptr, _size, 0, nullptr, nullptr); |
| 139 | _mapping = _ptr; |
| 140 | return _mapping; |
| 141 | } |
| 142 | |
| 143 | void CLCoarseSVMMemoryRegion::unmap(cl::CommandQueue &q) |
| 144 | { |
| 145 | ARM_COMPUTE_ERROR_ON(_ptr == nullptr); |
| 146 | clEnqueueSVMUnmap(q.get(), _ptr, 0, nullptr, nullptr); |
| 147 | _mapping = nullptr; |
| 148 | } |
| 149 | |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame^] | 150 | CLFineSVMMemoryRegion::CLFineSVMMemoryRegion(CLCoreRuntimeContext *ctx, cl_mem_flags flags, size_t size, size_t alignment) |
| 151 | : ICLSVMMemoryRegion(ctx, flags, size, alignment) |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 152 | { |
| 153 | } |
| 154 | |
| 155 | void *CLFineSVMMemoryRegion::map(cl::CommandQueue &q, bool blocking) |
| 156 | { |
| 157 | if(blocking) |
| 158 | { |
| 159 | clFinish(q.get()); |
| 160 | } |
| 161 | _mapping = _ptr; |
| 162 | return _mapping; |
| 163 | } |
| 164 | |
| 165 | void CLFineSVMMemoryRegion::unmap(cl::CommandQueue &q) |
| 166 | { |
| 167 | ARM_COMPUTE_UNUSED(q); |
| 168 | _mapping = nullptr; |
| 169 | } |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame^] | 170 | } // namespace arm_compute |