BioDynaMo  v1.05.119-a4ff3934
memory_manager.h
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2021 CERN & University of Surrey for the benefit of the
4 // BioDynaMo collaboration. All Rights Reserved.
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // See the LICENSE file distributed with this work for details.
10 // See the NOTICE file distributed with this work for additional information
11 // regarding copyright ownership.
12 //
13 // -----------------------------------------------------------------------------
14 
15 #ifndef CORE_MEMORY_MEMORY_MANAGER_H_
16 #define CORE_MEMORY_MEMORY_MANAGER_H_
17 
18 #include <cassert>
19 #include <list>
20 #include <utility>
21 #include <vector>
22 
23 #include "core/container/flatmap.h"
24 #include "core/real_t.h"
25 #include "core/util/numa.h"
26 #include "core/util/spinlock.h"
27 #include "core/util/thread_info.h"
28 
29 namespace bdm {
30 namespace memory_manager_detail {
31 
32 struct Node {
33  Node* next = nullptr;
34 };
35 
40 class List {
41  public:
44  explicit List(uint64_t n);
45 
46  List(const List& other);
47 
48  Node* PopFront();
49 
50  void PushFront(Node* head);
51 
52  void PushFrontThreadSafe(Node* head);
53 
54  void PushBackN(Node* head, Node* tail);
55 
56  void PushBackNThreadSafe(Node* head, Node* tail);
57 
58  void PopBackN(Node** head, Node** tail);
59 
60  void PopBackNThreadSafe(Node** head, Node** tail);
61 
62  bool Empty() const;
63 
64  bool CanPopBackN() const;
65 
66  uint64_t Size() const;
67 
68  uint64_t GetN() const;
69 
70  private:
71  Node* head_ = nullptr;
72  Node* tail_ = nullptr;
73  std::list<Node*> skip_list_;
74  uint64_t size_ = 0;
77  uint64_t n_;
79 };
80 
83  bool IsFullyInitialized() const;
84 
85  void GetNextPageBatch(uint64_t size_n_pages, char** start, uint64_t* size);
86 
88  char* end_pointer_;
91 };
92 
95  public:
96  static uint64_t RoundUpTo(uint64_t number, uint64_t multiple);
97 
98  NumaPoolAllocator(uint64_t size, int nid, uint64_t size_n_pages,
99  real_t growth_rate, uint64_t max_mem_per_thread_factor);
100 
102 
103  void* New(int tid);
104 
105  void Delete(void* p);
106 
107  uint64_t GetSize() const;
108 
109  private:
110  static constexpr uint64_t kMetadataSize = 8;
111  uint64_t size_n_pages_;
115  uint64_t total_size_ = 0;
116  uint64_t size_;
117  int nid_;
119  std::vector<AllocatedBlock> memory_blocks_;
120  std::vector<List> free_lists_; // one per thread
123 
124  void AllocNewMemoryBlock(std::size_t size);
125 
126  void InitializeNPages(List* tl_list, char* block, uint64_t mem_block_size);
127 };
128 
130  public:
131  PoolAllocator(std::size_t size, uint64_t size_n_pages, real_t growth_rate,
132  uint64_t max_mem_per_thread_factor);
133 
134  PoolAllocator(PoolAllocator&& other) noexcept;
135  PoolAllocator(const PoolAllocator& other) = delete;
136 
137  ~PoolAllocator();
138 
139  void* New(std::size_t size);
140 
141  private:
142  std::size_t size_;
144  std::vector<NumaPoolAllocator*> numa_allocators_;
145 };
146 
147 } // namespace memory_manager_detail
148 
150  public:
151  MemoryManager(uint64_t aligned_pages_shift, real_t growth_rate,
152  uint64_t max_mem_per_thread_factor);
153 
154  ~MemoryManager();
155 
156  void* New(std::size_t size);
157 
158  void Delete(void* p);
159 
160  void SetIgnoreDelete(bool value);
161 
162  private:
165  uint64_t page_size_;
166  uint64_t page_shift_;
168  uint64_t aligned_pages_;
169  uint64_t size_n_pages_;
170  uint64_t num_threads_;
171  bool ignore_delete_ = false;
172 
175 
177 };
178 
179 } // namespace bdm
180 
181 #endif // CORE_MEMORY_MEMORY_MANAGER_H_
bdm::memory_manager_detail::NumaPoolAllocator
Pool allocator for a specific allocation size and numa node. .
Definition: memory_manager.h:94
bdm::memory_manager_detail::PoolAllocator::tinfo_
ThreadInfo * tinfo_
Definition: memory_manager.h:143
bdm::memory_manager_detail::NumaPoolAllocator::kMetadataSize
static constexpr uint64_t kMetadataSize
Definition: memory_manager.h:110
bdm::memory_manager_detail::AllocatedBlock::initialized_until_
char * initialized_until_
Memory to the left has been initialized.
Definition: memory_manager.h:90
bdm::memory_manager_detail::List::nodes_before_skip_list_
uint64_t nodes_before_skip_list_
Definition: memory_manager.h:75
bdm::memory_manager_detail::List::PushBackN
void PushBackN(Node *head, Node *tail)
Definition: memory_manager.cc:83
spinlock.h
bdm::memory_manager_detail::PoolAllocator::numa_allocators_
std::vector< NumaPoolAllocator * > numa_allocators_
Definition: memory_manager.h:144
bdm::MemoryManager::growth_rate_
real_t growth_rate_
Definition: memory_manager.h:163
bdm
Definition: agent.cc:39
bdm::memory_manager_detail::PoolAllocator::size_
std::size_t size_
Definition: memory_manager.h:142
bdm::memory_manager_detail::PoolAllocator::PoolAllocator
PoolAllocator(std::size_t size, uint64_t size_n_pages, real_t growth_rate, uint64_t max_mem_per_thread_factor)
Definition: memory_manager.cc:297
bdm::memory_manager_detail::NumaPoolAllocator::size_n_pages_
uint64_t size_n_pages_
Definition: memory_manager.h:111
thread_info.h
bdm::MemoryManager::SetIgnoreDelete
void SetIgnoreDelete(bool value)
Definition: memory_manager.cc:402
bdm::memory_manager_detail::NumaPoolAllocator::lock_
Spinlock lock_
Definition: memory_manager.h:122
bdm::MemoryManager::page_shift_
uint64_t page_shift_
Definition: memory_manager.h:166
bdm::memory_manager_detail::PoolAllocator::New
void * New(std::size_t size)
Definition: memory_manager.cc:321
bdm::MemoryManager::lock_
Spinlock lock_
Definition: memory_manager.h:176
bdm::real_t
double real_t
Definition: real_t.h:21
bdm::MemoryManager::~MemoryManager
~MemoryManager()
Definition: memory_manager.cc:352
bdm::memory_manager_detail::NumaPoolAllocator::max_nodes_per_thread_
uint64_t max_nodes_per_thread_
Definition: memory_manager.h:113
bdm::memory_manager_detail::NumaPoolAllocator::memory_blocks_
std::vector< AllocatedBlock > memory_blocks_
Definition: memory_manager.h:119
bdm::MemoryManager::aligned_pages_shift_
uint64_t aligned_pages_shift_
Definition: memory_manager.h:167
bdm::MemoryManager::Delete
void Delete(void *p)
Definition: memory_manager.cc:389
bdm::memory_manager_detail::NumaPoolAllocator::free_lists_
std::vector< List > free_lists_
Definition: memory_manager.h:120
bdm::MemoryManager::ignore_delete_
bool ignore_delete_
Definition: memory_manager.h:171
bdm::memory_manager_detail::List::PopBackN
void PopBackN(Node **head, Node **tail)
Definition: memory_manager.cc:106
bdm::memory_manager_detail::NumaPoolAllocator::nid_
int nid_
Definition: memory_manager.h:117
bdm::memory_manager_detail::List::head_
Node * head_
Definition: memory_manager.h:71
bdm::memory_manager_detail::NumaPoolAllocator::RoundUpTo
static uint64_t RoundUpTo(uint64_t number, uint64_t multiple)
Definition: memory_manager.cc:289
bdm::memory_manager_detail::NumaPoolAllocator::Delete
void Delete(void *p)
Definition: memory_manager.cc:218
bdm::MemoryManager::allocators_
UnorderedFlatmap< std::size_t, memory_manager_detail::PoolAllocator * > allocators_
Definition: memory_manager.h:174
bdm::memory_manager_detail::AllocatedBlock
Contains metadata for an allocated memory block.
Definition: memory_manager.h:82
bdm::memory_manager_detail::List::skip_list_
std::list< Node * > skip_list_
Definition: memory_manager.h:73
bdm::memory_manager_detail::NumaPoolAllocator::GetSize
uint64_t GetSize() const
Definition: memory_manager.cc:233
bdm::memory_manager_detail::List::n_
uint64_t n_
Number of nodes for which fast migrations are supported.
Definition: memory_manager.h:77
bdm::MemoryManager::page_size_
uint64_t page_size_
Definition: memory_manager.h:165
bdm::memory_manager_detail::NumaPoolAllocator::AllocNewMemoryBlock
void AllocNewMemoryBlock(std::size_t size)
Definition: memory_manager.cc:235
bdm::Spinlock
Definition: spinlock.h:22
bdm::MemoryManager
Definition: memory_manager.h:149
bdm::MemoryManager::max_mem_per_thread_factor_
uint64_t max_mem_per_thread_factor_
Definition: memory_manager.h:164
bdm::memory_manager_detail::NumaPoolAllocator::New
void * New(int tid)
Definition: memory_manager.cc:176
bdm::MemoryManager::aligned_pages_
uint64_t aligned_pages_
Definition: memory_manager.h:168
numa.h
bdm::memory_manager_detail::List::PopBackNThreadSafe
void PopBackNThreadSafe(Node **head, Node **tail)
Definition: memory_manager.cc:122
bdm::memory_manager_detail::List::size_
uint64_t size_
Definition: memory_manager.h:74
bdm::memory_manager_detail::NumaPoolAllocator::~NumaPoolAllocator
~NumaPoolAllocator()
Definition: memory_manager.cc:169
bdm::memory_manager_detail::List::PushFront
void PushFront(Node *head)
Definition: memory_manager.cc:60
bdm::memory_manager_detail::Node
Definition: memory_manager.h:32
bdm::memory_manager_detail::NumaPoolAllocator::num_elements_per_n_pages_
uint64_t num_elements_per_n_pages_
Definition: memory_manager.h:114
bdm::memory_manager_detail::List::PopFront
Node * PopFront()
Definition: memory_manager.cc:38
bdm::memory_manager_detail::List::List
List(uint64_t n)
Definition: memory_manager.cc:28
bdm::memory_manager_detail::List
Definition: memory_manager.h:40
bdm::memory_manager_detail::AllocatedBlock::GetNextPageBatch
void GetNextPageBatch(uint64_t size_n_pages, char **start, uint64_t *size)
Definition: memory_manager.cc:140
bdm::memory_manager_detail::List::PushBackNThreadSafe
void PushBackNThreadSafe(Node *head, Node *tail)
Definition: memory_manager.cc:101
bdm::memory_manager_detail::NumaPoolAllocator::central_
List central_
Definition: memory_manager.h:121
bdm::memory_manager_detail::List::CanPopBackN
bool CanPopBackN() const
Definition: memory_manager.cc:129
bdm::memory_manager_detail::NumaPoolAllocator::size_
uint64_t size_
Definition: memory_manager.h:116
bdm::memory_manager_detail::List::Size
uint64_t Size() const
Definition: memory_manager.cc:131
bdm::MemoryManager::New
void * New(std::size_t size)
Definition: memory_manager.cc:358
bdm::MemoryManager::num_threads_
uint64_t num_threads_
Definition: memory_manager.h:170
bdm::MemoryManager::size_n_pages_
uint64_t size_n_pages_
Definition: memory_manager.h:169
real_t.h
bdm::memory_manager_detail::NumaPoolAllocator::total_size_
uint64_t total_size_
Definition: memory_manager.h:115
bdm::memory_manager_detail::Node::next
Node * next
Definition: memory_manager.h:33
bdm::memory_manager_detail::NumaPoolAllocator::InitializeNPages
void InitializeNPages(List *tl_list, char *block, uint64_t mem_block_size)
Definition: memory_manager.cc:252
bdm::memory_manager_detail::List::Empty
bool Empty() const
Definition: memory_manager.cc:127
bdm::memory_manager_detail::List::lock_
Spinlock lock_
Definition: memory_manager.h:78
bdm::memory_manager_detail::NumaPoolAllocator::growth_rate_
real_t growth_rate_
Definition: memory_manager.h:112
bdm::memory_manager_detail::NumaPoolAllocator::tinfo_
ThreadInfo * tinfo_
Definition: memory_manager.h:118
bdm::memory_manager_detail::PoolAllocator
Definition: memory_manager.h:129
flatmap.h
bdm::memory_manager_detail::AllocatedBlock::start_pointer_
char * start_pointer_
Definition: memory_manager.h:87
bdm::memory_manager_detail::List::GetN
uint64_t GetN() const
Definition: memory_manager.cc:133
bdm::memory_manager_detail::PoolAllocator::~PoolAllocator
~PoolAllocator()
Definition: memory_manager.cc:313
bdm::memory_manager_detail::AllocatedBlock::IsFullyInitialized
bool IsFullyInitialized() const
Definition: memory_manager.cc:136
bdm::ThreadInfo
This class stores information about each thread. (e.g. to which NUMA node it belongs to....
Definition: thread_info.h:31
bdm::memory_manager_detail::NumaPoolAllocator::NumaPoolAllocator
NumaPoolAllocator(uint64_t size, int nid, uint64_t size_n_pages, real_t growth_rate, uint64_t max_mem_per_thread_factor)
Definition: memory_manager.cc:151
bdm::memory_manager_detail::List::tail_
Node * tail_
Definition: memory_manager.h:72
bdm::MemoryManager::MemoryManager
MemoryManager(uint64_t aligned_pages_shift, real_t growth_rate, uint64_t max_mem_per_thread_factor)
Definition: memory_manager.cc:332
bdm::UnorderedFlatmap
Definition: flatmap.h:28
bdm::memory_manager_detail::AllocatedBlock::end_pointer_
char * end_pointer_
Definition: memory_manager.h:88
bdm::memory_manager_detail::List::PushFrontThreadSafe
void PushFrontThreadSafe(Node *head)
Definition: memory_manager.cc:78