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

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

Y
Yi Wang 已提交
15 16
#include "paddle/fluid/memory/detail/memory_block.h"
#include "paddle/fluid/platform/assert.h"
L
liaogang 已提交
17 18 19 20 21

namespace paddle {
namespace memory {
namespace detail {

Y
Yi Wang 已提交
22
void MemoryBlock::init(MetadataCache* cache, Type t, size_t index, size_t size,
L
liaogang 已提交
23
                       void* left_buddy, void* right_buddy) {
Y
Yi Wang 已提交
24
  cache->save(this, Metadata(t, index, size - sizeof(Metadata), size,
25 26
                             static_cast<MemoryBlock*>(left_buddy),
                             static_cast<MemoryBlock*>(right_buddy)));
L
liaogang 已提交
27 28 29 30 31 32
}

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

Y
Yi Wang 已提交
33
size_t MemoryBlock::size(const MetadataCache& cache) const {
L
liaogang 已提交
34 35 36
  return cache.load(this).size;
}

Y
Yi Wang 已提交
37 38 39 40 41
size_t MemoryBlock::index(const MetadataCache& cache) const {
  return cache.load(this).index;
}

size_t MemoryBlock::total_size(const MetadataCache& cache) const {
L
liaogang 已提交
42 43 44
  return cache.load(this).total_size;
}

Y
Yi Wang 已提交
45 46 47 48 49 50 51 52 53
bool MemoryBlock::has_left_buddy(const MetadataCache& cache) const {
  return left_buddy(cache) != nullptr;
}

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

MemoryBlock* MemoryBlock::left_buddy(const MetadataCache& cache) const {
L
liaogang 已提交
54 55 56
  return cache.load(this).left_buddy;
}

Y
Yi Wang 已提交
57
MemoryBlock* MemoryBlock::right_buddy(const MetadataCache& cache) const {
L
liaogang 已提交
58 59 60
  return cache.load(this).right_buddy;
}

Y
Yi Wang 已提交
61
void MemoryBlock::split(MetadataCache* cache, size_t size) {
L
liaogang 已提交
62
  // make sure the split fits
Y
Yi Wang 已提交
63
  PADDLE_ASSERT(total_size(*cache) >= size);
L
liaogang 已提交
64 65

  // bail out if there is no room for another partition
Y
Yi Wang 已提交
66
  if (total_size(*cache) - size <= sizeof(Metadata)) {
L
liaogang 已提交
67 68 69 70 71 72
    return;
  }

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

Y
Yi Wang 已提交
73
  size_t remaining_size = total_size(*cache) - size;
L
liaogang 已提交
74 75

  // Add the new block as a buddy
Y
Yi Wang 已提交
76
  auto metadata = cache->load(this);
L
liaogang 已提交
77 78 79 80

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

Y
Yi Wang 已提交
81
  cache->save(
82
      static_cast<MemoryBlock*>(right_partition),
Y
Yi Wang 已提交
83
      Metadata(FREE_CHUNK, index(*cache), remaining_size - sizeof(Metadata),
84
               remaining_size, this, new_block_right_buddy));
L
liaogang 已提交
85 86

  metadata.right_buddy = static_cast<MemoryBlock*>(right_partition);
87
  metadata.size = size - sizeof(Metadata);
L
liaogang 已提交
88 89
  metadata.total_size = size;

Y
Yi Wang 已提交
90
  cache->save(this, metadata);
L
liaogang 已提交
91 92 93

  // Write metadata for the new block's right buddy
  if (new_block_right_buddy != nullptr) {
Y
Yi Wang 已提交
94
    auto buddy_metadata = cache->load(new_block_right_buddy);
L
liaogang 已提交
95 96 97

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

Y
Yi Wang 已提交
98
    cache->save(new_block_right_buddy, buddy_metadata);
L
liaogang 已提交
99 100 101
  }
}

Y
Yi Wang 已提交
102
void MemoryBlock::merge(MetadataCache* cache, MemoryBlock* right_buddy) {
L
liaogang 已提交
103
  // only free blocks can be merged
Y
Yi Wang 已提交
104 105
  PADDLE_ASSERT(type(*cache) == FREE_CHUNK);
  PADDLE_ASSERT(right_buddy->type(*cache) == FREE_CHUNK);
L
liaogang 已提交
106

Y
Yi Wang 已提交
107
  auto metadata = cache->load(this);
L
liaogang 已提交
108 109

  // link this->buddy's buddy
Y
Yi Wang 已提交
110
  metadata.right_buddy = right_buddy->right_buddy(*cache);
L
liaogang 已提交
111 112 113

  // link buddy's buddy -> this
  if (metadata.right_buddy != nullptr) {
Y
Yi Wang 已提交
114
    auto buddy_metadata = cache->load(metadata.right_buddy);
L
liaogang 已提交
115 116 117

    buddy_metadata.left_buddy = this;

Y
Yi Wang 已提交
118
    cache->save(metadata.right_buddy, buddy_metadata);
L
liaogang 已提交
119 120
  }

Y
Yi Wang 已提交
121 122
  metadata.size += right_buddy->total_size(*cache);
  metadata.total_size += right_buddy->total_size(*cache);
L
liaogang 已提交
123

Y
Yi Wang 已提交
124 125
  cache->save(this, metadata);
  cache->save(right_buddy, Metadata(INVALID_CHUNK, 0, 0, 0, nullptr, nullptr));
L
liaogang 已提交
126 127
}

Y
Yi Wang 已提交
128
void MemoryBlock::mark_as_free(MetadataCache* cache) {
L
liaogang 已提交
129
  // check for double free or corruption
Y
Yi Wang 已提交
130 131
  PADDLE_ASSERT(type(*cache) != FREE_CHUNK);
  PADDLE_ASSERT(type(*cache) != INVALID_CHUNK);
132
  set_type(cache, FREE_CHUNK);
L
liaogang 已提交
133 134
}

Y
Yi Wang 已提交
135 136
void MemoryBlock::set_type(MetadataCache* cache, Type t) {
  auto metadata = cache->load(this);
L
liaogang 已提交
137
  metadata.type = t;
Y
Yi Wang 已提交
138
  cache->save(this, metadata);
L
liaogang 已提交
139 140 141
}

void* MemoryBlock::data() const {
142
  return const_cast<Metadata*>(reinterpret_cast<const Metadata*>(this)) + 1;
L
liaogang 已提交
143 144 145 146
}

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

L
liaogang 已提交
150 151 152
}  // namespace detail
}  // namespace memory
}  // namespace paddle