buddy_allocator.h 2.8 KB
Newer Older
L
liaogang 已提交
1 2
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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

7
   http://www.apache.org/licenses/LICENSE-2.0
L
liaogang 已提交
8

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 "paddle/memory/detail/meta_cache.h"
L
liaogang 已提交
18
#include "paddle/memory/detail/meta_data.h"
L
liaogang 已提交
19
#include "paddle/memory/detail/system_allocator.h"
L
liaogang 已提交
20 21 22
#include "paddle/platform/assert.h"
#include "paddle/platform/cpu_info.h"
#include "paddle/platform/gpu_info.h"
L
liaogang 已提交
23

L
liaogang 已提交
24
#include <mutex>
L
liaogang 已提交
25
#include <set>
L
liaogang 已提交
26
#include <unordered_map>
L
liaogang 已提交
27
#include <vector>
L
liaogang 已提交
28

L
liaogang 已提交
29 30 31 32 33
namespace paddle {
namespace memory {
namespace detail {

class BuddyAllocator {
34
 public:
L
liaogang 已提交
35 36 37
  BuddyAllocator(SystemAllocator* system_allocator, size_t min_chunk_size,
                 size_t max_chunk_size);

38 39
  ~BuddyAllocator();

L
liaogang 已提交
40 41
 public:
  void* Alloc(size_t unaligned_size);
42 43 44
  void Free(void*);
  size_t Used();

45
 private:
L
liaogang 已提交
46 47 48 49
  // Disable copy and assignment.
  BuddyAllocator(const BuddyAllocator&) = delete;
  BuddyAllocator& operator=(const BuddyAllocator&) = delete;

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

L
liaogang 已提交
55 56
  /*! \brief Allocate fixed-size memory from system */
  void* SystemAlloc(size_t size);
57

L
liaogang 已提交
58 59
  /*! \brief If existing chunks are not suitable, refill pool */
  PoolSet::iterator RefillPool();
L
liaogang 已提交
60
  /**
L
liaogang 已提交
61
   *  \brief Find the suitable chunk from existing pool
L
liaogang 已提交
62
   *
L
liaogang 已提交
63 64 65 66
   *  \param it   pool iterator which contains suitable block.
   *  \param size the size of allocation.
   */
  void* SplitToAlloc(PoolSet::iterator it, size_t size);
L
liaogang 已提交
67

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

L
liaogang 已提交
71 72 73 74 75 76 77 78
 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

 private:
79 80 81 82 83
  /**
   * \brief A list of free allocation
   *
   * \note  Only store free chunk memory in pool
   */
L
liaogang 已提交
84 85 86
  PoolSet pool_;

 private:
87
  /*! Unify the metadata format between GPU and CPU allocations */
L
liaogang 已提交
88 89 90
  MetadataCache cache_;

 private:
91
  /*! Allocate CPU/GPU memory from system */
L
liaogang 已提交
92 93
  SystemAllocator* system_allocator_;
  std::mutex mutex_;
L
liaogang 已提交
94 95 96 97 98
};

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