diff options
author | Georgios Pinitas <georgios.pinitas@arm.com> | 2018-11-14 13:16:56 +0000 |
---|---|---|
committer | Isabella Gottardi <isabella.gottardi@arm.com> | 2018-11-21 09:52:04 +0000 |
commit | df3103622b7de05f4e35b22a2c94b4a46eab4efc (patch) | |
tree | 17e10253e7a069c69d10bea0882b699b99d74b86 /src/runtime/CL | |
parent | c47ef20d69e8ea0f519fdc679435cd7037fc18fe (diff) | |
download | ComputeLibrary-df3103622b7de05f4e35b22a2c94b4a46eab4efc.tar.gz |
COMPMID-1088: Use IMemoryRegion in interfaces where possible
-Simplifies import memory interface
-Changes the used of void** handles with appropriate interfaces.
Change-Id: I5918c855c11f46352058864623336b352162a4b7
Diffstat (limited to 'src/runtime/CL')
-rw-r--r-- | src/runtime/CL/CLMemory.cpp | 34 | ||||
-rw-r--r-- | src/runtime/CL/CLMemoryRegion.cpp | 11 | ||||
-rw-r--r-- | src/runtime/CL/CLTensorAllocator.cpp | 73 |
3 files changed, 73 insertions, 45 deletions
diff --git a/src/runtime/CL/CLMemory.cpp b/src/runtime/CL/CLMemory.cpp index bbc513d783..5bea85cfae 100644 --- a/src/runtime/CL/CLMemory.cpp +++ b/src/runtime/CL/CLMemory.cpp @@ -24,23 +24,20 @@ #include "arm_compute/runtime/CL/CLMemory.h" #include "arm_compute/core/Error.h" +#include "arm_compute/core/utils/misc/Cast.h" namespace arm_compute { CLMemory::CLMemory() : _region(nullptr), _region_owned(nullptr) { - create_empty_region(); } CLMemory::CLMemory(std::shared_ptr<ICLMemoryRegion> memory) : _region(nullptr), _region_owned(std::move(memory)) { - if(_region_owned == nullptr) - { - create_empty_region(); - } - _region = _region_owned.get(); + _region_owned = memory; + _region = _region_owned.get(); } CLMemory::CLMemory(ICLMemoryRegion *memory) @@ -49,19 +46,36 @@ CLMemory::CLMemory(ICLMemoryRegion *memory) _region = memory; } -ICLMemoryRegion *CLMemory::region() +ICLMemoryRegion *CLMemory::cl_region() +{ + return _region; +} + +ICLMemoryRegion *CLMemory::cl_region() const +{ + return _region; +} + +IMemoryRegion *CLMemory::region() { return _region; } -ICLMemoryRegion *CLMemory::region() const +IMemoryRegion *CLMemory::region() const { return _region; } -void CLMemory::create_empty_region() +void CLMemory::set_region(IMemoryRegion *region) +{ + auto cl_region = utils::cast::polymorphic_downcast<ICLMemoryRegion *>(region); + _region_owned = nullptr; + _region = cl_region; +} + +void CLMemory::set_owned_region(std::unique_ptr<IMemoryRegion> region) { - _region_owned = std::make_shared<CLBufferMemoryRegion>(cl::Context(), CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, 0); + _region_owned = utils::cast::polymorphic_downcast_unique_ptr<ICLMemoryRegion>(std::move(region)); _region = _region_owned.get(); } } // namespace arm_compute
\ No newline at end of file diff --git a/src/runtime/CL/CLMemoryRegion.cpp b/src/runtime/CL/CLMemoryRegion.cpp index 15fd7f333e..9578d73934 100644 --- a/src/runtime/CL/CLMemoryRegion.cpp +++ b/src/runtime/CL/CLMemoryRegion.cpp @@ -48,9 +48,10 @@ void *ICLMemoryRegion::buffer() const return _mapping; } -void **ICLMemoryRegion::handle() +std::unique_ptr<IMemoryRegion> ICLMemoryRegion::extract_subregion(size_t offset, size_t size) { - return reinterpret_cast<void **>(&_mem); + ARM_COMPUTE_UNUSED(offset, size); + return nullptr; } CLBufferMemoryRegion::CLBufferMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size) @@ -62,6 +63,12 @@ CLBufferMemoryRegion::CLBufferMemoryRegion(cl::Context ctx, cl_mem_flags flags, } } +CLBufferMemoryRegion::CLBufferMemoryRegion(const cl::Buffer &buffer) + : ICLMemoryRegion(buffer.getInfo<CL_MEM_CONTEXT>(), buffer.getInfo<CL_MEM_SIZE>()) +{ + _mem = buffer; +} + void *CLBufferMemoryRegion::ptr() { return nullptr; diff --git a/src/runtime/CL/CLTensorAllocator.cpp b/src/runtime/CL/CLTensorAllocator.cpp index dd716f77ff..0307498335 100644 --- a/src/runtime/CL/CLTensorAllocator.cpp +++ b/src/runtime/CL/CLTensorAllocator.cpp @@ -28,86 +28,87 @@ #include "arm_compute/runtime/CL/CLMemoryGroup.h" #include "arm_compute/runtime/CL/CLScheduler.h" -using namespace arm_compute; +namespace arm_compute +{ +const cl::Buffer CLTensorAllocator::_empty_buffer = cl::Buffer(); namespace { -std::shared_ptr<arm_compute::ICLMemoryRegion> allocate_region(cl::Context context, size_t size, cl_uint alignment) +std::unique_ptr<ICLMemoryRegion> allocate_region(cl::Context context, size_t size, cl_uint alignment) { // Try fine-grain SVM - std::shared_ptr<ICLMemoryRegion> region = std::make_shared<CLFineSVMMemoryRegion>(context, CL_MEM_READ_WRITE | CL_MEM_SVM_FINE_GRAIN_BUFFER, size, alignment); + std::unique_ptr<ICLMemoryRegion> region = support::cpp14::make_unique<CLFineSVMMemoryRegion>(context, + CL_MEM_READ_WRITE | CL_MEM_SVM_FINE_GRAIN_BUFFER, + size, + alignment); // Try coarse-grain SVM in case of failure if(region != nullptr && region->ptr() == nullptr) { - region = std::make_shared<CLCoarseSVMMemoryRegion>(context, CL_MEM_READ_WRITE, size, alignment); + region = support::cpp14::make_unique<CLCoarseSVMMemoryRegion>(context, CL_MEM_READ_WRITE, size, alignment); } // Try legacy buffer memory in case of failure if(region != nullptr && region->ptr() == nullptr) { - region = std::make_shared<CLBufferMemoryRegion>(context, CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, size); + region = support::cpp14::make_unique<CLBufferMemoryRegion>(context, CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, size); } return region; } } // namespace CLTensorAllocator::CLTensorAllocator(CLTensor *owner) - : _associated_memory_group(nullptr), _memory(), _owner(owner) + : _associated_memory_group(nullptr), _memory(), _mapping(nullptr), _owner(owner) { } uint8_t *CLTensorAllocator::data() { - ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr); - return reinterpret_cast<uint8_t *>(_memory.region()->buffer()); + return _mapping; } const cl::Buffer &CLTensorAllocator::cl_data() const { - ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr); - return _memory.region()->cl_data(); + return _memory.region() == nullptr ? _empty_buffer : _memory.cl_region()->cl_data(); } void CLTensorAllocator::allocate() { - ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr); - if(_associated_memory_group == nullptr) { - if(_memory.region()->cl_data().get() != nullptr) + if(_memory.region() != nullptr && _memory.cl_region()->cl_data().get() != nullptr) { // Memory is already allocated. Reuse it if big enough, otherwise fire an assertion - ARM_COMPUTE_ERROR_ON_MSG(info().total_size() > _memory.region()->size(), "Reallocation of a bigger memory region is not allowed!"); + ARM_COMPUTE_ERROR_ON_MSG(info().total_size() > _memory.region()->size(), + "Reallocation of a bigger memory region is not allowed!"); } else { // Perform memory allocation - _memory = CLMemory(allocate_region(CLScheduler::get().context(), info().total_size(), 0)); + _memory.set_owned_region(allocate_region(CLScheduler::get().context(), info().total_size(), 0)); } } else { - _associated_memory_group->finalize_memory(_owner, _memory.region()->handle(), info().total_size()); - _memory.region()->set_size(info().total_size()); + _associated_memory_group->finalize_memory(_owner, _memory, info().total_size()); } info().set_is_resizable(false); } void CLTensorAllocator::free() { - if(_associated_memory_group == nullptr) - { - _memory = CLMemory(); - info().set_is_resizable(true); - } + _mapping = nullptr; + _memory.set_region(nullptr); + info().set_is_resizable(true); } -arm_compute::Status CLTensorAllocator::import_memory(CLMemory memory) +arm_compute::Status CLTensorAllocator::import_memory(cl::Buffer buffer) { - ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr); - ARM_COMPUTE_RETURN_ERROR_ON(memory.region()->cl_data().get() == nullptr); + ARM_COMPUTE_RETURN_ERROR_ON(buffer.get() == nullptr); + ARM_COMPUTE_RETURN_ERROR_ON(buffer.getInfo<CL_MEM_SIZE>() == 0); + ARM_COMPUTE_RETURN_ERROR_ON(buffer.getInfo<CL_MEM_CONTEXT>().get() != CLScheduler::get().context().get()); ARM_COMPUTE_RETURN_ERROR_ON(_associated_memory_group != nullptr); - _memory = memory; + + _memory.set_owned_region(support::cpp14::make_unique<CLBufferMemoryRegion>(buffer)); info().set_is_resizable(false); return Status{}; @@ -115,11 +116,10 @@ arm_compute::Status CLTensorAllocator::import_memory(CLMemory memory) void CLTensorAllocator::set_associated_memory_group(CLMemoryGroup *associated_memory_group) { - ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr); ARM_COMPUTE_ERROR_ON(associated_memory_group == nullptr); ARM_COMPUTE_ERROR_ON(_associated_memory_group != nullptr); - ARM_COMPUTE_ERROR_ON(_memory.region()->cl_data().get() != nullptr); - _memory = CLMemory(std::make_shared<CLBufferMemoryRegion>(CLScheduler::get().context(), CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, 0)); + ARM_COMPUTE_ERROR_ON(_memory.region() != nullptr && _memory.cl_region()->cl_data().get() != nullptr); + _associated_memory_group = associated_memory_group; } @@ -136,16 +136,23 @@ void CLTensorAllocator::unlock() uint8_t *CLTensorAllocator::map(cl::CommandQueue &q, bool blocking) { + ARM_COMPUTE_ERROR_ON(_mapping != nullptr); ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr); ARM_COMPUTE_ERROR_ON(_memory.region()->buffer() != nullptr); - _memory.region()->map(q, blocking); - return reinterpret_cast<uint8_t *>(_memory.region()->buffer()); + + _mapping = reinterpret_cast<uint8_t *>(_memory.cl_region()->map(q, blocking)); + return _mapping; } void CLTensorAllocator::unmap(cl::CommandQueue &q, uint8_t *mapping) { - ARM_COMPUTE_UNUSED(mapping); + ARM_COMPUTE_ERROR_ON(_mapping == nullptr); + ARM_COMPUTE_ERROR_ON(_mapping != mapping); ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr); ARM_COMPUTE_ERROR_ON(_memory.region()->buffer() == nullptr); - _memory.region()->unmap(q); + ARM_COMPUTE_UNUSED(mapping); + + _memory.cl_region()->unmap(q); + _mapping = nullptr; } +} // namespace arm_compute |