memory_block.cc 4.0 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
#include "paddle/fluid/memory/detail/memory_block.h"
16
#include "paddle/fluid/platform/enforce.h"
L
liaogang 已提交
17 18 19 20 21

namespace paddle {
namespace memory {
namespace detail {

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

30 31
MemoryBlock* MemoryBlock::GetLeftBuddy(MetadataCache* cache) {
  return cache->LoadDesc(this)->left_buddy;
L
liaogang 已提交
32 33
}

34 35
MemoryBlock* MemoryBlock::GetRightBuddy(MetadataCache* cache) {
  return cache->LoadDesc(this)->right_buddy;
L
liaogang 已提交
36 37
}

38 39
void MemoryBlock::Split(MetadataCache* cache, size_t size) {
  auto desc = cache->LoadDesc(this);
L
liaogang 已提交
40
  // make sure the split fits
41
  PADDLE_ENFORCE_GE(desc->total_size, size);
L
liaogang 已提交
42 43

  // bail out if there is no room for another partition
44
  if (desc->total_size - size <= sizeof(MemoryBlock::Desc)) {
L
liaogang 已提交
45 46 47 48 49 50
    return;
  }

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

51
  size_t remaining_size = desc->total_size - size;
L
liaogang 已提交
52 53 54

  // Add the new block as a buddy
  // Write the metadata for the new block
55
  auto new_block_right_buddy = desc->right_buddy;
L
liaogang 已提交
56

57 58
  cache->Save(static_cast<MemoryBlock*>(right_partition),
              MemoryBlock::Desc(FREE_CHUNK, desc->index,
Y
Yi Wang 已提交
59 60
                                remaining_size - sizeof(MemoryBlock::Desc),
                                remaining_size, this, new_block_right_buddy));
L
liaogang 已提交
61

62 63 64
  desc->right_buddy = static_cast<MemoryBlock*>(right_partition);
  desc->size = size - sizeof(MemoryBlock::Desc);
  desc->total_size = size;
L
liaogang 已提交
65

66
  desc->UpdateGuards();
L
liaogang 已提交
67 68 69

  // Write metadata for the new block's right buddy
  if (new_block_right_buddy != nullptr) {
70
    auto buddy_desc = cache->LoadDesc(new_block_right_buddy);
L
liaogang 已提交
71

72 73
    buddy_desc->left_buddy = static_cast<MemoryBlock*>(right_partition);
    buddy_desc->UpdateGuards();
L
liaogang 已提交
74 75 76
  }
}

77
void MemoryBlock::Merge(MetadataCache* cache, MemoryBlock* right_buddy) {
L
liaogang 已提交
78
  // only free blocks can be merged
79 80 81 82
  auto desc = cache->LoadDesc(this);
  auto rb_desc = cache->LoadDesc(right_buddy);
  PADDLE_ENFORCE_EQ(desc->type, FREE_CHUNK);
  PADDLE_ENFORCE_EQ(rb_desc->type, FREE_CHUNK);
L
liaogang 已提交
83 84

  // link this->buddy's buddy
85
  desc->right_buddy = rb_desc->right_buddy;
L
liaogang 已提交
86 87

  // link buddy's buddy -> this
88 89
  if (desc->right_buddy != nullptr) {
    auto buddy_metadata = cache->LoadDesc(desc->right_buddy);
L
liaogang 已提交
90

91 92
    buddy_metadata->left_buddy = this;
    buddy_metadata->UpdateGuards();
L
liaogang 已提交
93 94
  }

95 96 97 98
  desc->size += rb_desc->total_size;
  desc->total_size += rb_desc->total_size;

  desc->UpdateGuards();
L
liaogang 已提交
99

100
  cache->Save(right_buddy,
Y
Yi Wang 已提交
101
              MemoryBlock::Desc(INVALID_CHUNK, 0, 0, 0, nullptr, nullptr));
L
liaogang 已提交
102 103
}

104
void MemoryBlock::MarkAsFree(MetadataCache* cache) {
L
liaogang 已提交
105
  // check for double free or corruption
106 107 108 109 110
  auto desc = cache->LoadDesc(this);
  PADDLE_ENFORCE_NE(desc->type, FREE_CHUNK);
  PADDLE_ENFORCE_NE(desc->type, INVALID_CHUNK);
  desc->type = FREE_CHUNK;
  desc->UpdateGuards();
L
liaogang 已提交
111 112
}

113
void* MemoryBlock::Data() const {
Y
Yi Wang 已提交
114 115 116
  return const_cast<MemoryBlock::Desc*>(
             reinterpret_cast<const MemoryBlock::Desc*>(this)) +
         1;
L
liaogang 已提交
117 118
}

119
MemoryBlock* MemoryBlock::Metadata() const {
L
liaogang 已提交
120
  return const_cast<MemoryBlock*>(reinterpret_cast<const MemoryBlock*>(
Y
Yi Wang 已提交
121
      reinterpret_cast<const MemoryBlock::Desc*>(this) - 1));
L
liaogang 已提交
122 123
}

L
liaogang 已提交
124 125 126
}  // namespace detail
}  // namespace memory
}  // namespace paddle