ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
bfc_allocator.h
Go to the documentation of this file.
1#ifndef BASE_CORE_BFC_ALLOCATOR_H_
2#define BASE_CORE_BFC_ALLOCATOR_H_
3
5
6namespace base {
7namespace core {
14class BFCAllocator : public Allocator {
15public:
16
17 struct Options {
18 bool allow_growth = true;
19 double fragment_fraction = 0.0;
20 };
21
22 BFCAllocator(std::unique_ptr<Allocator> sub_alloc, const size_t& total_memory, const Options& options = Options());
23
32 void* allocate(size_t size) override;
33
42 void* allocate(size_t size, size_t alignment) override;
43
49 void free(void* ptr) override;
50
56 DeviceType GetDeviceType() override;
57
58 private:
59
60 // The sub allocator to use for extending the BFC's memory pool.
61 std::unique_ptr<Allocator> sub_alloc_;
62
63 struct bin;
64 mutable std::mutex mtx_;
65
66 // A chunk_handle is an index into the chunks_ vector in BFCAllocator
67 // kInvalidChunkHandle means an invalid chunk index.
68 typedef size_t chunk_handle_t;
69 static constexpr chunk_handle_t kInvalidChunkHandle = UINT64_MAX;
70
71 typedef int bin_index_t;
72 static constexpr int kInvalidBinNum = -1;
73 // The following means that the largest bin'd chunk size is 256 << 21 = 512MB.
74 static constexpr int kNumBins = 21;
75
76 struct chunk {
77 // The size of the chunk in bytes.
78 size_t size = 0;
79 // The bin index of the chunk.
81 // We sometimes give chunks that are larger than needed to reduce
82 // fragmentation. requested_size keeps track of what the client
83 // actually wanted so we can understand whether our splitting
84 // strategy is efficient.
85 size_t requested_size = 0;
86 // allocation_id is set to -1 when the chunk is not in use. It is assigned a
87 // value greater than zero before the chunk is returned from
88 // AllocateRaw, and this value is unique among values assigned by
89 // the parent allocator.
90 int64_t allocation_id = -1;
91 // pointer to granted subbuffer.
92 void* ptr = nullptr;
94 // The handle of the previous chunk in the bin.
96 // Whether the chunk is allocated.
97 bool allocated() const { return allocation_id > 0; }
98 };
99
100 struct bin {
101 // The size of the chunks in this bin.
102 size_t bin_size = 0;
103 // The number of chunks in this bin.
104 size_t num_chunks = 0;
105 // The handle of the first chunk in the bin.
107 // The handle of the last chunk in the bin.
109
111 public:
112 explicit chunk_comparator(BFCAllocator* allocator) : allocator_(allocator) {}
113 // Sort first by size and then use pointer address as a tie breaker.
115 const chunk_handle_t hb) const {
116 const chunk* a = allocator_->chunk_from_handle(ha);
117 const chunk* b = allocator_->chunk_from_handle(hb);
118 if (a->size != b->size) {
119 return a->size < b->size;
120 }
121 return a->ptr < b->ptr;
122 }
123
124 private:
125 BFCAllocator* allocator_; // The parent allocator
126 };
127
128 using free_chunk_set_t = std::set<ChunkHandle, ChunkComparator>;
129 };
130
131
132};
133
134} // namespace core
135} // namespace base
136
137#endif // BASE_CORE_BFC_ALLOCATOR_H_
An abstract base class for memory allocators.
Definition allocator.h:17
chunk_comparator(BFCAllocator *allocator)
Definition bfc_allocator.h:112
BFCAllocator * allocator_
Definition bfc_allocator.h:125
bool operator()(const chunk_handle_t ha, const chunk_handle_t hb) const
Definition bfc_allocator.h:114
An allocator that allocates memory on a GPU device.
Definition bfc_allocator.h:14
void * allocate(size_t size) override
Allocate a block of memory with the given size and default alignment on GPU.
std::unique_ptr< Allocator > sub_alloc_
Definition bfc_allocator.h:61
int bin_index_t
Definition bfc_allocator.h:71
void * allocate(size_t size, size_t alignment) override
Allocate a block of memory with the given size and alignment on GPU.
static constexpr chunk_handle_t kInvalidChunkHandle
Definition bfc_allocator.h:69
std::mutex mtx_
Definition bfc_allocator.h:64
void free(void *ptr) override
Free a block of GPU memory that was previously allocated by this allocator.
static constexpr int kInvalidBinNum
Definition bfc_allocator.h:72
static constexpr int kNumBins
Definition bfc_allocator.h:74
BFCAllocator(std::unique_ptr< Allocator > sub_alloc, const size_t &total_memory, const Options &options=Options())
size_t chunk_handle_t
Definition bfc_allocator.h:68
DeviceType GetDeviceType() override
Get the type of memory used by the TensorBuffer.
Definition allocator.h:6
Definition bfc_allocator.h:17
double fragment_fraction
Definition bfc_allocator.h:19
bool allow_growth
Definition bfc_allocator.h:18
Definition bfc_allocator.h:100
std::set< ChunkHandle, ChunkComparator > free_chunk_set_t
Definition bfc_allocator.h:128
chunk_handle_t last_chunk_handle
Definition bfc_allocator.h:108
size_t bin_size
Definition bfc_allocator.h:102
chunk_handle_t first_chunk_handle
Definition bfc_allocator.h:106
size_t num_chunks
Definition bfc_allocator.h:104
Definition bfc_allocator.h:76
void * ptr
Definition bfc_allocator.h:92
size_t size
Definition bfc_allocator.h:78
bin_index_t bin_index
Definition bfc_allocator.h:80
size_t requested_size
Definition bfc_allocator.h:85
int64_t allocation_id
Definition bfc_allocator.h:90
bool allocated() const
Definition bfc_allocator.h:97
chunk_handle_t prev_chunk_handle
Definition bfc_allocator.h:95
chunk_handle_t next_chunk_handle
Definition bfc_allocator.h:93