memory_block.cc 4.8 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 25 26 27
  cache->save(
      this, MemoryBlock::Desc(t, index, size - sizeof(MemoryBlock::Desc), size,
                              static_cast<MemoryBlock*>(left_buddy),
                              static_cast<MemoryBlock*>(right_buddy)));
L
liaogang 已提交
28 29
}

Y
Yi Wang 已提交
30
MemoryBlock::Type MemoryBlock::type(const MetadataCache& cache) const {
L
liaogang 已提交
31 32 33
  return cache.load(this).type;
}

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

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

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

Y
Yi Wang 已提交
46 47 48 49 50 51 52 53 54
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 已提交
55 56 57
  return cache.load(this).left_buddy;
}

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

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

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

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

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

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

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

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

  metadata.right_buddy = static_cast<MemoryBlock*>(right_partition);
Y
Yi Wang 已提交
88
  metadata.size = size - sizeof(MemoryBlock::Desc);
L
liaogang 已提交
89 90
  metadata.total_size = size;

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

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

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

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

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

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

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

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

    buddy_metadata.left_buddy = this;

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

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

Y
Yi Wang 已提交
125
  cache->save(this, metadata);
Y
Yi Wang 已提交
126 127
  cache->save(right_buddy,
              MemoryBlock::Desc(INVALID_CHUNK, 0, 0, 0, nullptr, nullptr));
L
liaogang 已提交
128 129
}

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

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

void* MemoryBlock::data() const {
Y
Yi Wang 已提交
144 145 146
  return const_cast<MemoryBlock::Desc*>(
             reinterpret_cast<const MemoryBlock::Desc*>(this)) +
         1;
L
liaogang 已提交
147 148 149 150
}

MemoryBlock* MemoryBlock::metadata() const {
  return const_cast<MemoryBlock*>(reinterpret_cast<const MemoryBlock*>(
Y
Yi Wang 已提交
151
      reinterpret_cast<const MemoryBlock::Desc*>(this) - 1));
L
liaogang 已提交
152 153
}

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