From df3103622b7de05f4e35b22a2c94b4a46eab4efc Mon Sep 17 00:00:00 2001 From: Georgios Pinitas Date: Wed, 14 Nov 2018 13:16:56 +0000 Subject: COMPMID-1088: Use IMemoryRegion in interfaces where possible -Simplifies import memory interface -Changes the used of void** handles with appropriate interfaces. Change-Id: I5918c855c11f46352058864623336b352162a4b7 --- arm_compute/runtime/BlobMemoryPool.h | 9 +- arm_compute/runtime/CL/CLMemory.h | 20 ++-- arm_compute/runtime/CL/CLMemoryRegion.h | 25 +++-- arm_compute/runtime/CL/CLTensorAllocator.h | 14 +-- arm_compute/runtime/GLES_COMPUTE/GCMemory.h | 85 ++++++++++++++++ arm_compute/runtime/GLES_COMPUTE/GCMemoryRegion.h | 108 +++++++++++++++++++++ .../runtime/GLES_COMPUTE/GCTensorAllocator.h | 28 ++---- arm_compute/runtime/ILifetimeManager.h | 12 ++- arm_compute/runtime/IMemory.h | 63 ++++++++++++ arm_compute/runtime/IMemoryRegion.h | 23 +++-- arm_compute/runtime/ISimpleLifetimeManager.h | 12 +-- arm_compute/runtime/Memory.h | 24 ++--- arm_compute/runtime/MemoryGroupBase.h | 18 ++-- arm_compute/runtime/MemoryRegion.h | 34 +++++-- arm_compute/runtime/OffsetMemoryPool.h | 11 ++- arm_compute/runtime/TensorAllocator.h | 10 +- arm_compute/runtime/Types.h | 6 +- 17 files changed, 387 insertions(+), 115 deletions(-) create mode 100644 arm_compute/runtime/GLES_COMPUTE/GCMemory.h create mode 100644 arm_compute/runtime/GLES_COMPUTE/GCMemoryRegion.h create mode 100644 arm_compute/runtime/IMemory.h (limited to 'arm_compute/runtime') diff --git a/arm_compute/runtime/BlobMemoryPool.h b/arm_compute/runtime/BlobMemoryPool.h index 25bfd539f6..c9c4da0f54 100644 --- a/arm_compute/runtime/BlobMemoryPool.h +++ b/arm_compute/runtime/BlobMemoryPool.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 ARM Limited. + * Copyright (c) 2017-2018 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -26,6 +26,7 @@ #include "arm_compute/runtime/IMemoryPool.h" +#include "arm_compute/runtime/IMemoryRegion.h" #include "arm_compute/runtime/Types.h" #include @@ -75,9 +76,9 @@ private: void free_blobs(); private: - IAllocator *_allocator; /**< Allocator to use for internal allocation */ - std::vector _blobs; /**< Vector holding all the memory blobs */ - std::vector _blob_sizes; /**< Sizes of each blob */ + IAllocator *_allocator; /**< Allocator to use for internal allocation */ + std::vector> _blobs; /**< Vector holding all the memory blobs */ + std::vector _blob_sizes; /**< Sizes of each blob */ }; } // namespace arm_compute #endif /* __ARM_COMPUTE_BLOBMEMORYPOOL_H__ */ diff --git a/arm_compute/runtime/CL/CLMemory.h b/arm_compute/runtime/CL/CLMemory.h index edd9de8097..02d36614ae 100644 --- a/arm_compute/runtime/CL/CLMemory.h +++ b/arm_compute/runtime/CL/CLMemory.h @@ -24,6 +24,8 @@ #ifndef __ARM_COMPUTE_RUNTIME_CL_CLMEMORY_H__ #define __ARM_COMPUTE_RUNTIME_CL_CLMEMORY_H__ +#include "arm_compute/runtime/IMemory.h" + #include "arm_compute/core/CL/OpenCL.h" #include "arm_compute/runtime/CL/CLMemoryRegion.h" @@ -33,7 +35,7 @@ namespace arm_compute { /** OpenCL implementation of memory object */ -class CLMemory +class CLMemory : public IMemory { public: /** Default Constructor */ @@ -59,20 +61,22 @@ public: CLMemory(CLMemory &&) noexcept = default; /** Allow instances of this class to be move assigned */ CLMemory &operator=(CLMemory &&) noexcept = default; - /** Region accessor + /** OpenCL Region accessor * * @return Memory region */ - ICLMemoryRegion *region(); - /** Region accessor + ICLMemoryRegion *cl_region(); + /** OpenCL Region accessor * * @return Memory region */ - ICLMemoryRegion *region() const; + ICLMemoryRegion *cl_region() const; -private: - /** Creates empty region */ - void create_empty_region(); + // Inherited methods overridden: + IMemoryRegion *region() final; + IMemoryRegion *region() const final; + void set_region(IMemoryRegion *region) final; + void set_owned_region(std::unique_ptr region) final; private: ICLMemoryRegion *_region; diff --git a/arm_compute/runtime/CL/CLMemoryRegion.h b/arm_compute/runtime/CL/CLMemoryRegion.h index 01dd54e391..dbfd8225ca 100644 --- a/arm_compute/runtime/CL/CLMemoryRegion.h +++ b/arm_compute/runtime/CL/CLMemoryRegion.h @@ -81,9 +81,9 @@ public: virtual void unmap(cl::CommandQueue &q) = 0; // Inherited methods overridden : - void *buffer() override; - void *buffer() const override; - void **handle() override; + void *buffer() override; + void *buffer() const override; + std::unique_ptr extract_subregion(size_t offset, size_t size) override; protected: cl::Context _ctx; @@ -102,11 +102,16 @@ public: * @param[in] size Region size */ CLBufferMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size); + /** Constructor + * + * @param[in] buffer Buffer to be used as a memory region + */ + CLBufferMemoryRegion(const cl::Buffer &buffer); // Inherited methods overridden : - void *ptr() override; - void *map(cl::CommandQueue &q, bool blocking) override; - void unmap(cl::CommandQueue &q) override; + void *ptr() final; + void *map(cl::CommandQueue &q, bool blocking) final; + void unmap(cl::CommandQueue &q) final; }; /** OpenCL SVM memory region interface */ @@ -153,8 +158,8 @@ public: CLCoarseSVMMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size, size_t alignment); // Inherited methods overridden : - void *map(cl::CommandQueue &q, bool blocking) override; - void unmap(cl::CommandQueue &q) override; + void *map(cl::CommandQueue &q, bool blocking) final; + void unmap(cl::CommandQueue &q) final; }; /** OpenCL fine-grain SVM memory region implementation */ @@ -171,8 +176,8 @@ public: CLFineSVMMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size, size_t alignment); // Inherited methods overridden : - void *map(cl::CommandQueue &q, bool blocking) override; - void unmap(cl::CommandQueue &q) override; + void *map(cl::CommandQueue &q, bool blocking) final; + void unmap(cl::CommandQueue &q) final; }; } // namespace arm_compute #endif /* __ARM_COMPUTE_RUNTIME_CL_CL_MEMORY_REGION_H__ */ diff --git a/arm_compute/runtime/CL/CLTensorAllocator.h b/arm_compute/runtime/CL/CLTensorAllocator.h index a372195555..de5f482d05 100644 --- a/arm_compute/runtime/CL/CLTensorAllocator.h +++ b/arm_compute/runtime/CL/CLTensorAllocator.h @@ -103,17 +103,13 @@ public: void free() override; /** Import an existing memory as a tensor's backing memory * - * @warning If the tensor is flagged to be managed by a memory manager, - * this call will lead to an error. - * @warning Ownership of memory depends on the way the @ref CLMemory object was constructed - * @note Calling free on a tensor with imported memory will just clear - * the internal pointer value. + * @warning ownership of memory is not transferred * - * @param[in] memory Memory to import + * @param[in] buffer Buffer to import * * @return error status */ - arm_compute::Status import_memory(CLMemory memory); + arm_compute::Status import_memory(cl::Buffer buffer); /** Associates the tensor with a memory group * * @param[in] associated_memory_group Memory group to associate the tensor with @@ -129,9 +125,13 @@ protected: /** Call unmap() on the OpenCL buffer. */ void unlock() override; +private: + static const cl::Buffer _empty_buffer; + private: CLMemoryGroup *_associated_memory_group; /**< Registered memory manager */ CLMemory _memory; /**< OpenCL memory */ + uint8_t *_mapping; /**< Pointer to the CPU mapping of the OpenCL buffer. */ CLTensor *_owner; /**< Owner of the allocator */ }; } // namespace arm_compute diff --git a/arm_compute/runtime/GLES_COMPUTE/GCMemory.h b/arm_compute/runtime/GLES_COMPUTE/GCMemory.h new file mode 100644 index 0000000000..bf0428a341 --- /dev/null +++ b/arm_compute/runtime/GLES_COMPUTE/GCMemory.h @@ -0,0 +1,85 @@ +/* + * 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. + */ +#ifndef __ARM_COMPUTE_RUNTIME_GLES_COMPUTE_GCMEMORY_H__ +#define __ARM_COMPUTE_RUNTIME_GLES_COMPUTE_GCMEMORY_H__ + +#include "arm_compute/runtime/IMemory.h" + +#include "arm_compute/runtime/GLES_COMPUTE/GCMemoryRegion.h" + +#include +#include + +namespace arm_compute +{ +/** GLES implementation of memory object */ +class GCMemory : public IMemory +{ +public: + /** Default Constructor */ + GCMemory(); + /** Default Constructor + * + * @param[in] memory Memory to be imported + */ + GCMemory(std::shared_ptr memory); + /** Default Constructor + * + * @note Ownership of the memory is not transferred to this object. + * Thus management (allocate/free) should be done by the client. + * + * @param[in] memory Memory to be imported + */ + GCMemory(IGCMemoryRegion *memory); + /** Allow instances of this class to be copied */ + GCMemory(const GCMemory &) = default; + /** Allow instances of this class to be copy assigned */ + GCMemory &operator=(const GCMemory &) = default; + /** Allow instances of this class to be moved */ + GCMemory(GCMemory &&) noexcept = default; + /** Allow instances of this class to be move assigned */ + GCMemory &operator=(GCMemory &&) noexcept = default; + /** GLES Region accessor + * + * @return Memory region + */ + IGCMemoryRegion *gc_region(); + /** GLES Region accessor + * + * @return Memory region + */ + IGCMemoryRegion *gc_region() const; + + // Inherited methods overridden: + IMemoryRegion *region() final; + IMemoryRegion *region() const final; + void set_region(IMemoryRegion *region) final; + void set_owned_region(std::unique_ptr region) final; + +private: + IGCMemoryRegion *_region; + std::shared_ptr _region_owned; +}; +} // namespace arm_compute +#endif /* __ARM_COMPUTE_RUNTIME_GLES_COMPUTE_GCMEMORY_H__ */ diff --git a/arm_compute/runtime/GLES_COMPUTE/GCMemoryRegion.h b/arm_compute/runtime/GLES_COMPUTE/GCMemoryRegion.h new file mode 100644 index 0000000000..23e3cebe3f --- /dev/null +++ b/arm_compute/runtime/GLES_COMPUTE/GCMemoryRegion.h @@ -0,0 +1,108 @@ +/* + * 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. + */ +#ifndef __ARM_COMPUTE_RUNTIME_GLES_COMPUTE_GC_MEMORY_REGION_H__ +#define __ARM_COMPUTE_RUNTIME_GLES_COMPUTE_GC_MEMORY_REGION_H__ + +#include "arm_compute/core/GLES_COMPUTE/OpenGLES.h" +#include "arm_compute/runtime/IMemoryRegion.h" + +#include + +namespace arm_compute +{ +/** GLES memory region interface */ +class IGCMemoryRegion : public IMemoryRegion +{ +public: + /** Constructor + * + * @param[in] size Region size + */ + IGCMemoryRegion(size_t size); + /** Default Destructor */ + virtual ~IGCMemoryRegion() = default; + /** Prevent instances of this class from being copied (As this class contains pointers) */ + IGCMemoryRegion(const IGCMemoryRegion &) = delete; + /** Default move constructor */ + IGCMemoryRegion(IGCMemoryRegion &&) = default; + /** Prevent instances of this class from being copied (As this class contains pointers) */ + IGCMemoryRegion &operator=(const IGCMemoryRegion &) = delete; + /** Default move assignment operator */ + IGCMemoryRegion &operator=(IGCMemoryRegion &&) = default; + /** Returns the underlying CL buffer + * + * @return CL memory buffer object + */ + const GLuint &gc_ssbo_name() const; + /** Host/SVM pointer accessor + * + * @return Host/SVM pointer base + */ + virtual void *ptr() = 0; + /** Enqueue a map operation of the allocated buffer on the given queue. + * + * @param[in] blocking If true, then the mapping will be ready to use by the time + * this method returns, else it is the caller's responsibility + * to flush the queue and wait for the mapping operation to have completed before using the returned mapping pointer. + * + * @return The mapping address. + */ + virtual void *map(bool blocking) = 0; + /** Enqueue an unmap operation of the allocated buffer on the given queue. + * + * @note This method simply enqueue the unmap operation, it is the caller's responsibility to flush the queue and make sure the unmap is finished before + * the memory is accessed by the device. + * + */ + virtual void unmap() = 0; + + // Inherited methods overridden : + void *buffer() override; + void *buffer() const override; + +protected: + void *_mapping; + GLuint _ssbo_name; +}; + +/** GLES buffer memory region implementation */ +class GCBufferMemoryRegion final : public IGCMemoryRegion +{ +public: + /** Constructor + * + * @param[in] size Region size + */ + GCBufferMemoryRegion(size_t size); + /** Destructor */ + ~GCBufferMemoryRegion(); + + // Inherited methods overridden : + void *ptr() final; + void *map(bool blocking) final; + void unmap() final; + std::unique_ptr extract_subregion(size_t offset, size_t size) final; +}; +} // namespace arm_compute +#endif /* __ARM_COMPUTE_RUNTIME_GLES_COMPUTE_GC_MEMORY_REGION_H__ */ diff --git a/arm_compute/runtime/GLES_COMPUTE/GCTensorAllocator.h b/arm_compute/runtime/GLES_COMPUTE/GCTensorAllocator.h index 1bd3582b6c..b7067664fc 100644 --- a/arm_compute/runtime/GLES_COMPUTE/GCTensorAllocator.h +++ b/arm_compute/runtime/GLES_COMPUTE/GCTensorAllocator.h @@ -26,6 +26,7 @@ #define __ARM_COMPUTE_GCTENSORALLOCATOR_H__ #include "arm_compute/core/GLES_COMPUTE/OpenGLES.h" +#include "arm_compute/runtime/GLES_COMPUTE/GCMemory.h" #include "arm_compute/runtime/ITensorAllocator.h" #include "arm_compute/runtime/MemoryGroupBase.h" @@ -38,20 +39,6 @@ template class MemoryGroupBase; using GCMemoryGroup = MemoryGroupBase; -class GLBufferWrapper -{ -public: - GLBufferWrapper() - : _ssbo_name(0) - { - ARM_COMPUTE_GL_CHECK(glGenBuffers(1, &_ssbo_name)); - } - ~GLBufferWrapper() - { - ARM_COMPUTE_GL_CHECK(glDeleteBuffers(1, &_ssbo_name)); - } - GLuint _ssbo_name; -}; /** Basic implementation of a GLES memory tensor allocator. */ class GCTensorAllocator : public ITensorAllocator { @@ -72,7 +59,7 @@ public: GCTensorAllocator &operator=(GCTensorAllocator &&) = default; /** Default destructor */ - ~GCTensorAllocator(); + ~GCTensorAllocator() = default; /** Interface to be implemented by the child class to return the pointer to the mapped data. * @@ -135,11 +122,10 @@ protected: void unlock() override; private: - GCMemoryGroup *_associated_memory_group; /**< Registered memory group */ - std::unique_ptr _gl_buffer; /**< OpenGL ES object containing the tensor data. */ - uint8_t *_mapping; /**< Pointer to the CPU mapping of the OpenGL ES buffer. */ - GCTensor *_owner; /**< Owner of the allocator */ + GCMemoryGroup *_associated_memory_group; /**< Registered memory group */ + GCMemory _memory; /**< OpenGL ES memory */ + uint8_t *_mapping; /**< Pointer to the CPU mapping of the OpenGL ES buffer. */ + GCTensor *_owner; /**< Owner of the allocator */ }; -} - +} // namespace arm_compute #endif /* __ARM_COMPUTE_GCTENSORALLOCATOR_H__ */ diff --git a/arm_compute/runtime/ILifetimeManager.h b/arm_compute/runtime/ILifetimeManager.h index 36743ac404..f2e9b497c9 100644 --- a/arm_compute/runtime/ILifetimeManager.h +++ b/arm_compute/runtime/ILifetimeManager.h @@ -32,8 +32,10 @@ namespace arm_compute { -class IMemoryGroup; +// Forward declarations class IAllocator; +class IMemory; +class IMemoryGroup; /** Interface for managing the lifetime of objects */ class ILifetimeManager @@ -53,11 +55,11 @@ public: virtual void start_lifetime(void *obj) = 0; /** Ends lifetime of an object * - * @param[in] obj Object - * @param[in] handle Memory handle of the object - * @param[in] size Size of the given object at given time + * @param[in] obj Object + * @param[in] obj_memory Object memory + * @param[in] size Size of the given object at given time */ - virtual void end_lifetime(void *obj, void **handle, size_t size) = 0; + virtual void end_lifetime(void *obj, IMemory &obj_memory, size_t size) = 0; /** Checks if the lifetime of the registered object is complete * * @return True if all object lifetimes are finalized else false. diff --git a/arm_compute/runtime/IMemory.h b/arm_compute/runtime/IMemory.h new file mode 100644 index 0000000000..e6f5058d94 --- /dev/null +++ b/arm_compute/runtime/IMemory.h @@ -0,0 +1,63 @@ +/* + * 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. + */ +#ifndef __ARM_COMPUTE_IMEMORY_H__ +#define __ARM_COMPUTE_IMEMORY_H__ + +#include "arm_compute/runtime/IMemoryRegion.h" + +namespace arm_compute +{ +/** Memory interface*/ +class IMemory +{ +public: + /** Virtual default destructor */ + virtual ~IMemory() = default; + /** Region accessor + * + * @return Memory region + */ + virtual IMemoryRegion *region() = 0; + /** Region accessor + * + * @return Memory region + */ + virtual IMemoryRegion *region() const = 0; + /** Sets a memory region + * + * @warning Ownership of the memory region remains to the caller + * + * @param region Memory region + */ + virtual void set_region(IMemoryRegion *region) = 0; + /** Sets a memory region + * + * @warning Ownership of the memory region is transfered along + * + * @param region Memory region + */ + virtual void set_owned_region(std::unique_ptr region) = 0; +}; +} // namespace arm_compute +#endif /* __ARM_COMPUTE_IMEMORY_H__ */ diff --git a/arm_compute/runtime/IMemoryRegion.h b/arm_compute/runtime/IMemoryRegion.h index 4c08b4ab09..aa8c07ef2a 100644 --- a/arm_compute/runtime/IMemoryRegion.h +++ b/arm_compute/runtime/IMemoryRegion.h @@ -25,6 +25,7 @@ #define __ARM_COMPUTE_RUNTIME_IMEMORY_REGION_H__ #include +#include namespace arm_compute { @@ -36,12 +37,25 @@ public: * * @param[in] size Region size */ - IMemoryRegion(size_t size) + explicit IMemoryRegion(size_t size) : _size(size) { } /** Virtual Destructor */ virtual ~IMemoryRegion() = default; + /** Extract a sub-region from the memory + * + * @warning Ownership is maintained by the parent memory, + * while a wrapped raw memory region is returned by this function. + * Thus parent memory should not be released before this. + * + * + * @param[in] offset Offset to the region + * @param[in] size Size of the region + * + * @return A wrapped memory sub-region with no ownership of the underlying memory + */ + virtual std::unique_ptr extract_subregion(size_t offset, size_t size) = 0; /** Returns the pointer to the allocated data. * * @return Pointer to the allocated data @@ -52,16 +66,11 @@ public: * @return Pointer to the allocated data */ virtual void *buffer() const = 0; - /** Handle of internal memory - * - * @return Handle of memory - */ - virtual void **handle() = 0; /** Memory region size accessor * * @return Memory region size */ - size_t size() + size_t size() const { return _size; } diff --git a/arm_compute/runtime/ISimpleLifetimeManager.h b/arm_compute/runtime/ISimpleLifetimeManager.h index 7942e40f7f..f2eb4f5904 100644 --- a/arm_compute/runtime/ISimpleLifetimeManager.h +++ b/arm_compute/runtime/ISimpleLifetimeManager.h @@ -58,7 +58,7 @@ public: // Inherited methods overridden: void register_group(IMemoryGroup *group) override; void start_lifetime(void *obj) override; - void end_lifetime(void *obj, void **handle, size_t size) override; + void end_lifetime(void *obj, IMemory &obj_memory, size_t size) override; bool are_all_finalized() const override; protected: @@ -69,14 +69,14 @@ protected: /** Element struct */ struct Element { - Element(void *id_ = nullptr, void **handle_ = nullptr, size_t size_ = 0, bool status_ = false) + Element(void *id_ = nullptr, IMemory *handle_ = nullptr, size_t size_ = 0, bool status_ = false) : id(id_), handle(handle_), size(size_), status(status_) { } - void *id; /**< Element id */ - void **handle; /**< Element's memory handle */ - size_t size; /**< Element's size */ - bool status; /**< Lifetime status */ + void *id; /**< Element id */ + IMemory *handle; /**< Element's memory handle */ + size_t size; /**< Element's size */ + bool status; /**< Lifetime status */ }; /** Blob struct */ diff --git a/arm_compute/runtime/Memory.h b/arm_compute/runtime/Memory.h index 2dadccf254..6f5254a689 100644 --- a/arm_compute/runtime/Memory.h +++ b/arm_compute/runtime/Memory.h @@ -24,6 +24,8 @@ #ifndef __ARM_COMPUTE_MEMORY_H__ #define __ARM_COMPUTE_MEMORY_H__ +#include "arm_compute/runtime/IMemory.h" + #include "arm_compute/runtime/IMemoryRegion.h" #include @@ -32,7 +34,7 @@ namespace arm_compute { /** CPU implementation of memory object */ -class Memory +class Memory : public IMemory { public: /** Default Constructor */ @@ -58,24 +60,16 @@ public: Memory(Memory &&) noexcept = default; /** Allow instances of this class to be move assigned */ Memory &operator=(Memory &&) noexcept = default; - /** Region accessor - * - * @return Memory region - */ - IMemoryRegion *region(); - /** Region accessor - * - * @return Memory region - */ - IMemoryRegion *region() const; -private: - /** Creates empty region */ - void create_empty_region(); + // Inherited methods overridden: + IMemoryRegion *region() final; + IMemoryRegion *region() const final; + void set_region(IMemoryRegion *region) final; + void set_owned_region(std::unique_ptr region) final; private: IMemoryRegion *_region; std::shared_ptr _region_owned; }; -} +} // namespace arm_compute #endif /* __ARM_COMPUTE_MEMORY_H__ */ diff --git a/arm_compute/runtime/MemoryGroupBase.h b/arm_compute/runtime/MemoryGroupBase.h index 06e4321410..0ceaa900c5 100644 --- a/arm_compute/runtime/MemoryGroupBase.h +++ b/arm_compute/runtime/MemoryGroupBase.h @@ -35,6 +35,9 @@ namespace arm_compute { +// Forward declarations +class IMemory; + /** Memory group */ template class MemoryGroupBase : public IMemoryGroup @@ -63,11 +66,12 @@ public: * * @note Manager must not be finalized * - * @param[in] obj Object to request memory for - * @param[in] handle Handle to store the memory - * @param[in] size Size of memory to allocate + * @param[in, out] obj Object to request memory for + * @param[in, out] obj_memory Object's memory handling interface which can be used to alter the underlying memory + * that is used by the object. + * @param[in] size Size of memory to allocate */ - void finalize_memory(TensorType *obj, void **handle, size_t size); + void finalize_memory(TensorType *obj, IMemory &obj_memory, size_t size); // Inherited methods overridden: void acquire() override; @@ -112,16 +116,16 @@ inline void MemoryGroupBase::manage(TensorType *obj) } template -inline void MemoryGroupBase::finalize_memory(TensorType *obj, void **handle, size_t size) +inline void MemoryGroupBase::finalize_memory(TensorType *obj, IMemory &obj_memory, size_t size) { // TODO (geopin01) : Check size (track size in MemoryMappings) // Check if existing mapping is valid - ARM_COMPUTE_ERROR_ON(!_mappings.empty() && (_mappings.find(handle) == std::end(_mappings))); + ARM_COMPUTE_ERROR_ON(!_mappings.empty() && (_mappings.find(&obj_memory) == std::end(_mappings))); if(_memory_manager && _mappings.empty()) { ARM_COMPUTE_ERROR_ON(!_memory_manager->lifetime_manager()); - _memory_manager->lifetime_manager()->end_lifetime(obj, handle, size); + _memory_manager->lifetime_manager()->end_lifetime(obj, obj_memory, size); } } diff --git a/arm_compute/runtime/MemoryRegion.h b/arm_compute/runtime/MemoryRegion.h index 481b20d375..335486ed9d 100644 --- a/arm_compute/runtime/MemoryRegion.h +++ b/arm_compute/runtime/MemoryRegion.h @@ -37,13 +37,13 @@ namespace arm_compute class MemoryRegion final : public IMemoryRegion { public: - /** Default constructor + /** Constructor * * @param[in] size Region size * @param[in] alignment Alignment in bytes of the base pointer. Defaults to 0 */ MemoryRegion(size_t size, size_t alignment = 0) - : IMemoryRegion(size), _mem(nullptr), _alignment(alignment), _offset(0) + : IMemoryRegion(size), _mem(nullptr), _ptr(nullptr) { if(size != 0) { @@ -53,16 +53,25 @@ public: { delete[] ptr; }); + _ptr = _mem.get(); // Calculate alignment offset if(alignment != 0) { void *aligned_ptr = _mem.get(); support::cpp11::align(alignment, size, aligned_ptr, space); - _offset = reinterpret_cast(aligned_ptr) - reinterpret_cast(_mem.get()); + _ptr = aligned_ptr; } } } + MemoryRegion(void *ptr, size_t size) + : IMemoryRegion(size), _mem(nullptr), _ptr(nullptr) + { + if(size != 0) + { + _ptr = ptr; + } + } /** Prevent instances of this class from being copied (As this class contains pointers) */ MemoryRegion(const MemoryRegion &) = delete; /** Default move constructor */ @@ -75,22 +84,27 @@ public: // Inherited methods overridden : void *buffer() final { - return reinterpret_cast(_mem.get() + _offset); + return _ptr; } void *buffer() const final { - // FIXME (COMPMID-1088) : Remove handle() and _offset when done - return reinterpret_cast(_mem.get() + _offset); + return _ptr; } - void **handle() final + std::unique_ptr extract_subregion(size_t offset, size_t size) final { - return reinterpret_cast(&_mem); + if(_ptr != nullptr && (offset < _size) && (_size - offset >= size)) + { + return support::cpp14::make_unique(static_cast(_ptr) + offset, size); + } + else + { + return nullptr; + } } protected: std::shared_ptr _mem; - size_t _alignment; - size_t _offset; + void *_ptr; }; } // namespace arm_compute #endif /* __ARM_COMPUTE_RUNTIME_MEMORY_REGION_H__ */ diff --git a/arm_compute/runtime/OffsetMemoryPool.h b/arm_compute/runtime/OffsetMemoryPool.h index 9685fd1319..480d424b5b 100644 --- a/arm_compute/runtime/OffsetMemoryPool.h +++ b/arm_compute/runtime/OffsetMemoryPool.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 ARM Limited. + * Copyright (c) 2017-2018 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -26,6 +26,7 @@ #include "arm_compute/runtime/IMemoryPool.h" +#include "arm_compute/runtime/IMemoryRegion.h" #include "arm_compute/runtime/Types.h" #include @@ -47,7 +48,7 @@ public: */ OffsetMemoryPool(IAllocator *allocator, size_t blob_size); /** Default Destructor */ - ~OffsetMemoryPool(); + ~OffsetMemoryPool() = default; /** Prevent instances of this class to be copy constructed */ OffsetMemoryPool(const OffsetMemoryPool &) = delete; /** Prevent instances of this class to be copy assigned */ @@ -64,9 +65,9 @@ public: std::unique_ptr duplicate() override; private: - IAllocator *_allocator; /**< Allocator to use for internal allocation */ - void *_blob; /**< Memory blob */ - size_t _blob_size; /**< Sizes of the allocated memory blob */ + IAllocator *_allocator; /**< Allocator to use for internal allocation */ + std::unique_ptr _blob; /**< Memory blob */ + size_t _blob_size; /**< Sizes of the allocated memory blob */ }; } // namespace arm_compute #endif /* __ARM_COMPUTE_OFFSETMEMORYPOOL_H__ */ diff --git a/arm_compute/runtime/TensorAllocator.h b/arm_compute/runtime/TensorAllocator.h index 2ad37d0576..ba9e5163ab 100644 --- a/arm_compute/runtime/TensorAllocator.h +++ b/arm_compute/runtime/TensorAllocator.h @@ -95,17 +95,11 @@ public: void free() override; /** Import an existing memory as a tensor's backing memory * - * @warning If the tensor is flagged to be managed by a memory manager, - * this call will lead to an error. - * @warning Ownership of memory depends on the way the @ref Memory object was constructed - * @note Calling free on a tensor with imported memory will just clear - * the internal pointer value. - * - * @param[in] memory Memory to import + * @warning ownership of memory is not transferred * * @return error status */ - arm_compute::Status import_memory(Memory memory); + arm_compute::Status import_memory(void *memory, size_t size); /** Associates the tensor with a memory group * * @param[in] associated_memory_group Memory group to associate the tensor with diff --git a/arm_compute/runtime/Types.h b/arm_compute/runtime/Types.h index 9916e6d47b..b962427ef8 100644 --- a/arm_compute/runtime/Types.h +++ b/arm_compute/runtime/Types.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2017 ARM Limited. + * Copyright (c) 2016-2018 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -24,6 +24,8 @@ #ifndef __ARM_COMPUTE_RUNTIME_TYPES_H__ #define __ARM_COMPUTE_RUNTIME_TYPES_H__ +#include "arm_compute/runtime/IMemory.h" + #include namespace arm_compute @@ -40,7 +42,7 @@ enum class MappingType * * @note All objects are pre-pinned to specific buffers to avoid any relevant overheads */ -using MemoryMappings = std::map; +using MemoryMappings = std::map; /** A map of the groups and memory mappings */ using GroupMappings = std::map; -- cgit v1.2.1