buddy_allocator.cc 5.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

   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. */

L
liaogang 已提交
15
#include "paddle/memory/detail/buddy_allocator.h"
L
liaogang 已提交
16
#include "glog/logging.h"
17 18 19 20 21

namespace paddle {
namespace memory {
namespace detail {

L
liaogang 已提交
22 23 24 25
BuddyAllocator::BuddyAllocator(SystemAllocator* system_allocator,
                               size_t min_chunk_size, size_t max_chunk_size) {
  PADDLE_ASSERT(min_chunk_size > 0);
  PADDLE_ASSERT(max_chunk_size > 0);
26
  PADDLE_ASSERT(system_allocator != nullptr);
L
liaogang 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

  system_allocator_ = std::move(system_allocator);
  min_chunk_size_ = min_chunk_size;
  max_chunk_size_ = max_chunk_size;
}

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

void* BuddyAllocator::Alloc(size_t unaligned_size) {
  // adjust allocation alignment
  size_t size = align(unaligned_size + sizeof(Metadata), min_chunk_size_);

  // acquire the allocator lock
  std::lock_guard<std::mutex> lock(mutex_);

  DLOG(INFO) << "Allocate " << unaligned_size << " bytes from chunk size "
             << size;

  // if the allocation is huge, send directly to the system allocator
  if (size > max_chunk_size_) {
    DLOG(INFO) << "Allocate from system allocator.";

    return SystemAlloc(size);
  }

  // query and allocate from the existing chunk
  auto it = FindExistChunk(size);

  // refill the pool if failure
  if (it == pool_.end()) {
    it = RefillPool();
L
liaogang 已提交
61 62 63 64
    // if still failure, fail fatally
    if (it == pool_.end()) {
      return nullptr;
    }
L
liaogang 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77
  } else {
    DLOG(INFO) << " Allocation from existing memory block " << std::get<2>(*it)
               << " at address "
               << reinterpret_cast<MemoryBlock*>(std::get<2>(*it))->data();
  }

  total_used_ += size;
  total_free_ -= size;

  // split the allocation and return data for use
  return reinterpret_cast<MemoryBlock*>(SplitToAlloc(it, size))->data();
}

L
liaogang 已提交
78 79 80 81 82 83 84
void BuddyAllocator::Free(void* p) {
  auto block = static_cast<MemoryBlock*>(p)->metadata();

  // acquire the allocator lock
  std::lock_guard<std::mutex> lock(mutex_);
}

L
liaogang 已提交
85 86 87 88 89 90 91 92 93 94 95 96 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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
void* BuddyAllocator::SystemAlloc(size_t size) {
  size_t index = 0;
  void* p = system_allocator_->Alloc(index, size);

  DLOG(INFO) << "Allocated " << p << " from system allocator.";

  if (p == nullptr) return nullptr;

  static_cast<MemoryBlock*>(p)->init(cache_, MemoryBlock::HUGE_CHUNK, index,
                                     size, nullptr, nullptr);

  return static_cast<MemoryBlock*>(p)->data();
}

BuddyAllocator::PoolSet::iterator BuddyAllocator::RefillPool() {
#ifndef PADDLE_ONLY_CPU
  if (system_allocator_->UseGpu()) {
    if ((total_used_ + total_free_) == 0) {
      // Compute the maximum allocation size for the first allocation.
      max_chunk_size_ = platform::GpuMaxChunkSize();
    }
  }
#endif  // PADDLE_ONLY_CPU

  // Allocate a new maximum sized block
  size_t index = 0;
  void* p = system_allocator_->Alloc(index, max_chunk_size_);

  if (p == nullptr) return pool_.end();

  DLOG(INFO) << " Creating and inserting new block " << p
             << " from system allocator";

  static_cast<MemoryBlock*>(p)->init(cache_, MemoryBlock::FREE_CHUNK, index,
                                     max_chunk_size_, nullptr, nullptr);

  total_free_ += max_chunk_size_;

  // dump the block into pool
  return pool_.insert({index, max_chunk_size_, p}).first;
}

BuddyAllocator::PoolSet::iterator BuddyAllocator::FindExistChunk(size_t size) {
  size_t index = 0;

  while (1) {
    auto it = pool_.lower_bound({index, size, nullptr});
    if (it == pool_.end()) return it;

    if (std::get<0>(*it) > index) {
      if (std::get<1>(*it) >= size) {
        return it;
      }

      index = std::get<0>(*it);
      continue;
    }
    return it;
  }
}

void* BuddyAllocator::SplitToAlloc(BuddyAllocator::PoolSet::iterator it,
                                   size_t size) {
  auto block = static_cast<MemoryBlock*>(std::get<2>(*it));
  pool_.erase(it);

  DLOG(INFO) << " Split block (" << block << ", " << block->total_size(cache_)
             << ") into";
  block->split(cache_, size);

  DLOG(INFO) << " Left block (" << block << ", " << block->total_size(cache_)
             << ")";
  block->set_type(cache_, MemoryBlock::ARENA_CHUNK);

  // the rest of memory if exist
  if (block->has_right_buddy(cache_)) {
    if (block->right_buddy(cache_)->type(cache_) == MemoryBlock::FREE_CHUNK) {
      DLOG(INFO) << " Insert right block (" << block->right_buddy(cache_)
                 << ", " << block->right_buddy(cache_)->total_size(cache_)
                 << ")";

      pool_.insert({block->right_buddy(cache_)->index(cache_),
                    block->right_buddy(cache_)->total_size(cache_),
                    block->right_buddy(cache_)});
    }
  }

  return block;
173 174 175 176 177
}

}  // namespace detail
}  // namespace memory
}  // namespace paddle