auto_growth_best_fit_allocator.cc 4.6 KB
Newer Older
S
sneaxiy 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2019 PaddlePaddle Authors. 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.

S
sneaxiy 已提交
15
#include "paddle/fluid/memory/allocation/auto_growth_best_fit_allocator.h"
S
sneaxiy 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include <algorithm>
#include <list>
#include <map>
#include <memory>
#include <mutex>  // NOLINT
#include <unordered_map>

namespace paddle {
namespace memory {
namespace allocation {

static size_t align(size_t size, size_t alignment) {
  auto remaining = size % alignment;
  return remaining == 0 ? size : size + alignment - remaining;
}

S
sneaxiy 已提交
32
AutoGrowthBestFitAllocator::AutoGrowthBestFitAllocator(
S
sneaxiy 已提交
33 34 35 36 37 38
    const std::shared_ptr<Allocator> &underlying_allocator, size_t chunk_size,
    size_t alignment)
    : underlying_allocator_(underlying_allocator),
      chunk_size_(align(chunk_size, alignment)),
      alignment_(alignment) {}

S
sneaxiy 已提交
39
Allocation *AutoGrowthBestFitAllocator::AllocateImpl(size_t size, Attr attr) {
S
sneaxiy 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  size = align(size, alignment_);
  std::lock_guard<std::mutex> guard(mtx_);
  auto iter = free_blocks_.lower_bound(std::make_pair(size, nullptr));
  BlockIt block_it;
  if (iter != free_blocks_.end()) {
    VLOG(2) << "Found " << iter->second->size_ << " for " << size;
    block_it = iter->second;
    free_blocks_.erase(iter);
    auto *chunk = block_it->chunk_;
    size_t remaining_size = block_it->size_ - size;
    if (remaining_size == 0) {
      block_it->is_free_ = false;
      VLOG(2) << "Found and no remaining";
    } else {
      auto remaining_free_block = chunk->blocks_.insert(
          block_it, Chunk::Block(block_it->ptr_, remaining_size, true, chunk));
      free_blocks_.emplace(std::make_pair(remaining_size, block_it->ptr_),
                           remaining_free_block);
      block_it->ptr_ =
          reinterpret_cast<uint8_t *>(block_it->ptr_) + remaining_size;
      block_it->size_ = size;
      block_it->is_free_ = false;
      VLOG(2) << "Found and remaining " << remaining_size;
    }
  } else {
    size_t alloc_size = size;
    if (!underlying_allocator_exhaustive_ && chunk_size_ > size) {
      alloc_size = chunk_size_;
    }

    try {
      chunks_.emplace_back(underlying_allocator_->Allocate(alloc_size, attr));
    } catch (BadAlloc &ex) {
      if (size == alloc_size) throw ex;
      underlying_allocator_exhaustive_ = true;
      alloc_size = size;
      chunks_.emplace_back(underlying_allocator_->Allocate(alloc_size, attr));
    }
    auto *chunk = &(*chunks_.rbegin());
    uint8_t *p = reinterpret_cast<uint8_t *>(chunk->allocation_->ptr());
    auto &blocks = chunk->blocks_;

    size_t remaining_size = alloc_size - size;
    if (remaining_size > 0) {
      blocks.emplace_back(p, remaining_size, true, chunk);
      free_blocks_.emplace(std::make_pair(remaining_size, p), --(blocks.end()));
    }
    blocks.emplace_back(p + remaining_size, size, false, chunk);
    block_it = --(blocks.end());
    VLOG(2) << "Not found and allocate " << alloc_size << ", and remaining "
            << remaining_size;
  }
  VLOG(2) << "After allocate, free blocks " << free_blocks_.size();
  return new Chunk::BlockAllocation(block_it);
}

S
sneaxiy 已提交
96
void AutoGrowthBestFitAllocator::FreeImpl(Allocation *allocation) {
S
sneaxiy 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  auto &block_it = static_cast<Chunk::BlockAllocation *>(allocation)->block_it_;
  auto &blocks = block_it->chunk_->blocks_;

  std::lock_guard<std::mutex> guard(mtx_);
  block_it->is_free_ = true;

  if (block_it != blocks.begin()) {
    auto prev_it = block_it;
    --prev_it;

    if (prev_it->is_free_) {
      free_blocks_.erase(std::make_pair(prev_it->size_, prev_it->ptr_));
      prev_it->size_ += block_it->size_;
      blocks.erase(block_it);
      block_it = prev_it;
    }
  }

  auto next_it = block_it;
  ++next_it;

  if (next_it != blocks.end() && next_it->is_free_) {
    free_blocks_.erase(std::make_pair(next_it->size_, next_it->ptr_));
    block_it->size_ += next_it->size_;
    blocks.erase(next_it);
  }

  free_blocks_.emplace(std::make_pair(block_it->size_, block_it->ptr_),
                       block_it);

  VLOG(2) << "Combine " << block_it->size_ << ", " << blocks.size() << ", "
          << free_blocks_.size();
  delete allocation;
}

}  // namespace allocation
}  // namespace memory
}  // namespace paddle