buddy_allocator.h 4.1 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
L
liaogang 已提交
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
L
liaogang 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
L
liaogang 已提交
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. */
L
liaogang 已提交
14 15 16

#pragma once

17
#include <stdint.h>
Y
Yang 已提交
18 19 20

#include <functional>
#include <map>
G
gongweibao 已提交
21
#include <memory>
Y
Yi Wang 已提交
22 23 24
#include <mutex>  // NOLINT
#include <set>
#include <tuple>
Y
Yang 已提交
25
#include <utility>
Y
Yi Wang 已提交
26 27 28
#include <vector>

#include "paddle/fluid/memory/detail/memory_block.h"
Y
Yi Wang 已提交
29 30
#include "paddle/fluid/memory/detail/system_allocator.h"
#include "paddle/fluid/platform/cpu_info.h"
31
#include "paddle/fluid/platform/device/gpu/gpu_info.h"
32
#include "paddle/fluid/platform/device/npu/npu_info.h"
L
liaogang 已提交
33 34 35 36 37 38

namespace paddle {
namespace memory {
namespace detail {

class BuddyAllocator {
39
 public:
G
gongweibao 已提交
40
  BuddyAllocator(std::unique_ptr<SystemAllocator> system_allocator,
41
                 size_t min_chunk_size, size_t max_chunk_size,
42 43
                 size_t extra_padding_size = 0,
                 const std::string dev_type = "");
L
liaogang 已提交
44

45 46
  ~BuddyAllocator();

L
liaogang 已提交
47 48
 public:
  void* Alloc(size_t unaligned_size);
L
liaogang 已提交
49
  void Free(void* ptr);
50
  // Release the unused memory pool, a real free operation for the OS.
W
Wilber 已提交
51
  uint64_t Release();
52
  size_t Used();
53 54
  size_t GetMinChunkSize();
  size_t GetMaxChunkSize();
55

L
liaogang 已提交
56
 public:
L
liaogang 已提交
57
  // Disable copy and assignment
L
liaogang 已提交
58 59 60
  BuddyAllocator(const BuddyAllocator&) = delete;
  BuddyAllocator& operator=(const BuddyAllocator&) = delete;

61
 private:
62
  // Tuple (allocator index, memory size, memory address)
L
liaogang 已提交
63
  using IndexSizeAddress = std::tuple<size_t, size_t, void*>;
64
  // Each element in PoolSet is a free allocation
L
liaogang 已提交
65
  using PoolSet = std::set<IndexSizeAddress>;
Y
Yang 已提交
66 67 68
  // Each element in PoolMap is an allocation record
  // key: <size, ptr>, value: index
  using PoolMap = std::map<std::pair<size_t, void*>, size_t>;
69

L
liaogang 已提交
70 71
  /*! \brief Allocate fixed-size memory from system */
  void* SystemAlloc(size_t size);
72

L
liaogang 已提交
73
  /*! \brief If existing chunks are not suitable, refill pool */
74
  PoolSet::iterator RefillPool(size_t request_bytes);
L
liaogang 已提交
75

L
liaogang 已提交
76
  /**
L
liaogang 已提交
77 78 79 80 81
   *  \brief   Find the suitable chunk from existing pool and split
   *           it to left and right buddies
   *
   *  \param   it     the iterator of pool list
   *  \param   size   the size of allocation
L
liaogang 已提交
82
   *
L
liaogang 已提交
83
   *  \return  the left buddy address
L
liaogang 已提交
84 85
   */
  void* SplitToAlloc(PoolSet::iterator it, size_t size);
L
liaogang 已提交
86

L
liaogang 已提交
87
  /*! \brief Find the existing chunk which used to allocation */
L
liaogang 已提交
88
  PoolSet::iterator FindExistChunk(size_t size);
L
liaogang 已提交
89

Y
Yang 已提交
90 91 92 93 94
  /*! \brief Allocate bytes from the device */
  size_t DeviceAllocateSize(std::function<size_t()> init_allocate_size_func,
                            std::function<size_t()> re_allocate_size_func,
                            size_t request_bytes);

L
liaogang 已提交
95 96 97 98 99 100 101
 private:
  size_t total_used_ = 0;  // the total size of used memory
  size_t total_free_ = 0;  // the total size of free memory

  size_t min_chunk_size_;  // the minimum size of each chunk
  size_t max_chunk_size_;  // the maximum size of each chunk

102 103 104
  size_t realloc_size_ = 0;        // the size of re-allocated chunk
  size_t extra_padding_size_ = 0;  // the size of padding to the size of memory
                                   // to alloc, especially used in NPU
Z
zhhsplendid 已提交
105

L
liaogang 已提交
106
 private:
107 108 109 110 111
  /**
   * \brief A list of free allocation
   *
   * \note  Only store free chunk memory in pool
   */
L
liaogang 已提交
112 113
  PoolSet pool_;

114 115 116
  /**
   * \brief Record the allocated chunks when Refill pool.
   */
Y
Yang 已提交
117
  PoolMap chunks_;
118

L
liaogang 已提交
119
 private:
120
  /*! Unify the metadata format between GPU and CPU allocations */
L
liaogang 已提交
121 122 123
  MetadataCache cache_;

 private:
124
  /*! Allocate CPU/GPU memory from system */
G
gongweibao 已提交
125
  std::unique_ptr<SystemAllocator> system_allocator_;
L
liaogang 已提交
126
  std::mutex mutex_;
127 128 129
#ifdef PADDLE_WITH_CUSTOM_DEVICE
  std::function<size_t()> init_allocate_size_func_, re_allocate_size_func_;
#endif
L
liaogang 已提交
130 131 132 133 134
};

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