Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 | #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 | { |
| 31 | ICLMemoryRegion::ICLMemoryRegion(cl::Context ctx, size_t size) |
| 32 | : IMemoryRegion(size), _ctx(std::move(ctx)), _mapping(nullptr), _mem() |
| 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 | |
| 46 | void *ICLMemoryRegion::buffer() const |
| 47 | { |
| 48 | return _mapping; |
| 49 | } |
| 50 | |
| 51 | void **ICLMemoryRegion::handle() |
| 52 | { |
| 53 | return reinterpret_cast<void **>(&_mem); |
| 54 | } |
| 55 | |
| 56 | CLBufferMemoryRegion::CLBufferMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size) |
| 57 | : ICLMemoryRegion(std::move(ctx), size) |
| 58 | { |
| 59 | if(_size != 0) |
| 60 | { |
| 61 | _mem = cl::Buffer(_ctx, flags, _size); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | void *CLBufferMemoryRegion::ptr() |
| 66 | { |
| 67 | return nullptr; |
| 68 | } |
| 69 | |
| 70 | void *CLBufferMemoryRegion::map(cl::CommandQueue &q, bool blocking) |
| 71 | { |
| 72 | ARM_COMPUTE_ERROR_ON(_mem.get() == nullptr); |
| 73 | _mapping = q.enqueueMapBuffer(_mem, blocking ? CL_TRUE : CL_FALSE, CL_MAP_READ | CL_MAP_WRITE, 0, _size); |
| 74 | return _mapping; |
| 75 | } |
| 76 | |
| 77 | void CLBufferMemoryRegion::unmap(cl::CommandQueue &q) |
| 78 | { |
| 79 | ARM_COMPUTE_ERROR_ON(_mem.get() == nullptr); |
| 80 | q.enqueueUnmapMemObject(_mem, _mapping); |
| 81 | _mapping = nullptr; |
| 82 | } |
| 83 | |
| 84 | ICLSVMMemoryRegion::ICLSVMMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size, size_t alignment) |
| 85 | : ICLMemoryRegion(std::move(ctx), size), _ptr(nullptr) |
| 86 | { |
| 87 | if(size != 0) |
| 88 | { |
| 89 | _ptr = clSVMAlloc(_ctx.get(), flags, size, alignment); |
| 90 | if(_ptr != nullptr) |
| 91 | { |
| 92 | _mem = cl::Buffer(_ctx, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, _size, _ptr); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | ICLSVMMemoryRegion::~ICLSVMMemoryRegion() |
| 98 | { |
| 99 | if(_ptr != nullptr) |
| 100 | { |
| 101 | clFinish(CLScheduler::get().queue().get()); |
| 102 | _mem = cl::Buffer(); |
| 103 | clSVMFree(_ctx.get(), _ptr); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void *ICLSVMMemoryRegion::ptr() |
| 108 | { |
| 109 | return _ptr; |
| 110 | } |
| 111 | |
| 112 | CLCoarseSVMMemoryRegion::CLCoarseSVMMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size, size_t alignment) |
| 113 | : ICLSVMMemoryRegion(std::move(ctx), flags, size, alignment) |
| 114 | { |
| 115 | } |
| 116 | |
| 117 | void *CLCoarseSVMMemoryRegion::map(cl::CommandQueue &q, bool blocking) |
| 118 | { |
| 119 | ARM_COMPUTE_ERROR_ON(_ptr == nullptr); |
| 120 | clEnqueueSVMMap(q.get(), blocking ? CL_TRUE : CL_FALSE, CL_MAP_READ | CL_MAP_WRITE, _ptr, _size, 0, nullptr, nullptr); |
| 121 | _mapping = _ptr; |
| 122 | return _mapping; |
| 123 | } |
| 124 | |
| 125 | void CLCoarseSVMMemoryRegion::unmap(cl::CommandQueue &q) |
| 126 | { |
| 127 | ARM_COMPUTE_ERROR_ON(_ptr == nullptr); |
| 128 | clEnqueueSVMUnmap(q.get(), _ptr, 0, nullptr, nullptr); |
| 129 | _mapping = nullptr; |
| 130 | } |
| 131 | |
| 132 | CLFineSVMMemoryRegion::CLFineSVMMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size, size_t alignment) |
| 133 | : ICLSVMMemoryRegion(std::move(ctx), flags, size, alignment) |
| 134 | { |
| 135 | } |
| 136 | |
| 137 | void *CLFineSVMMemoryRegion::map(cl::CommandQueue &q, bool blocking) |
| 138 | { |
| 139 | if(blocking) |
| 140 | { |
| 141 | clFinish(q.get()); |
| 142 | } |
| 143 | _mapping = _ptr; |
| 144 | return _mapping; |
| 145 | } |
| 146 | |
| 147 | void CLFineSVMMemoryRegion::unmap(cl::CommandQueue &q) |
| 148 | { |
| 149 | ARM_COMPUTE_UNUSED(q); |
| 150 | _mapping = nullptr; |
| 151 | } |
| 152 | } // namespace arm_compute |