From 8359b415e4fcf759512b2905a3bc105d842076f9 Mon Sep 17 00:00:00 2001 From: Zeng Jinle <32832641+sneaxiy@users.noreply.github.com> Date: Fri, 20 Sep 2019 12:42:51 +0800 Subject: [PATCH] add free chunks to auto growth allocator, test=develop (#19890) --- .../allocation/auto_growth_best_fit_allocator.cc | 15 +++++++++++++++ .../allocation/auto_growth_best_fit_allocator.h | 4 +++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/paddle/fluid/memory/allocation/auto_growth_best_fit_allocator.cc b/paddle/fluid/memory/allocation/auto_growth_best_fit_allocator.cc index 0d3b11f78c5..9ce4fd07829 100644 --- a/paddle/fluid/memory/allocation/auto_growth_best_fit_allocator.cc +++ b/paddle/fluid/memory/allocation/auto_growth_best_fit_allocator.cc @@ -57,6 +57,7 @@ Allocation *AutoGrowthBestFitAllocator::AllocateImpl(size_t size) { block_it->is_free_ = false; } } else { + FreeIdleChunks(); size_t realloc_size = std::max(size, chunk_size_); try { @@ -119,6 +120,20 @@ void AutoGrowthBestFitAllocator::FreeImpl(Allocation *allocation) { delete allocation; } +void AutoGrowthBestFitAllocator::FreeIdleChunks() { + for (auto chunk_it = chunks_.begin(); chunk_it != chunks_.end();) { + auto &blocks = chunk_it->blocks_; + if (blocks.size() == 1 && blocks.begin()->is_free_) { + auto &block = *blocks.begin(); + VLOG(2) << "Free chunk with size " << block.size_; + free_blocks_.erase(std::make_pair(block.size_, block.ptr_)); + chunk_it = chunks_.erase(chunk_it); + } else { + ++chunk_it; + } + } +} + } // namespace allocation } // namespace memory } // namespace paddle diff --git a/paddle/fluid/memory/allocation/auto_growth_best_fit_allocator.h b/paddle/fluid/memory/allocation/auto_growth_best_fit_allocator.h index a31dd7cf773..27257883d55 100644 --- a/paddle/fluid/memory/allocation/auto_growth_best_fit_allocator.h +++ b/paddle/fluid/memory/allocation/auto_growth_best_fit_allocator.h @@ -27,7 +27,7 @@ namespace allocation { class AutoGrowthBestFitAllocator : public Allocator { public: - explicit AutoGrowthBestFitAllocator( + AutoGrowthBestFitAllocator( const std::shared_ptr &underlying_allocator, size_t alignment, size_t chunk_size = 0); @@ -39,6 +39,8 @@ class AutoGrowthBestFitAllocator : public Allocator { void FreeImpl(Allocation *allocation) override; private: + void FreeIdleChunks(); + template using List = std::list; -- GitLab