memory_block.cc 4.7 KB
Newer Older
1 2
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

L
Luo Tao 已提交
3 4 5
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
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
14

L
liaogang 已提交
15
#include "paddle/memory/detail/memory_block.h"
16 17
#include "paddle/memory/detail/meta_cache.h"
#include "paddle/memory/detail/meta_data.h"
L
liaogang 已提交
18 19 20 21 22 23 24 25
#include "paddle/platform/assert.h"

namespace paddle {
namespace memory {
namespace detail {

void MemoryBlock::init(MetadataCache& cache, Type t, size_t index, size_t size,
                       void* left_buddy, void* right_buddy) {
26 27 28
  cache.store(this, Metadata(t, index, size - sizeof(Metadata), size,
                             static_cast<MemoryBlock*>(left_buddy),
                             static_cast<MemoryBlock*>(right_buddy)));
L
liaogang 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
}

MemoryBlock::Type MemoryBlock::type(MetadataCache& cache) const {
  return cache.load(this).type;
}

size_t MemoryBlock::size(MetadataCache& cache) const {
  return cache.load(this).size;
}

size_t MemoryBlock::total_size(MetadataCache& cache) const {
  return cache.load(this).total_size;
}

MemoryBlock* MemoryBlock::left_buddy(MetadataCache& cache) const {
  return cache.load(this).left_buddy;
}

MemoryBlock* MemoryBlock::right_buddy(MetadataCache& cache) const {
  return cache.load(this).right_buddy;
}

void MemoryBlock::split(MetadataCache& cache, size_t size) {
  // make sure the split fits
53
  PADDLE_ASSERT(total_size(cache) >= size);
L
liaogang 已提交
54 55

  // bail out if there is no room for another partition
56
  if (total_size(cache) - size <= sizeof(Metadata)) {
L
liaogang 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70
    return;
  }

  // find the position of the split
  void* right_partition = reinterpret_cast<uint8_t*>(this) + size;

  size_t remaining_size = total_size(cache) - size;

  // Add the new block as a buddy
  auto metadata = cache.load(this);

  // Write the metadata for the new block
  auto new_block_right_buddy = metadata.right_buddy;

71 72 73 74
  cache.store(
      static_cast<MemoryBlock*>(right_partition),
      Metadata(FREE_CHUNK, index(cache), remaining_size - sizeof(Metadata),
               remaining_size, this, new_block_right_buddy));
L
liaogang 已提交
75 76

  metadata.right_buddy = static_cast<MemoryBlock*>(right_partition);
77
  metadata.size = size - sizeof(Metadata);
L
liaogang 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
  metadata.total_size = size;

  cache.store(this, metadata);

  // Write metadata for the new block's right buddy
  if (new_block_right_buddy != nullptr) {
    auto buddy_metadata = cache.load(new_block_right_buddy);

    buddy_metadata.left_buddy = static_cast<MemoryBlock*>(right_partition);

    cache.store(new_block_right_buddy, buddy_metadata);
  }
}

void MemoryBlock::merge(MetadataCache& cache, MemoryBlock* right_buddy) {
  // only free blocks can be merged
94 95
  PADDLE_ASSERT(type(cache) == FREE_CHUNK);
  PADDLE_ASSERT(right_buddy->type(cache) == FREE_CHUNK);
L
liaogang 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

  auto metadata = cache.load(this);

  // link this->buddy's buddy
  metadata.right_buddy = right_buddy->right_buddy(cache);

  // link buddy's buddy -> this
  if (metadata.right_buddy != nullptr) {
    auto buddy_metadata = cache.load(metadata.right_buddy);

    buddy_metadata.left_buddy = this;

    cache.store(metadata.right_buddy, buddy_metadata);
  }

  metadata.size += right_buddy->total_size(cache);
  metadata.total_size += right_buddy->total_size(cache);

  cache.store(this, metadata);
115
  cache.store(right_buddy, Metadata(INVALID_CHUNK, 0, 0, 0, nullptr, nullptr));
L
liaogang 已提交
116 117 118 119
}

void MemoryBlock::mark_as_free(MetadataCache& cache) {
  // check for double free or corruption
120 121
  PADDLE_ASSERT(type(cache) != FREE_CHUNK);
  PADDLE_ASSERT(type(cache) != INVALID_CHUNK);
L
liaogang 已提交
122

123
  set_type(cache, FREE_CHUNK);
L
liaogang 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
}

void MemoryBlock::set_type(MetadataCache& cache, Type t) {
  auto metadata = cache.load(this);

  metadata.type = t;

  cache.store(this, metadata);
}

bool MemoryBlock::has_left_buddy(MetadataCache& cache) const {
  return left_buddy(cache) != nullptr;
}

bool MemoryBlock::has_right_buddy(MetadataCache& cache) const {
  return right_buddy(cache) != nullptr;
}

size_t MemoryBlock::index(MetadataCache& cache) const {
  return cache.load(this).index;
}

void* MemoryBlock::data() const {
147
  return const_cast<Metadata*>(reinterpret_cast<const Metadata*>(this)) + 1;
L
liaogang 已提交
148 149 150 151
}

MemoryBlock* MemoryBlock::metadata() const {
  return const_cast<MemoryBlock*>(reinterpret_cast<const MemoryBlock*>(
152
      reinterpret_cast<const Metadata*>(this) - 1));
L
liaogang 已提交
153 154
}

L
liaogang 已提交
155 156 157
}  // namespace detail
}  // namespace memory
}  // namespace paddle