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
10#include <AMReX_Arena.H>
11
12#include <cstddef>
13#include <functional>
14#include <iosfwd>
15#include <map>
16#include <mutex>
17#include <set>
18#include <string>
19#include <unordered_set>
20#include <vector>
21
22namespace amrex {
23
33class CArena
34 :
35 public Arena
36{
37public:
44 CArena (std::size_t hunk_size = 0, ArenaInfo info = ArenaInfo());
45
46 CArena (const CArena& rhs) = delete;
47 CArena (CArena&& rhs) = delete;
48 CArena& operator= (const CArena& rhs) = delete;
49 CArena& operator= (CArena&& rhs) = delete;
50
52 ~CArena () override;
53
55 [[nodiscard]] void* alloc (std::size_t nbytes) final;
56
78 [[nodiscard]] std::pair<void*,std::size_t>
79 alloc_in_place (void* pt, std::size_t szmin, std::size_t szmax) final;
80
94 [[nodiscard]] void*
95 shrink_in_place (void* pt, std::size_t new_size) final;
96
103 void free (void* vp) final;
104
109 std::size_t freeUnused () final;
110
114 [[nodiscard]] std::size_t freeableMemory () const;
115
117 [[nodiscard]] std::size_t largestFreeBlock () const final;
118
127 [[nodiscard]] bool hasFreeDeviceMemory (std::size_t sz) final;
128
130 std::size_t heap_space_used () const noexcept;
131
133 std::size_t heap_space_actually_used () const noexcept;
134
140 std::size_t sizeOf (void* p) const noexcept;
141
157 void PrintUsage (std::string const& name, bool print_max_usage) const;
158
171 void PrintUsage (std::ostream& os, std::string const& name, std::string const& space) const;
172
174 void ResetMaxUsageCounter () final {
176 }
177
179 constexpr static std::size_t DefaultHunkSize = 1024*1024*8;
180
181protected:
182
183 void* alloc_protected (std::size_t nbytes);
184
185 std::size_t freeUnused_protected () final;
186
188 class Node
189 {
190 public:
191 Node (void* a_block, void* a_owner, std::size_t a_size, MemStat* a_stat=nullptr) noexcept
192 :
193 m_block(a_block), m_owner(a_owner), m_size(a_size), m_stat(a_stat) {}
194
196 bool operator< (const Node& rhs) const noexcept
197 {
198 return std::less<>{}(m_block, rhs.m_block);
199 }
200
202 bool operator== (const Node& rhs) const noexcept
203 {
204 return m_block == rhs.m_block;
205 }
206
208 [[nodiscard]] void* block () const noexcept { return m_block; }
209
211 void block (void* blk) noexcept { m_block = blk; }
212
214 [[nodiscard]] std::size_t size () const noexcept { return m_size; }
215
217 void size (std::size_t sz) noexcept { m_size = sz; }
218
219 [[nodiscard]] void* owner () const noexcept { return m_owner; }
220
221 [[nodiscard]] bool coalescable (const Node& rhs) const noexcept {
222 return m_owner == rhs.m_owner;
223 }
224
226 [[nodiscard]] MemStat* mem_stat () const { return m_stat; }
227
229 void mem_stat (MemStat* a_stat) noexcept { m_stat = a_stat; }
230
231 struct hash {
232 std::size_t operator() (const Node& n) const noexcept {
233 return std::hash<void*>{}(n.m_block);
234 }
235 };
236
237 private:
239 void* m_block;
241 void* m_owner;
243 std::size_t m_size;
245 MemStat* m_stat;
246 };
247
249 std::vector<std::pair<void*,std::size_t> > m_alloc;
250
255 using NL = std::set<Node>;
256
262
267// NL m_busylist;
268 std::unordered_set<Node, Node::hash> m_busylist;
270 std::size_t m_hunk;
272 std::size_t m_used{0};
274 std::size_t m_max_used{0};
276 std::size_t m_actually_used{0};
278 std::size_t m_max_actually_used{0};
279
280 mutable std::mutex carena_mutex;
281
283 friend std::ostream& operator<< (std::ostream& os, const CArena& arena);
284};
285
286}
287
288#endif /*BL_CARENA_H*/
Memory arena base class and global arena accessors.
Abstract base class for memory arenas.
Definition AMReX_Arena.H:153
The nodes in our free list and block list.
Definition AMReX_CArena.H:189
std::size_t size() const noexcept
The size of the memory block.
Definition AMReX_CArena.H:214
void block(void *blk) noexcept
Set block address.
Definition AMReX_CArena.H:211
void size(std::size_t sz) noexcept
Set size.
Definition AMReX_CArena.H:217
void * owner() const noexcept
Definition AMReX_CArena.H:219
MemStat * mem_stat() const
Get the MemStat object of the function where this block was allocated.
Definition AMReX_CArena.H:226
void mem_stat(MemStat *a_stat) noexcept
Set MemStat.
Definition AMReX_CArena.H:229
bool coalescable(const Node &rhs) const noexcept
Definition AMReX_CArena.H:221
void * block() const noexcept
The block address.
Definition AMReX_CArena.H:208
Node(void *a_block, void *a_owner, std::size_t a_size, MemStat *a_stat=nullptr) noexcept
Definition AMReX_CArena.H:191
Coalescing first-fit dynamic memory arena.
Definition AMReX_CArena.H:36
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:261
std::size_t sizeOf(void *p) const noexcept
Return the size of the allocation referenced by p (0 if unknown).
Definition AMReX_CArena.cpp:506
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:179
void * shrink_in_place(void *pt, std::size_t new_size) final
Shrink allocation size in-place.
Definition AMReX_CArena.cpp:238
std::mutex carena_mutex
Definition AMReX_CArena.H:280
std::size_t freeUnused_protected() final
Definition AMReX_CArena.cpp:418
std::size_t m_max_actually_used
The max amount of memory given out via alloc().
Definition AMReX_CArena.H:278
std::size_t heap_space_actually_used() const noexcept
Return the total amount of memory given out via alloc.
Definition AMReX_CArena.cpp:500
std::size_t heap_space_used() const noexcept
The current amount of heap space used by the CArena object.
Definition AMReX_CArena.cpp:494
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:165
CArena & operator=(const CArena &rhs)=delete
friend std::ostream & operator<<(std::ostream &os, const CArena &arena)
Print a summary of the arena's free and busy lists to os.
Definition AMReX_CArena.cpp:558
void ResetMaxUsageCounter() final
Reset the maximum usage counter.
Definition AMReX_CArena.H:174
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:255
std::size_t m_hunk
The minimal size of hunks to request from system.
Definition AMReX_CArena.H:270
std::size_t freeUnused() final
Free unused memory back to the system. Return value is the amount of memory freed.
Definition AMReX_CArena.cpp:387
std::size_t m_used
The amount of heap space currently allocated.
Definition AMReX_CArena.H:272
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:268
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:296
std::size_t largestFreeBlock() const final
Return the largest free memory block already held by this CArena.
Definition AMReX_CArena.cpp:407
~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:456
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:521
std::vector< std::pair< void *, std::size_t > > m_alloc
The list of blocks allocated.
Definition AMReX_CArena.H:249
std::size_t freeableMemory() const
Return the amount of memory that can be released back to the heap.
Definition AMReX_CArena.cpp:394
std::size_t m_max_used
The max amount of heap space currently allocated.
Definition AMReX_CArena.H:274
std::size_t m_actually_used
The amount of memory given out via alloc().
Definition AMReX_CArena.H:276
Definition AMReX_Amr.cpp:50
bool operator==(A1 const &a1, A2 const &a2)
Definition AMReX_GpuAllocators.H:214
Configuration options for constructing an Arena.
Definition AMReX_Arena.H:91
Definition AMReX_CArena.H:231
Definition AMReX_Arena.H:20