From: 김용섭/동작제어Lab(SR)/Engineer/삼성전자 Date: Thu, 8 Nov 2018 01:09:12 +0000 (+0900) Subject: [neurun] Introduce MemoryPlanner and Allocator (#3511) X-Git-Tag: 0.3~451 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d97988e882c00e780dd7f35f3f7a13852dbb11b0;p=platform%2Fcore%2Fml%2Fnnfw.git [neurun] Introduce MemoryPlanner and Allocator (#3511) Introduces IMemoryPlanner, BumpPlanner, FirstFitPlanner and Allocator to replace IMemoryAllocator. Current IMemoryAllocator has two roles, Planner and Allocator. This commits separates IMemoryAllocator to the two classes. After this commit lands, IMemoryAllocator will be removed soon. Signed-off-by: Yongseop Kim --- diff --git a/runtimes/neurun/src/backend/cpu/MemoryPlanner.cc b/runtimes/neurun/src/backend/cpu/MemoryPlanner.cc new file mode 100644 index 0000000..c24102b --- /dev/null +++ b/runtimes/neurun/src/backend/cpu/MemoryPlanner.cc @@ -0,0 +1,127 @@ +/* + * 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. + */ + +#include "MemoryPlanner.h" +#include "logging.h" +#include + +namespace neurun +{ +namespace backend +{ +namespace cpu +{ + +Allocator::Allocator(uint32_t capacity) +{ + assert(!_base && capacity != 0); + + _base = new uint8_t[capacity]; + + VERBOSE(ALLOC) << "allocation capacity: " << capacity << std::endl; + VERBOSE(ALLOC) << "base pointer: " << static_cast(_base) << std::endl; +} + +Allocator::~Allocator() { delete[] _base; } + +void BumpPlanner::claim(const graph::operand::Index &ind, size_t size) +{ + assert(size != 0); + + Block blk{_capacity, size}; + _mem_plans[ind] = blk; + _capacity += size; + + VERBOSE(BP_PLANNER) << "CLAIM(#" << ind.value() << "): " << blk.offset << ", " << blk.size + << std::endl; +} + +void BumpPlanner::release(const graph::operand::Index &ind) +{ + VERBOSE(BP_PLANNER) << "RELEASE(#" << ind.value() << "): " + << "NOTHING does" << std::endl; +} + +// There are some assumptions for claiming memory(== making a reservation for memory). +// 1. About _claim_table(std::map). +// - The table's data structure is std::map so that it always sorts +// value(graph::operand::Index) by key(base_offset). +// - This claim() inserts key/value into _claim_table and the release() removes the key/value from +// _claim_table. +// - _claim_table shows the memory status at a certain point in time. Therefore, +// - If _claim_table has an offset and a certain size at a certain point in time, +// it means the place at the offset has been already claimed(== can't claim now. need to find +// someplace new). +// - If _claim_table doesn't have any element for an offset and a certain size at a certain +// point in time, it means the place at the offset can be claimed. +// 2. In the loop for _claim_table, we can assume the current claim_base_offset value is bigger than +// the previous claim_base_offset. +void FirstFitPlanner::claim(const graph::operand::Index &ind, size_t size) +{ + assert(size != 0); + + // Find the right position for claiming + uint32_t next_offset = 0; + for (auto &mem_claim : _claim_table) + { + auto claimed_base_offset = mem_claim.first; + auto claimed_size = _mem_plans[mem_claim.second].size; + if (next_offset + size <= claimed_base_offset) + { + break; + } + else + { + next_offset = claimed_base_offset + claimed_size; + } + } + + // Now next_offset is set to the proper offset + _claim_table[next_offset] = ind; + _mem_plans[ind] = {next_offset, size}; + + VERBOSE(FF_PLANNER) << "claim(#" << ind.value() << "): [+" << next_offset << ", " << size << "sz]" + << std::endl; + + if (_capacity < next_offset + size) + { + _capacity = next_offset + size; + } +} + +void FirstFitPlanner::release(const graph::operand::Index &ind) +{ + for (auto it = _claim_table.cbegin(); it != _claim_table.cend(); ++it) + { + if (it->second == ind) + { + uint32_t offset = it->first; + uint32_t index = ind.value(); + uint32_t size = _mem_plans[ind].size; + + _claim_table.erase(it); + + VERBOSE(FF_ALLOC) << "release(#" << index << "): [+" << offset << ", " << size << "sz]" + << std::endl; + return; + } + } + assert(false && "CAN'T ENTER HERE"); +} + +} // namespace cpu +} // namespace backend +} // namespace neurun diff --git a/runtimes/neurun/src/backend/cpu/MemoryPlanner.h b/runtimes/neurun/src/backend/cpu/MemoryPlanner.h new file mode 100644 index 0000000..a7e92d1 --- /dev/null +++ b/runtimes/neurun/src/backend/cpu/MemoryPlanner.h @@ -0,0 +1,91 @@ +/* + * 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. + */ + +#ifndef __NEURUN_BACKEND_CPU_MEMORY_PLANNER_H__ +#define __NEURUN_BACKEND_CPU_MEMORY_PLANNER_H__ + +#include +#include + +#include "graph/operand/Index.h" + +namespace neurun +{ +namespace backend +{ +namespace cpu +{ + +struct Block +{ + uint32_t offset; + uint32_t size; +}; + +class Allocator +{ +public: + Allocator(uint32_t capacity); + ~Allocator(); + uint8_t *base() const { return _base; } + +private: + uint8_t *_base = nullptr; +}; + +struct IMemoryPlanner +{ + using MemoryPlans = std::unordered_map; + + virtual void claim(const graph::operand::Index &, size_t) = 0; + virtual void release(const graph::operand::Index &) = 0; + virtual uint32_t capacity() = 0; + virtual MemoryPlans &memory_plans() = 0; +}; + +class BumpPlanner : public IMemoryPlanner +{ +public: + virtual void claim(const graph::operand::Index &, size_t) override; + virtual void release(const graph::operand::Index &) override; + virtual uint32_t capacity() override { return _capacity; } + virtual MemoryPlans &memory_plans() override { return _mem_plans; } + +private: + uint32_t _capacity = 0; + MemoryPlans _mem_plans; +}; + +class FirstFitPlanner : public IMemoryPlanner +{ +public: + virtual void claim(const graph::operand::Index &, size_t) override; + virtual void release(const graph::operand::Index &) override; + virtual uint32_t capacity() override { return _capacity; } + 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 _claim_table; +}; + +} // namespace cpu +} // namespace backend +} // namespace neurun + +#endif // __NEURUN_BACKEND_CPU_MEMORY_PLANNER_H__ diff --git a/runtimes/neurun/test/backend/cpu/MemoryPlanner.cc b/runtimes/neurun/test/backend/cpu/MemoryPlanner.cc new file mode 100644 index 0000000..69d17d1 --- /dev/null +++ b/runtimes/neurun/test/backend/cpu/MemoryPlanner.cc @@ -0,0 +1,127 @@ +/* + * 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. + */ + +#include + +#include "backend/cpu/MemoryPlanner.h" +#include "graph/operand/Index.h" + +TEST(Allocator, allocate_test) +{ + ::neurun::backend::cpu::Allocator allocator(1024); + ASSERT_NE(allocator.base(), nullptr); +} + +TEST(BumpPlanner, claim_test) +{ + ::neurun::backend::cpu::BumpPlanner planner; + + auto claim = [&planner](uint32_t index, size_t size, uint32_t expected_offset) { + ::neurun::graph::operand::Index mem_idx(index); + planner.claim(mem_idx, size); + auto mem_blk = planner.memory_plans()[mem_idx]; + ASSERT_EQ(mem_blk.offset, expected_offset); + ASSERT_EQ(mem_blk.size, size); + }; + + claim(0, 10, 0); + claim(1, 20, 10); + claim(2, 30, 30); +} + +TEST(FirstFitAllocator, claim_release_test) +{ + ::neurun::backend::cpu::FirstFitPlanner planner; + + auto claim = [&planner](uint32_t index, size_t size, uint32_t expected_offset) { + ::neurun::graph::operand::Index mem_idx(index); + planner.claim(mem_idx, size); + auto mem_blk = planner.memory_plans()[mem_idx]; + ASSERT_EQ(mem_blk.offset, expected_offset); + ASSERT_EQ(mem_blk.size, size); + }; + + auto release = [&planner](uint32_t index) { + ::neurun::graph::operand::Index mem_idx(index); + planner.release(mem_idx); + }; + + // 0 CLAIM - 10 + claim(0, 10, 0); + + // 1 CLAIM - 20 + claim(1, 20, 10); + + // 2 CLAIM - 30 + claim(2, 30, 30); + + // 0 RELEASE - 10 + release(0); + + // 3 CLAIM - 20 + claim(3, 20, 60); + + // 4 CLAIM - 5 + claim(4, 5, 0); + + // 5 CLAIM - 10 + claim(5, 10, 80); + + // 6 CLAIM - 5 + claim(6, 5, 5); + + // 2 RELEASE - 30 + release(2); + + // 7 CLAIM - 35 + claim(7, 35, 90); + + // 8 CLAIM - 10 + claim(8, 10, 30); + + // 4 RELEASE - 5 + release(4); + + // 9 CLAIM - 10 + claim(9, 10, 40); + + // 10 CLAIM - 10 + claim(10, 10, 50); + + // 6 RELEASE + release(6); + + // 1 RELEASE + release(1); + + // 8 RELEASE + release(8); + + // 9 RELEASE + release(9); + + // 10 RELEASE + release(10); + + // 3 RELEASE + release(3); + + // 5 RELEASE + release(5); + + // 7 RELEASE + release(7); +}