From 99d40951df87790fb884ce1c42d5e2a7a0009ee0 Mon Sep 17 00:00:00 2001 From: Georgios Pinitas Date: Mon, 23 Apr 2018 16:26:46 +0100 Subject: COMPMID-1023: Import memory for OpenCL Change-Id: I201bc00a1261814737e6b6878ecfe9904bae0cc1 Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/128212 Tested-by: Jenkins Reviewed-by: Anthony Barbier --- src/runtime/CL/CLMemoryRegion.cpp | 152 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 src/runtime/CL/CLMemoryRegion.cpp (limited to 'src/runtime/CL/CLMemoryRegion.cpp') diff --git a/src/runtime/CL/CLMemoryRegion.cpp b/src/runtime/CL/CLMemoryRegion.cpp new file mode 100644 index 0000000000..15fd7f333e --- /dev/null +++ b/src/runtime/CL/CLMemoryRegion.cpp @@ -0,0 +1,152 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/runtime/CL/CLMemoryRegion.h" + +#include "arm_compute/core/Error.h" +#include "arm_compute/runtime/CL/CLScheduler.h" + +namespace arm_compute +{ +ICLMemoryRegion::ICLMemoryRegion(cl::Context ctx, size_t size) + : IMemoryRegion(size), _ctx(std::move(ctx)), _mapping(nullptr), _mem() +{ +} + +const cl::Buffer &ICLMemoryRegion::cl_data() const +{ + return _mem; +} + +void *ICLMemoryRegion::buffer() +{ + return _mapping; +} + +void *ICLMemoryRegion::buffer() const +{ + return _mapping; +} + +void **ICLMemoryRegion::handle() +{ + return reinterpret_cast(&_mem); +} + +CLBufferMemoryRegion::CLBufferMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size) + : ICLMemoryRegion(std::move(ctx), size) +{ + if(_size != 0) + { + _mem = cl::Buffer(_ctx, flags, _size); + } +} + +void *CLBufferMemoryRegion::ptr() +{ + return nullptr; +} + +void *CLBufferMemoryRegion::map(cl::CommandQueue &q, bool blocking) +{ + ARM_COMPUTE_ERROR_ON(_mem.get() == nullptr); + _mapping = q.enqueueMapBuffer(_mem, blocking ? CL_TRUE : CL_FALSE, CL_MAP_READ | CL_MAP_WRITE, 0, _size); + return _mapping; +} + +void CLBufferMemoryRegion::unmap(cl::CommandQueue &q) +{ + ARM_COMPUTE_ERROR_ON(_mem.get() == nullptr); + q.enqueueUnmapMemObject(_mem, _mapping); + _mapping = nullptr; +} + +ICLSVMMemoryRegion::ICLSVMMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size, size_t alignment) + : ICLMemoryRegion(std::move(ctx), size), _ptr(nullptr) +{ + if(size != 0) + { + _ptr = clSVMAlloc(_ctx.get(), flags, size, alignment); + if(_ptr != nullptr) + { + _mem = cl::Buffer(_ctx, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, _size, _ptr); + } + } +} + +ICLSVMMemoryRegion::~ICLSVMMemoryRegion() +{ + if(_ptr != nullptr) + { + clFinish(CLScheduler::get().queue().get()); + _mem = cl::Buffer(); + clSVMFree(_ctx.get(), _ptr); + } +} + +void *ICLSVMMemoryRegion::ptr() +{ + return _ptr; +} + +CLCoarseSVMMemoryRegion::CLCoarseSVMMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size, size_t alignment) + : ICLSVMMemoryRegion(std::move(ctx), flags, size, alignment) +{ +} + +void *CLCoarseSVMMemoryRegion::map(cl::CommandQueue &q, bool blocking) +{ + ARM_COMPUTE_ERROR_ON(_ptr == nullptr); + clEnqueueSVMMap(q.get(), blocking ? CL_TRUE : CL_FALSE, CL_MAP_READ | CL_MAP_WRITE, _ptr, _size, 0, nullptr, nullptr); + _mapping = _ptr; + return _mapping; +} + +void CLCoarseSVMMemoryRegion::unmap(cl::CommandQueue &q) +{ + ARM_COMPUTE_ERROR_ON(_ptr == nullptr); + clEnqueueSVMUnmap(q.get(), _ptr, 0, nullptr, nullptr); + _mapping = nullptr; +} + +CLFineSVMMemoryRegion::CLFineSVMMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size, size_t alignment) + : ICLSVMMemoryRegion(std::move(ctx), flags, size, alignment) +{ +} + +void *CLFineSVMMemoryRegion::map(cl::CommandQueue &q, bool blocking) +{ + if(blocking) + { + clFinish(q.get()); + } + _mapping = _ptr; + return _mapping; +} + +void CLFineSVMMemoryRegion::unmap(cl::CommandQueue &q) +{ + ARM_COMPUTE_UNUSED(q); + _mapping = nullptr; +} +} // namespace arm_compute \ No newline at end of file -- cgit v1.2.1