buddy_allocator.h 3.2 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

G
gongweibao 已提交
17
#include <memory>
Y
Yi Wang 已提交
18 19 20 21 22 23 24
#include <mutex>  // NOLINT
#include <set>
#include <tuple>
#include <unordered_map>
#include <vector>

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

namespace paddle {
namespace memory {
namespace detail {

class BuddyAllocator {
35
 public:
G
gongweibao 已提交
36
  BuddyAllocator(std::unique_ptr<SystemAllocator> system_allocator,
S
sneaxiy 已提交
37
                 size_t min_chunk_size, size_t max_chunk_size);
L
liaogang 已提交
38

39 40
  ~BuddyAllocator();

L
liaogang 已提交
41 42
 public:
  void* Alloc(size_t unaligned_size);
L
liaogang 已提交
43
  void Free(void* ptr);
44
  size_t Used();
45 46
  size_t GetMinChunkSize();
  size_t GetMaxChunkSize();
47

L
liaogang 已提交
48
 public:
L
liaogang 已提交
49
  // Disable copy and assignment
L
liaogang 已提交
50 51 52
  BuddyAllocator(const BuddyAllocator&) = delete;
  BuddyAllocator& operator=(const BuddyAllocator&) = delete;

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

L
liaogang 已提交
59
  /*! \brief Allocate fixed-size memory from system */
S
sneaxiy 已提交
60
  void* SystemAlloc(size_t size);
61

L
liaogang 已提交
62 63
  /*! \brief If existing chunks are not suitable, refill pool */
  PoolSet::iterator RefillPool();
L
liaogang 已提交
64

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

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

L
liaogang 已提交
79 80 81 82 83
  /*! \brief Clean idle fallback allocation */
  void CleanIdleFallBackAlloc();

  /*! \brief Clean idle normal allocation */
  void CleanIdleNormalAlloc();
L
liaogang 已提交
84

L
liaogang 已提交
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
S
sneaxiy 已提交
90
  size_t max_chunk_size_;  // the maximum size of each chunk
L
liaogang 已提交
91 92

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

100 101 102
  /*! Record fallback allocation count for auto-scaling */
  size_t fallback_alloc_count_ = 0;

L
liaogang 已提交
103
 private:
104
  /*! Unify the metadata format between GPU and CPU allocations */
L
liaogang 已提交
105 106 107
  MetadataCache cache_;

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

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