buddy_allocator.h 3.5 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>
G
gongweibao 已提交
18
#include <memory>
Y
Yi Wang 已提交
19 20 21 22 23 24 25
#include <mutex>  // NOLINT
#include <set>
#include <tuple>
#include <unordered_map>
#include <vector>

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

namespace paddle {
namespace memory {
namespace detail {

class BuddyAllocator {
36
 public:
G
gongweibao 已提交
37
  BuddyAllocator(std::unique_ptr<SystemAllocator> system_allocator,
38 39
                 size_t min_chunk_size, size_t max_chunk_size,
                 size_t extra_padding_size = 0);
L
liaogang 已提交
40

41 42
  ~BuddyAllocator();

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

L
liaogang 已提交
52
 public:
L
liaogang 已提交
53
  // Disable copy and assignment
L
liaogang 已提交
54 55 56
  BuddyAllocator(const BuddyAllocator&) = delete;
  BuddyAllocator& operator=(const BuddyAllocator&) = delete;

57
 private:
58
  // Tuple (allocator index, memory size, memory address)
L
liaogang 已提交
59
  using IndexSizeAddress = std::tuple<size_t, size_t, void*>;
60
  // Each element in PoolSet is a free allocation
L
liaogang 已提交
61
  using PoolSet = std::set<IndexSizeAddress>;
62

L
liaogang 已提交
63 64
  /*! \brief Allocate fixed-size memory from system */
  void* SystemAlloc(size_t size);
65

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

L
liaogang 已提交
69
  /**
L
liaogang 已提交
70 71 72 73 74
   *  \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 已提交
75
   *
L
liaogang 已提交
76
   *  \return  the left buddy address
L
liaogang 已提交
77 78
   */
  void* SplitToAlloc(PoolSet::iterator it, size_t size);
L
liaogang 已提交
79

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

L
liaogang 已提交
83 84 85 86 87 88 89
 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

90 91 92
  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 已提交
93

L
liaogang 已提交
94
 private:
95 96 97 98 99
  /**
   * \brief A list of free allocation
   *
   * \note  Only store free chunk memory in pool
   */
L
liaogang 已提交
100 101
  PoolSet pool_;

102 103 104 105 106
  /**
   * \brief Record the allocated chunks when Refill pool.
   */
  PoolSet chunks_;

L
liaogang 已提交
107
 private:
108
  /*! Unify the metadata format between GPU and CPU allocations */
L
liaogang 已提交
109 110 111
  MetadataCache cache_;

 private:
112
  /*! Allocate CPU/GPU memory from system */
G
gongweibao 已提交
113
  std::unique_ptr<SystemAllocator> system_allocator_;
L
liaogang 已提交
114
  std::mutex mutex_;
L
liaogang 已提交
115 116 117 118 119
};

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