diff options
Diffstat (limited to 'runtimes/neurun/src/backend/cpu/MemoryPlanner.h')
-rw-r--r-- | runtimes/neurun/src/backend/cpu/MemoryPlanner.h | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/runtimes/neurun/src/backend/cpu/MemoryPlanner.h b/runtimes/neurun/src/backend/cpu/MemoryPlanner.h new file mode 100644 index 000000000..4b2661223 --- /dev/null +++ b/runtimes/neurun/src/backend/cpu/MemoryPlanner.h @@ -0,0 +1,166 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file MemoryPlanner.h + * @brief This file contains Memory Planning related classes + */ + +#ifndef __NEURUN_BACKEND_CPU_MEMORY_PLANNER_H__ +#define __NEURUN_BACKEND_CPU_MEMORY_PLANNER_H__ + +#include <map> +#include <unordered_map> + +#include "model/operand/Index.h" + +namespace neurun +{ +namespace backend +{ +namespace cpu +{ + +/** + * @brief Structure to have memory offset and size + */ +struct Block +{ + uint32_t offset; + uint32_t size; +}; + +/** + * @brief Class to allocate memory + */ +class Allocator +{ +public: + Allocator(uint32_t capacity); + ~Allocator(); + /** + * @brief Get memory base pointer + * @return base pointer + */ + uint8_t *base() const { return _base; } + +private: + uint8_t *_base = nullptr; +}; + +/** + * @brief Interface to plan memory + */ +struct IMemoryPlanner +{ + using MemoryPlans = std::unordered_map<model::operand::Index, Block>; + + /** + * @brief Claim memory for operand + * @param[in] index The operand index + * @param[in] size The size of the memory + */ + virtual void claim(const model::operand::Index &, size_t) = 0; + /** + * @brief Release memory for operand + * @param[in] index The operand index + */ + virtual void release(const model::operand::Index &) = 0; + /** + * @brief Get capacity for memory planning + * @return The value of capacity + */ + virtual uint32_t capacity() = 0; + /** + * @brief Get MemoryPlans + * @return MemoryPlans + */ + virtual MemoryPlans &memory_plans() = 0; +}; + +/** + * @brief Class to plan memory by bump way + */ +class BumpPlanner : public IMemoryPlanner +{ +public: + /** + * @brief Claim memory for operand by bump way + * @param[in] index The operand index + * @param[in] size The size of the memory + */ + virtual void claim(const model::operand::Index &, size_t) override; + /** + * @brief Release memory for operand by bump way + * @param[in] index The operand index + */ + virtual void release(const model::operand::Index &) override; + /** + * @brief Get capacity for memory planning + * @return The value of capacity + */ + virtual uint32_t capacity() override { return _capacity; } + /** + * @brief Get MemoryPlans + * @return MemoryPlans + */ + virtual MemoryPlans &memory_plans() override { return _mem_plans; } + +private: + uint32_t _capacity = 0; + MemoryPlans _mem_plans; +}; + +/** + * @brief Class to plan memory by firstfit way + */ +class FirstFitPlanner : public IMemoryPlanner +{ +public: + /** + * @brief Claim memory for operand by firstfit way + * @param[in] index The operand index + * @param[in] size The size of the memory + */ + virtual void claim(const model::operand::Index &, size_t) override; + /** + * @brief Release memory for operand by firstfit way + * @param[in] index The operand index + */ + virtual void release(const model::operand::Index &) override; + /** + * @brief Get capacity for memory planning + * @return The value of capacity + */ + virtual uint32_t capacity() override { return _capacity; } + /** + * @brief Get MemoryPlans + * @return MemoryPlans + */ + virtual MemoryPlans &memory_plans() override { return _mem_plans; } + +private: + uint32_t _capacity = 0; + MemoryPlans _mem_plans; + // Use std::map because claim() assumes that _claim_table is sorted by uint32_t(base_offset) + std::map<uint32_t, model::operand::Index> _claim_table; +}; + +} // namespace cpu +} // namespace backend +} // namespace neurun + +#endif // __NEURUN_BACKEND_CPU_MEMORY_PLANNER_H__ |