aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGeorgios Pinitas <georgios.pinitas@arm.com>2018-04-23 16:26:46 +0100
committerAnthony Barbier <anthony.barbier@arm.com>2018-11-02 16:50:15 +0000
commit99d40951df87790fb884ce1c42d5e2a7a0009ee0 (patch)
tree774e2c6d0849a7fb3f13c7cc4773f7cbfea448fb /src
parentcda0c38373b2f114509392ba16ef04e8c1e0f819 (diff)
downloadComputeLibrary-99d40951df87790fb884ce1c42d5e2a7a0009ee0.tar.gz
COMPMID-1023: Import memory for OpenCL
Change-Id: I201bc00a1261814737e6b6878ecfe9904bae0cc1 Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/128212 Tested-by: Jenkins <bsgcomp@arm.com> Reviewed-by: Anthony Barbier <anthony.barbier@arm.com>
Diffstat (limited to 'src')
-rw-r--r--src/runtime/Allocator.cpp10
-rw-r--r--src/runtime/CL/CLBufferAllocator.cpp10
-rw-r--r--src/runtime/CL/CLMemory.cpp67
-rw-r--r--src/runtime/CL/CLMemoryRegion.cpp152
-rw-r--r--src/runtime/CL/CLTensor.cpp4
-rw-r--r--src/runtime/CL/CLTensorAllocator.cpp147
-rw-r--r--src/runtime/GLES_COMPUTE/GCBufferAllocator.cpp6
-rw-r--r--src/runtime/Memory.cpp45
-rw-r--r--src/runtime/TensorAllocator.cpp29
9 files changed, 343 insertions, 127 deletions
diff --git a/src/runtime/Allocator.cpp b/src/runtime/Allocator.cpp
index 50b0f0e6bb..7f0e37495e 100644
--- a/src/runtime/Allocator.cpp
+++ b/src/runtime/Allocator.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2017 ARM Limited.
+ * Copyright (c) 2017-2018 ARM Limited.
*
* SPDX-License-Identifier: MIT
*
@@ -22,8 +22,10 @@
* SOFTWARE.
*/
#include "arm_compute/runtime/Allocator.h"
+#include "arm_compute/runtime/MemoryRegion.h"
#include "arm_compute/core/Error.h"
+#include "support/ToolchainSupport.h"
#include <cstddef>
@@ -39,3 +41,9 @@ void Allocator::free(void *ptr)
{
::operator delete(ptr);
}
+
+std::unique_ptr<IMemoryRegion> Allocator::make_region(size_t size, size_t alignment)
+{
+ ARM_COMPUTE_UNUSED(alignment);
+ return arm_compute::support::cpp14::make_unique<MemoryRegion>(size);
+} \ No newline at end of file
diff --git a/src/runtime/CL/CLBufferAllocator.cpp b/src/runtime/CL/CLBufferAllocator.cpp
index 9a5c13ac5a..84789e70d2 100644
--- a/src/runtime/CL/CLBufferAllocator.cpp
+++ b/src/runtime/CL/CLBufferAllocator.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2017 ARM Limited.
+ * Copyright (c) 2017-2018 ARM Limited.
*
* SPDX-License-Identifier: MIT
*
@@ -22,9 +22,11 @@
* SOFTWARE.
*/
#include "arm_compute/runtime/CL/CLBufferAllocator.h"
+#include "arm_compute/runtime/CL/CLMemoryRegion.h"
#include "arm_compute/core/CL/OpenCL.h"
#include "arm_compute/core/Error.h"
+#include "support/ToolchainSupport.h"
#include <cstddef>
@@ -47,3 +49,9 @@ void CLBufferAllocator::free(void *ptr)
ARM_COMPUTE_ERROR_ON(ptr == nullptr);
clReleaseMemObject(static_cast<cl_mem>(ptr));
}
+
+std::unique_ptr<IMemoryRegion> CLBufferAllocator::make_region(size_t size, size_t alignment)
+{
+ ARM_COMPUTE_UNUSED(alignment);
+ return arm_compute::support::cpp14::make_unique<CLBufferMemoryRegion>(_context, CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, size);
+}
diff --git a/src/runtime/CL/CLMemory.cpp b/src/runtime/CL/CLMemory.cpp
new file mode 100644
index 0000000000..534c4f9e34
--- /dev/null
+++ b/src/runtime/CL/CLMemory.cpp
@@ -0,0 +1,67 @@
+/*
+ * 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/CLMemory.h"
+
+#include "arm_compute/core/Error.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();
+}
+
+CLMemory::CLMemory(ICLMemoryRegion *memory)
+ : _region(memory), _region_owned(nullptr)
+{
+ _region = memory;
+}
+
+ICLMemoryRegion *CLMemory::region()
+{
+ return _region;
+}
+
+ICLMemoryRegion *CLMemory::region() const
+{
+ return _region;
+}
+
+void CLMemory::create_empty_region()
+{
+ _region_owned = std::make_shared<CLBufferMemoryRegion>(cl::Context::getDefault(), CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, 0);
+ _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
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<void **>(&_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
diff --git a/src/runtime/CL/CLTensor.cpp b/src/runtime/CL/CLTensor.cpp
index bc513d139b..dd277384c7 100644
--- a/src/runtime/CL/CLTensor.cpp
+++ b/src/runtime/CL/CLTensor.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2016, 2017 ARM Limited.
+ * Copyright (c) 2016-2018 ARM Limited.
*
* SPDX-License-Identifier: MIT
*
@@ -47,7 +47,7 @@ const cl::Buffer &CLTensor::cl_buffer() const
return _allocator.cl_data();
}
-ITensorAllocator *CLTensor::allocator()
+CLTensorAllocator *CLTensor::allocator()
{
return &_allocator;
}
diff --git a/src/runtime/CL/CLTensorAllocator.cpp b/src/runtime/CL/CLTensorAllocator.cpp
index c5524b1ccb..54e7c5b336 100644
--- a/src/runtime/CL/CLTensorAllocator.cpp
+++ b/src/runtime/CL/CLTensorAllocator.cpp
@@ -30,67 +30,57 @@
using namespace arm_compute;
-CLTensorAllocator::CLTensorAllocator(CLTensor *owner)
- : _associated_memory_group(nullptr), _buffer(), _mapping(nullptr), _owner(owner), _svm_memory()
+namespace
+{
+std::shared_ptr<arm_compute::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);
+
+ // 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);
+ }
+ // 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);
+ }
+ return region;
}
+} // namespace
-CLTensorAllocator::~CLTensorAllocator()
+CLTensorAllocator::CLTensorAllocator(CLTensor *owner)
+ : _associated_memory_group(nullptr), _memory(), _owner(owner)
{
- _buffer = cl::Buffer();
}
uint8_t *CLTensorAllocator::data()
{
- return _mapping;
+ ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
+ return reinterpret_cast<uint8_t *>(_memory.region()->buffer());
}
const cl::Buffer &CLTensorAllocator::cl_data() const
{
- return _buffer;
-}
-
-void *SVMMemory::allocate(cl_context context, size_t size, cl_svm_mem_flags flags, cl_uint alignment)
-{
- ARM_COMPUTE_ERROR_ON_NULLPTR(context);
- ARM_COMPUTE_ERROR_ON(size == 0);
- ARM_COMPUTE_ERROR_ON(_ptr != nullptr);
- ARM_COMPUTE_ERROR_ON(size > CL_DEVICE_MAX_MEM_ALLOC_SIZE);
- _ptr = clSVMAlloc(context, flags, size, alignment);
- if(_ptr != nullptr)
- {
- _size = size;
- _fine_grain = static_cast<bool>(flags & CL_MEM_SVM_FINE_GRAIN_BUFFER);
- }
- return _ptr;
-}
-void *CLTensorAllocator::svm_ptr()
-{
- return _svm_memory.ptr();
+ ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
+ return _memory.region()->cl_data();
}
void CLTensorAllocator::allocate()
{
+ ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
+
if(_associated_memory_group == nullptr)
{
- ARM_COMPUTE_ERROR_ON(_buffer.get() != nullptr);
- if(_svm_memory.allocate(CLScheduler::get().context()(), CL_MEM_READ_WRITE | CL_MEM_SVM_FINE_GRAIN_BUFFER, info().total_size(), 0) == nullptr)
- {
- // try at coarse grain svm memory
- _svm_memory.allocate(CLScheduler::get().context()(), CL_MEM_READ_WRITE, info().total_size(), 0);
- }
- if(_svm_memory.ptr() != nullptr)
- {
- _buffer = cl::Buffer(CLScheduler::get().context(), CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, info().total_size(), _svm_memory.ptr());
- }
- else
- {
- _buffer = cl::Buffer(CLScheduler::get().context(), CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, info().total_size());
- }
+ ARM_COMPUTE_ERROR_ON(_memory.region()->cl_data().get() != nullptr);
+ _memory = CLMemory(allocate_region(CLScheduler::get().context(), info().total_size(), 0));
}
else
{
- _associated_memory_group->finalize_memory(_owner, reinterpret_cast<void **>(&_buffer()), info().total_size());
+ _associated_memory_group->finalize_memory(_owner, _memory.region()->handle(), info().total_size());
+ _memory.region()->set_size(info().total_size());
}
info().set_is_resizable(false);
}
@@ -99,80 +89,55 @@ void CLTensorAllocator::free()
{
if(_associated_memory_group == nullptr)
{
- _buffer = cl::Buffer();
- if(_svm_memory.ptr() != nullptr)
- {
- clSVMFree(CLScheduler::get().context()(), _svm_memory.ptr());
- }
+ _memory = CLMemory();
info().set_is_resizable(true);
}
}
+arm_compute::Status CLTensorAllocator::import_memory(CLMemory memory)
+{
+ ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
+ ARM_COMPUTE_RETURN_ERROR_ON(memory.region()->cl_data().get() == nullptr);
+ ARM_COMPUTE_RETURN_ERROR_ON(_associated_memory_group != nullptr);
+ _memory = memory;
+ info().set_is_resizable(false);
+
+ return Status{};
+}
+
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(_buffer.get() != 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));
_associated_memory_group = associated_memory_group;
}
uint8_t *CLTensorAllocator::lock()
{
- ARM_COMPUTE_ERROR_ON(_mapping != nullptr);
- _mapping = map(CLScheduler::get().queue(), true);
- return _mapping;
+ return map(CLScheduler::get().queue(), true);
}
void CLTensorAllocator::unlock()
{
- ARM_COMPUTE_ERROR_ON(_mapping == nullptr);
- unmap(CLScheduler::get().queue(), _mapping);
- _mapping = nullptr;
+ ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
+ unmap(CLScheduler::get().queue(), reinterpret_cast<uint8_t *>(_memory.region()->buffer()));
}
uint8_t *CLTensorAllocator::map(cl::CommandQueue &q, bool blocking)
{
- const bool svm_mem = _svm_memory.ptr() != nullptr;
- const bool fine_grain_svm = _svm_memory.fine_grain();
- if(!svm_mem)
- {
- ARM_COMPUTE_ERROR_ON(_buffer.get() == nullptr);
- return static_cast<uint8_t *>(q.enqueueMapBuffer(_buffer, blocking ? CL_TRUE : CL_FALSE, CL_MAP_READ | CL_MAP_WRITE, 0, info().total_size()));
- }
- else if(!fine_grain_svm)
- {
- const cl_int ret = clEnqueueSVMMap(q(), blocking ? CL_TRUE : CL_FALSE, CL_MAP_READ | CL_MAP_WRITE, _svm_memory.ptr(), _svm_memory.size(), 0, nullptr, nullptr);
- ARM_COMPUTE_ERROR_ON(ret != CL_SUCCESS);
- if(ret == CL_SUCCESS)
- {
- return reinterpret_cast<uint8_t *>(_svm_memory.ptr());
- }
- else
- {
- return nullptr;
- }
- }
- else
- {
- if(blocking)
- {
- clFinish(q());
- }
- return reinterpret_cast<uint8_t *>(_svm_memory.ptr());
- }
+ 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());
}
void CLTensorAllocator::unmap(cl::CommandQueue &q, uint8_t *mapping)
{
- const bool svm_mem = _svm_memory.ptr() != nullptr;
- const bool fine_grain_svm = _svm_memory.fine_grain();
- if(!svm_mem)
- {
- ARM_COMPUTE_ERROR_ON(_buffer.get() == nullptr);
- q.enqueueUnmapMemObject(_buffer, mapping);
- }
- else if(!fine_grain_svm)
- {
- clEnqueueSVMUnmap(q(), _svm_memory.ptr(), 0, nullptr, nullptr);
- }
+ ARM_COMPUTE_UNUSED(mapping);
+ ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
+ ARM_COMPUTE_ERROR_ON(_memory.region()->buffer() == nullptr);
+ _memory.region()->unmap(q);
}
diff --git a/src/runtime/GLES_COMPUTE/GCBufferAllocator.cpp b/src/runtime/GLES_COMPUTE/GCBufferAllocator.cpp
index d8f6867634..cdd12c3ad5 100644
--- a/src/runtime/GLES_COMPUTE/GCBufferAllocator.cpp
+++ b/src/runtime/GLES_COMPUTE/GCBufferAllocator.cpp
@@ -48,4 +48,10 @@ void GCBufferAllocator::free(void *ptr)
auto *gl_buffer = reinterpret_cast<GLBufferWrapper *>(ptr);
delete gl_buffer;
}
+
+std::unique_ptr<IMemoryRegion> GCBufferAllocator::make_region(size_t size, size_t alignment)
+{
+ ARM_COMPUTE_UNUSED(size, alignment);
+ return nullptr;
+}
} // namespace arm_compute
diff --git a/src/runtime/Memory.cpp b/src/runtime/Memory.cpp
index 35d0c824bb..15bbb17675 100644
--- a/src/runtime/Memory.cpp
+++ b/src/runtime/Memory.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2017 ARM Limited.
+ * Copyright (c) 2017-2018 ARM Limited.
*
* SPDX-License-Identifier: MIT
*
@@ -23,40 +23,45 @@
*/
#include "arm_compute/runtime/Memory.h"
-#include "arm_compute/core/Error.h"
-
-using namespace arm_compute;
+#include "arm_compute/runtime/MemoryRegion.h"
+namespace arm_compute
+{
Memory::Memory()
- : _memory(nullptr), _memory_owned(nullptr)
+ : _region(nullptr), _region_owned(nullptr)
{
+ create_empty_region();
}
-Memory::Memory(std::shared_ptr<uint8_t> memory)
- : _memory(nullptr), _memory_owned(std::move(memory))
+Memory::Memory(std::shared_ptr<IMemoryRegion> memory)
+ : _region(nullptr), _region_owned(std::move(memory))
{
- ARM_COMPUTE_ERROR_ON(_memory_owned.get() == nullptr);
- _memory = _memory_owned.get();
+ if(_region_owned == nullptr)
+ {
+ create_empty_region();
+ }
+ _region = _region_owned.get();
}
-Memory::Memory(uint8_t *memory)
- : _memory(memory), _memory_owned(nullptr)
+Memory::Memory(IMemoryRegion *memory)
+ : _region(memory), _region_owned(nullptr)
{
- ARM_COMPUTE_ERROR_ON(memory == nullptr);
+ _region = memory;
}
-uint8_t *Memory::buffer()
+IMemoryRegion *Memory::region()
{
- return _memory;
+ return _region;
}
-uint8_t *Memory::buffer() const
+IMemoryRegion *Memory::region() const
{
- return _memory;
+ return _region;
}
-uint8_t **Memory::handle()
+void Memory::create_empty_region()
{
- ARM_COMPUTE_ERROR_ON(_memory_owned.get() != nullptr);
- return &_memory;
-} \ No newline at end of file
+ _region_owned = std::make_shared<MemoryRegion>(0);
+ _region = _region_owned.get();
+}
+} // namespace arm_compute
diff --git a/src/runtime/TensorAllocator.cpp b/src/runtime/TensorAllocator.cpp
index a0d41b28ee..993a95b6c3 100644
--- a/src/runtime/TensorAllocator.cpp
+++ b/src/runtime/TensorAllocator.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2016, 2017 ARM Limited.
+ * Copyright (c) 2016-2018 ARM Limited.
*
* SPDX-License-Identifier: MIT
*
@@ -27,6 +27,7 @@
#include "arm_compute/core/Error.h"
#include "arm_compute/core/TensorInfo.h"
#include "arm_compute/runtime/MemoryGroup.h"
+#include "arm_compute/runtime/MemoryRegion.h"
#include "support/ToolchainSupport.h"
#include <cstddef>
@@ -114,7 +115,7 @@ void TensorAllocator::init(const TensorAllocator &allocator, const Coordinates &
ARM_COMPUTE_UNUSED(validate_subtensor_shape);
// Copy pointer to buffer
- _memory = Memory(allocator._memory.buffer());
+ _memory = Memory(allocator._memory.region());
// Init tensor info with new dimensions
size_t total_size = parent_info.offset_element_in_bytes(coords) + sub_info.total_size() - sub_info.offset_first_element_in_bytes();
@@ -126,22 +127,23 @@ void TensorAllocator::init(const TensorAllocator &allocator, const Coordinates &
uint8_t *TensorAllocator::data() const
{
- return _memory.buffer();
+ ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
+ return reinterpret_cast<uint8_t *>(_memory.region()->buffer());
}
void TensorAllocator::allocate()
{
- ARM_COMPUTE_ERROR_ON(_memory.buffer() != nullptr);
+ ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
+ ARM_COMPUTE_ERROR_ON(_memory.region()->buffer() != nullptr);
+
if(_associated_memory_group == nullptr)
{
- _memory = Memory(std::shared_ptr<uint8_t>(new uint8_t[info().total_size()](), [](uint8_t *ptr)
- {
- delete[] ptr;
- }));
+ _memory = Memory(std::make_shared<MemoryRegion>(info().total_size()));
}
else
{
- _associated_memory_group->finalize_memory(_owner, reinterpret_cast<void **>(_memory.handle()), info().total_size());
+ _associated_memory_group->finalize_memory(_owner, reinterpret_cast<void **>(_memory.region()->handle()), info().total_size());
+ _memory.region()->set_size(info().total_size());
}
info().set_is_resizable(false);
}
@@ -154,7 +156,8 @@ void TensorAllocator::free()
arm_compute::Status TensorAllocator::import_memory(Memory memory)
{
- ARM_COMPUTE_RETURN_ERROR_ON(memory.buffer() == nullptr);
+ ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
+ ARM_COMPUTE_RETURN_ERROR_ON(memory.region()->buffer() == nullptr);
ARM_COMPUTE_RETURN_ERROR_ON(_associated_memory_group != nullptr);
_memory = memory;
info().set_is_resizable(false);
@@ -164,15 +167,17 @@ arm_compute::Status TensorAllocator::import_memory(Memory memory)
void TensorAllocator::set_associated_memory_group(MemoryGroup *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.buffer() != nullptr);
+ ARM_COMPUTE_ERROR_ON(_memory.region()->buffer() != nullptr);
_associated_memory_group = associated_memory_group;
}
uint8_t *TensorAllocator::lock()
{
- return _memory.buffer();
+ ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
+ return reinterpret_cast<uint8_t *>(_memory.region()->buffer());
}
void TensorAllocator::unlock()