From baf174e85ddb5399355281cd34d0f459d92124a7 Mon Sep 17 00:00:00 2001 From: Georgios Pinitas Date: Fri, 8 Sep 2017 19:47:30 +0100 Subject: COMPMID-485: Memory Manager Change-Id: Ib421b7622838f050038cd81e7426bb1413a7d6e6 Reviewed-on: http://mpd-gerrit.cambridge.arm.com/87376 Tested-by: Kaizen Reviewed-by: Anthony Barbier --- arm_compute/runtime/MemoryGroupBase.h | 159 ++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 arm_compute/runtime/MemoryGroupBase.h (limited to 'arm_compute/runtime/MemoryGroupBase.h') diff --git a/arm_compute/runtime/MemoryGroupBase.h b/arm_compute/runtime/MemoryGroupBase.h new file mode 100644 index 0000000000..ab8acb3494 --- /dev/null +++ b/arm_compute/runtime/MemoryGroupBase.h @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2017 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_MEMORYGROUPBASE_H__ +#define __ARM_COMPUTE_MEMORYGROUPBASE_H__ + +#include "arm_compute/runtime/IMemoryGroup.h" + +#include "arm_compute/runtime/IMemoryManager.h" +#include "arm_compute/runtime/IMemoryPool.h" + +#include +#include + +namespace arm_compute +{ +/** Memory group */ +template +class MemoryGroupBase : public IMemoryGroup +{ +public: + /** Default Constructor */ + MemoryGroupBase(std::shared_ptr memory_manager = nullptr); + /** Default destructor */ + ~MemoryGroupBase() = default; + /** Prevent instances of this class from being copied (As this class contains pointers). */ + MemoryGroupBase(const MemoryGroupBase &) = delete; + /** Prevent instances of this class from being copy assigned (As this class contains pointers). */ + MemoryGroupBase &operator=(const MemoryGroupBase &) = delete; + /** Allow instances of this class to be moved */ + MemoryGroupBase(MemoryGroupBase &&) = default; + /** Allow instances of this class to be moved */ + MemoryGroupBase &operator=(MemoryGroupBase &&) = default; + /** Sets a object to be managed by the given memory group + * + * @note Manager must not be finalized + * + * @param[in] obj Object to be managed + */ + void manage(TensorType *obj); + /** Finalizes memory for a given object + * + * @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 + */ + void finalize_memory(TensorType *obj, void **handle, size_t size); + + // Inherited methods overridden: + void acquire() override; + void release() override; + MemoryMappings &mappings() override; + +private: + void associate_memory_group(TensorType *obj); + +private: + std::shared_ptr _memory_manager; /**< Memory manager to be used by the group */ + IMemoryPool *_pool; /**< Memory pool that the group is scheduled with */ + MemoryMappings _mappings; /**< Memory mappings of the group */ +}; + +template +inline MemoryGroupBase::MemoryGroupBase(std::shared_ptr memory_manager) + : _memory_manager(std::move(memory_manager)), _pool(nullptr), _mappings() +{ + if(_memory_manager) + { + ARM_COMPUTE_ERROR_ON(!_memory_manager->lifetime_manager()); + } +} + +template +inline void MemoryGroupBase::manage(TensorType *obj) +{ + if(_memory_manager) + { + ARM_COMPUTE_ERROR_ON(!_memory_manager->lifetime_manager()); + + // Defer registration to the first managed object + _memory_manager->lifetime_manager()->register_group(this); + + // Associate this memory group with the tensor + associate_memory_group(obj); + + // Start object lifetime + _memory_manager->lifetime_manager()->start_lifetime(obj); + } +} + +template +inline void MemoryGroupBase::finalize_memory(TensorType *obj, void **handle, size_t size) +{ + if(_memory_manager) + { + ARM_COMPUTE_ERROR_ON(!_memory_manager->lifetime_manager()); + _memory_manager->lifetime_manager()->end_lifetime(obj, handle, size); + } +} + +template +inline void MemoryGroupBase::acquire() +{ + if(!_mappings.empty()) + { + ARM_COMPUTE_ERROR_ON(!_memory_manager->pool_manager()); + _pool = _memory_manager->pool_manager()->lock_pool(); + _pool->acquire(_mappings); + } +} + +template +inline void MemoryGroupBase::release() +{ + if(_pool != nullptr) + { + ARM_COMPUTE_ERROR_ON(!_memory_manager->pool_manager()); + ARM_COMPUTE_ERROR_ON(_mappings.empty()); + _pool->release(_mappings); + _memory_manager->pool_manager()->unlock_pool(_pool); + _pool = nullptr; + } +} + +template +inline MemoryMappings &MemoryGroupBase::mappings() +{ + return _mappings; +} + +template +inline void MemoryGroupBase::associate_memory_group(TensorType *) +{ + ARM_COMPUTE_ERROR("Must be implemented by child class"); +} +} // arm_compute +#endif /*__ARM_COMPUTE_MEMORYGROUPBASE_H__ */ -- cgit v1.2.1