Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_CArena.H
Go to the documentation of this file.
1#ifndef BL_CARENA_H
2#define BL_CARENA_H
3#include <AMReX_Config.H>
4
5#include <AMReX_Arena.H>
6
7#include <cstddef>
8#include <functional>
9#include <iosfwd>
10#include <map>
11#include <mutex>
12#include <set>
13#include <string>
14#include <unordered_set>
15#include <vector>
16
17namespace amrex {
18
26class CArena
27 :
28 public Arena
29{
30public:
36 CArena (std::size_t hunk_size = 0, ArenaInfo info = ArenaInfo());
37
38 CArena (const CArena& rhs) = delete;
39 CArena (CArena&& rhs) = delete;
40 CArena& operator= (const CArena& rhs) = delete;
41 CArena& operator= (CArena&& rhs) = delete;
42
44 ~CArena () override;
45
47 [[nodiscard]] void* alloc (std::size_t nbytes) final;
48
70 [[nodiscard]] std::pair<void*,std::size_t>
71 alloc_in_place (void* pt, std::size_t szmin, std::size_t szmax) final;
72
86 [[nodiscard]] void*
87 shrink_in_place (void* pt, std::size_t new_size) final;
88
93 void free (void* vp) final;
94
99 std::size_t freeUnused () final;
100
102 [[nodiscard]] std::size_t freeableMemory () const;
103
110 [[nodiscard]] bool hasFreeDeviceMemory (std::size_t sz) final;
111
113 std::size_t heap_space_used () const noexcept;
114
116 std::size_t heap_space_actually_used () const noexcept;
117
119 std::size_t sizeOf (void* p) const noexcept;
120
136 void PrintUsage (std::string const& name, bool print_max_usage) const;
137
150 void PrintUsage (std::ostream& os, std::string const& name, std::string const& space) const;
151
153 void ResetMaxUsageCounter () final {
155 }
156
158 constexpr static std::size_t DefaultHunkSize = 1024*1024*8;
159
160protected:
161
162 void* alloc_protected (std::size_t nbytes);
163
164 std::size_t freeUnused_protected () final;
165
167 class Node
168 {
169 public:
170 Node (void* a_block, void* a_owner, std::size_t a_size, MemStat* a_stat=nullptr) noexcept
171 :
172 m_block(a_block), m_owner(a_owner), m_size(a_size), m_stat(a_stat) {}
173
175 bool operator< (const Node& rhs) const noexcept
176 {
177 return std::less<>{}(m_block, rhs.m_block);
178 }
179
181 bool operator== (const Node& rhs) const noexcept
182 {
183 return m_block == rhs.m_block;
184 }
185
187 [[nodiscard]] void* block () const noexcept { return m_block; }
188
190 void block (void* blk) noexcept { m_block = blk; }
191
193 [[nodiscard]] std::size_t size () const noexcept { return m_size; }
194
196 void size (std::size_t sz) noexcept { m_size = sz; }
197
198 [[nodiscard]] void* owner () const noexcept { return m_owner; }
199
200 [[nodiscard]] bool coalescable (const Node& rhs) const noexcept {
201 return m_owner == rhs.m_owner;
202 }
203
205 [[nodiscard]] MemStat* mem_stat () const { return m_stat; }
206
208 void mem_stat (MemStat* a_stat) noexcept { m_stat = a_stat; }
209
210 struct hash {
211 std::size_t operator() (const Node& n) const noexcept {
212 return std::hash<void*>{}(n.m_block);
213 }
214 };
215
216 private:
218 void* m_block;
220 void* m_owner;
222 std::size_t m_size;
224 MemStat* m_stat;
225 };
226
228 std::vector<std::pair<void*,std::size_t> > m_alloc;
229
234 using NL = std::set<Node>;
235
241
246// NL m_busylist;
247 std::unordered_set<Node, Node::hash> m_busylist;
249 std::size_t m_hunk;
251 std::size_t m_used{0};
253 std::size_t m_max_used{0};
255 std::size_t m_actually_used{0};
257 std::size_t m_max_actually_used{0};
258
259 std::mutex carena_mutex;
260
261 friend std::ostream& operator<< (std::ostream& os, const CArena& arena);
262};
263
264}
265
266#endif /*BL_CARENA_H*/
A virtual base class for objects that manage their own dynamic memory allocation.
Definition AMReX_Arena.H:124
The nodes in our free list and block list.
Definition AMReX_CArena.H:168
std::size_t size() const noexcept
The size of the memory block.
Definition AMReX_CArena.H:193
void block(void *blk) noexcept
Set block address.
Definition AMReX_CArena.H:190
void size(std::size_t sz) noexcept
Set size.
Definition AMReX_CArena.H:196
void * owner() const noexcept
Definition AMReX_CArena.H:198
MemStat * mem_stat() const
Get the MemStat object of the function where this block was allocated.
Definition AMReX_CArena.H:205
void mem_stat(MemStat *a_stat) noexcept
Set MemStat.
Definition AMReX_CArena.H:208
bool coalescable(const Node &rhs) const noexcept
Definition AMReX_CArena.H:200
void * block() const noexcept
The block address.
Definition AMReX_CArena.H:187
Node(void *a_block, void *a_owner, std::size_t a_size, MemStat *a_stat=nullptr) noexcept
Definition AMReX_CArena.H:170
A Concrete Class for Dynamic Memory Management using first fit. This is a coalescing memory manager....
Definition AMReX_CArena.H:29
NL m_freelist
The free list of allocated but not currently used blocks. Maintained in lo to hi memory sorted order.
Definition AMReX_CArena.H:240
std::size_t sizeOf(void *p) const noexcept
Return the amount of memory in this pointer. Return 0 for unknown pointer.
Definition AMReX_CArena.cpp:495
CArena(const CArena &rhs)=delete
static constexpr std::size_t DefaultHunkSize
The default memory hunk size to grab from the heap.
Definition AMReX_CArena.H:158
void * shrink_in_place(void *pt, std::size_t new_size) final
Shrink allocation size in-place.
Definition AMReX_CArena.cpp:236
std::mutex carena_mutex
Definition AMReX_CArena.H:259
std::size_t freeUnused_protected() final
Definition AMReX_CArena.cpp:404
std::size_t m_max_actually_used
The max amount of memory given out via alloc().
Definition AMReX_CArena.H:257
std::size_t heap_space_actually_used() const noexcept
Return the total amount of memory given out via alloc.
Definition AMReX_CArena.cpp:489
std::size_t heap_space_used() const noexcept
The current amount of heap space used by the CArena object.
Definition AMReX_CArena.cpp:483
std::pair< void *, std::size_t > alloc_in_place(void *pt, std::size_t szmin, std::size_t szmax) final
Allocate memory in-place if possible.
Definition AMReX_CArena.cpp:163
CArena & operator=(const CArena &rhs)=delete
friend std::ostream & operator<<(std::ostream &os, const CArena &arena)
Definition AMReX_CArena.cpp:547
void ResetMaxUsageCounter() final
Reset the maximum usage counter.
Definition AMReX_CArena.H:153
std::set< Node > NL
The type of our freelist and blocklist. We use a set sorted from lo to hi memory addresses.
Definition AMReX_CArena.H:234
std::size_t m_hunk
The minimal size of hunks to request from system.
Definition AMReX_CArena.H:249
std::size_t freeUnused() final
Free unused memory back to the system. Return value is the amount of memory freed.
Definition AMReX_CArena.cpp:384
std::size_t m_used
The amount of heap space currently allocated.
Definition AMReX_CArena.H:251
std::unordered_set< Node, Node::hash > m_busylist
The list of busy blocks. A block is either on the freelist or on the blocklist, but not on both.
Definition AMReX_CArena.H:247
void * alloc(std::size_t nbytes) final
Allocate some memory.
Definition AMReX_CArena.cpp:29
CArena(CArena &&rhs)=delete
void free(void *vp) final
Free up allocated memory. Merge neighboring free memory chunks into largest possible chunk.
Definition AMReX_CArena.cpp:294
~CArena() override
The destructor.
Definition AMReX_CArena.cpp:21
bool hasFreeDeviceMemory(std::size_t sz) final
Does the device have enough free memory for allocating this much memory? For CPU builds,...
Definition AMReX_CArena.cpp:445
void * alloc_protected(std::size_t nbytes)
Definition AMReX_CArena.cpp:37
void PrintUsage(std::string const &name, bool print_max_usage) const
Print memory usage information of this arena.
Definition AMReX_CArena.cpp:510
std::vector< std::pair< void *, std::size_t > > m_alloc
The list of blocks allocated.
Definition AMReX_CArena.H:228
std::size_t freeableMemory() const
Return the amount of memory that can be freed.
Definition AMReX_CArena.cpp:391
std::size_t m_max_used
The max amount of heap space currently allocated.
Definition AMReX_CArena.H:253
std::size_t m_actually_used
The amount of memory given out via alloc().
Definition AMReX_CArena.H:255
Definition AMReX_Amr.cpp:49
bool operator==(A1 const &a1, A2 const &a2)
Definition AMReX_GpuAllocators.H:208
Definition AMReX_Arena.H:71
Definition AMReX_CArena.H:210
Definition AMReX_Arena.H:12