diff options
Diffstat (limited to 'src/runtime/CL/CLMemoryRegion.cpp')
-rw-r--r-- | src/runtime/CL/CLMemoryRegion.cpp | 72 |
1 files changed, 43 insertions, 29 deletions
diff --git a/src/runtime/CL/CLMemoryRegion.cpp b/src/runtime/CL/CLMemoryRegion.cpp index 0952139a8b..c9ddf9b85c 100644 --- a/src/runtime/CL/CLMemoryRegion.cpp +++ b/src/runtime/CL/CLMemoryRegion.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020 Arm Limited. + * Copyright (c) 2018-2021, 2023 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -23,18 +23,15 @@ */ #include "arm_compute/runtime/CL/CLMemoryRegion.h" -#include "arm_compute/core/CL/CLCoreRuntimeContext.h" #include "arm_compute/core/Error.h" #include "arm_compute/runtime/CL/CLScheduler.h" +#include "src/common/utils/Log.h" + namespace arm_compute { -ICLMemoryRegion::ICLMemoryRegion(CLCoreRuntimeContext *ctx, size_t size) - : IMemoryRegion(size), - _queue((ctx != nullptr) ? ctx->queue() : CLScheduler::get().queue()), - _ctx((ctx != nullptr) ? ctx->context() : CLScheduler::get().context()), - _mapping(nullptr), - _mem() +ICLMemoryRegion::ICLMemoryRegion(size_t size) + : IMemoryRegion(size), _ctx(CLScheduler::get().context()), _mapping(nullptr), _mem() { } @@ -59,21 +56,34 @@ std::unique_ptr<IMemoryRegion> ICLMemoryRegion::extract_subregion(size_t offset, return nullptr; } -CLBufferMemoryRegion::CLBufferMemoryRegion(CLCoreRuntimeContext *ctx, cl_mem_flags flags, size_t size) - : ICLMemoryRegion(ctx, size) +CLBufferMemoryRegion::CLBufferMemoryRegion(cl_mem_flags flags, size_t size) : ICLMemoryRegion(size) { - if(_size != 0) + if (_size != 0) { - _mem = cl::Buffer((ctx != nullptr) ? ctx->context() : CLScheduler::get().context(), flags, _size); + _mem = cl::Buffer(CLScheduler::get().context(), flags, _size); } } -CLBufferMemoryRegion::CLBufferMemoryRegion(const cl::Buffer &buffer, CLCoreRuntimeContext *ctx) - : ICLMemoryRegion(ctx, buffer.getInfo<CL_MEM_SIZE>()) +CLBufferMemoryRegion::CLBufferMemoryRegion(const cl::Buffer &buffer) : ICLMemoryRegion(buffer.getInfo<CL_MEM_SIZE>()) { _mem = buffer; } +CLBufferMemoryRegion::~CLBufferMemoryRegion() +{ + // Flush the command queue to ensure all commands that may use this memory buffer are scheduled to be finished before + // this buffer is freed + // Do not call finish as it is a blocking call which affects the performance + try + { + CLScheduler::get().queue().flush(); + } + catch (const std::exception &e) + { + ARM_COMPUTE_LOG_ERROR_ACL(e.what()); + } +} + void *CLBufferMemoryRegion::ptr() { return nullptr; @@ -93,30 +103,33 @@ void CLBufferMemoryRegion::unmap(cl::CommandQueue &q) _mapping = nullptr; } -ICLSVMMemoryRegion::ICLSVMMemoryRegion(CLCoreRuntimeContext *ctx, cl_mem_flags flags, size_t size, size_t alignment) - : ICLMemoryRegion(ctx, size), _ptr(nullptr) +ICLSVMMemoryRegion::ICLSVMMemoryRegion(cl_mem_flags flags, size_t size, size_t alignment) + : ICLMemoryRegion(size), _ptr(nullptr) { - if(size != 0) + if (size != 0) { - _ptr = clSVMAlloc((ctx != nullptr) ? ctx->context().get() : CLScheduler::get().context().get(), flags, size, alignment); - if(_ptr != nullptr) + _ptr = clSVMAlloc(CLScheduler::get().context().get(), flags, size, alignment); + if (_ptr != nullptr) { - _mem = cl::Buffer((ctx != nullptr) ? ctx->context() : CLScheduler::get().context(), CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, _size, _ptr); + _mem = cl::Buffer(CLScheduler::get().context(), CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, _size, _ptr); } } } ICLSVMMemoryRegion::~ICLSVMMemoryRegion() { - if(_ptr != nullptr) + if (_ptr != nullptr) { try { - clFinish(_queue.get()); + // Can only use the blocking finish instead of the non-blocking flush here, because clSVMFree requires all + // commands that may use the svm pointer to finish beforehand + // https://registry.khronos.org/OpenCL/sdk/3.0/docs/man/html/clSVMFree.html + clFinish(CLScheduler::get().queue().get()); _mem = cl::Buffer(); clSVMFree(_ctx.get(), _ptr); } - catch(...) + catch (...) { } } @@ -127,15 +140,16 @@ void *ICLSVMMemoryRegion::ptr() return _ptr; } -CLCoarseSVMMemoryRegion::CLCoarseSVMMemoryRegion(CLCoreRuntimeContext *ctx, cl_mem_flags flags, size_t size, size_t alignment) - : ICLSVMMemoryRegion(ctx, flags, size, alignment) +CLCoarseSVMMemoryRegion::CLCoarseSVMMemoryRegion(cl_mem_flags flags, size_t size, size_t alignment) + : ICLSVMMemoryRegion(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); + clEnqueueSVMMap(q.get(), blocking ? CL_TRUE : CL_FALSE, CL_MAP_READ | CL_MAP_WRITE, _ptr, _size, 0, nullptr, + nullptr); _mapping = _ptr; return _mapping; } @@ -147,14 +161,14 @@ void CLCoarseSVMMemoryRegion::unmap(cl::CommandQueue &q) _mapping = nullptr; } -CLFineSVMMemoryRegion::CLFineSVMMemoryRegion(CLCoreRuntimeContext *ctx, cl_mem_flags flags, size_t size, size_t alignment) - : ICLSVMMemoryRegion(ctx, flags, size, alignment) +CLFineSVMMemoryRegion::CLFineSVMMemoryRegion(cl_mem_flags flags, size_t size, size_t alignment) + : ICLSVMMemoryRegion(flags, size, alignment) { } void *CLFineSVMMemoryRegion::map(cl::CommandQueue &q, bool blocking) { - if(blocking) + if (blocking) { clFinish(q.get()); } |