diff options
-rw-r--r-- | arm_compute/runtime/BlobLifetimeManager.h | 41 | ||||
-rw-r--r-- | arm_compute/runtime/BlobMemoryPool.h | 2 | ||||
-rw-r--r-- | arm_compute/runtime/ILifetimeManager.h | 14 | ||||
-rw-r--r-- | arm_compute/runtime/ISimpleLifetimeManager.h | 85 | ||||
-rw-r--r-- | arm_compute/runtime/OffsetLifetimeManager.h | 67 | ||||
-rw-r--r-- | arm_compute/runtime/OffsetMemoryPool.h | 72 | ||||
-rw-r--r-- | src/runtime/BlobLifetimeManager.cpp | 65 | ||||
-rw-r--r-- | src/runtime/ISimpleLifetimeManager.cpp | 103 | ||||
-rw-r--r-- | src/runtime/OffsetLifetimeManager.cpp | 76 | ||||
-rw-r--r-- | src/runtime/OffsetMemoryPool.cpp | 80 |
10 files changed, 503 insertions, 102 deletions
diff --git a/arm_compute/runtime/BlobLifetimeManager.h b/arm_compute/runtime/BlobLifetimeManager.h index ec43f47fe6..edf4d43421 100644 --- a/arm_compute/runtime/BlobLifetimeManager.h +++ b/arm_compute/runtime/BlobLifetimeManager.h @@ -24,21 +24,20 @@ #ifndef __ARM_COMPUTE_BLOBLIFETIMEMANAGER_H__ #define __ARM_COMPUTE_BLOBLIFETIMEMANAGER_H__ -#include "arm_compute/runtime/ILifetimeManager.h" +#include "arm_compute/runtime/ISimpleLifetimeManager.h" -#include "arm_compute/runtime/IMemoryGroup.h" +#include "arm_compute/runtime/IMemoryPool.h" #include "arm_compute/runtime/Types.h" #include <cstddef> -#include <map> +#include <memory> #include <vector> namespace arm_compute { -class IMemoryGroup; - -/** Class that tracks the lifetime of registered tensors and calculates the systems memory requirements in terms of blobs */ -class BlobLifetimeManager : public ILifetimeManager +/** Concrete class that tracks the lifetime of registered tensors and + * calculates the systems memory requirements in terms of blobs */ +class BlobLifetimeManager : public ISimpleLifetimeManager { public: /** Constructor */ @@ -53,35 +52,15 @@ public: BlobLifetimeManager &operator=(BlobLifetimeManager &&) = default; // 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; std::unique_ptr<IMemoryPool> create_pool(IAllocator *allocator) override; - bool are_all_finalized() const override; MappingType mapping_type() const override; private: - /** Update blobs and mappings */ - void update_blobs_and_mappings(); + // Inherited methods overridden: + void update_blobs_and_mappings() override; private: - /** Element struct */ - struct Element - { - Element(void *id_ = nullptr, void **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 */ - }; - - IMemoryGroup *_active_group; /**< Active group */ - std::vector<Element> _active_elements; /**< A map that contains the active elements */ - std::map<IMemoryGroup *, std::vector<Element>> _finalized_groups; /**< A map that contains the finalized groups */ - std::vector<size_t> _blobs; + std::vector<size_t> _blobs; /**< Memory blobs' sizes */ }; -} // arm_compute +} // namespace arm_compute #endif /* __ARM_COMPUTE_BLOBLIFETIMEMANAGER_H__ */ diff --git a/arm_compute/runtime/BlobMemoryPool.h b/arm_compute/runtime/BlobMemoryPool.h index f703bf0b82..25bfd539f6 100644 --- a/arm_compute/runtime/BlobMemoryPool.h +++ b/arm_compute/runtime/BlobMemoryPool.h @@ -79,5 +79,5 @@ private: std::vector<void *> _blobs; /**< Vector holding all the memory blobs */ std::vector<size_t> _blob_sizes; /**< Sizes of each blob */ }; -} // arm_compute +} // namespace arm_compute #endif /* __ARM_COMPUTE_BLOBMEMORYPOOL_H__ */ diff --git a/arm_compute/runtime/ILifetimeManager.h b/arm_compute/runtime/ILifetimeManager.h index 4f9af6f535..6f2c68d372 100644 --- a/arm_compute/runtime/ILifetimeManager.h +++ b/arm_compute/runtime/ILifetimeManager.h @@ -28,7 +28,7 @@ #include "arm_compute/runtime/Types.h" #include <cstddef> -#include <vector> +#include <memory> namespace arm_compute { @@ -58,6 +58,11 @@ public: * @param[in] size Size of the given object at given time */ virtual void end_lifetime(void *obj, void **handle, size_t size) = 0; + /** Checks if the lifetime of the registered object is complete + * + * @return True if all object lifetimes are finalized else false. + */ + virtual bool are_all_finalized() const = 0; /** Creates a memory pool depending on the memory requirements * * @param allocator Allocator to use @@ -65,16 +70,11 @@ public: * @return A memory pool */ virtual std::unique_ptr<IMemoryPool> create_pool(IAllocator *allocator) = 0; - /** Checks if the lifetime of the registered object is complete - * - * @return True if all object lifetimes are finalized else false. - */ - virtual bool are_all_finalized() const = 0; /** Returns the type of mappings that the lifetime manager returns * * @return Mapping type of the lifetime manager */ virtual MappingType mapping_type() const = 0; }; -} // arm_compute +} // namespace arm_compute #endif /* __ARM_COMPUTE_ILIFETIMEMANAGER_H__ */ diff --git a/arm_compute/runtime/ISimpleLifetimeManager.h b/arm_compute/runtime/ISimpleLifetimeManager.h new file mode 100644 index 0000000000..792ab0b558 --- /dev/null +++ b/arm_compute/runtime/ISimpleLifetimeManager.h @@ -0,0 +1,85 @@ +/* + * 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_ISIMPLELIFETIMEMANAGER_H__ +#define __ARM_COMPUTE_ISIMPLELIFETIMEMANAGER_H__ + +#include "arm_compute/runtime/ILifetimeManager.h" + +#include "arm_compute/runtime/IMemoryPool.h" +#include "arm_compute/runtime/Types.h" + +#include <cstddef> +#include <map> +#include <vector> + +namespace arm_compute +{ +class IAllocator; +class IMemoryGroup; + +/** Abstract class of the simple lifetime manager interface */ +class ISimpleLifetimeManager : public ILifetimeManager +{ +public: + /** Constructor */ + ISimpleLifetimeManager(); + /** Prevent instances of this class to be copy constructed */ + ISimpleLifetimeManager(const ISimpleLifetimeManager &) = delete; + /** Prevent instances of this class to be copied */ + ISimpleLifetimeManager &operator=(const ISimpleLifetimeManager &) = delete; + /** Allow instances of this class to be move constructed */ + ISimpleLifetimeManager(ISimpleLifetimeManager &&) = default; + /** Allow instances of this class to be moved */ + ISimpleLifetimeManager &operator=(ISimpleLifetimeManager &&) = default; + + // 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; + bool are_all_finalized() const override; + +protected: + /** Update blobs and mappings */ + virtual void update_blobs_and_mappings() = 0; + +protected: + /** Element struct */ + struct Element + { + Element(void *id_ = nullptr, void **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 */ + }; + + IMemoryGroup *_active_group; /**< Active group */ + std::vector<Element> _active_elements; /**< A map that contains the active elements */ + std::map<IMemoryGroup *, std::vector<Element>> _finalized_groups; /**< A map that contains the finalized groups */ +}; +} // namespace arm_compute +#endif /* __ARM_COMPUTE_ISIMPLELIFETIMEMANAGER_H__ */ diff --git a/arm_compute/runtime/OffsetLifetimeManager.h b/arm_compute/runtime/OffsetLifetimeManager.h new file mode 100644 index 0000000000..e39d6a0d6a --- /dev/null +++ b/arm_compute/runtime/OffsetLifetimeManager.h @@ -0,0 +1,67 @@ +/* + * 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_OFFSETLIFETIMEMANAGER_H__ +#define __ARM_COMPUTE_OFFSETLIFETIMEMANAGER_H__ + +#include "arm_compute/runtime/ISimpleLifetimeManager.h" + +#include "arm_compute/runtime/Types.h" + +#include <cstddef> +#include <map> +#include <vector> + +namespace arm_compute +{ +class IMemoryPool; + +/** Concrete class that tracks the lifetime of registered tensors and + * calculates the systems memory requirements in terms of a single blob and a list of offsets */ +class OffsetLifetimeManager : public ISimpleLifetimeManager +{ +public: + /** Constructor */ + OffsetLifetimeManager(); + /** Prevent instances of this class to be copy constructed */ + OffsetLifetimeManager(const OffsetLifetimeManager &) = delete; + /** Prevent instances of this class to be copied */ + OffsetLifetimeManager &operator=(const OffsetLifetimeManager &) = delete; + /** Allow instances of this class to be move constructed */ + OffsetLifetimeManager(OffsetLifetimeManager &&) = default; + /** Allow instances of this class to be moved */ + OffsetLifetimeManager &operator=(OffsetLifetimeManager &&) = default; + + // Inherited methods overridden: + std::unique_ptr<IMemoryPool> create_pool(IAllocator *allocator) override; + MappingType mapping_type() const override; + +private: + // Inherited methods overridden: + void update_blobs_and_mappings() override; + +private: + size_t _blob; /**< Memory blob size */ +}; +} // namespace arm_compute +#endif /* __ARM_COMPUTE_OFFSETLIFETIMEMANAGER_H__ */ diff --git a/arm_compute/runtime/OffsetMemoryPool.h b/arm_compute/runtime/OffsetMemoryPool.h new file mode 100644 index 0000000000..9685fd1319 --- /dev/null +++ b/arm_compute/runtime/OffsetMemoryPool.h @@ -0,0 +1,72 @@ +/* + * 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_OFFSETMEMORYPOOL_H__ +#define __ARM_COMPUTE_OFFSETMEMORYPOOL_H__ + +#include "arm_compute/runtime/IMemoryPool.h" + +#include "arm_compute/runtime/Types.h" + +#include <cstddef> + +namespace arm_compute +{ +class IAllocator; + +/** Offset based memory pool */ +class OffsetMemoryPool : public IMemoryPool +{ +public: + /** Default Constructor + * + * @note allocator should outlive the memory pool + * + * @param[in] allocator Backing memory allocator + * @param[in] blob_size Size of the memory be allocated + */ + OffsetMemoryPool(IAllocator *allocator, size_t blob_size); + /** Default Destructor */ + ~OffsetMemoryPool(); + /** Prevent instances of this class to be copy constructed */ + OffsetMemoryPool(const OffsetMemoryPool &) = delete; + /** Prevent instances of this class to be copy assigned */ + OffsetMemoryPool &operator=(const OffsetMemoryPool &) = delete; + /** Allow instances of this class to be move constructed */ + OffsetMemoryPool(OffsetMemoryPool &&) = default; + /** Allow instances of this class to be move assigned */ + OffsetMemoryPool &operator=(OffsetMemoryPool &&) = default; + + // Inherited methods overridden: + void acquire(MemoryMappings &handles) override; + void release(MemoryMappings &handles) override; + MappingType mapping_type() const override; + std::unique_ptr<IMemoryPool> 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 */ +}; +} // namespace arm_compute +#endif /* __ARM_COMPUTE_OFFSETMEMORYPOOL_H__ */ diff --git a/src/runtime/BlobLifetimeManager.cpp b/src/runtime/BlobLifetimeManager.cpp index 69292b9319..3ca5071d91 100644 --- a/src/runtime/BlobLifetimeManager.cpp +++ b/src/runtime/BlobLifetimeManager.cpp @@ -37,77 +37,16 @@ using namespace arm_compute; BlobLifetimeManager::BlobLifetimeManager() - : _active_group(nullptr), _active_elements(), _finalized_groups(), _blobs() + : _blobs() { } -void BlobLifetimeManager::register_group(IMemoryGroup *group) -{ - if(_active_group == nullptr) - { - ARM_COMPUTE_ERROR_ON(group == nullptr); - _active_group = group; - } -} - -void BlobLifetimeManager::start_lifetime(void *obj) -{ - ARM_COMPUTE_ERROR_ON(obj == nullptr); - ARM_COMPUTE_ERROR_ON_MSG(std::find_if(std::begin(_active_elements), std::end(_active_elements), [&obj](const Element & e) - { - return obj == e.id; - }) != std::end(_active_elements), - "Memory object is already registered!"); - - // Insert object in groups and mark its finalized state to false - _active_elements.emplace_back(obj); -} - -void BlobLifetimeManager::end_lifetime(void *obj, void **handle, size_t size) -{ - ARM_COMPUTE_ERROR_ON(obj == nullptr); - - // Find object - auto it = std::find_if(std::begin(_active_elements), std::end(_active_elements), [&obj](const Element & e) - { - return obj == e.id; - }); - ARM_COMPUTE_ERROR_ON(it == std::end(_active_elements)); - - // Update object fields and mark object as complete - it->handle = handle; - it->size = size; - it->status = true; - - // Check if all object are finalized and reset active group - if(are_all_finalized()) - { - // Update finalized groups - _finalized_groups[_active_group].insert(std::end(_finalized_groups[_active_group]), std::begin(_active_elements), std::end(_active_elements)); - - // Update blobs and group mappings - update_blobs_and_mappings(); - - // Reset state - _active_elements.clear(); - _active_group = nullptr; - } -} - std::unique_ptr<IMemoryPool> BlobLifetimeManager::create_pool(IAllocator *allocator) { ARM_COMPUTE_ERROR_ON(allocator == nullptr); return support::cpp14::make_unique<BlobMemoryPool>(allocator, _blobs); } -bool BlobLifetimeManager::are_all_finalized() const -{ - return !std::any_of(std::begin(_active_elements), std::end(_active_elements), [](const Element e) - { - return !e.status; - }); -} - MappingType BlobLifetimeManager::mapping_type() const { return MappingType::BLOBS; @@ -118,7 +57,7 @@ void BlobLifetimeManager::update_blobs_and_mappings() ARM_COMPUTE_ERROR_ON(!are_all_finalized()); ARM_COMPUTE_ERROR_ON(_active_group == nullptr); - // Sort active group requirements in descending order + // Sort active group requirements in descending order. std::sort(std::begin(_active_elements), std::end(_active_elements), [](const Element & a, const Element & b) { return a.size > b.size; diff --git a/src/runtime/ISimpleLifetimeManager.cpp b/src/runtime/ISimpleLifetimeManager.cpp new file mode 100644 index 0000000000..2c64475b39 --- /dev/null +++ b/src/runtime/ISimpleLifetimeManager.cpp @@ -0,0 +1,103 @@ +/* + * 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. + */ +#include "arm_compute/runtime/ISimpleLifetimeManager.h" + +#include "arm_compute/core/Error.h" +#include "arm_compute/runtime/IAllocator.h" +#include "arm_compute/runtime/IMemoryGroup.h" +#include "arm_compute/runtime/IMemoryPool.h" +#include "support/ToolchainSupport.h" + +#include <algorithm> +#include <cmath> +#include <map> +#include <vector> + +using namespace arm_compute; + +ISimpleLifetimeManager::ISimpleLifetimeManager() + : _active_group(nullptr), _active_elements(), _finalized_groups() +{ +} + +void ISimpleLifetimeManager::register_group(IMemoryGroup *group) +{ + if(_active_group == nullptr) + { + ARM_COMPUTE_ERROR_ON(group == nullptr); + _active_group = group; + } +} + +void ISimpleLifetimeManager::start_lifetime(void *obj) +{ + ARM_COMPUTE_ERROR_ON(obj == nullptr); + ARM_COMPUTE_ERROR_ON_MSG(std::find_if(std::begin(_active_elements), std::end(_active_elements), [&obj](const Element & e) + { + return obj == e.id; + }) != std::end(_active_elements), + "Memory object is already registered!"); + + // Insert object in groups and mark its finalized state to false + _active_elements.emplace_back(obj); +} + +void ISimpleLifetimeManager::end_lifetime(void *obj, void **handle, size_t size) +{ + ARM_COMPUTE_ERROR_ON(obj == nullptr); + + // Find object + auto it = std::find_if(std::begin(_active_elements), std::end(_active_elements), [&obj](const Element & e) + { + return obj == e.id; + }); + ARM_COMPUTE_ERROR_ON(it == std::end(_active_elements)); + + // Update object fields and mark object as complete + it->handle = handle; + it->size = size; + it->status = true; + + // Check if all object are finalized and reset active group + if(are_all_finalized()) + { + // Update finalized groups + _finalized_groups[_active_group].insert(std::end(_finalized_groups[_active_group]), std::begin(_active_elements), std::end(_active_elements)); + + // Update blobs and group mappings + update_blobs_and_mappings(); + + // Reset state + _active_elements.clear(); + _active_group = nullptr; + } +} + +bool ISimpleLifetimeManager::are_all_finalized() const +{ + return !std::any_of(std::begin(_active_elements), std::end(_active_elements), [](const Element e) + { + return !e.status; + }); +} diff --git a/src/runtime/OffsetLifetimeManager.cpp b/src/runtime/OffsetLifetimeManager.cpp new file mode 100644 index 0000000000..4540aeab28 --- /dev/null +++ b/src/runtime/OffsetLifetimeManager.cpp @@ -0,0 +1,76 @@ +/* + * 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. + */ +#include "arm_compute/runtime/OffsetLifetimeManager.h" + +#include "arm_compute/core/Error.h" +#include "arm_compute/runtime/IAllocator.h" +#include "arm_compute/runtime/IMemoryGroup.h" +#include "arm_compute/runtime/OffsetMemoryPool.h" +#include "support/ToolchainSupport.h" + +#include <algorithm> +#include <cmath> +#include <map> +#include <vector> + +using namespace arm_compute; + +OffsetLifetimeManager::OffsetLifetimeManager() + : _blob(0) +{ +} + +std::unique_ptr<IMemoryPool> OffsetLifetimeManager::create_pool(IAllocator *allocator) +{ + ARM_COMPUTE_ERROR_ON(allocator == nullptr); + return support::cpp14::make_unique<OffsetMemoryPool>(allocator, _blob); +} + +MappingType OffsetLifetimeManager::mapping_type() const +{ + return MappingType::OFFSETS; +} + +void OffsetLifetimeManager::update_blobs_and_mappings() +{ + ARM_COMPUTE_ERROR_ON(!are_all_finalized()); + ARM_COMPUTE_ERROR_ON(_active_group == nullptr); + + // Update blob size + size_t max_group_size = std::accumulate(std::begin(_active_elements), std::end(_active_elements), static_cast<size_t>(0), [](size_t s, const Element & e) + { + return s + e.size; + }); + _blob = std::max(_blob, max_group_size); + + // Calculate group mappings + auto &group_mappings = _active_group->mappings(); + size_t offset = 0; + for(auto &e : _active_elements) + { + group_mappings[e.handle] = offset; + offset += e.size; + ARM_COMPUTE_ERROR_ON(offset > _blob); + } +} diff --git a/src/runtime/OffsetMemoryPool.cpp b/src/runtime/OffsetMemoryPool.cpp new file mode 100644 index 0000000000..96f54f890f --- /dev/null +++ b/src/runtime/OffsetMemoryPool.cpp @@ -0,0 +1,80 @@ +/* + * 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. + */ +#include <algorithm> + +#include "arm_compute/runtime/OffsetMemoryPool.h" + +#include "arm_compute/core/Error.h" +#include "arm_compute/runtime/IAllocator.h" +#include "arm_compute/runtime/IMemoryPool.h" +#include "arm_compute/runtime/Types.h" +#include "support/ToolchainSupport.h" + +using namespace arm_compute; + +OffsetMemoryPool::OffsetMemoryPool(IAllocator *allocator, size_t blob_size) + : _allocator(allocator), _blob(), _blob_size(blob_size) +{ + ARM_COMPUTE_ERROR_ON(!allocator); + _blob = _allocator->allocate(_blob_size, 0); +} + +OffsetMemoryPool::~OffsetMemoryPool() +{ + ARM_COMPUTE_ERROR_ON(!_allocator); + _allocator->free(_blob); + _blob = nullptr; +} + +void OffsetMemoryPool::acquire(MemoryMappings &handles) +{ + ARM_COMPUTE_ERROR_ON(_blob == nullptr); + + // Set memory to handlers + for(auto &handle : handles) + { + ARM_COMPUTE_ERROR_ON(handle.first == nullptr); + *handle.first = reinterpret_cast<uint8_t *>(_blob) + handle.second; + } +} + +void OffsetMemoryPool::release(MemoryMappings &handles) +{ + for(auto &handle : handles) + { + ARM_COMPUTE_ERROR_ON(handle.first == nullptr); + *handle.first = nullptr; + } +} + +MappingType OffsetMemoryPool::mapping_type() const +{ + return MappingType::OFFSETS; +} + +std::unique_ptr<IMemoryPool> OffsetMemoryPool::duplicate() +{ + ARM_COMPUTE_ERROR_ON(!_allocator); + return support::cpp14::make_unique<OffsetMemoryPool>(_allocator, _blob_size); +}
\ No newline at end of file |